-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdijkstra.html
More file actions
762 lines (648 loc) · 26.2 KB
/
dijkstra.html
File metadata and controls
762 lines (648 loc) · 26.2 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dijkstra's Algorithm Visualizer</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
color: #333;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
h1 {
color: #2c3e50;
text-align: center;
margin-bottom: 30px;
}
.controls {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
margin-bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 15px;
}
.control-group {
flex: 1;
min-width: 200px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input, select, button {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
width: 100%;
box-sizing: border-box;
}
button {
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
button:disabled {
background-color: #95a5a6;
cursor: not-allowed;
}
.visualization {
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.graph-container {
flex: 2;
min-width: 600px;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
position: relative;
}
#graphCanvas {
border: 1px solid #ddd;
background-color: white;
width: 100%;
height: 500px;
}
.info-panel {
flex: 1;
min-width: 300px;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.step-info {
margin-top: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 4px;
border-left: 4px solid #3498db;
min-height: 100px;
}
.distance-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.distance-table th, .distance-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.distance-table th {
background-color: #f2f2f2;
}
.distance-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-top: 20px;
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
font-size: 14px;
}
.legend-color {
width: 20px;
height: 20px;
border-radius: 50%;
}
.node-info {
margin-top: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>Dijkstra's Algorithm Visualizer</h1>
<div class="controls">
<div class="control-group">
<label for="speedInput">Animation Speed:</label>
<select id="speedInput">
<option value="1500">Slow</option>
<option value="800" selected>Medium</option>
<option value="400">Fast</option>
</select>
</div>
<div class="control-group">
<label for="startNode">Start Node:</label>
<select id="startNode"></select>
</div>
<div class="control-group">
<label for="endNode">Target Node (optional):</label>
<select id="endNode">
<option value="">None</option>
</select>
</div>
<div class="control-group">
<label> </label>
<button id="startBtn">Start Visualization</button>
<button id="resetBtn">Reset</button>
<button id="nextStepBtn" disabled>Next Step</button>
</div>
</div>
<div class="visualization">
<div class="graph-container">
<h2>Graph Visualization</h2>
<canvas id="graphCanvas"></canvas>
<div class="legend">
<div class="legend-item">
<div class="legend-color" style="background-color: #2ecc71;"></div>
<span>Start Node</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #e74c3c;"></div>
<span>Target Node</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #3498db;"></div>
<span>Current Node</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #f39c12;"></div>
<span>Visited Node</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #9b59b6;"></div>
<span>Shortest Path</span>
</div>
</div>
</div>
<div class="info-panel">
<h2>Algorithm Progress</h2>
<div class="step-info" id="stepInfo">
Click "Start Visualization" to begin Dijkstra's algorithm.
</div>
<h3>Distance Table</h3>
<table class="distance-table" id="distanceTable">
<thead>
<tr>
<th>Node</th>
<th>Distance</th>
<th>Previous</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div class="node-info">
<h3>Current Priority Queue</h3>
<div id="priorityQueue"></div>
</div>
</div>
</div>
</div>
<script>
// DOM elements
const speedInput = document.getElementById('speedInput');
const startNodeSelect = document.getElementById('startNode');
const endNodeSelect = document.getElementById('endNode');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const nextStepBtn = document.getElementById('nextStepBtn');
const graphCanvas = document.getElementById('graphCanvas');
const stepInfo = document.getElementById('stepInfo');
const distanceTable = document.getElementById('distanceTable').querySelector('tbody');
const priorityQueueDisplay = document.getElementById('priorityQueue');
// Graph state
const graph = {
nodes: [
{ id: 'A', x: 150, y: 100 },
{ id: 'B', x: 300, y: 100 },
{ id: 'C', x: 450, y: 100 },
{ id: 'D', x: 150, y: 250 },
{ id: 'E', x: 300, y: 250 },
{ id: 'F', x: 450, y: 250 }
],
edges: [
{ from: 'A', to: 'B', weight: 4 },
{ from: 'A', to: 'D', weight: 1 },
{ from: 'B', to: 'C', weight: 1 },
{ from: 'B', to: 'E', weight: 3 },
{ from: 'C', to: 'F', weight: 2 },
{ from: 'D', to: 'E', weight: 3 },
{ from: 'E', to: 'F', weight: 7 },
{ from: 'B', to: 'D', weight: 2 }
]
};
// Dijkstra's algorithm state
let distances = {};
let previous = {};
let visited = new Set();
let priorityQueue = [];
let currentNode = null;
let animationSpeed = 800;
let isAutoPlaying = false;
let animationTimeout = null;
let startNode = 'A';
let endNode = '';
let shortestPath = [];
let steps = [];
let currentStep = 0;
// Canvas setup
const ctx = graphCanvas.getContext('2d');
graphCanvas.width = graphCanvas.offsetWidth;
graphCanvas.height = graphCanvas.offsetHeight;
// Node radius
const nodeRadius = 25;
// Colors
const colors = {
defaultNode: '#3498db',
startNode: '#2ecc71',
endNode: '#e74c3c',
currentNode: '#3498db',
visitedNode: '#f39c12',
shortestPath: '#9b59b6',
edge: '#95a5a6',
edgeText: '#333',
defaultText: 'white'
};
// Initialize the application
function init() {
// Populate node selectors
graph.nodes.forEach(node => {
const option1 = document.createElement('option');
option1.value = node.id;
option1.textContent = node.id;
startNodeSelect.appendChild(option1);
const option2 = document.createElement('option');
option2.value = node.id;
option2.textContent = node.id;
endNodeSelect.appendChild(option2);
});
// Set default start node
startNodeSelect.value = 'A';
// Initialize distance table
updateDistanceTable();
// Draw initial graph
drawGraph();
// Event listeners
startBtn.addEventListener('click', startAlgorithm);
resetBtn.addEventListener('click', resetAlgorithm);
nextStepBtn.addEventListener('click', nextStep);
// Canvas interaction
graphCanvas.addEventListener('click', handleCanvasClick);
}
// Draw the graph
function drawGraph() {
// Clear canvas
ctx.clearRect(0, 0, graphCanvas.width, graphCanvas.height);
// Draw edges
graph.edges.forEach(edge => {
const fromNode = graph.nodes.find(n => n.id === edge.from);
const toNode = graph.nodes.find(n => n.id === edge.to);
// Check if edge is in shortest path
const isInShortestPath = shortestPath.some((node, i) => {
return (node === edge.from && shortestPath[i+1] === edge.to) ||
(node === edge.to && shortestPath[i+1] === edge.from);
});
ctx.beginPath();
ctx.moveTo(fromNode.x, fromNode.y);
ctx.lineTo(toNode.x, toNode.y);
ctx.strokeStyle = isInShortestPath ? colors.shortestPath : colors.edge;
ctx.lineWidth = isInShortestPath ? 3 : 2;
ctx.stroke();
// Draw edge weight
const midX = (fromNode.x + toNode.x) / 2;
const midY = (fromNode.y + toNode.y) / 2;
ctx.fillStyle = colors.edgeText;
ctx.font = '14px Arial';
ctx.textAlign = 'center';
ctx.fillText(edge.weight.toString(), midX, midY - 10);
});
// Draw nodes
graph.nodes.forEach(node => {
// Determine node color
let nodeColor = colors.defaultNode;
if (node.id === startNode) {
nodeColor = colors.startNode;
} else if (node.id === endNode && endNode) {
nodeColor = colors.endNode;
} else if (node.id === currentNode) {
nodeColor = colors.currentNode;
} else if (visited.has(node.id)) {
nodeColor = colors.visitedNode;
} else if (shortestPath.includes(node.id)) {
nodeColor = colors.shortestPath;
}
// Draw node circle
ctx.beginPath();
ctx.arc(node.x, node.y, nodeRadius, 0, Math.PI * 2);
ctx.fillStyle = nodeColor;
ctx.fill();
ctx.strokeStyle = '#2c3e50';
ctx.lineWidth = 2;
ctx.stroke();
// Draw node label
ctx.fillStyle = colors.defaultText;
ctx.font = 'bold 16px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(node.id, node.x, node.y);
// Draw distance if available
if (distances[node.id] !== Infinity && distances[node.id] !== undefined) {
ctx.fillStyle = '#333';
ctx.font = '12px Arial';
ctx.fillText(distances[node.id].toString(), node.x, node.y + nodeRadius + 15);
}
});
}
// Handle canvas clicks
function handleCanvasClick(event) {
if (isAutoPlaying) return;
const rect = graphCanvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
// Check if a node was clicked
const clickedNode = graph.nodes.find(node => {
const distance = Math.sqrt((x - node.x) ** 2 + (y - node.y) ** 2);
return distance <= nodeRadius;
});
if (clickedNode) {
if (!startNode) {
startNode = clickedNode.id;
startNodeSelect.value = startNode;
} else if (!endNode && clickedNode.id !== startNode) {
endNode = clickedNode.id;
endNodeSelect.value = endNode;
} else {
// Reset selection
if (clickedNode.id === startNode) {
startNode = '';
startNodeSelect.value = '';
} else if (clickedNode.id === endNode) {
endNode = '';
endNodeSelect.value = '';
}
}
resetAlgorithm();
drawGraph();
}
}
// Initialize Dijkstra's algorithm
function initDijkstra() {
distances = {};
previous = {};
visited = new Set();
priorityQueue = [];
currentNode = null;
shortestPath = [];
steps = [];
currentStep = 0;
// Initialize distances
graph.nodes.forEach(node => {
distances[node.id] = node.id === startNode ? 0 : Infinity;
previous[node.id] = null;
});
// Initialize priority queue
priorityQueue = [...graph.nodes.map(node => node.id)];
priorityQueue.sort((a, b) => distances[a] - distances[b]);
// Generate steps for visualization
generateSteps();
// Update UI
updateDistanceTable();
updatePriorityQueueDisplay();
drawGraph();
}
// Generate steps for visualization
function generateSteps() {
// Make a copy of the state for simulation
const simDistances = {...distances};
const simPrevious = {...previous};
const simVisited = new Set(visited);
const simQueue = [...priorityQueue];
let simCurrent = null;
while (simQueue.length > 0) {
// Get node with smallest distance
simQueue.sort((a, b) => simDistances[a] - simDistances[b]);
simCurrent = simQueue.shift();
// Skip if already visited
if (simVisited.has(simCurrent)) continue;
// Mark as visited
simVisited.add(simCurrent);
// Add step for selecting current node
steps.push({
type: 'select',
node: simCurrent,
message: `Selected node ${simCurrent} with smallest tentative distance (${simDistances[simCurrent]})`
});
// Get neighbors
const neighbors = graph.edges
.filter(edge => edge.from === simCurrent || edge.to === simCurrent)
.map(edge => edge.from === simCurrent ? edge.to : edge.from);
// Process each neighbor
neighbors.forEach(neighbor => {
const edge = graph.edges.find(e =>
(e.from === simCurrent && e.to === neighbor) ||
(e.from === neighbor && e.to === simCurrent));
const alt = simDistances[simCurrent] + edge.weight;
// Add step for considering neighbor
steps.push({
type: 'consider',
node: neighbor,
edge: edge,
message: `Considering neighbor ${neighbor} (current distance: ${simDistances[neighbor]}, alternative distance: ${alt})`
});
if (alt < simDistances[neighbor]) {
// Add step for updating distance
steps.push({
type: 'update',
node: neighbor,
previous: simCurrent,
distance: alt,
message: `Updating node ${neighbor}: new distance ${alt} (previous: ${simCurrent})`
});
simDistances[neighbor] = alt;
simPrevious[neighbor] = simCurrent;
}
});
}
// If we have an end node, find the shortest path
if (endNode) {
let path = [];
let current = endNode;
while (current !== null) {
path.unshift(current);
current = simPrevious[current];
}
if (path.length > 1) {
steps.push({
type: 'path',
path: path,
message: `Shortest path from ${startNode} to ${endNode}: ${path.join(' → ')} (distance: ${simDistances[endNode]})`
});
} else {
steps.push({
type: 'path',
path: [],
message: `No path exists from ${startNode} to ${endNode}`
});
}
}
// Final step
steps.push({
type: 'complete',
message: `Algorithm complete. Shortest distances calculated from ${startNode}.`
});
}
// Perform one step of the algorithm
function performStep() {
if (currentStep >= steps.length) return;
const step = steps[currentStep];
// Update visualization based on step type
switch (step.type) {
case 'select':
currentNode = step.node;
visited.add(currentNode);
priorityQueue = priorityQueue.filter(node => node !== currentNode);
break;
case 'consider':
// Just for visualization - no state change
break;
case 'update':
distances[step.node] = step.distance;
previous[step.node] = step.previous;
break;
case 'path':
shortestPath = step.path;
break;
case 'complete':
currentNode = null;
break;
}
// Update UI
stepInfo.innerHTML = step.message;
updateDistanceTable();
updatePriorityQueueDisplay();
drawGraph();
currentStep++;
// If auto-playing and not finished, schedule next step
if (isAutoPlaying && currentStep < steps.length) {
animationTimeout = setTimeout(performStep, animationSpeed);
} else if (currentStep >= steps.length) {
isAutoPlaying = false;
startBtn.disabled = true;
}
// Enable next step button if not auto-playing
if (!isAutoPlaying && currentStep < steps.length) {
nextStepBtn.disabled = false;
}
}
// Update the distance table
function updateDistanceTable() {
distanceTable.innerHTML = '';
graph.nodes.forEach(node => {
const row = document.createElement('tr');
// Node cell
const nodeCell = document.createElement('td');
nodeCell.textContent = node.id;
if (node.id === currentNode) nodeCell.style.fontWeight = 'bold';
row.appendChild(nodeCell);
// Distance cell
const distCell = document.createElement('td');
distCell.textContent = distances[node.id] === Infinity ? '∞' : distances[node.id];
row.appendChild(distCell);
// Previous cell
const prevCell = document.createElement('td');
prevCell.textContent = previous[node.id] || '-';
row.appendChild(prevCell);
distanceTable.appendChild(row);
});
}
// Update the priority queue display
function updatePriorityQueueDisplay() {
const queueCopy = [...priorityQueue];
queueCopy.sort((a, b) => distances[a] - distances[b]);
priorityQueueDisplay.innerHTML = queueCopy.length > 0
? queueCopy.map(node => `${node} (${distances[node]})`).join(', ')
: 'Empty';
}
// Start the algorithm
function startAlgorithm() {
startNode = startNodeSelect.value;
endNode = endNodeSelect.value || '';
animationSpeed = parseInt(speedInput.value);
initDijkstra();
isAutoPlaying = true;
startBtn.disabled = true;
nextStepBtn.disabled = true;
performStep();
}
// Perform next step manually
function nextStep() {
isAutoPlaying = false;
if (animationTimeout) {
clearTimeout(animationTimeout);
animationTimeout = null;
}
if (currentStep === 0) {
startNode = startNodeSelect.value;
endNode = endNodeSelect.value || '';
animationSpeed = parseInt(speedInput.value);
initDijkstra();
}
performStep();
}
// Reset the algorithm
function resetAlgorithm() {
if (animationTimeout) {
clearTimeout(animationTimeout);
animationTimeout = null;
}
distances = {};
previous = {};
visited = new Set();
priorityQueue = [];
currentNode = null;
shortestPath = [];
steps = [];
currentStep = 0;
startNode = startNodeSelect.value;
endNode = endNodeSelect.value || '';
isAutoPlaying = false;
startBtn.disabled = false;
nextStepBtn.disabled = false;
// Initialize distance table with empty values
const emptyDistances = {};
graph.nodes.forEach(node => {
emptyDistances[node.id] = node.id === startNode ? 0 : Infinity;
});
distances = emptyDistances;
updateDistanceTable();
priorityQueueDisplay.innerHTML = '';
stepInfo.innerHTML = startNode
? `Ready to find shortest paths from node ${startNode}${endNode ? ` to node ${endNode}` : ''}. Click "Start Visualization" to begin.`
: 'Please select a start node.';
drawGraph();
}
// Initialize the application
init();
</script>
</body>
</html>