3
3
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer' ;
4
4
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer' ;
5
5
6
+ // Colors for array, graph nodes, graph edges
6
7
// OMG, colors for array and graph require different types and are
7
8
// inconsistent!
8
9
// XXX not sure how this interracts with color perception options -
9
10
// doesnt seem to work like this
10
11
// XXX should do similar for edge colors?
11
12
const FRONTIER_COLOR_A = '0' ; // Blue
12
- const FRONTIER_COLOR_G = 4 ; // Blue
13
+ const FRONTIER_COLOR_N = 4 ; // Blue
14
+ const FRONTIER_COLOR_E = 4 ; // Blue
15
+ const N_M_COLOR_E = 2 ; // Orange - edge between n and m
13
16
const FINALISED_COLOR_A = '1' ; // Green
14
- const FINALISED_COLOR_G = 1 ; // Green
17
+ const FINALISED_COLOR_N = 1 ; // Green
18
+ const FINALISED_COLOR_E = 3 ; // Red
19
+ // if we find a path to end node:
20
+ const SUCCESS_COLOR_A = '1' ; // Green
21
+ const SUCCESS_COLOR_E = 1 ; // Green
15
22
16
23
export default {
17
24
initVisualisers ( ) {
@@ -73,7 +80,7 @@ export default {
73
80
const end = endNodes [ 0 ] - 1 ;
74
81
75
82
let lastNeighbor = null ;
76
- let n = null
83
+ let n = null ;
77
84
78
85
// BFS(G, s) B1
79
86
chunker . add (
@@ -138,7 +145,7 @@ export default {
138
145
139
146
// select start node in blue
140
147
vis . array . select ( 0 , b + 1 ) ;
141
- vis . graph . colorNode ( b , FRONTIER_COLOR_G ) ;
148
+ vis . graph . colorNode ( b , FRONTIER_COLOR_N ) ;
142
149
} ,
143
150
[ [ displayedNodes , displayedParent , displayedVisited ] , displayedQueue , explored , visited , s , Nodes ]
144
151
) ;
@@ -162,7 +169,7 @@ export default {
162
169
while ( Nodes . length > 0 ) {
163
170
chunker . add (
164
171
2 ,
165
- ( vis , c_n , y , z , c_visited , c_Nodes ) => {
172
+ ( vis , c_n , c_lastNei , c_parent , c_visited , c_Nodes ) => {
166
173
// removes m if it exists; need to redo node selection etc
167
174
// for green nodes:(
168
175
vis . array . assignVariable ( 'm' , 2 , undefined ) ; // removes m if there
@@ -181,23 +188,25 @@ export default {
181
188
182
189
// remove the highlight between n
183
190
// and the last visited neighbor
184
- if ( ( c_n != null ) && ( y != null ) ) {
185
- vis . graph . removeEdgeColor ( c_n , y ) ;
186
- // recolor its edge connecting to its parent
187
- if ( z [ y ] != null ) {
188
- vis . graph . removeEdgeColor ( z [ y ] , y ) ;
189
- vis . graph . colorEdge ( z [ y ] , y , 3 ) ;
190
- }
191
-
191
+ if ( ( c_n != null ) && ( c_lastNei != null ) ) {
192
+
193
+ vis . graph . removeEdgeColor ( c_n , c_lastNei ) ;
194
+ let prevColor ;
195
+ if ( c_Nodes . includes ( c_lastNei ) )
196
+ prevColor = FRONTIER_COLOR_E ;
197
+ else
198
+ prevColor = FINALISED_COLOR_E ;
199
+ vis . graph . removeEdgeColor ( c_n , c_lastNei ) ;
200
+ vis . graph . colorEdge ( c_n , c_lastNei , prevColor ) ;
192
201
}
193
202
194
- // recolor in red if it has a child
195
- for ( let i = 0 ; i < z . length ; i ++ ) {
196
- if ( z [ i ] == y ) {
197
- vis . graph . removeEdgeColor ( i , y ) ;
198
- vis . graph . colorEdge ( i , y , 3 ) ;
199
- }
200
- }
203
+ // // recolor in red if it has a child
204
+ // for(let i = 0; i < c_parent .length; i ++){
205
+ // if (c_parent [i] == c_lastNei ){
206
+ // vis.graph.removeEdgeColor(i, c_lastNei );
207
+ // vis.graph.colorEdge(i, c_lastNei, FRONTIER_COLOR_E );
208
+ // }
209
+ // }
201
210
} ,
202
211
[ n , lastNeighbor , parent , visited , Nodes ]
203
212
@@ -208,13 +217,13 @@ export default {
208
217
displayedQueue . shift ( ) ;
209
218
chunker . add (
210
219
10 ,
211
- ( vis , x , y , z , a , b , Nodes ) => {
220
+ ( vis , x , c_n , z , a , b , c_parent , Nodes ) => {
212
221
//reset
213
222
vis . array . set ( x , 'BFS' ) ;
214
223
//add a string "n" below the currently popped out node
215
- vis . array . assignVariable ( 'n' , 2 , y + 1 ) ;
216
-
217
- // highlight all finalised nodes in blue
224
+ vis . array . assignVariable ( 'n' , 2 , c_n + 1 ) ;
225
+
226
+ // highlight all frontier nodes
218
227
for ( let i = 0 ; i < a . length ; i ++ ) {
219
228
if ( a [ i ] == true && Nodes . includes ( i ) ) {
220
229
vis . array . select ( 0 , i + 1 , 0 , i + 1 , FRONTIER_COLOR_A ) ;
@@ -223,21 +232,28 @@ export default {
223
232
}
224
233
}
225
234
226
- // highlight all other seen nodes in green
235
+ // highlight all other finalised nodes
227
236
for ( let i = 0 ; i < b . length ; i ++ )
228
237
{
229
238
if ( b [ i ] == true && ! Nodes . includes ( i ) )
230
239
{
231
240
vis . array . select ( 0 , i + 1 , 0 , i + 1 , FINALISED_COLOR_A ) ;
232
241
vis . graph . removeNodeColor ( i ) ;
233
- vis . graph . colorNode ( i , FINALISED_COLOR_G ) ;
242
+ vis . graph . colorNode ( i , FINALISED_COLOR_N ) ;
234
243
}
235
244
}
245
+
246
+ // finalise edge color to n from parent
247
+ if ( c_parent [ c_n ] !== null ) {
248
+ vis . graph . removeEdgeColor ( c_n , c_parent [ c_n ] ) ;
249
+ vis . graph . colorEdge ( c_n , c_parent [ c_n ] , FINALISED_COLOR_E ) ;
250
+ }
236
251
237
252
//redisplay queue
238
253
vis . array . setList ( z ) ;
239
254
} ,
240
- [ [ displayedNodes , displayedParent , displayedVisited ] , n , displayedQueue , explored , visited , Nodes ]
255
+ [ [ displayedNodes , displayedParent , displayedVisited ] , n ,
256
+ displayedQueue , explored , visited , parent , Nodes ]
241
257
) ;
242
258
243
259
// If is_end_node(n) B11
@@ -260,14 +276,14 @@ export default {
260
276
if ( c_parent [ y ] != null )
261
277
{
262
278
vis . graph . removeEdgeColor ( c_parent [ y ] , y ) ;
263
- vis . graph . colorEdge ( c_parent [ y ] , y , 3 ) ;
279
+ vis . graph . colorEdge ( c_parent [ y ] , y , FRONTIER_COLOR_E ) ;
264
280
}
265
281
266
282
// recolor in red if it has a child
267
283
for ( let i = 0 ; i < c_parent . length ; i ++ ) {
268
284
if ( c_parent [ i ] == y ) {
269
285
vis . graph . removeEdgeColor ( i , y ) ;
270
- vis . graph . colorEdge ( i , y , 3 ) ;
286
+ vis . graph . colorEdge ( i , y , FRONTIER_COLOR_E ) ;
271
287
}
272
288
}
273
289
}
@@ -285,9 +301,9 @@ export default {
285
301
// + color parent array
286
302
while ( ( current != a ) && ( c_parent [ current ] != null ) )
287
303
{
288
- vis . array . select ( 1 , current + 1 , 1 , current + 1 , FINALISED_COLOR_A ) ;
304
+ vis . array . select ( 1 , current + 1 , 1 , current + 1 , SUCCESS_COLOR_A ) ;
289
305
vis . graph . removeEdgeColor ( current , c_parent [ current ] ) ;
290
- vis . graph . colorEdge ( current , c_parent [ current ] , 1 ) ;
306
+ vis . graph . colorEdge ( current , c_parent [ current ] , SUCCESS_COLOR_E ) ;
291
307
current = c_parent [ current ] ;
292
308
}
293
309
} ,
@@ -298,45 +314,49 @@ export default {
298
314
}
299
315
300
316
// for each node m neighbouring n B4
317
+ lastNeighbor = null ;
301
318
for ( let m = 0 ; m < numVertices ; m ++ ) {
302
319
303
320
if ( E [ n ] [ m ] != 0 ) {
304
321
305
322
chunker . add (
306
323
4 ,
307
- ( vis , c_n , c_m , z , a , c_visited , c_Nodes ) => {
324
+ ( vis , c_n , c_m , c_lastNei , c_parent , c_visited , c_Nodes ) => {
308
325
//remove the color on Edge connecting the last neighbor
309
326
310
327
311
328
312
329
//remove the orange highlight from the edge connecting the last neighbour
313
- if ( z != null )
330
+ if ( c_lastNei != null )
314
331
{
315
- vis . graph . removeEdgeColor ( c_n , z ) ;
316
-
317
- // recolor in red if it has a parent
318
- if ( a [ z ] != null )
319
- {
320
- vis . graph . removeEdgeColor ( a [ z ] , z ) ;
321
- vis . graph . colorEdge ( a [ z ] , z , 3 ) ;
332
+ vis . graph . removeEdgeColor ( c_n , c_lastNei ) ;
333
+
334
+ // recolor if node has a parent or is start
335
+ if ( c_parent [ c_lastNei ] != null || c_lastNei == start ) {
336
+ vis . graph . removeEdgeColor ( c_n , c_lastNei ) ;
337
+ // XXX if not just added or previously FINALISED_COLOR_E
338
+ if ( c_parent [ c_n ] === c_lastNei )
339
+ vis . graph . colorEdge ( c_n , c_lastNei , FINALISED_COLOR_E ) ;
340
+ else if ( c_parent [ c_lastNei ] === c_n )
341
+ vis . graph . colorEdge ( c_n , c_lastNei , FRONTIER_COLOR_E ) ;
322
342
}
323
343
324
344
// recolor in red if it has a child
325
- for ( let i = 0 ; i < a . length ; i ++ ) {
326
- if ( a [ i ] == z ) {
327
- vis . graph . removeEdgeColor ( i , z ) ;
328
- vis . graph . colorEdge ( i , z , 3 ) ;
329
- }
330
- }
345
+ // for(let i = 0; i < c_parent .length; i ++){
346
+ // if (c_parent [i] == c_lastNei ){
347
+ // vis.graph.removeEdgeColor(i, c_lastNei );
348
+ // vis.graph.colorEdge(i, c_lastNei, FRONTIER_COLOR_E );
349
+ // }
350
+ // }
331
351
332
352
}
333
353
334
354
//highlight the edge connecting the neighbor
335
355
vis . graph . removeEdgeColor ( c_n , c_m ) ;
336
356
// color 2 = orange doesn't show up well - 4 = green better?
337
- vis . graph . colorEdge ( c_n , c_m , 2 ) ;
357
+ vis . graph . colorEdge ( c_n , c_m , N_M_COLOR_E ) ;
338
358
339
- // add var m; need to color elements again:(
359
+ // add var m; need to color elements again
340
360
vis . array . assignVariable ( 'm' , 2 , c_m + 1 ) ;
341
361
// highlight all nodes explored in green in the array
342
362
// and all other seen nodes in blue in the array
@@ -394,7 +414,7 @@ export default {
394
414
395
415
// color the graph node in blue as seen
396
416
vis . graph . removeNodeColor ( c_m ) ;
397
- vis . graph . colorNode ( c_m , FRONTIER_COLOR_G ) ;
417
+ vis . graph . colorNode ( c_m , FRONTIER_COLOR_N ) ;
398
418
} ,
399
419
400
420
[ [ displayedNodes , displayedParent , displayedVisited ] , displayedQueue , explored , visited , n , m , Nodes ]
@@ -433,7 +453,7 @@ export default {
433
453
434
454
//highlight the edge from n to this neighbor in red
435
455
vis . graph . removeEdgeColor ( b , c ) ;
436
- vis . graph . colorEdge ( b , c , 3 ) ;
456
+ vis . graph . colorEdge ( b , c , FRONTIER_COLOR_E ) ;
437
457
438
458
439
459
// vis.array.set(x, 'BFS');
@@ -483,14 +503,14 @@ export default {
483
503
if ( z [ y ] != null )
484
504
{
485
505
vis . graph . removeEdgeColor ( z [ y ] , y ) ;
486
- vis . graph . colorEdge ( z [ y ] , y , 3 ) ;
506
+ vis . graph . colorEdge ( z [ y ] , y , FRONTIER_COLOR_E ) ;
487
507
}
488
508
489
509
// recolor in red if it has a child
490
510
for ( let i = 0 ; i < z . length ; i ++ ) {
491
511
if ( z [ i ] == y ) {
492
512
vis . graph . removeEdgeColor ( i , y ) ;
493
- vis . graph . colorEdge ( i , y , 3 ) ;
513
+ vis . graph . colorEdge ( i , y , FRONTIER_COLOR_E ) ;
494
514
}
495
515
}
496
516
}
0 commit comments