Dijkstra's Shortest Path
About
An algorithm to find shortest path, meaning find the edge connections with smallest weight.
Array Solution
A way to make it run on would be to use a minHeap to hold the weights.
Getting the next lowest unseen would be , and there would be no reason for the seen array since the seen nodes would be removed from the heap.
The way this specific algorithm works is
Will start from the
sourcenode (Ex.:Node 0), and check how much it costs to go to each of the unseen neighbors (Node 1andNode 2).After this check it will save:
In
weightshow much it costs to get toNode 1 = 1andNode 2 = 5.In
pathit will have that the best way yet to get toNode 1andNode 2is fromNode 0.
Then it will check the next
unseennode that has the lowestweightinweights. (So it will try to stay on the best path). In this example it will check nowNode 1, and its unseen neighbors.After the check:
In
weightsit will have that to get toNode 2 = 7andNode 3 = 1. But we already know that there is a better path forNode 2which is fromNode 0, so reachingNode 2fromNode 1is ignored.In
pathwe have now that reachingNode 1 & 2should be fromNode 0and reachingNode 3should be fromNode 1.
Step 2 is repeated until there are no more unseen nodes. The algorithm will prioritize paths with lowest
weightand will override them if a better one is found.At the end it just reconstruct the
pathto return it.
Heap Solution
Last updated