Dijkstra's shortest path algorithm finds the shortest path from the source node to the target node.
It is a greedy algorithm which makes it usually more time efficent compared to other dynamic algorithms like the Bellman-Ford Algorithm. However, Dijkstra's algorithm does not work with negative weight.
Time Complexity
I will only analyze the runtime of Dijkstra's algorithm without the graph building part. I will assume that the graph was given in dictionary form.
If we don't use min-heap:
1) We will visit all the nodes, so it should be
2) We will have to go through each nodes in the dist list to get the minimum value node, so that is another
If we use min-heap:
1) We will visit all the nodes, so it should be
2) We will be inserting new nodes into the min-heap, which is .
3) Getting the minimum value node will take since it will be the root node of the min-heap
Space Complexity
1) The graph dictionary will consist of V keys.
2) Heap will consist at most V nodes.
3) Set to keep track of nodes visited will take up V space.
Advantages
1. Dijkstra's SPA is fast. It is a greedy algorithm, so it can return as soon as the target node has been reach.
Disadvantages
1. Dijkstra's SPA does not work if there is a negative weight in the graph.
A graph can be implemented in many ways:
1. Adjacency Matrix
2. Adjacency List
3. Dictionary with List Values
I will be choosing to use dictionary with list values, but the implementation itself would not be different in its essence even if the graph was represented in other ways.
We could short circuit the function if we were to end after finding the target node.
This code is written based on the Dijkstra's SPA explained in CS 2110: Data Structures class in Cornell.
I've intentionally added unnecessary variables like the frontier_nodes set and probs set in order to explicitly show how Dijkstra's algorithm works. This code was written as a solution for LeetCode Question #1514: Path with Maximum Probability.
More theoretical explanation can be found on Shortest Path Algorithm Lecture on CS 2110 website.