Skip to content

Commit

Permalink
Graph alg final displays, input bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-naish committed May 8, 2024
1 parent c1a018d commit 11f7f69
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 81 deletions.
44 changes: 31 additions & 13 deletions src/algorithms/controllers/AStar.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ export default {
}

// color Min in PQ
if (c_Min != null)
if (c_Min != null) {
vis.array.select(MCOST, c_Min + 1, MCOST, c_Min + 1, colors.PQ_MIN_A);
vis.array.select(HEUR, c_Min + 1, HEUR, c_Min + 1, colors.PQ_MIN_A);
}
}

const endX = coords[end][0];
Expand Down Expand Up @@ -213,7 +214,7 @@ export default {

let currentVertex = null;
// while Nodes not Empty
// while (visited.size < numVertices) {
// while (visited.size < numVertices)
// extra chunk before break to make loop termination clearer
/* eslint-disable no-constant-condition */
while (true) {
Expand Down Expand Up @@ -296,10 +297,21 @@ export default {
chunker.add(10);
if (currentVertex === null // || currentVertex === end
|| cost[currentVertex] === Infinity) {
// terminate without finding end node
// currentVertex should never be null (?) but cost may be
// infinite
chunker.add(3);
// return without exploring all components & no end node

// undo last "remove next element" operation
minCosts[currentVertex+1] = infinityStr;
finalCosts[currentVertex+1] = dashStr;
miniIndex = currentVertex;
chunker.add(
3,
(vis, v, c_miniIndex, c_cV, c_m) => {
vis.graph.removeNodeColor(currentVertex);
refresh(vis, v, c_miniIndex, c_cV, c_m);
},
[[nodes, parents, minCosts, finalCosts], miniIndex,
currentVertex, null]
);
// return
break;
}
Expand All @@ -313,21 +325,27 @@ export default {
vis.array.assignVariable('end', 2, end + 1);
vis.array.assignVariable('start', 2, start + 1);
// remove color from node numbers
for(let i = 0; i < c_nodes_etc[NODE].length; i++){
vis.array.deselect(0, i);
}
// (done by vis.array.assignVariable, it seems):
// for(let i = 0; i < c_nodes_etc[NODE].length; i++){
// vis.array.deselect(0, i);
// }

// color the path from the start node to the end node
// + color nodes and parent array
let current = end;
let next = 0;
while((current != start) && (c_nodes_etc[PAR][current+1] != null))
{
next = c_nodes_etc[PAR][current+1]-1;
vis.array.select(NODE, current + 1, NODE, current + 1, colors.SUCCESS_A);
vis.array.select(PAR, current + 1, PAR, current + 1, colors.SUCCESS_A);
vis.graph.removeEdgeColor(current, c_nodes_etc[PAR][current+1]-1);
vis.graph.colorEdge(current, c_nodes_etc[PAR][current+1]-1, colors.SUCCESS_E);
current = c_nodes_etc[PAR][current+1]-1;
vis.graph.removeEdgeColor(current, next);
vis.graph.colorEdge(current, next, colors.SUCCESS_E);
current = next;
}

// also colour start node in array + final cost for end
vis.array.select(NODE, start + 1, NODE, start + 1, colors.SUCCESS_A);
vis.array.select(FCOST, end + 1, FCOST, end + 1, colors.SUCCESS_A);
},
[[nodes, heuristics, minCosts, parents, finalCosts]]
)
Expand Down
7 changes: 5 additions & 2 deletions src/algorithms/controllers/BFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default {
// ?nice to have while true and break out of the
// loop after the chunk (conditionally) so when the loop exits
// we have an extra chunk at the start of the loop
// while (Nodes.length > 0) {
// while (Nodes.length > 0)
/* eslint-disable no-constant-condition */
while (true) {
chunker.add(
Expand Down Expand Up @@ -286,6 +286,8 @@ export default {
vis.graph.colorEdge(current, c_parent[current], colors.SUCCESS_E);
current = c_parent[current];
}
// also color start node
vis.array.select(0, start + 1, 0, start + 1, colors.SUCCESS_A);
},
[n, lastNeighbor, parent, start, end]

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

// remove the highlight between n
// and the last visited neighbor
// and the last visited neighbor (XXX not needed?)
if((c_n != null) && (c_lastNei != null)) {
vis.graph.removeEdgeColor(c_n, c_lastNei);
if((c_n != null) && (c_lastNei != null)){
Expand Down
59 changes: 33 additions & 26 deletions src/algorithms/controllers/DFS.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// XXX add support for multiple end nodes?
// XXX see README_graph_search
// XXX see README_graph_search - this code is a nightmare to maintain
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer';
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer';
import {colors} from './graphSearchColours';
Expand Down Expand Up @@ -119,7 +119,7 @@ export default {
// ?nice to have while true and break out of the
// loop after the chunk (conditionally) so when the loop exits
// we have an extra chunk at the start of the loop
// while (Nodes.length > 0) {
// while (Nodes.length > 0)
/* eslint-disable no-constant-condition */
while (true) {
chunker.add(
Expand Down Expand Up @@ -212,9 +212,22 @@ export default {
// Return B12
chunker.add(
12,
(vis, x, y, z, a, b) => {
(vis, x, a, b) => {

vis.array.set(x, 'DFS');
// remove 'n'
vis.array.assignVariable('n', 2, undefined);

//highlight all finalized nodes in green in the array
for(let i = 0; i < a.length; i++)
{
if(a[i] == true)
{
vis.array.select(0, i + 1, 0, i + 1, colors.FINALISED_A);
}
}
},
[n, lastNeighbor, parent, start, end]
[[displayedNodes, displayedParent, displayedVisited], visited]
);
return;
}
Expand Down Expand Up @@ -333,6 +346,8 @@ displayedStack, explored, visited, n, parent]
vis.graph.colorEdge(current, c_parent[current], colors.SUCCESS_E);
current = c_parent[current];
}
// color start node
vis.array.select(0, start + 1, 0, start + 1, colors.SUCCESS_A);
},
[n, lastNeighbor, parent, start, end]
);
Expand Down Expand Up @@ -469,30 +484,22 @@ i+1);
//return B5
chunker.add(
5,
(vis, x, y, z, a, b) => {
//remove the orange highlight from the last neighbour
// not needed??
if (y != null)
{
vis.graph.removeEdgeColor(y, x);

// recolor in red if it has a parent
if(z[y] != null)
{
vis.graph.removeEdgeColor(z[y], y);
vis.graph.colorEdge(z[y], y , colors.FINALISED_E);
}

// recolor in red if it has a child
for(let i = 0; i < z.length; i ++){
if (z[i] == y){
vis.graph.removeEdgeColor(i, y);
vis.graph.colorEdge(i, y, colors.FINALISED_E);
}
(vis, x, a, b) => {

vis.array.set(x, 'DFS');
// remove 'n'
vis.array.assignVariable('n', 2, undefined);

//highlight all finalized nodes in green in the array
for(let i = 0; i < a.length; i++)
{
if(a[i] == true)
{
vis.array.select(0, i + 1, 0, i + 1, colors.FINALISED_A);
}
}
}
},
[n, lastNeighbor, parent, start, end]
[[displayedNodes, displayedParent, displayedVisited], visited]
);
};
/*
Expand Down
50 changes: 39 additions & 11 deletions src/algorithms/controllers/dijkstra.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default {

let currentVertex = null;
// while Nodes not Empty
// while (visited.size < numVertices) {
// while (visited.size < numVertices)
// extra chunk before break to make loop termination clearer
/* eslint-disable no-constant-condition */
while (true) {
Expand Down Expand Up @@ -235,6 +235,15 @@ export default {
);
if (!(visited.size < numVertices)) {
chunker.add(99); // return at end of function
chunker.add(
99,
(vis, v, c_miniIndex, c_cV, c_m) => {
// XXXXXX
// refresh(vis, v, c_miniIndex, c_cV, c_m);
},
[[nodes, parents, minCosts, finalCosts], miniIndex,
null, null]
);
break;
}

Expand Down Expand Up @@ -270,8 +279,21 @@ export default {
chunker.add(10);
if (currentVertex === null // || currentVertex === end
|| cost[currentVertex] === Infinity) {
// terminate without finding end node
chunker.add(3);
// return without exploring all components & no end node

// undo last "remove next element" operation
minCosts[currentVertex+1] = infinityStr;
finalCosts[currentVertex+1] = dashStr;
miniIndex = currentVertex;
chunker.add(
3,
(vis, v, c_miniIndex, c_cV, c_m) => {
vis.graph.removeNodeColor(currentVertex);
refresh(vis, v, c_miniIndex, c_cV, c_m);
},
[[nodes, parents, minCosts, finalCosts], miniIndex,
null, null]
);
// return
break;
}
Expand All @@ -281,25 +303,31 @@ export default {
(vis, c_nodes_etc) => {
// remove n, add start and end
vis.array.set(c_nodes_etc, algNameStr);
vis.array.assignVariable('n', 2, null);
vis.array.assignVariable('end', 2, end + 1);
vis.array.assignVariable('start', 2, start + 1);
vis.array.assignVariable('n', 2, null);
// remove color from node numbers
for(let i = 0; i < c_nodes_etc[NODE].length; i++){
vis.array.deselect(0, i);
}
// (done by vis.array.assignVariable, it seems):
// for(let i = 0; i < c_nodes_etc[NODE].length; i++){
// vis.array.deselect(0, i);
// }

// color the path from the start node to the end node
// + color nodes and parent array
let current = end;
let next = 0;
while((current != start) && (c_nodes_etc[PAR][current+1] != null))
{
next = c_nodes_etc[PAR][current+1]-1;
vis.array.select(NODE, current + 1, NODE, current + 1, colors.SUCCESS_A);
vis.array.select(PAR, current + 1, PAR, current + 1, colors.SUCCESS_A);
vis.graph.removeEdgeColor(current, c_nodes_etc[PAR][current+1]-1);
vis.graph.colorEdge(current, c_nodes_etc[PAR][current+1]-1, colors.SUCCESS_E);
current = c_nodes_etc[PAR][current+1]-1;
vis.graph.removeEdgeColor(current, next);
vis.graph.colorEdge(current, next, colors.SUCCESS_E);
current = next;
}

// also colour start node in array + final cost for end
vis.array.select(NODE, start + 1, NODE, start + 1, colors.SUCCESS_A);
vis.array.select(FCOST, end + 1, FCOST, end + 1, colors.SUCCESS_A);
},
[[nodes, parents, minCosts, finalCosts]]
)
Expand Down
Loading

0 comments on commit 11f7f69

Please sign in to comment.