Skip to content

Commit 096efa3

Browse files
committed
Release candidate 1
1 parent 286632e commit 096efa3

File tree

8 files changed

+146
-120
lines changed

8 files changed

+146
-120
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust_graph_visualiser"
3-
version = "1.0.1"
3+
version = "1.9.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/graph.rs

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,15 @@ impl DijkstraGraph
4949
{ return DijkstraGraph { ..DijkstraGraph::default() }; }
5050

5151
pub(crate) fn clear(&mut self)
52-
{
53-
self.points = self.points.iter()
54-
.map(|_| None)
55-
.collect::<Vec<_>>()
56-
.try_into()
57-
.unwrap();
58-
59-
self.start = None;
60-
self.end = None;
61-
}
52+
{ *self = DijkstraGraph::default(); }
6253

6354
pub(crate) fn clear_path(&mut self)
6455
{
6556
for option in self.points.iter_mut()
6657
{
6758
let Some(point) = option.as_mut() else { continue; };
6859
point.parent = None;
60+
point.distance = None;
6961
point.visited = false;
7062
}
7163
}
@@ -82,6 +74,7 @@ impl DijkstraGraph
8274
{
8375
if id > 100 { return; }
8476
if self.points[id].is_none() { self.points[id] = Some(DijkstraNode::new(x, y)); }
77+
self.clear_path();
8578
}
8679

8780
/// Inserts a node at the first missing instance of the array
@@ -95,12 +88,14 @@ impl DijkstraGraph
9588
return;
9689
}
9790
}
91+
self.clear_path();
9892
}
9993

10094
pub(crate) fn remove_point(&mut self, id: usize)
10195
{
10296
if id > 100 { return; }
10397
self.points[id] = None;
98+
self.clear_path();
10499
}
105100

106101
/// Adds a line; if it already exists, the length gets updated
@@ -121,14 +116,18 @@ impl DijkstraGraph
121116
}
122117

123118
point.edges.push(Edge { destination: to, distance });
119+
120+
self.clear_path();
124121
}
125122

126123
pub(crate) fn remove_line(&mut self, from: usize, to: usize)
127124
{
128125
if from > 100 || to > 100 { return; }
129126

130127
let Some(from_point) = self.points[from].as_mut() else { return; };
131-
from_point.edges.remove(to);
128+
from_point.edges.retain(|edge| edge.destination != to);
129+
130+
self.clear_path();
132131
}
133132

134133
pub(crate) fn get(&self, id: usize) -> &Option<DijkstraNode>
@@ -144,10 +143,14 @@ impl DijkstraGraph
144143
{
145144
if start > 100 { return; }
146145
self.start = Some(start);
146+
self.clear_path();
147147
}
148148

149149
pub(crate) fn clear_start(&mut self)
150-
{ self.start = None; }
150+
{
151+
self.start = None;
152+
self.clear_path();
153+
}
151154

152155
pub(crate) fn end(&self) -> Option<usize>
153156
{ return self.end; }
@@ -156,10 +159,14 @@ impl DijkstraGraph
156159
{
157160
if end > 100 { return; }
158161
self.end = Some(end);
162+
self.clear_path();
159163
}
160164

161165
pub(crate) fn clear_end(&mut self)
162-
{ self.end = None; }
166+
{
167+
self.end = None;
168+
self.clear_path();
169+
}
163170

164171
/// Returns true if the shortest path has been found
165172
pub(crate) fn find_shortest_path(&mut self)
@@ -170,18 +177,18 @@ impl DijkstraGraph
170177

171178
self.clear_path();
172179

180+
// --- DIJKSTRA'S SHORTEST PATH ALGORITHM ---
181+
173182
self.points[self.start.unwrap()].as_mut().unwrap().distance = Some(0);
174183
self.points[self.start.unwrap()].as_mut().unwrap().parent = Some(self.start.unwrap());
175184

176185
let mut unvisited_points = vec![];
177186
unvisited_points.push(self.start.unwrap());
178187
let mut current_id;
179-
let mut path_length: u16; // TODO: this
188+
let mut possible_path_length = u16::MAX;
180189

