Skip to content

Commit 0196ac3

Browse files
committed
DFS++ mods - Linda's feedback
1 parent 51b539f commit 0196ac3

File tree

7 files changed

+78
-55
lines changed

7 files changed

+78
-55
lines changed

src/algorithms/controllers/DFSrec.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default {
104104
// if p !== 0 the n-p has been highlighted in the for loop
105105
if (c_p !== 0) {
106106
// set edge to finalised if parent unassigned or it was
107-
// previously finalised, otherwise bqack to frontier
107+
// previously finalised, otherwise back to frontier
108108
if (c_nodes_etc[PAR][c_n] === unassigned ||
109109
c_nodes_etc[PAR][c_n] === c_p ||
110110
c_nodes_etc[PAR][c_p] === c_n) {
@@ -256,6 +256,7 @@ export default {
256256
parents.push(unassigned);
257257
}
258258

259+
seen[startNode] = true; // moved before init
259260
chunker.add(
260261
'init',
261262
(vis, c_nodes_etc, c_n, c_m, c_stack) => {
@@ -265,15 +266,17 @@ export default {
265266
startNode, null, stack], 0
266267
);
267268

268-
seen[startNode] = true;
269-
chunker.add(
270-
'frontier_s',
271-
(vis, c_nodes_etc, c_n, c_m, c_stack) => {
272-
refresh(vis, c_nodes_etc, c_n, null, c_stack);
273-
},
274-
[[nodes, parents, seen],
275-
startNode, null, stack], 0
276-
);
269+
// Previously a separate line
270+
// seen[startNode] = true; // moved before init
271+
// chunker.add(
272+
// 'frontier_s',
273+
// (vis, c_nodes_etc, c_n, c_m, c_stack) => {
274+
// refresh(vis, c_nodes_etc, c_n, null, c_stack);
275+
// },
276+
// [[nodes, parents, seen],
277+
// startNode, null, stack], 0
278+
// );
279+
277280
let result = dfs1(startNode, 0, 0);
278281
chunker.add(
279282
'top_call',

src/algorithms/controllers/graphSearchColours.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
//
44
// OMG, colors for array and graph require different types and are
55
// inconsistent!
6+
// Arrary: '0'=Blue, '1'=Green
7+
// Nodes: 1=Green, 4= Blue
8+
// Edges: 1=Green, 2=Orange, 3=Red, 4=Blue
69
// XXX not sure how this interracts with color perception options -
710
// doesn't seem to work like this - should figure out how it's done if
811
// it's still supported
912
export const colors = {
1013
FRONTIER_A: '0', // Blue
1114
FRONTIER_N: 4, // Blue
1215
FRONTIER_E: 4, // Blue
13-
N_M_E: 3, // Red - edge between n and m
16+
N_M_E: 2, // edge between n and m
1417
FINALISED_A: '1', // Green
1518
FINALISED_N: 1, // Green
16-
FINALISED_E: 2, // Orange
19+
FINALISED_E: 1,
1720
PQ_MIN_A: '1', // XXX poor color but setting colors is a mystery
1821
// if we find a path to end node:
1922
SUCCESS_A: '1', // Green
20-
SUCCESS_E: 1, // Green
23+
SUCCESS_E: 3,
2124
}

src/algorithms/controllers/prim.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ export default {
260260
99,
261261
(vis, v, c_miniIndex, c_cV, c_m, c_totalCost) => {
262262
refresh(vis, v, c_miniIndex, c_cV, c_m, c_totalCost);
263+
// re-do finalised edges in success colour for extra highlight
264+
for (let i = 0; i < numVertices; i++) {
265+
for (let j = 0; j < numVertices; j++) {
266+
if (v[PAR][i+1] === j+1) {
267+
vis.graph.removeEdgeColor(i, j);
268+
vis.graph.colorEdge(i, j, colors.SUCCESS_E);
269+
}
270+
}
271+
}
263272
},
264273
[[nodes, parents, minCosts, finalCosts], null,
265274
null, null, totalCost]
@@ -314,6 +323,15 @@ export default {
314323
(vis, v, c_miniIndex, c_cV, c_m, c_totalCost) => {
315324
vis.graph.removeNodeColor(currentVertex);
316325
refresh(vis, v, c_miniIndex, c_cV, c_m, c_totalCost);
326+
// re-do finalised edges in success colour for extra highlight
327+
for (let i = 0; i < numVertices; i++) {
328+
for (let j = 0; j < numVertices; j++) {
329+
if (v[PAR][i+1] === j+1) {
330+
vis.graph.removeEdgeColor(i, j);
331+
vis.graph.colorEdge(i, j, colors.SUCCESS_E);
332+
}
333+
}
334+
}
317335
},
318336
[[nodes, parents, minCosts, finalCosts], miniIndex,
319337
currentVertex, null, totalCost]

src/algorithms/explanations/BFSExp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ data structures are needed.
5858
For consistency with other algorithm animations, the layout of the
5959
graph is on a two-dimensional grid where each node has (x,y) integer
6060
coordinates. You can choose the start and end nodes and change the
61-
graph choice (see the instructions tab for more details). Weights of
62-
edges can be included in the text box input but BFS will ignore weights
61+
graph choice (see the instructions tab for more details). While weights of
62+
edges can be included in the text box input, BFS will ignore weights
6363
and positions of nodes. Only a single end node is supported; choosing
6464
0 results in finding paths to all connected nodes.
6565

src/algorithms/explanations/DFSExp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ data structures are needed.
4545
For consistency with other algorithm animations, the layout of the
4646
graph is on a two-dimensional grid where each node has (x,y) integer
4747
coordinates. You can choose the start and end nodes and change the
48-
graph choice (see the instructions tab for more details). Weights of
49-
edges can be included in the text box input but DFS will ignore weights
48+
graph choice (see the instructions tab for more details). While eights of
49+
edges can be included in the text box input, DFS will ignore weights
5050
and positions of nodes. Only a single end node is supported; choosing
5151
0 results in finding paths to all connected nodes.
5252

src/algorithms/index.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,22 @@ const allalgs = {
107107
},
108108
},
109109

110-
'BFS': {
111-
112-
name: 'Breadth First Search',
110+
'DFSrec': {
111+
name: 'Depth First Search',
113112
category: 'Graph',
114-
param: <Param.BFSParam/>,
115-
instructions: Instructions.BFSInstruction,
116-
explanation: Explanation.BFSExp,
117-
extraInfo: ExtraInfo.BFSInfo,
113+
param: <Param.DFSrecParam />,
114+
instructions: Instructions.DFSrecInstruction,
115+
explanation: Explanation.DFSrecExp,
116+
extraInfo: ExtraInfo.DFSrecInfo,
118117
pseudocode: {
119-
find: Pseudocode.BFS,
118+
find: Pseudocode.DFSrec,
120119
},
121120
controller: {
122-
find: Controller.BFS,
121+
find: Controller.DFSrec,
123122
},
124123
},
125124
'DFS': {
126-
name: 'Depth First Search',
125+
name: 'DFS (iterative)',
127126
category: 'Graph',
128127
param: <Param.DFSParam />,
129128
instructions: Instructions.DFSInstruction,
@@ -136,6 +135,21 @@ const allalgs = {
136135
find: Controller.DFS,
137136
},
138137
},
138+
'BFS': {
139+
140+
name: 'Breadth First Search',
141+
category: 'Graph',
142+
param: <Param.BFSParam/>,
143+
instructions: Instructions.BFSInstruction,
144+
explanation: Explanation.BFSExp,
145+
extraInfo: ExtraInfo.BFSInfo,
146+
pseudocode: {
147+
find: Pseudocode.BFS,
148+
},
149+
controller: {
150+
find: Controller.BFS,
151+
},
152+
},
139153
'dijkstra': {
140154
name: 'Dijkstra\'s (shortest path)',
141155
category: 'Graph',
@@ -196,20 +210,6 @@ const allalgs = {
196210
find: Controller.prim_old,
197211
},
198212
},
199-
'DFSrec': {
200-
name: 'DFS (recursive)',
201-
category: 'Graph',
202-
param: <Param.DFSrecParam />,
203-
instructions: Instructions.DFSrecInstruction,
204-
explanation: Explanation.DFSrecExp,
205-
extraInfo: ExtraInfo.DFSrecInfo,
206-
pseudocode: {
207-
find: Pseudocode.DFSrec,
208-
},
209-
controller: {
210-
find: Controller.DFSrec,
211-
},
212-
},
213213
'transitiveClosure': {
214214
name: 'Warshall\'s (transitive closure)',
215215
category: 'Graph',

src/algorithms/pseudocode/DFSrec.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,25 @@ made explicit
2626
2727
\\Code{
2828
Main
29-
DFS(G, n) // Given a graph G find a path from start node n to an \\B start
30-
// end node. It is assumed the end node(s) are defined
31-
// separately; if there are no end nodes, paths to all connected
32-
// nodes are found. Nodes are numbered 1..nmax. Returns the Parent[i]
33-
// array, which gives the previous node in the path from n to i.
34-
// Parent[n] = 0 and parents of unvisited nodes are null.
29+
DFS(G, n) // Given a graph G find a path from start node n to a \\B start
30+
// separately defined end node; if there are no end nodes, paths
31+
// to all connected nodes are found.
3532
\\In{
3633
initialise all parents to null \\B init
3734
\\Expl{ We initialise to some special value so we can later check if a
3835
parent has been assigned (we display null as an empty cell).
39-
\\Expl}
40-
// The start node is the implicit "frontier" initially \\B frontier_s
41-
\\Expl{ For recursive DFS it is not necessary to understand the frontier.
42-
We mention it just for comparison with the iterative version
43-
of DFS and related graph search algorithms.
36+
The start node is the implicit "frontier" for the top level DFS1
37+
call. It is not
38+
necessary to understand the frontier. We mention it just for
39+
comparison with the iterative version of DFS and related graph
40+
search algorithms.
4441
\\Expl}
4542
result <- DFS1(G, n, 0) // recursive DFS with zero as parent node \\B top_call
4643
return result \\B finish
4744
\\Expl{ If there can be several end nodes we may want to
4845
return which one is found as well as the Parent array for
49-
successful searches. Here we highlight the path found in
46+
successful searches (it defines a path from the end node back to
47+
the start node). Here we highlight the path found in
5048
the Parent array.
5149
\\Expl}
5250
\\In}
@@ -68,11 +66,12 @@ DFS1(G, n, p) // Search from node n using parent node p. This visits all \\B dfs
6866
\\In{
6967
return FOUND // Success! \\B found
7068
\\Expl{ If there can be several end nodes we may want to
71-
return which one is found as well as the Parent array.
69+
return which one is found as well as the Parent array,
70+
which defined the path from the end node back to the start.
7271
\\Expl}
7372
\\In}
7473
// The neighbouring nodes are now part of the (implicit) frontier \\B frontier
75-
\\Expl{ For recursive DFS it is not necessary to understand the frontier.
74+
\\Expl{ It is not necessary to understand the frontier.
7675
We mention it just for comparison with the iterative version
7776
of DFS and related graph search algorithms. The neighbouring nodes
7877
will be checked via a recursive call in the loop below (finalised

0 commit comments

Comments
 (0)