Article Directory
Dijkstra's algorithm allows us to find the shortest path between any two vertices of the graph.
It is different from the minimum spanning tree because the shortest distance between two vertices may not include all the vertices of the graph.
How Dijkstra's algorithm works
The working principle of Dijkstra's algorithm is that a certain sub-path B -> D of the shortest path A -> D between vertices A and D is also the shortest path between vertices B and D.
Djikstra uses this property in reverse, that is, we overestimate the distance from each vertex to the starting vertex. Then we visit each node and its neighbors and find the shortest sub-path to these neighbors.
The algorithm uses a greedy method, that is, we look for the best solution, hoping that the final result is the best solution for the entire problem.
Example of Dijkstra's algorithm
It is easier to start with an example and then consider the algorithm.
Start with a weighted graph.
Choose a starting vertex and assign the infinite path value to all other nodes.
Update the path length of each vertex.
If the path length of the adjacent vertex is less than the length of the new path, it is not updated.
Avoid updating the path length of the visited vertices.
After each iteration, we select the unvisited vertex with the smallest path length. So we choose 5 before 7.
Notice how the path length of the rightmost vertex is updated twice.
Repeat until all vertices have been visited.
Djikstra algorithm pseudo code
We need to maintain the path distance of each vertex. We can store it in an array of size v, where v is the number of vertices.
We also hope to get the shortest path, not only know the length of the shortest path. To do this, we map each vertex to the vertex whose path length was last updated.
After the algorithm is over, we can backtrack from the target vertex to the source vertex to find the path.
The smallest priority queue can effectively receive the vertex with the smallest path distance.
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]
Dijkstra algorithm code
// Dijkstra's Algorithm in C
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start);
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
// Creating cost matrix
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];
for (i = 0; i < n; i++) {
distance[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}
distance[start] = 0;
visited[start] = 1;
count = 1;
while (count < n - 1) {
mindistance = INFINITY;
for (i = 0; i < n; i++)
if (distance[i] < mindistance && !visited[i]) {
mindistance = distance[i];
nextnode = i;
}
visited[nextnode] = 1;
for (i = 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
count++;
}
// Printing the distance
for (i = 0; i < n; i++)
if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
}
int main() {
int Graph[MAX][MAX], i, j, n, u;
n = 7;
Graph[0][0] = 0;
Graph[0][1] = 0;
Graph[0][2] = 1;
Graph[0][3] = 2;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;
Graph[1][0] = 0;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 3;
Graph[1][6] = 0;
Graph[2][0] = 1;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 1;
Graph[2][4] = 3;
Graph[2][5] = 0;
Graph[2][6] = 0;
Graph[3][0] = 2;
Graph[3][1] = 0;
Graph[3][2] = 1;
Graph[3][3] = 0;
Graph[3][4] = 0;
Graph[3][5] = 0;
Graph[3][6] = 1;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 3;
Graph[4][3] = 0;
Graph[4][4] = 0;
Graph[4][5] = 2;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 3;
Graph[5][2] = 0;
Graph[5][3] = 0;
Graph[5][4] = 2;
Graph[5][5] = 0;
Graph[5][6] = 1;
Graph[6][0] = 0;
Graph[6][1] = 0;
Graph[6][2] = 0;
Graph[6][3] = 1;
Graph[6][4] = 0;
Graph[6][5] = 1;
Graph[6][6] = 0;
u = 0;
Dijkstra(Graph, n, u);
return 0;
}
The complexity of Dijkstra's algorithm
Time complexity: O(E Log V)
where E is the number of edges and V is the number of vertices.
Space complexity: O(V)
Application of Dijkstra's algorithm
- Find the shortest path
- Application in social networks
- In the telephone network
- Find location on the map
Reference documents
[1]Parewa Labs Pvt. Ltd.Dijkstra’s Algorithm[EB/OL].https://www.programiz.com/dsa/dijkstra-algorithm,2020-01-01.