This algorithm is a graph search algorithm that solves the shortest path problem for a graph.
In the graph, the path between vertices has a cost or a length, so Dijkstra’s algorithm simply determines the path with the lowest cost between a vertex and another. The algorithm ends when the shortest path from a vertex to a destination vertex was discovered.
How it works:
- 1) First thing to do is to set the distance value, which is 0 for the current node and infinity for the rest.
- 2) Set all the nodes as unvisited and initial node as current one;
- 3) For the current node, calculate the possible distances to the neighbors (if A has distance of 1 and a connecting node B has distance of 4, then the distance from A to B will be 1 + 4 = 5) If this distance is less than the previous recorded one, overwrite it with the newer one. Also, you have to add the unvisited neighbors to the unvisited list;
- 4) After we have finished checking all the connecting nodes, mark the node as visited. The distance that was recorded is final and minimal;
- 5) The next current node will be the node with the lowest distance in the unvisited set.
- 6) If all nodes have been visited, finish. Otherwise, set the unvisited node with the smallest distance (from the initial node, considering all nodes in graph) as the next "current node" and continue from step 3.
Step by step example:
Let us take a peek at a simple example, here is the graph:
As you can see, A is the starting node, the distance to B is 1 and the distance to C is 5, so we co to B:
If you look closely when we were in A, the distance to D was infinite, because at that time we had no path to D from A, but since we are in B, we have a new distance. Also, the best distance to choose now is D because it is the shortest:
And finally, we are left with only one option, the route is direct through E:
Now we have to identify the route. The previous node of E is D, and the previous node of D is B, and B’s previous node is A. So the best route is ABDE. In this case, the total weigh is 4 (1+2+1).
using namespace std;
int shortest(int ,int);
int shortest(int v,int n)
if(dist[j]<min && S[j]!=1)
if(cost[k][j]!=31999 && dist[j]>=dist[k]+cost[k][j] && S[j]!=1)
cout << "Enter number of vertices = ";
cin >> n;
cout << "Enter number of edges = ";
cout <<"nEnternEDGE Costn";
cin >> i >> j >>c;
cout <<"enter initial vertex";
cout << v<<"n";
Just like in the example that I explained earlier, at first it sets the distance of 0 for the current node, next the path to the connecting nodes is checked. The minimal distance will be retained in the min variable, of course after it is checked to be the absolute minimal distance at that point. Also, the cost is compare to the previous value that may exist, and if it is smaller, it will replace the old value. This procedure repeats until there are no more nodes to check.
A visual representation of the graph:
How to insert the values: at first input the number of vertices which is 4 for the above graph and 4 is the name of edges. Then, input the edge and the cost like 1 2 5, which means the cost from 1 to 2 is 5, then 1 3 1 which means the cost from 1 to 3 is 1 and so on. In the end, you will have to input the starting point, which could be 1 like in the above example.
The worst-case running time for the Dijkstra algorithm on a graph with n nodes and m edges is O(n^2) because it allows for directed cycles. It even finds the shortest paths from a source node to all other nodes in the graph. This is basically O(n^2) for node selection and O(m) for distance updates. While O(n^2) is the best possible complexity for dense graphs, the complexity can be improved significantly for sparse graphs.
- Telephone Network – In a telephone network the lines have bandwidth, BW. We want to route the phone call via the highest BW.
- OSPF (open shortest path first) is a well known real-world implementation of Dijkstra’s algorithm used in internet routing.
- A related problem is the traveling salesman problem, which is the problem of finding the shortest path that goes through every vertex exactly once, and returns to the start.