Skip to content

Commit 11f7f69

Browse files
committed
Graph alg final displays, input bug fix
1 parent c1a018d commit 11f7f69

File tree

9 files changed

+191
-81
lines changed

9 files changed

+191
-81
lines changed

src/algorithms/controllers/AStar.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ export default {
113113
}
114114

115115
// color Min in PQ
116-
if (c_Min != null)
116+
if (c_Min != null) {
117117
vis.array.select(MCOST, c_Min + 1, MCOST, c_Min + 1, colors.PQ_MIN_A);
118118
vis.array.select(HEUR, c_Min + 1, HEUR, c_Min + 1, colors.PQ_MIN_A);
119+
}
119120
}
120121

121122
const endX = coords[end][0];
@@ -213,7 +214,7 @@ export default {
213214

214215
let currentVertex = null;
215216
// while Nodes not Empty
216-
// while (visited.size < numVertices) {
217+
// while (visited.size < numVertices)
217218
// extra chunk before break to make loop termination clearer
218219
/* eslint-disable no-constant-condition */
219220
while (true) {
@@ -296,10 +297,21 @@ export default {
296297
chunker.add(10);
297298
if (currentVertex === null // || currentVertex === end
298299
|| cost[currentVertex] === Infinity) {
299-
// terminate without finding end node
300-
// currentVertex should never be null (?) but cost may be
301-
// infinite
302-
chunker.add(3);
300+
// return without exploring all components & no end node
301+
302+
// undo last "remove next element" operation
303+
minCosts[currentVertex+1] = infinityStr;
304+
finalCosts[currentVertex+1] = dashStr;
305+
miniIndex = currentVertex;
306+
chunker.add(
307+
3,
308+
(vis, v, c_miniIndex, c_cV, c_m) => {
309+
vis.graph.removeNodeColor(currentVertex);
310+
refresh(vis, v, c_miniIndex, c_cV, c_m);
311+
},
312+
[[nodes, parents, minCosts, finalCosts], miniIndex,
313+
currentVertex, null]
314+
);
303315
// return
304316
break;
305317
}
@@ -313,21 +325,27 @@ export default {
313325
vis.array.assignVariable('end', 2, end + 1);
314326
vis.array.assignVariable('start', 2, start + 1);
315327
// remove color from node numbers
316-
for(let i = 0; i < c_nodes_etc[NODE].length; i++){
317-
vis.array.deselect(0, i);
318-
}
328+
// (done by vis.array.assignVariable, it seems):
329+
// for(let i = 0; i < c_nodes_etc[NODE].length; i++){
330+
// vis.array.deselect(0, i);
331+
// }
332+
319333
// color the path from the start node to the end node
320334
// + color nodes and parent array
321335
let current = end;
336+
let next = 0;
322337
while((current != start) && (c_nodes_etc[PAR][current+1] != null))
323338
{
339+
next = c_nodes_etc[PAR][current+1]-1;
324340
vis.array.select(NODE, current + 1, NODE, current + 1, colors.SUCCESS_A);
325341
vis.array.select(PAR, current + 1, PAR, current + 1, colors.SUCCESS_A);
326-
vis.graph.removeEdgeColor(current, c_nodes_etc[PAR][current+1]-1);
327-
vis.graph.colorEdge(current, c_nodes_etc[PAR][current+1]-1, colors.SUCCESS_E);
328-
current = c_nodes_etc[PAR][current+1]-1;
342+
vis.graph.removeEdgeColor(current, next);
343+
vis.graph.colorEdge(current, next, colors.SUCCESS_E);
344+
current = next;
329345
}
330-
346+
// also colour start node in array + final cost for end
347+
vis.array.select(NODE, start + 1, NODE, start + 1, colors.SUCCESS_A);
348+
vis.array.select(FCOST, end + 1, FCOST, end + 1, colors.SUCCESS_A);
331349
},
332350
[[nodes, heuristics, minCosts, parents, finalCosts]]
333351
)

src/algorithms/controllers/BFS.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export default {
151151
// ?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
154-
// while (Nodes.length > 0) {
154+
// while (Nodes.length > 0)
155155
/* eslint-disable no-constant-condition */
156156
while (true) {
157157
chunker.add(
@@ -286,6 +286,8 @@ export default {
286286
vis.graph.colorEdge(current, c_parent[current], colors.SUCCESS_E);
287287
current = c_parent[current];
288288
}
289+
// also color start node
290+
vis.array.select(0, start + 1, 0, start + 1, colors.SUCCESS_A);
289291
},
290292
[n, lastNeighbor, parent, start, end]
291293

@@ -462,6 +464,7 @@ export default {
462464
// removes m if it exists; need to redo node selection etc
463465
// for green nodes:
464466
vis.array.assignVariable('m', 2, undefined); // removes m if there
467+
vis.array.assignVariable('n', 2, undefined); // removes n
465468
// highlight all nodes explored in green in the array
466469
// and all other seen nodes in blue in the array
467470
for(let i = 0; i < c_visited.length; i++)
@@ -476,7 +479,7 @@ export default {
476479
}
477480

478481
// remove the highlight between n
479-
// and the last visited neighbor
482+
// and the last visited neighbor (XXX not needed?)
480483
if((c_n != null) && (c_lastNei != null)) {
481484
vis.graph.removeEdgeColor(c_n, c_lastNei);
482485
if((c_n != null) && (c_lastNei != null)){

src/algorithms/controllers/DFS.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// XXX add support for multiple end nodes?
2-
// XXX see README_graph_search
2+
// XXX see README_graph_search - this code is a nightmare to maintain
33
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer';
44
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer';
55
import {colors} from './graphSearchColours';
@@ -119,7 +119,7 @@ export default {
119119
// ?nice to have while true and break out of the
120120
// loop after the chunk (conditionally) so when the loop exits
121121
// we have an extra chunk at the start of the loop
122-
// while (Nodes.length > 0) {
122+
// while (Nodes.length > 0)
123123
/* eslint-disable no-constant-condition */
124124
while (true) {
125125
chunker.add(
@@ -212,9 +212,22 @@ export default {
212212
// Return B12
213213
chunker.add(
214214
12,
215-
(vis, x, y, z, a, b) => {
215+
(vis, x, a, b) => {
216+
217+
vis.array.set(x, 'DFS');
218+
// remove 'n'
219+
vis.array.assignVariable('n', 2, undefined);
220+
221+
//highlight all finalized nodes in green in the array
222+
for(let i = 0; i < a.length; i++)
223+
{
224+
if(a[i] == true)
225+
{
226+
vis.array.select(0, i + 1, 0, i + 1, colors.FINALISED_A);
227+
}
228+
}
216229
},
217-
[n, lastNeighbor, parent, start, end]
230+
[[displayedNodes, displayedParent, displayedVisited], visited]
218231
);
219232
return;
220233
}
@@ -333,6 +346,8 @@ displayedStack, explored, visited, n, parent]
333346
vis.graph.colorEdge(current, c_parent[current], colors.SUCCESS_E);
334347
current = c_parent[current];
335348
}
349+
// color start node
350+
vis.array.select(0, start + 1, 0, start + 1, colors.SUCCESS_A);
336351
},
337352
[n, lastNeighbor, parent, start, end]
338353
);
@@ -469,30 +484,22 @@ i+1);
469484
//return B5
470485
chunker.add(
471486
5,
472-
(vis, x, y, z, a, b) => {
473-
//remove the orange highlight from the last neighbour
474-
// not needed??
475-
if (y != null)
476-
{
477-
vis.graph.removeEdgeColor(y, x);
478-
479-
// recolor in red if it has a parent
480-
if(z[y] != null)
481-
{
482-
vis.graph.removeEdgeColor(z[y], y);
483-
vis.graph.colorEdge(z[y], y , colors.FINALISED_E);
484-
}
485-
486-
// recolor in red if it has a child
487-
for(let i = 0; i < z.length; i ++){
488-
if (z[i] == y){
489-
vis.graph.removeEdgeColor(i, y);
490-
vis.graph.colorEdge(i, y, colors.FINALISED_E);
491-
}
487+
(vis, x, a, b) => {
488+
489+
vis.array.set(x, 'DFS');
490+
// remove 'n'
491+
vis.array.assignVariable('n', 2, undefined);
492+
493+
//highlight all finalized nodes in green in the array
494+
for(let i = 0; i < a.length; i++)
495+
{
496+
if(a[i] == true)
497+
{
498+
vis.array.select(0, i + 1, 0, i + 1, colors.FINALISED_A);
492499
}
493-
}
500+
}
494501
},
495-
[n, lastNeighbor, parent, start, end]
502+
[[displayedNodes, displayedParent, displayedVisited], visited]
496503
);
497504
};
498505
/*

src/algorithms/controllers/dijkstra.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export default {
188188

189189
let currentVertex = null;
190190
// while Nodes not Empty
191-
// while (visited.size < numVertices) {
191+
// while (visited.size < numVertices)
192192
// extra chunk before break to make loop termination clearer
193193
/* eslint-disable no-constant-condition */
194194
while (true) {
@@ -235,6 +235,15 @@ export default {
235235
);
236236
if (!(visited.size < numVertices)) {
237237
chunker.add(99); // return at end of function
238+
chunker.add(
239+
99,
240+
(vis, v, c_miniIndex, c_cV, c_m) => {
241+
// XXXXXX
242+
// refresh(vis, v, c_miniIndex, c_cV, c_m);
243+
},
244+
[[nodes, parents, minCosts, finalCosts], miniIndex,
245+
null, null]
246+
);
238247
break;
239248
}
240249

@@ -270,8 +279,21 @@ export default {
270279
chunker.add(10);
271280
if (currentVertex === null // || currentVertex === end
272281
|| cost[currentVertex] === Infinity) {
273-
// terminate without finding end node
274-
chunker.add(3);
282+
// return without exploring all components & no end node
283+
284+
// undo last "remove next element" operation
285+
minCosts[currentVertex+1] = infinityStr;
286+
finalCosts[currentVertex+1] = dashStr;
287+
miniIndex = currentVertex;
288+
chunker.add(
289+
3,
290+
(vis, v, c_miniIndex, c_cV, c_m) => {
291+
vis.graph.removeNodeColor(currentVertex);
292+
refresh(vis, v, c_miniIndex, c_cV, c_m);
293+
},
294+
[[nodes, parents, minCosts, finalCosts], miniIndex,
295+
null, null]
296+
);
275297
// return
276298
break;
277299
}
@@ -281,25 +303,31 @@ export default {
281303
(vis, c_nodes_etc) => {
282304
// remove n, add start and end
283305
vis.array.set(c_nodes_etc, algNameStr);
284-
vis.array.assignVariable('n', 2, null);
285306
vis.array.assignVariable('end', 2, end + 1);
286307
vis.array.assignVariable('start', 2, start + 1);
308+
vis.array.assignVariable('n', 2, null);
287309
// remove color from node numbers
288-
for(let i = 0; i < c_nodes_etc[NODE].length; i++){
289-
vis.array.deselect(0, i);
290-
}
310+
// (done by vis.array.assignVariable, it seems):
311+
// for(let i = 0; i < c_nodes_etc[NODE].length; i++){
312+
// vis.array.deselect(0, i);
313+
// }
314+
291315
// color the path from the start node to the end node
292316
// + color nodes and parent array
293317
let current = end;
318+
let next = 0;
294319
while((current != start) && (c_nodes_etc[PAR][current+1] != null))
295320
{
321+
next = c_nodes_etc[PAR][current+1]-1;
296322
vis.array.select(NODE, current + 1, NODE, current + 1, colors.SUCCESS_A);
297323
vis.array.select(PAR, current + 1, PAR, current + 1, colors.SUCCESS_A);
298-
vis.graph.removeEdgeColor(current, c_nodes_etc[PAR][current+1]-1);
299-
vis.graph.colorEdge(current, c_nodes_etc[PAR][current+1]-1, colors.SUCCESS_E);
300-
current = c_nodes_etc[PAR][current+1]-1;
324+
vis.graph.removeEdgeColor(current, next);
325+
vis.graph.colorEdge(current, next, colors.SUCCESS_E);
326+
current = next;
301327
}
302-
328+
// also colour start node in array + final cost for end
329+
vis.array.select(NODE, start + 1, NODE, start + 1, colors.SUCCESS_A);
330+
vis.array.select(FCOST, end + 1, FCOST, end + 1, colors.SUCCESS_A);
303331
},
304332
[[nodes, parents, minCosts, finalCosts]]
305333
)

0 commit comments

Comments
 (0)