Skip to content

Commit 9c65965

Browse files
committed
Dijkstra in good shape; minor mods
1 parent 0e9bf96 commit 9c65965

File tree

5 files changed

+75
-62
lines changed

5 files changed

+75
-62
lines changed

src/algorithms/controllers/BFS.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ export default {
277277
}
278278
// color the path from the start node to the end node
279279
let current = c_end;
280-
// + color parent array
280+
// + color nodes and parent array
281281
while((current != a) && (c_parent[current] != null))
282282
{
283+
vis.array.select(0, current + 1, 0, current + 1, colors.SUCCESS_A);
283284
vis.array.select(1, current + 1, 1, current + 1, colors.SUCCESS_A);
284285
vis.graph.removeEdgeColor(current, c_parent[current]);
285286
vis.graph.colorEdge(current, c_parent[current], colors.SUCCESS_E);

src/algorithms/controllers/DFS.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ displayedStack, explored, visited, n, parent]
324324
}
325325
// color the path from the start node to the end node
326326
let current = c_end;
327-
// + color parent array
327+
// + color nodes and parent array
328328
while((current != a) && (c_parent[current] != null))
329329
{
330+
vis.array.select(0, current + 1, 0, current + 1, colors.SUCCESS_A);
330331
vis.array.select(1, current + 1, 1, current + 1, colors.SUCCESS_A);
331332
vis.graph.removeEdgeColor(current, c_parent[current]);
332333
vis.graph.colorEdge(current, c_parent[current], colors.SUCCESS_E);

src/algorithms/controllers/dijkstra.js

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// XXX termination when there is no end node needs fixing
2-
// XXX add extra highlighting of code etc for UpdateNodes, like BFS
31
// XXX add support for multiple end nodes
42
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer';
53
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer';
@@ -40,6 +38,14 @@ export default {
4038
const finalCosts = [];
4139
const start = startNode - 1;
4240
const end = endNodes[0] - 1;
41+
42+
// Display has table [nodes, parents, minCosts, finalCosts]
43+
// and display code indexes into this table; we define the indices here
44+
const NODE = 0;
45+
const PAR = 1;
46+
const MCOST = 2;
47+
const FCOST = 3;
48+
4349
// Create a set to keep track of visited vertices
4450
const visited = new Set();
4551
let miniIndex = start;
@@ -89,20 +95,20 @@ export default {
8995

9096
// highlight nodes as finalised/frontier in array
9197
for (let i = 0; i < numVertices; i++) {
92-
if (c_nodes_etc[2][i+1] === null) {
93-
vis.array.select(0, i + 1, 0, i + 1, colors.FINALISED_A);
98+
if (c_nodes_etc[MCOST][i+1] === null) {
99+
vis.array.select(NODE, i + 1, NODE, i + 1, colors.FINALISED_A);
94100
vis.graph.removeNodeColor(i);
95101
vis.graph.colorNode(i, colors.FINALISED_N);
96-
} else if (isNumber(c_nodes_etc[2][i+1])) {
97-
vis.array.select(0, i + 1, 0, i + 1, colors.FRONTIER_A);
102+
} else if (isNumber(c_nodes_etc[MCOST][i+1])) {
103+
vis.array.select(NODE, i + 1, NODE, i + 1, colors.FRONTIER_A);
98104
vis.graph.removeNodeColor(i);
99105
vis.graph.colorNode(i, colors.FRONTIER_N);
100106
}
101107
}
102108

103109
// color Min in PQ
104110
if (c_Min != null)
105-
vis.array.select(2, c_Min + 1, 2, c_Min + 1, colors.PQ_MIN_A);
111+
vis.array.select(MCOST, c_Min + 1, MCOST, c_Min + 1, colors.PQ_MIN_A);
106112
}
107113

108114
chunker.add(
@@ -159,20 +165,27 @@ export default {
159165
chunker.add(
160166
7,
161167
(vis, v, w) => {
168+
// re-render table with new cost
162169
vis.array.set(v, algNameStr);
163-
vis.array.assignVariable(minStr, 2, w + 1);
164-
vis.array.select(2, w + 1, 2, w + 1, colors.PQ_MIN_A);
165-
166170
// color start node in array + graph
167-
vis.array.select(0, w + 1, 0, w + 1, colors.FRONTIER_A);
171+
vis.array.select(NODE, w + 1, NODE, w + 1, colors.FRONTIER_A);
168172
vis.graph.colorNode(w, colors.FRONTIER_N);
169-
170173
},
171174
[[nodes, parents, minCosts, finalCosts], start]
172175
);
173176

174177
// Nodes <- PQ containing all nodes
175-
chunker.add(8);
178+
chunker.add(
179+
8,
180+
(vis, v, w) => {
181+
// add min to display
182+
vis.array.set(v, algNameStr);
183+
vis.array.assignVariable(minStr, 2, w + 1);
184+
vis.array.select(MCOST, w + 1, MCOST, w + 1, colors.PQ_MIN_A);
185+
vis.array.select(NODE, w + 1, NODE, w + 1, colors.FRONTIER_A);
186+
},
187+
[[nodes, parents, minCosts, finalCosts], start]
188+
);
176189

177190
let currentVertex = null;
178191
// while Nodes not Empty
@@ -187,35 +200,30 @@ export default {
187200
2,
188201
(vis, c_nodes_etc, c_miniIndex, c_last, c_prev, c_cV) => {
189202
refresh(vis, c_nodes_etc, c_miniIndex, c_cV, null);
190-
// visit1(x,y,2) highlights the edge xy,and nodes x and y
191-
// in the 2nd color
192-
// leave1(x,y,2) removes the highlight on nodes x, y and
193-
// edge xy(placed by the visit1 function in the 2nd color)
203+
194204
if (c_last[0] != null) {
195205
vis.graph.removeEdgeColor(c_last[0], c_last[1]);
196-
197-
198206

199207
// restore the original color of the edge
200-
if(c_prev[c_last[0]] != null && c_nodes_etc[3][c_last[0]+1] == dashStr)
208+
if(c_prev[c_last[0]] != null && c_nodes_etc[FCOST][c_last[0]+1] == dashStr)
201209
{
202210
vis.graph.removeEdgeColor(c_prev[c_last[0]], c_last[0]);
203211
vis.graph.colorEdge(c_prev[c_last[0]], c_last[0], colors.FRONTIER_E);
204212
}
205213

206-
if(c_prev[c_last[1]] != null && c_nodes_etc[3][c_last[1]+1] == dashStr)
214+
if(c_prev[c_last[1]] != null && c_nodes_etc[FCOST][c_last[1]+1] == dashStr)
207215
{
208216
vis.graph.removeEdgeColor(c_prev[c_last[1]], c_last[1]);
209217
vis.graph.colorEdge(c_prev[c_last[1]], c_last[1], colors.FRONTIER_E);
210218
}
211219

212-
if(c_nodes_etc[3][c_last[0]+1] != dashStr)
220+
if(c_nodes_etc[FCOST][c_last[0]+1] != dashStr)
213221
{
214222
vis.graph.removeEdgeColor(c_prev[c_last[0]], c_last[0]);
215223
vis.graph.colorEdge(c_prev[c_last[0]], c_last[0], colors.FINALISED_E);
216224
}
217225

218-
if(c_nodes_etc[3][c_last[1]+1] != dashStr)
226+
if(c_nodes_etc[FCOST][c_last[1]+1] != dashStr)
219227
{
220228
vis.graph.removeEdgeColor(c_prev[c_last[1]], c_last[1]);
221229
vis.graph.colorEdge(c_prev[c_last[1]], c_last[1], colors.FINALISED_E);
@@ -226,8 +234,10 @@ export default {
226234
},
227235
[[nodes, parents, minCosts, finalCosts], miniIndex, last, prev, currentVertex]
228236
);
229-
if (!(visited.size < numVertices))
237+
if (!(visited.size < numVertices)) {
238+
chunker.add(99); // return at end of function
230239
break;
240+
}
231241

232242
// Find the unvisited vertex with the smallest cost
233243

@@ -245,24 +255,11 @@ export default {
245255
chunker.add(
246256
9,
247257
(vis, c_nodes_etc, c_miniIndex, c_cV, c_prev_cV) => {
248-
// color graph node
249-
//vis.graph.colorNode(c_cV, colors.FINALISED_N);
250-
251-
// reset
258+
// reset previous highlight if any
252259
if (c_prev_cV != null) {
253260
vis.graph.removeEdgeColor(c_prev_cV, c_cV);
254-
// vis.graph.colorNode(c_prev_cV, 1);
255-
// vis.graph.colorEdge(c_prev_cV, c_cV, 1);
256261
vis.graph.colorEdge(c_prev_cV, c_cV, colors.FINALISED_E);
257-
//vis.graph.deselect(y, x);
258-
//vis.graph.select(y, y);
259-
//vis.graph.visit1(y, x, 1);
260-
//vis.graph.leave1(x, x, 1);
261-
//vis.graph.leave1(y, y, 1);
262262
}
263-
// vis.graph.colorNode(c_cV, 1);
264-
//vis.graph.select(x, x);
265-
266263
refresh(vis, c_nodes_etc, c_miniIndex, c_cV, null);
267264
},
268265
[[nodes, parents, minCosts, finalCosts], miniIndex,
@@ -272,14 +269,45 @@ export default {
272269
// If we can't find a reachable vertex, exit
273270
// if is_end_node(n) or Cost[n] = infinity
274271
chunker.add(10);
275-
if (currentVertex === null || currentVertex === end ||
276-
cost[currentVertex] === Infinity) {
272+
if (currentVertex === null // || currentVertex === end
273+
|| cost[currentVertex] === Infinity) {
274+
// terminate without finding end node
277275
chunker.add(3);
278276
// return
279277
break;
280278
}
279+
if (currentVertex === end) {
280+
// found end node - highlight path
281+
chunker.add(3,
282+
(vis, c_nodes_etc) => {
283+
// remove n, add start and end
284+
vis.array.set(c_nodes_etc, 'BFS');
285+
vis.array.assignVariable('n', 2, null);
286+
vis.array.assignVariable('end', 2, end + 1);
287+
vis.array.assignVariable('start', 2, start + 1);
288+
// remove color from node numbers
289+
for(let i = 0; i < c_nodes_etc[NODE].length; i++){
290+
vis.array.deselect(0, i);
291+
}
292+
// color the path from the start node to the end node
293+
// + color nodes and parent array
294+
let current = end;
295+
while((current != start) && (c_nodes_etc[PAR][current+1] != null))
296+
{
297+
vis.array.select(NODE, current + 1, NODE, current + 1, colors.SUCCESS_A);
298+
vis.array.select(PAR, current + 1, PAR, current + 1, colors.SUCCESS_A);
299+
vis.graph.removeEdgeColor(current, c_nodes_etc[PAR][current+1]-1);
300+
vis.graph.colorEdge(current, c_nodes_etc[PAR][current+1]-1, colors.SUCCESS_E);
301+
current = c_nodes_etc[PAR][current+1]-1;
302+
}
303+
304+
},
305+
[[nodes, parents, minCosts, finalCosts]]
306+
)
307+
break;
308+
}
309+
281310
// Mark the vertex as visited
282-
283311

284312
// Update the cost and prev arrays
285313

@@ -320,14 +348,10 @@ colors.FRONTIER_E);
320348
}
321349
}
322350

323-
324-
// console.log(['vis.graph.colorEdge', c_cV, c_m, colors.N_M_E]);
325351
vis.graph.removeEdgeColor(c_cV, c_m);
326352
vis.graph.colorEdge(c_cV, c_m, colors.N_M_E);
327353

328354
refresh(vis, v, c_miniIndex, c_cV, c_m);
329-
330-
331355
},
332356
[[nodes, parents, minCosts, finalCosts], miniIndex, last, prev, currentVertex,
333357
m]
@@ -401,19 +425,10 @@ colors.FRONTIER_E);
401425
vis.graph.removeNodeColor(c_prev_m);
402426
vis.graph.colorEdge(c_prev_m, c_m, colors.FRONTIER_E);
403427
vis.graph.colorNode(c_prev_m, 1);
404-
//vis.graph.deselect(c_prev_m, c_prev_m);
405-
//vis.graph.select(c_prev_m, c_m);
406428

407429
// disconnect from the previous parent
408430
if (c_lastP != null) {
409-
410-
// vis.graph.visit(c_m,[c_lastP]);
411-
412431
vis.graph.removeEdgeColor(c_lastP, c_m);
413-
//vis.graph.deselect(c_lastP, c_m);
414-
415-
// vis.graph.colorNode(c_lastP, 1);
416-
//vis.graph.select(c_lastP, c_lastP);
417432
}
418433

419434
refresh(vis, c_nodes_etc, c_miniIndex, c_cV, c_m);
@@ -425,11 +440,6 @@ colors.FRONTIER_E);
425440
}
426441
}
427442
}
428-
// XXX add stuff to deal with while loop termination due to
429-
// PQ being empty
430443
},
431444

432-
433-
434-
435445
};

src/algorithms/explanations/DIJKExp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ results in finding shortest paths to all connected nodes.
6969

7070

7171

72+

src/algorithms/pseudocode/dijkstra.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Shortest(G, s) //Given a graph G find a shortest path from start node s \\B 1
3434
update Nodes, Parent etc with n & m \\Ref UpdateNodes
3535
\\In}
3636
\\In}
37-
return \\B 5a
37+
return \\B 99
3838
\\Expl{ The shortest path to every node connected to s has been found.
3939
If we were searching for an end node we have failed
4040
and some indication of this should be returned.

0 commit comments

Comments
 (0)