Skip to content

Commit ff7230f

Browse files
committed
Edge colours fixed for BFS, DFS
1 parent 46d4564 commit ff7230f

File tree

2 files changed

+106
-196
lines changed

2 files changed

+106
-196
lines changed

src/algorithms/controllers/BFS.js

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ export default {
121121

122122
chunker.add(
123123
8,
124-
(vis, x, y, z, a, b) => {
124+
(vis, x, y) => {
125125
vis.array.set(x, 'BFS');
126126
vis.array.setList(y);
127127

128128
// select start node in blue
129-
vis.array.select(0,b + 1);
130-
vis.graph.colorNode(b, colors.FRONTIER_N);
129+
vis.array.select(0, s + 1, 0, s + 1, colors.FRONTIER_A);
130+
vis.graph.colorNode(s, colors.FRONTIER_N);
131131
},
132-
[[displayedNodes, displayedParent, displayedVisited], displayedQueue, explored, visited, s, Nodes]
132+
[[displayedNodes, displayedParent, displayedVisited], displayedQueue]
133133
);
134134

135135
// Nodes <- queue containing just s B9
@@ -148,7 +148,7 @@ export default {
148148
);
149149

150150
// while Nodes not empty B2
151-
// XXX ?nice to have while true and break out of the
151+
// ?nice to have while true and break out of the
152152
// loop after the chunk (conditionally) so when the loop exits
153153
// we have an extra chunk at the start of the loop
154154
// while (Nodes.length > 0) {
@@ -175,17 +175,16 @@ export default {
175175

176176
// remove the highlight between n
177177
// and the last visited neighbor
178-
if((c_n != null) && (c_lastNei != null)){
179-
180-
vis.graph.removeEdgeColor(c_n, c_lastNei);
181-
let prevColor;
182-
if (c_Nodes.includes(c_lastNei))
183-
prevColor = colors.FRONTIER_E;
184-
else
185-
prevColor = colors.FINALISED_E;
178+
if((c_n != null) && (c_lastNei != null)) {
186179
vis.graph.removeEdgeColor(c_n, c_lastNei);
187-
vis.graph.colorEdge(c_n, c_lastNei, prevColor);
188-
}
180+
if((c_n != null) && (c_lastNei != null)){
181+
vis.graph.removeEdgeColor(c_n, c_lastNei);
182+
if (c_parent[c_lastNei] === c_n)
183+
vis.graph.colorEdge(c_n, c_lastNei, colors.FRONTIER_E);
184+
else if (c_parent[c_n] === c_lastNei)
185+
vis.graph.colorEdge(c_n, c_lastNei, colors.FINALISED_E);
186+
}
187+
}
189188
},
190189
[n, lastNeighbor, parent, visited, Nodes]
191190
);
@@ -225,7 +224,7 @@ export default {
225224

226225
// finalise edge color to n from parent
227226
if (c_parent[c_n] !== null) {
228-
vis.graph.removeEdgeColor(c_n, c_parent[c_n]);
227+
vis.graph.removeEdgeColor(c_n, c_parent[c_n]);
229228
vis.graph.colorEdge(c_n, c_parent[c_n], colors.FINALISED_E);
230229
}
231230

@@ -302,23 +301,14 @@ export default {
302301
chunker.add(
303302
4,
304303
(vis, c_n, c_m, c_lastNei, c_parent, c_visited, c_Nodes) => {
305-
//remove the color on Edge connecting the last neighbor
306-
307-
308-
309-
//remove the orange highlight from the edge connecting the last neighbour
310-
if (c_lastNei != null)
311-
{
304+
// remove the highlight between n
305+
// and the last visited neighbor
306+
if((c_n != null) && (c_lastNei != null)) {
312307
vis.graph.removeEdgeColor(c_n, c_lastNei);
313-
314-
// recolor if node has a parent or is start
315-
if(c_parent[c_lastNei] != null || c_lastNei == start) {
316-
vis.graph.removeEdgeColor(c_n, c_lastNei);
317-
if (c_parent[c_n] === c_lastNei)
318-
vis.graph.colorEdge(c_n, c_lastNei, colors.FINALISED_E);
319-
else if (c_parent[c_lastNei] === c_n)
320-
vis.graph.colorEdge(c_n, c_lastNei, colors.FRONTIER_E);
321-
}
308+
if (c_parent[c_n] === c_lastNei)
309+
vis.graph.colorEdge(c_n, c_lastNei, colors.FINALISED_E);
310+
else if (c_parent[c_lastNei] === c_n)
311+
vis.graph.colorEdge(c_n, c_lastNei, colors.FRONTIER_E);
322312
}
323313

324314
//highlight the edge connecting the neighbor
@@ -486,17 +476,16 @@ export default {
486476

487477
// remove the highlight between n
488478
// and the last visited neighbor
489-
if((c_n != null) && (c_lastNei != null)){
490-
491-
vis.graph.removeEdgeColor(c_n, c_lastNei);
492-
let prevColor;
493-
if (c_Nodes.includes(c_lastNei))
494-
prevColor = colors.FRONTIER_E;
495-
else
496-
prevColor = colors.FINALISED_E;
479+
if((c_n != null) && (c_lastNei != null)) {
497480
vis.graph.removeEdgeColor(c_n, c_lastNei);
498-
vis.graph.colorEdge(c_n, c_lastNei, prevColor);
499-
}
481+
if((c_n != null) && (c_lastNei != null)){
482+
vis.graph.removeEdgeColor(c_n, c_lastNei);
483+
if (c_parent[c_lastNei] === c_n)
484+
vis.graph.colorEdge(c_n, c_lastNei, colors.FRONTIER_E);
485+
else if (c_parent[c_n] === c_lastNei)
486+
vis.graph.colorEdge(c_n, c_lastNei, colors.FINALISED_E);
487+
}
488+
}
500489
},
501490
[n, lastNeighbor, parent, visited, Nodes]
502491
);

0 commit comments

Comments
 (0)