forked from jarvisnn/Algorithm-Visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_traversal.html
More file actions
494 lines (449 loc) · 19 KB
/
graph_traversal.html
File metadata and controls
494 lines (449 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>VisuAlgo - Graph Traversal</title>
<link rel="icon" type="image/png" href="img/favicon.png" />
<!--css-->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/viz.css">
<link rel="stylesheet" href="css/graph_traversal.css"> <!--not yet implemented-->
<!--js-->
<script src="js/external/jquery-1.10.2.js"></script>
<script src="js/external/jquery-ui.js"></script>
<script src="js/external/d3.v3.js"></script>
<script src="js/viz.js"></script>
<script src="js/common.js"></script>
<script src="js/actions/graph_traversal_actions.js"></script> <!--not yet implemented-->
<!--script src="//connect.facebook.net/en_US/all.js"></script-->
<!--fonts-->
<link href="fonts/silkscreen/stylesheet.css" rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="top-bar">
<a id="home" href="index.html">Visu<span class="colour">Algo</span></a>
<span id="title">
<a class="selected-viz">Graph Traversal</a>
</span>
<!--a id="fb-login" href="#" target="_blank">Facebook connect</a>
<span id="trick">
<fb:login-button show-faces="false" width="200" max-rows="1" style="float: right; margin-top: 9px;"></fb:login-button>
</span-->
<div id="mode-menu">
<div id="mode-button">Exploration Mode<img src="img/arrow_white.png" alt="Home"/></div>
<div id="other-modes">
<a href="#">Tutorial Mode</a>
</div>
</div>
</div>
<div id="viz"></div>
<div id="current-action" class="panel"><p></p></div>
<div id="actions" class="panel">
<p id="create" onclick=createRandom(true)>Random Graph</p>
<p id="sample">Sample Graphs</p>
<p id="directedChange" onclick=directedChange() >Directed <-> Undirected </p>
<p id="bfs">BFS</p>
<p id="dfs">DFS</p>
<p id="bridge" onclick=bridge() >Cut Vertex & Bridge</p>
<p id="scc"> SCC Algorithms</p>
<p id="bipartite">Bipartite Graph check</p>
<p id="topo">Topo Sort</p>
<p id="twosat">Two-SAT checker</p>
</div>
<div id="actions-hide" class="panel-hide"><img src="img/arrow_white_right.png" title="show/hide actions panel"/></div>
<div id="actions-extras">
<!--put all the extra action pullouts (input) and error messages here-->
<div class="create action-menu-pullout">
<div id="create-err" class="err"></div>
</div>
<div class="samples action-menu-pullout">
<div id="sample1" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_1)><p>CP3 4.1</p></div>
<div id="sample2" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_3)><p>CP3 4.3</p></div>
<div id="sample3" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_4)><p>CP3 4.4</p></div>
<div id="sample4" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_9)><p>CP3 4.9</p></div>
<div id="sample5" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_17)><p>CP3 4.17</p></div>
<div id="sample6" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_18)><p>CP3 4.18</p></div>
<div id="sample7" class="execAction new-menu-option coloured-menu-option" onclick=sample(SSSP_EXAMPLE_CP3_4_19)><p>CP3 4.19</p></div>
<div id="samples-err" class="err" style="z-index: 2000;"></div>
</div>
<div class="bfs action-menu-pullout">
<div id="bfs-input" class="new-menu-option"><input type="text" id="bfs-v" title="Enter the starting vertex" autocomplete="off" value=0 /></div>
<div id="bfs-go" class="execAction coloured-menu-option" onclick=bfs()><p>GO</p></div>
<div id="bfs-err" class="err"></div>
</div>
<div class="dfs action-menu-pullout">
<div id="dfs-input" class="new-menu-option"><input type="text" id="dfs-v" title="Enter the starting vertex" autocomplete="off" value=0 /></div>
<div id="dfs-go" class="execAction coloured-menu-option" onclick=dfs()><p>GO</p></div>
<div id="dfs-err" class="err"></div>
</div>
<div class="bridge action-menu-pullout">
<div id="bridge-err" class="err"></div>
</div>
<div class="scc action-menu-pullout">
<div id="tarjan" class="execAction new-menu-option coloured-menu-option" onclick=tarjan()><p>Tarjan</p></div>
<div id="kosaraju" class="execAction new-menu-option coloured-menu-option" onclick=kosaraju()><p>Kosaraju</p></div>
<div id="scc-err" class="err"></div>
</div>
<div class="bipartite action-menu-pullout">
<div id="bipartiteDfs" class="execAction new-menu-option coloured-menu-option" onclick=bipartiteDfs()><p>DFS</p></div>
<div id="bipartiteBfs" class="execAction new-menu-option coloured-menu-option" onclick=bipartiteBfs()><p>BFS</p></div>
<div id="bipartite-err" class="err"></div>
</div>
<div class="topo action-menu-pullout">
<div id="toposortDfs" class="execAction new-menu-option coloured-menu-option" onclick=toposortDfs()><p>DFS</p></div>
<div id="toposortBfs" class="execAction new-menu-option coloured-menu-option" onclick=toposortBfs()><p>BFS</p></div>
<div id="topo-err" class="err"></div>
</div>
<div class="twosat action-menu-pullout">
<div class="new-menu-option coloured-menu-option"><p>Number of clauses</p></div>
<div id="twosat-input" class="new-menu-option"><input type="text" id="twosat-v1" title="Enter the number of clauses" autocomplete="off" value=0 /></div>
<div class="new-menu-option coloured-menu-option"><p>Number of variables</p></div>
<div id="twosat-input" class="new-menu-option"><input type="text" id="twosat-v2" title="Enter the number of variables" autocomplete="off" value=0 /></div>
<div id="twosat-go" class="execAction coloured-menu-option" onclick=twosat()><p>GO</p></div>
<div id="twosat-err" class="err"></div>
</div>
<div class="directedChange action-menu-pullout">
<div id="directedChange-err" class="err"></div>
</div>
</div>
<div id="status" class="panel"><p></p></div>
<div id="status-hide" class="panel-hide"><img src="img/arrow_white_right.png" title="show/hide status panel"/></div>
<div id="codetrace" class="panel">
<!--can add class "highlighed" to hightlight one line-->
<p id="code1" style="padding-top: 10px;"></p>
<p id="code2"></p>
<p id="code3"></p>
<p id="code4"></p>
<p id="code5"></p>
<p id="code6"></p>
<p id="code7" style="padding-bottom: 10px;"></p>
</div>
<div id="codetrace-hide" class="panel-hide"><img src="img/arrow_white_right.png" title="show/hide codetrace panel"/></div>
<div id="left-bar"></div>
<div id="right-bar"></div>
<!-- Bottom bar and overlay popups -->
<div id="bottom-bar">
<a id="trigger-about">About</a>
<a id="trigger-team">Team</a>
<a id="trigger-terms">Terms of use</a>
</div>
<div id="media-controls">
<div id="speed-control">slow<div id="speed-input"></div>fast</div>
<span id="go-to-beginning" class="media-control-button" title="go to beginning" onclick=goToBeginning()><img src="img/goToBeginning.png" alt="go to beginning" /></span>
<span id="previous" class="media-control-button" title="step backward" onclick=stepBackward()><img src="img/prevFrame.png" alt="previous frame" /></span>
<span id="pause" class="media-control-button" title="pause" onclick=pause()><img src="img/pause.png" alt="pause" /></span>
<span id="play" class="media-control-button" title="play" onclick=play()><img src="img/play.png" alt="play" /></span>
<span id="next" class="media-control-button" title="step forward" onclick=stepForward()><img src="img/nextFrame.png" alt="next frame" /></span>
<span id="go-to-end" class="media-control-button" title="go to end" onclick=goToEnd()><img src="img/goToEnd.png" alt="go to end"/></span>
<div id="progress-bar" class="media-control-button"></div>
</div>
<div id="dark-overlay"></div>
<div id="about" class="overlays"></div>
<div id="team" class="overlays"></div>
<div id="termsofuse" class="overlays"></div>
<div id="twosat-board" class="overlays"></div>
<!--tutorial dialogs here-->
<div id="sssp-tutorial-1" class="tutorial-dialog">
In the Graph-Traversal problem, we mainly use DFS (Depth-First-Search) and BFS (Breadth-Frist-Search) to travel the graph.<br/><br/> We also mention about a lot of DFS and BFS variants such as cut vertex, bridge and SCC algorithms, ...
<div class="tutorial-next">Next<img src="img/arrow_white_right.png"/></div>
</div>
<div id="sssp-tutorial-2" class="tutorial-dialog">
Choose a sample graph <!--or draw your own graph-->and try running the different algorithms on it:<br><br>The basis O(n) DFS and BFS<br>The O(n) cut vertex and bridge algorithm<br>The scc algorithms<br>The bipartite graph checker<br>The topological sort algorithms<br>And the two-sat checker algorithm<br>...<div class="tutorial-next">Next<img src="img/arrow_white_right.png"/></div>
</div>
<div id="sssp-tutorial-3" class="tutorial-dialog">
When the algorithm is running, the animation will be show here.
<div class="tutorial-next">Next<img src="img/arrow_white_right.png"/></div>
</div>
<div id="sssp-tutorial-4" class="tutorial-dialog">
The status bar explains the execution of the algorithm at each animation step.
<div class="tutorial-next">Next<img src="img/arrow_white_right.png"/></div>
</div>
<div id="sssp-tutorial-5" class="tutorial-dialog">
You can also follow the psuedocode highlights to trace the algorithm.
<div class="tutorial-next">Next<img src="img/arrow_white_right.png"/></div>
</div>
<div id="sssp-tutorial-6" class="tutorial-dialog">
Control the animation with the player controls! Keyboard shortcuts are:<br/>
<div style="margin-top: 8px;"><strong>Spacebar:</strong> play/pause/replay</div>
<strong>Left/right arrows:</strong> step backward/step forward<br/>
<strong>-/+:</strong> decrease/increase speed<br/>
<div class="tutorial-next">Next<img src="img/arrow_white_right.png"/></div>
</div>
<div id="sssp-tutorial-7" class="tutorial-dialog">
Return to "Exploration Mode" to start exploring!
</div>
<script src="js/graph_library/constant.js"></script>
<script src="js/graph_library/properties.js"></script>
<script src="js/graph_library/helperObjects.js"></script>
<script src="js/graph_library/misc.js"></script>
<script src="js/graph_library/Widget.js"></script>
<script src="js/graph_library/GraphWidget.js"></script>
<script src="js/graph_library/GraphVertexWidget.js"></script>
<script src="js/graph_library/GraphEdgeWidget.js"></script>
<script src="js/widgets/GraphTraversalWidget.js"></script>
<script type="text/javascript">
//start by showing actions panel
$('#play').hide();
var gtWidget = new GraphTraversal();
var gw = gtWidget.getGraphWidget();
gtWidget.examples(SSSP_EXAMPLE_CP3_4_17);
function createRandom(allowNegative) {
if(isPlaying) { stop(); }
if (mode=="exploration") {
gtWidget.initRandom(allowNegative);
$('#progress-bar').slider( "option", "max", 0);
closeCreate();
isPlaying = false;
}
}
function sample(id) {
if(isPlaying) { stop(); }
setTimeout( function() {
if ((mode=="exploration")&>Widget.examples(id)) {
$('#progress-bar').slider( "option", "max", 0);
closeSamples();
isPlaying = false;
}
}, 500);
}
function bfs() {
if(isPlaying) { stop(); }
var sourceS = parseInt($('#bfs-v').val());
setTimeout( function() {
if ((mode=="exploration")&>Widget.bfs(sourceS)) {
$('#current-action').show();
$('#current-action p').html("BFS(" + sourceS + ")");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function dfs() {
if(isPlaying) {stop();}
var sourceS = parseInt($('#dfs-v').val());
setTimeout( function() {
if ((mode=="exploration")&>Widget.dfs(sourceS)) {
$('#current-action').show();
$('#current-action p').html("DFS(" + sourceS + ")");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function bridge() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.bridge()) {
$('#current-action').show();
$('#current-action p').html("Articulation Points and Bridges check");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function directedChange() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.directedChange()) {
$('#progress-bar').slider( "option", "max", 0);
isPlaying = false;
}
}, 500);
}
function tarjan() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.tarjan()) {
$('#current-action').show();
$('#current-action p').html("Tarjan's Algorithm");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function kosaraju() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.kosaraju()) {
$('#current-action').show();
$('#current-action p').html("Kosaraju's Algorithm");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function bipartiteDfs() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.bipartiteDfs()) {
$('#current-action').show();
$('#current-action p').html("Bipartite Graph Checker");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function bipartiteBfs() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.bipartiteBfs()) {
$('#current-action').show();
$('#current-action p').html("Bipartite Graph Checker");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function toposortDfs() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.toposortDfs()) {
$('#current-action').show();
$('#current-action p').html("Topological Sort");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function toposortBfs() {
if(isPlaying) {stop();}
setTimeout( function() {
if ((mode=="exploration")&>Widget.toposortBfs()) {
$('#current-action').show();
$('#current-action p').html("Topological Sort");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
function twosat() {
if(isPlaying) {stop();}
var a = parseInt($('#twosat-v1').val());
var b = parseInt($('#twosat-v2').val());
setTimeout( function() {
if ((mode=="exploration")&>Widget.twosat(a, b)) {
$('#current-action').show();
$('#current-action p').html("2-SAT Checker");
$('#progress-bar').slider( "option", "max", gw.getTotalIteration()-1);
triggerRightPanels();
isPlaying = true;
}
}, 500);
}
//playback controls might fit better in viz.js, but put here in case some viz does not use GraphWidget.js
var isPaused = false;
function isAtEnd() {
return (gw.getCurrentIteration()==(gw.getTotalIteration()-1));
}
function pause() {
if(isPlaying) {
isPaused = true;
gw.pause();
$('#play').show();
$('#pause').hide();
}
}
function play() {
if(isPlaying) {
isPaused = false;
$('#pause').show();
$('#play').hide();
if(isAtEnd()) {
gw.replay();
} else {
gw.play();
}
}
}
function stepForward() {
if(isPlaying) {
pause();
gw.forceNext(250);
}
}
function stepBackward() {
if(isPlaying) {
pause();
gw.forcePrevious(250);
}
}
function goToBeginning() {
if(isPlaying) {
gw.jumpToIteration(0,0);
pause();
}
}
function goToEnd() {
if(isPlaying) {
gw.jumpToIteration(gw.getTotalIteration()-1,0);
pause();
}
}
function stop() {
gw.stop();
isPaused = false;
isPlaying = false;
$('#pause').show();
$('#play').hide();
}
//shortcut keys for playback controls
$(document).keydown( function(event) {
if(event.which == 32) { //spacebar
if(isPaused) { play(); } else { pause(); }
} else if(event.which == 37) { //left arrow
stepBackward();
} else if(event.which == 39) { //right arrow
stepForward();
} else if(event.which == 35) { //end
stop();
} else if (event.which == 189) { //minus
var d = (2200-gw.getAnimationDuration())-100;
if(d > 0) {
$("#speed-input").slider("value", d);
} else {
$("#speed-input").slider("value", 0);
}
} else if (event.which == 187) { //plus
var d = (2200-gw.getAnimationDuration())+100;
if(d <= 2000) {
$("#speed-input").slider("value", d);
} else {
$("#speed-input").slider("value", 2000);
}
}
});
//sliders
$("#speed-input").slider({
min: 200,
max: 2000,
value: 1700,
change: function(event, ui) {
gw.setAnimationDuration(2200-ui.value);
}
});
$("#progress-bar").slider({
range: "min",
min: 0,
value: 0,
slide: function(event, ui) {
gw.pause();
gw.jumpToIteration(ui.value,0);
},
stop: function(event, ui) {
if(!isPaused) { setTimeout( function(){gw.play();}, 500) }
}
});
</script>
</body>
</html>