Skip to content

Commit 4761119

Browse files
authored
Merge pull request #36 from tejas-2232/dijkstras_algo
whole details for Dijlstra's algorithm is added
2 parents 7948ebd + 30a0893 commit 4761119

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

README.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,3 +1341,195 @@ for(let i=1; i<= number; i++) {
13411341
13421342
<hr>
13431343
<hr>
1344+
1345+
<b>13. Shortest Path(Dijkstras) </b>
1346+
1347+
<p>
1348+
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+
const readline = 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+
const PriorityQueue = () => {
1422+
const heap = [];
1423+
1424+
const swap = (i, j) => {
1425+
const temp = heap[i];
1426+
heap[i] = heap[j];
1427+
heap[j] = temp;
1428+
};
1429+
1430+
const parent = (i) => Math.floor((i - 1) / 2);
1431+
1432+
const left = (i) => 2 * i + 1;
1433+
1434+
const right = (i) => 2 * i + 2;
1435+
1436+
const heapify = (i) => {
1437+
const l = left(i);
1438+
const r = 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+
const insert = (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+
const extractMin = () => {
1466+
if (heap.length === 1) {
1467+
return heap.pop().node;
1468+
}
1469+
1470+
const root = heap[0].node;
1471+
heap[0] = heap.pop();
1472+
heapify(0);
1473+
1474+
return root;
1475+
};
1476+
1477+
const isEmpty = () => heap.length === 0;
1478+
1479+
return {
1480+
insert,
1481+
extractMin,
1482+
isEmpty,
1483+
};
1484+
}
1485+
```
1486+
__Write dijkstra function:__
1487+
1488+
```js
1489+
1490+
const dijkstra = (graph, source) => {
1491+
const dist = new Array(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+
const pq = 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+
const u = 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+
const v = graph[u][i].node; // For each neighbor of the extracted node, do the following:
1502+
const weight = 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__
1516+
1517+
```js
1518+
1519+
const graph = [
1520+
[{ node: 1, weight: 1 }, { node: 2, weight: 2 }],
1521+
[{ node: 0, weight: 1 }, { node: 2, weight: 1 }, { node: 3, weight: 2 }],
1522+
[{ node: 0, weight: 2 }, { node: 1, weight: 1 }, { node: 3, weight: 1 }],
1523+
[{ node: 1, weight: 2 }, { node: 2, weight: 1 }],
1524+
[{ node: 5, weight: 1 }],
1525+
[{ node: 4, weight: 1 }],
1526+
[{ node: 7, weight: 1 }, { node: 8, weight: 2 }, { node: 9, weight: 3 }],
1527+
[{ node: 6, weight: 1 }, { node: 8, weight: 1 }],
1528+
];
1529+
1530+
const dist = dijkstra(graph, 6);
1531+
1532+
console.log(dist);
1533+
1534+
```
1535+
</p>

0 commit comments

Comments
 (0)