1
- // XXX add support for multiple end nodes
1
+ // XXX add support for multiple end nodes?
2
2
// XXX see README_graph_search
3
3
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer' ;
4
4
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer' ;
5
5
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
+
6
16
export default {
7
17
initVisualisers ( ) {
8
18
return {
@@ -128,7 +138,7 @@ export default {
128
138
129
139
// select start node in blue
130
140
vis . array . select ( 0 , b + 1 ) ;
131
- vis . graph . colorNode ( b , 4 ) ;
141
+ vis . graph . colorNode ( b , FRONTIER_COLOR_G ) ;
132
142
} ,
133
143
[ [ displayedNodes , displayedParent , displayedVisited ] , displayedQueue , explored , visited , s , Nodes ]
134
144
) ;
@@ -162,12 +172,10 @@ export default {
162
172
{
163
173
if ( c_visited [ i ] == true )
164
174
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 ) ;
167
176
} else {
168
177
// 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 ) ;
171
179
}
172
180
}
173
181
@@ -209,8 +217,7 @@ export default {
209
217
// highlight all finalised nodes in blue
210
218
for ( let i = 0 ; i < a . length ; i ++ ) {
211
219
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 ) ;
214
221
// vis.array.select(0,i + 1, 0, i + 1, '1');
215
222
216
223
}
@@ -221,10 +228,9 @@ export default {
221
228
{
222
229
if ( b [ i ] == true && ! Nodes . includes ( i ) )
223
230
{
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 ) ;
226
232
vis . graph . removeNodeColor ( i ) ;
227
- vis . graph . colorNode ( i , 1 ) ;
233
+ vis . graph . colorNode ( i , FINALISED_COLOR_G ) ;
228
234
}
229
235
}
230
236
@@ -240,41 +246,49 @@ export default {
240
246
) ;
241
247
if ( n == end )
242
248
{
243
- chunker . add (
244
- 3 ,
245
- ) ;
246
249
// return B3
247
250
chunker . add (
248
251
3 ,
249
- ( vis , x , y , z , a , b ) => {
252
+ ( vis , x , y , c_parent , a , c_end ) => {
250
253
//remove the orange highlight from the last neighbour
251
254
// XXX not needed?
252
255
if ( y != null )
253
256
{
254
257
vis . graph . removeEdgeColor ( y , x ) ;
255
258
256
259
// recolor in red if it has a parent
257
- if ( z [ y ] != null )
260
+ if ( c_parent [ y ] != null )
258
261
{
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 ) ;
261
264
}
262
265
263
266
// 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 ) {
266
269
vis . graph . removeEdgeColor ( i , y ) ;
267
270
vis . graph . colorEdge ( i , y , 3 ) ;
268
271
}
269
272
}
270
273
}
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
+ }
271
283
// 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 ) )
274
287
{
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 ] ;
278
292
}
279
293
} ,
280
294
[ n , lastNeighbor , parent , start , end ]
@@ -319,6 +333,7 @@ export default {
319
333
320
334
//highlight the edge connecting the neighbor
321
335
vis . graph . removeEdgeColor ( c_n , c_m ) ;
336
+ // color 2 = orange doesn't show up well - 4 = green better?
322
337
vis . graph . colorEdge ( c_n , c_m , 2 ) ;
323
338
324
339
// add var m; need to color elements again:(
@@ -329,12 +344,10 @@ export default {
329
344
{
330
345
if ( c_visited [ i ] == true )
331
346
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 ) ;
334
348
} else {
335
349
// 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 ) ;
338
351
}
339
352
}
340
353
} ,
@@ -371,19 +384,17 @@ export default {
371
384
{
372
385
if ( c_visited [ i ] == true )
373
386
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 ) ;
376
388
} else {
377
389
// 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 ) ;
380
391
}
381
392
}
382
393
vis . array . setList ( y ) ;
383
394
384
395
// color the graph node in blue as seen
385
396
vis . graph . removeNodeColor ( c_m ) ;
386
- vis . graph . colorNode ( c_m , 4 ) ;
397
+ vis . graph . colorNode ( c_m , FRONTIER_COLOR_G ) ;
387
398
} ,
388
399
389
400
[ [ displayedNodes , displayedParent , displayedVisited ] , displayedQueue , explored , visited , n , m , Nodes ]
@@ -401,21 +412,18 @@ export default {
401
412
vis . array . assignVariable ( 'n' , 2 , b + 1 ) ;
402
413
vis . array . assignVariable ( 'm' , 2 , c + 1 ) ;
403
414
404
-
405
- console . log ( z ) ;
406
- console . log ( c ) ;
415
+ // console.log(z);
416
+ // console.log(c);
407
417
// highlight all nodes explored in green in the array
408
418
// and all other seen nodes in blue in the array
409
419
for ( let i = 0 ; i < a . length ; i ++ )
410
420
{
411
421
if ( z [ i ] == true || i === c )
412
422
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 ) ;
415
424
} else {
416
425
// 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 ) ;
419
427
}
420
428
}
421
429
vis . array . setList ( y ) ;
@@ -448,7 +456,7 @@ export default {
448
456
// if it has not been seen
449
457
if ( y [ z ] == false && Nodes . includes ( z ) ) {
450
458
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 ) ;
452
460
//vis.graph.removeNodeColor(z);
453
461
//vis.graph.colorNode(z, 4);
454
462
}
0 commit comments