Skip to content

Commit d771dcb

Browse files
committed
BFS better, DFS similar
1 parent f93bddc commit d771dcb

File tree

7 files changed

+260
-140
lines changed

7 files changed

+260
-140
lines changed

src/algorithms/controllers/BFS.js

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
// XXX add support for multiple end nodes
1+
// XXX add support for multiple end nodes?
22
// XXX see README_graph_search
33
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer';
44
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer';
55

6+
// OMG, colors for array and graph require different types and are
7+
// inconsistent!
8+
// XXX not sure how this interracts with color perception options -
9+
// doesnt seem to work like this
10+
// XXX should do similar for edge colors?
11+
const FRONTIER_COLOR_A = '0'; // Blue
12+
const FRONTIER_COLOR_G = 4; // Blue
13+
const FINALISED_COLOR_A = '1'; // Green
14+
const FINALISED_COLOR_G = 1; // Green
15+
616
export default {
717
initVisualisers() {
818
return {
@@ -128,7 +138,7 @@ export default {
128138

129139
// select start node in blue
130140
vis.array.select(0,b + 1);
131-
vis.graph.colorNode(b, 4);
141+
vis.graph.colorNode(b, FRONTIER_COLOR_G);
132142
},
133143
[[displayedNodes, displayedParent, displayedVisited], displayedQueue, explored, visited, s, Nodes]
134144
);
@@ -162,12 +172,10 @@ export default {
162172
{
163173
if(c_visited[i] == true)
164174
if(!c_Nodes.includes(i)) {
165-
vis.array.select(0, i + 1, 0, i + 1, '1');
166-
// console.log(['Green', i+1]);
175+
vis.array.select(0, i + 1, 0, i + 1, FINALISED_COLOR_A);
167176
} else {
168177
// vis.array.deselect(0, i + 1);
169-
vis.array.select(0, i + 1);
170-
// console.log(['Blue', i+1]);
178+
vis.array.select(0, i + 1, 0, i + 1, FRONTIER_COLOR_A);
171179
}
172180
}
173181

@@ -209,8 +217,7 @@ export default {
209217
// highlight all finalised nodes in blue
210218
for (let i = 0; i < a.length; i ++){
211219
if(a[i] == true && Nodes.includes(i)){
212-
vis.array.select(0,i + 1);
213-
console.log(['Blue?', i+1]);
220+
vis.array.select(0,i + 1, 0, i + 1, FRONTIER_COLOR_A);
214221
// vis.array.select(0,i + 1, 0, i + 1, '1');
215222

216223
}
@@ -221,10 +228,9 @@ export default {
221228
{
222229
if(b[i] == true && !Nodes.includes(i))
223230
{
224-
console.log(['Green?', i+1]);
225-
vis.array.select(0, i + 1, 0, i + 1, '1');
231+
vis.array.select(0, i + 1, 0, i + 1, FINALISED_COLOR_A);
226232
vis.graph.removeNodeColor(i);
227-
vis.graph.colorNode(i, 1);
233+
vis.graph.colorNode(i, FINALISED_COLOR_G);
228234
}
229235
}
230236

@@ -240,41 +246,49 @@ export default {
240246
);
241247
if(n == end)
242248
{
243-
chunker.add(
244-
3,
245-
);
246249
// return B3
247250
chunker.add(
248251
3,
249-
(vis, x, y, z, a, b) => {
252+
(vis, x, y, c_parent, a, c_end) => {
250253
//remove the orange highlight from the last neighbour
251254
// XXX not needed?
252255
if (y != null)
253256
{
254257
vis.graph.removeEdgeColor(y, x);
255258

256259
// recolor in red if it has a parent
257-
if(z[y] != null)
260+
if(c_parent[y] != null)
258261
{
259-
vis.graph.removeEdgeColor(z[y], y);
260-
vis.graph.colorEdge(z[y], y , 3);
262+
vis.graph.removeEdgeColor(c_parent[y], y);
263+
vis.graph.colorEdge(c_parent[y], y , 3);
261264
}
262265

263266
// recolor in red if it has a child
264-
for(let i = 0; i < z.length; i ++){
265-
if (z[i] == y){
267+
for(let i = 0; i < c_parent.length; i ++){
268+
if (c_parent[i] == y){
266269
vis.graph.removeEdgeColor(i, y);
267270
vis.graph.colorEdge(i, y, 3);
268271
}
269272
}
270273
}
274+
// remove n, add start and end
275+
// vis.array.set(x, 'BFS');
276+
vis.array.assignVariable('n', 2, undefined);
277+
vis.array.assignVariable('end', 2, c_end + 1);
278+
vis.array.assignVariable('start', 2, s + 1);
279+
// remove color from node numbers
280+
for(let i = 0; i < c_parent.length; i++){
281+
vis.array.deselect(0, i);
282+
}
271283
// color the path from the start node to the end node
272-
let current = b;
273-
while((current != a) && (z[current] != null))
284+
let current = c_end;
285+
// + color parent array
286+
while((current != a) && (c_parent[current] != null))
274287
{
275-
vis.graph.removeEdgeColor(current, z[current]);
276-
vis.graph.colorEdge(current, z[current], 1);
277-
current = z[current];
288+
vis.array.select(1, current + 1, 1, current + 1, FINALISED_COLOR_A);
289+
vis.graph.removeEdgeColor(current, c_parent[current]);
290+
vis.graph.colorEdge(current, c_parent[current], 1);
291+
current = c_parent[current];
278292
}
279293
},
280294
[n, lastNeighbor, parent, start, end]
@@ -319,6 +333,7 @@ export default {
319333

320334
//highlight the edge connecting the neighbor
321335
vis.graph.removeEdgeColor(c_n,c_m);
336+
// color 2 = orange doesn't show up well - 4 = green better?
322337
vis.graph.colorEdge(c_n, c_m, 2);
323338

324339
// add var m; need to color elements again:(
@@ -329,12 +344,10 @@ export default {
329344
{
330345
if(c_visited[i] == true)
331346
if(!c_Nodes.includes(i)) {
332-
vis.array.select(0, i + 1, 0, i + 1, '1');
333-
// console.log(['Green', i+1]);
347+
vis.array.select(0, i + 1, 0, i + 1, FINALISED_COLOR_A);
334348
} else {
335349
// vis.array.deselect(0, i + 1);
336-
vis.array.select(0, i + 1);
337-
// console.log(['Blue', i+1]);
350+
vis.array.select(0, i + 1, 0, i + 1, FRONTIER_COLOR_A);
338351
}
339352
}
340353
},
@@ -371,19 +384,17 @@ export default {
371384
{
372385
if(c_visited[i] == true)
373386
if(!(c_Nodes.includes(i) || i === c_m)) {
374-
vis.array.select(0, i + 1, 0, i + 1, '1');
375-
// console.log(['Green', i+1]);
387+
vis.array.select(0, i + 1, 0, i + 1, FINALISED_COLOR_A);
376388
} else {
377389
// vis.array.deselect(0, i + 1);
378-
vis.array.select(0, i + 1);
379-
// console.log(['Blue', i+1]);
390+
vis.array.select(0, i + 1, 0, i + 1, FRONTIER_COLOR_A);
380391
}
381392
}
382393
vis.array.setList(y);
383394

384395
// color the graph node in blue as seen
385396
vis.graph.removeNodeColor(c_m);
386-
vis.graph.colorNode(c_m, 4);
397+
vis.graph.colorNode(c_m, FRONTIER_COLOR_G);
387398
},
388399

389400
[[displayedNodes, displayedParent, displayedVisited], displayedQueue, explored, visited, n, m, Nodes]
@@ -401,21 +412,18 @@ export default {
401412
vis.array.assignVariable('n', 2, b + 1);
402413
vis.array.assignVariable('m', 2, c + 1);
403414

404-
405-
console.log(z);
406-
console.log(c);
415+
// console.log(z);
416+
// console.log(c);
407417
// highlight all nodes explored in green in the array
408418
// and all other seen nodes in blue in the array
409419
for(let i = 0; i < a.length; i++)
410420
{
411421
if(z[i] == true || i === c)
412422
if(i !==c && !Nodes.includes(i)) {
413-
vis.array.select(0, i + 1, 0, i + 1, '1');
414-
console.log(['Green', i+1]);
423+
vis.array.select(0, i + 1, 0, i + 1, FINALISED_COLOR_A);
415424
} else {
416425
// vis.array.deselect(0, i + 1);
417-
vis.array.select(0, i + 1);
418-
console.log(['Blue', i+1]);
426+
vis.array.select(0, i + 1, 0, i + 1, FRONTIER_COLOR_A);
419427
}
420428
}
421429
vis.array.setList(y);
@@ -448,7 +456,7 @@ export default {
448456
// if it has not been seen
449457
if(y[z] == false && Nodes.includes(z)){
450458
vis.array.deselect(0, z + 1);
451-
vis.array.select(0, z + 1, 0, z + 1, '1');
459+
vis.array.select(0, z + 1, 0, z + 1, FINALISED_COLOR_A);
452460
//vis.graph.removeNodeColor(z);
453461
//vis.graph.colorNode(z, 4);
454462
}

0 commit comments

Comments
 (0)