181-
// --- DIJKSTRA'S SHORTEST PATH ALGORITHM ---
182190
while !unvisited_points.is_empty()
183191
{
184-
/*
185192
unvisited_points.sort_by(|a, b|
186193
{
187194
// It is basically impossible, that unvisited_points contains ids that aren't in the graph
@@ -197,7 +204,6 @@ impl DijkstraGraph
197204
(Some(dist_a), Some(dist_b)) => return dist_a.cmp(&dist_b),
198205
};
199206
});
200-
*/
201207

202208
/*
203209
// Removing all points that have been marked as visited
@@ -211,6 +217,11 @@ impl DijkstraGraph
211217
if self.points[current_id].is_none() { continue; }
212218

213219
let current_point = self.points[current_id].as_mut().unwrap();
220+
if current_id == self.end.unwrap()
221+
{
222+
if current_point.distance.unwrap() < possible_path_length { possible_path_length = current_point.distance.unwrap(); }
223+
continue;
224+
};
214225
current_point.visited = true;
215226
let current_point_distance = current_point.distance.clone().unwrap();
216227
let edges = current_point.edges.clone();
@@ -219,14 +230,17 @@ impl DijkstraGraph
219230

220231
for edge in edges
221232
{
233+
let possibly_lower_goal = current_point_distance + edge.distance;
234+
235+
if possibly_lower_goal > possible_path_length { continue; }
236+
222237
let Some(neighbour) = self.points[edge.destination].as_mut() else { return; };
223238

224239
if !neighbour.visited
225240
{ unvisited_points.push(edge.destination); }
226241

227-
let possibly_lower_goal = current_point_distance + edge.distance;
228-
229-
if neighbour.distance.is_none() || neighbour.distance.unwrap() > possibly_lower_goal
242+
// TODO: if both are same then toss coin
243+
if neighbour.distance.is_none() || neighbour.distance.unwrap() > possibly_lower_goal || (neighbour.distance.unwrap() == possibly_lower_goal && rand::random())
230244
{
231245
neighbour.distance = Some(possibly_lower_goal);
232246
neighbour.parent = Some(current_id);
@@ -336,14 +350,14 @@ impl DijkstraGraph
336350
{
337351
self.clear();
338352

339-
self.append_point(942_f32, 355_f32);
340-
self.append_point(720_f32, 208_f32);
341-
self.append_point(198_f32, 342_f32);
342-
self.append_point(463_f32, 507_f32);
343-
self.append_point(735_f32, 513_f32);
344-
self.append_point(458_f32, 346_f32);
345-
self.append_point(468_f32, 202_f32);
346-
self.append_point(721_f32, 360_f32);
353+
self.append_point(942., 355.);
354+
self.append_point(720., 208.);
355+
self.append_point(198., 342.);
356+
self.append_point(463., 507.);
357+
self.append_point(735., 513.);
358+
self.append_point(458., 346.);
359+
self.append_point(468., 202.);
360+
self.append_point(721., 360.);
347361

348362
self.add_line(3, 4, 3);
349363
self.add_line(2, 5, 5);
@@ -365,18 +379,18 @@ impl DijkstraGraph
365379
{
366380
self.clear();
367381

368-
self.append_point(959_f32, 211_f32);
369-
self.append_point(967_f32, 394_f32);
370-
self.append_point(946_f32, 532_f32);
371-
self.append_point(144_f32, 377_f32);
372-
self.append_point(775_f32, 295_f32);
373-
self.append_point(734_f32, 523_f32);
374-
self.append_point(559_f32, 493_f32);
375-
self.append_point(570_f32, 361_f32);
376-
self.append_point(569_f32, 200_f32);
377-
self.append_point(353_f32, 206_f32);
378-
self.append_point(355_f32, 350_f32);
379-
self.append_point(342_f32, 488_f32);
382+
self.append_point(959., 211.);
383+
self.append_point(967., 394.);
384+
self.append_point(946., 532.);
385+
self.append_point(144., 377.);
386+
self.append_point(775., 295.);
387+
self.append_point(734., 523.);
388+
self.append_point(559., 493.);
389+
self.append_point(570., 361.);
390+
self.append_point(569., 200.);
391+
self.append_point(353., 206.);
392+
self.append_point(355., 350.);
393+
self.append_point(342., 488.);
380394

381395
self.add_line(10, 6, 4);
382396
self.add_line(7, 1, 5);
@@ -401,32 +415,32 @@ impl DijkstraGraph
401415
{
402416
self.clear();
403417

404-
self.append_point(595_f32, 640_f32);
405-
self.append_point(864_f32, 300_f32);
406-
self.append_point(550_f32, 369_f32);
407-
self.append_point(280_f32, 606_f32);
408-
self.append_point(748_f32, 127_f32);
409-
self.append_point(177_f32, 71_f32);
410-
self.append_point(467_f32, 84_f32);
411-
self.append_point(260_f32, 431_f32);
412-
self.append_point(928_f32, 642_f32);
413-
self.append_point(466_f32, 181_f32);
414-
self.append_point(433_f32, 27_f32);
415-
self.append_point(667_f32, 52_f32);
416-
self.append_point(847_f32, 75_f32);
417-
self.append_point(734_f32, 270_f32);
418-
self.append_point(931_f32, 233_f32);
419-
self.append_point(904_f32, 389_f32);
420-
self.append_point(423_f32, 467_f32);
421-
self.append_point(445_f32, 551_f32);
422-
self.append_point(691_f32, 559_f32);
418+
self.append_point(595., 640.);
419+
self.append_point(864., 300.);
420+
self.append_point(550., 369.);
421+
self.append_point(280., 606.);
422+
self.append_point(748., 127.);
423+
self.append_point(177., 71.);
424+
self.append_point(467., 84.);
425+
self.append_point(260., 431.);
426+
self.append_point(928., 642.);
427+
self.append_point(466., 181.);
428+
self.append_point(433., 27.);
429+
self.append_point(667., 52.);
430+
self.append_point(847., 75.);
431+
self.append_point(734., 270.);
432+
self.append_point(931., 233.);
433+
self.append_point(904., 389.);
434+
self.append_point(423., 467.);
435+
self.append_point(445., 551.);
436+
self.append_point(691., 559.);
423437

424438
self.add_line(11, 12, 1);
425439
self.add_line(5, 7, 12);
426440
self.add_line(13, 2, 1);
427441
self.add_line(15, 8, 10);
428442
self.add_line(14, 8, 14);
429-
self.add_line(1, 18, 9);
443+
self.add_line(1, 18, 10);
430444
self.add_line(17, 18, 3);
431445
self.add_line(16, 17, 2);
432446
self.add_line(7, 3, 1);

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub(crate) const LINE_COLOR: u32 = 0xff8000;
3737
pub(crate) const POINT_COLOR: u32 = 0x00ffff;
3838
pub(crate) const LINE_LENGTH_COLOR: u32 = 0xc0c0c0;
3939

40-
// FIX: path from 6 to 18 is wrong, there exist a much shorter one (point 10 seems to cause that somehow)
4140
#[macroquad::main(window_configuration)]
4241
async fn main()
4342
{

0 commit comments

Comments
 (0)