Mark As Completed Discussion

The direction of edges is not important in Dijkstra's algorithm. As long as the input graph is in proper format, the algorithm will give the correct output path.

Implementation

Let's implement this algorithm. You can represent the graph in different ways, but for this tutorial, we will represent it as an adjacency list (in the form of nested dictionaries).

For this implementation, we have the source node, destination node, and the example graph is represented as:

1const graph = {
2  a: { b: 5, c: 2 },
3  b: { a: 5, c: 7, d: 8 },
4  c: { a: 2, b: 7, d: 4, e: 8 },
5  d: { b: 8, c: 4, e: 6, f: 4 },
6  e: { c: 8, d: 6, f: 3 },
7  f: { e: 3, d: 4 },
8};