You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a javascript implementation of dijkstra's algorithm, to find the shortest path between two nodes in a graph.The graph is represented as an adjacency list. The algorithm is implemented using a min heap.
1349
+
1350
+
__The Challenge:__
1351
+
1352
+
Find the shortest path between two nodes in a graph.
1353
+
1354
+
1355
+
__Algorithmic Thinking:__
1356
+
1357
+
1. Initialize the distance of all nodes to infinity.
1358
+
2. Initialize the distance of the source node to 0.
1359
+
3. Insert all the nodes into a min heap.
1360
+
4. While the heap is not empty, do the following:
1361
+
5. Extract the node with the minimum distance from the heap.
1362
+
6. For each neighbor of the extracted node, do the following:
1363
+
7. If the distance to the neighbor is greater than the distance to the extracted node plus the weight of the edge between them, then update the distance of the neighbor.
1364
+
8. Insert the neighbor into the heap.
1365
+
9. Repeat steps 4-8 until the heap is empty.
1366
+
10. The distance array now contains the shortest distance from the source node to all other nodes in the graph.
1367
+
1368
+
__Implementation:__
1369
+
1370
+
It can be implemented using a min heap. [Using class or function], for this implementation, I will use the functional approach to implement the min heap
1371
+
1372
+
```js
1373
+
constreadline=require('readline');
1374
+
1375
+
/**
1376
+
* Explanation : This is a javascript implementation of dijkstra's algorithm
1377
+
* to find the shortest path between two nodes in a graph.
1378
+
* The graph is represented as an adjacency list.
1379
+
* The algorithm is implemented using a min heap.
1380
+
*
1381
+
*
1382
+
* Sample Input :
1383
+
* 5 5
1384
+
* 1 2 1
1385
+
* 1 3 2
1386
+
* 2 3 1
1387
+
* 2 4 2
1388
+
*
1389
+
* Sample Output :
1390
+
* 1 2 1
1391
+
* 2 3 1
1392
+
* 3 4 2
1393
+
*
1394
+
* Time Complexity : O(ElogV)
1395
+
* Space Complexity : O(V)
1396
+
*
1397
+
*
1398
+
* Algorithm Thinking :
1399
+
* 1. Initialize the distance of all nodes to infinity.
1400
+
* 2. Initialize the distance of the source node to 0.
1401
+
* 3. Insert all the nodes into a min heap.
1402
+
* 4. While the heap is not empty, do the following:
1403
+
* 5. Extract the node with the minimum distance from the heap.
1404
+
* 6. For each neighbor of the extracted node, do the following:
1405
+
* 7. If the distance to the neighbor is greater than the distance to the
1406
+
* extracted node plus the weight of the edge between them, then update the
1407
+
* distance of the neighbor.
1408
+
* 8. Insert the neighbor into the heap.
1409
+
* 9. Repeat steps 4-8 until the heap is empty.
1410
+
* 10. The distance array now contains the shortest distance from the source
1411
+
* node to all other nodes in the graph.
1412
+
* 11. The algorithm is complete.
1413
+
*
1414
+
* Implementation :
1415
+
* It can be implemented using a min heap. [Using class or function]
1416
+
*
1417
+
* for this implementation, we will use the functional approach to implement the min heap
1418
+
*/
1419
+
1420
+
1421
+
constPriorityQueue= () => {
1422
+
constheap= [];
1423
+
1424
+
constswap= (i, j) => {
1425
+
consttemp= heap[i];
1426
+
heap[i] = heap[j];
1427
+
heap[j] = temp;
1428
+
};
1429
+
1430
+
constparent= (i) =>Math.floor((i -1) /2);
1431
+
1432
+
constleft= (i) =>2* i +1;
1433
+
1434
+
constright= (i) =>2* i +2;
1435
+
1436
+
constheapify= (i) => {
1437
+
constl=left(i);
1438
+
constr=right(i);
1439
+
let smallest = i;
1440
+
1441
+
if (l <heap.length&& heap[l].weight< heap[smallest].weight) {
1442
+
smallest = l;
1443
+
}
1444
+
1445
+
if (r <heap.length&& heap[r].weight< heap[smallest].weight) {
1446
+
smallest = r;
1447
+
}
1448
+
1449
+
if (smallest !== i) {
1450
+
swap(i, smallest);
1451
+
heapify(smallest);
1452
+
}
1453
+
};
1454
+
1455
+
constinsert= (node, weight) => {
1456
+
heap.push({ node, weight });
1457
+
let i =heap.length-1;
1458
+
1459
+
while (i !==0&& heap[parent(i)].weight> heap[i].weight) {
1460
+
swap(i, parent(i));
1461
+
i =parent(i);
1462
+
}
1463
+
};
1464
+
1465
+
constextractMin= () => {
1466
+
if (heap.length===1) {
1467
+
returnheap.pop().node;
1468
+
}
1469
+
1470
+
constroot= heap[0].node;
1471
+
heap[0] =heap.pop();
1472
+
heapify(0);
1473
+
1474
+
return root;
1475
+
};
1476
+
1477
+
constisEmpty= () =>heap.length===0;
1478
+
1479
+
return {
1480
+
insert,
1481
+
extractMin,
1482
+
isEmpty,
1483
+
};
1484
+
}
1485
+
```
1486
+
__Write dijkstra function:__
1487
+
1488
+
```js
1489
+
1490
+
constdijkstra= (graph, source) => {
1491
+
constdist=newArray(graph.length).fill(Infinity); // Initialize the distance of all nodes to infinity.
1492
+
dist[source] =0; // Initialize the distance of the source node to 0.
1493
+
1494
+
constpq=PriorityQueue(); // Insert all the nodes into a min heap.
1495
+
pq.insert(source, 0); // Insert the source node into the heap.
1496
+
1497
+
while (!pq.isEmpty()) { // While the heap is not empty, do the following:
1498
+
constu=pq.extractMin(); // Extract the node with the minimum distance from the heap.
1499
+
1500
+
for (let i =0; i < graph[u].length; i +=1) { // For each neighbor of the extracted node, do the following:
1501
+
constv= graph[u][i].node; // For each neighbor of the extracted node, do the following:
1502
+
constweight= graph[u][i].weight; // For each neighbor of the extracted node, do the following:
1503
+
1504
+
if (dist[v] > dist[u] + weight) { // If the distance to the neighbor is greater than the distance to the extracted node plus the weight of the edge between them, then update the distance of the neighbor.
1505
+
dist[v] = dist[u] + weight;
1506
+
pq.insert(v, dist[v]); // Insert the neighbor into the heap.
1507
+
}
1508
+
}
1509
+
}
1510
+
1511
+
return dist;
1512
+
}
1513
+
```
1514
+
1515
+
__Create graph variable consisting all nodes and weight__
0 commit comments