Dijkstra’s shortest path algorithm is an algorithm used to find the shortest path between two vertices in a weighted graph. The algorithm works by maintaining a set of visited vertices and a set of unvisited vertices, and assigning a tentative distance to each vertex representing the shortest known distance from the source vertex to that vertex. The algorithm repeatedly selects the unvisited vertex with the smallest tentative distance, updates the distances of its neighboring vertices if a shorter path is found, and marks the vertex as visited. The algorithm continues until the destination vertex is marked as visited, or all vertices have been visited and there is no path from the source vertex to the destination vertex.
Here are the steps for Dijkstra’s shortest path algorithm:
Create a set of unvisited vertices, and assign a tentative distance of infinity to all vertices except the source vertex, which is assigned a tentative distance of 0.
While there are unvisited vertices:
a. Select the unvisited vertex with the smallest tentative distance.
b. For each neighboring vertex, calculate the distance from the source vertex through the current vertex, and update the tentative distance if it is shorter than the current distance.
c. Mark the current vertex as visited.
If the destination vertex has been marked as visited, terminate the algorithm. Otherwise, there is no path from the source vertex to the destination vertex.
The shortest path from the source vertex to the destination vertex can be found by tracing back from the destination vertex to the source vertex, following the path with the lowest cumulative weight.
Here is an example implementation of Dijkstra’s shortest path algorithm in Java:
public class Dijkstra {
public static int[] shortestPath(int[][] graph, int source) {
int n = graph.length;
int[] distance = new int[n];
boolean[] visited = new boolean[n];
Arrays.fill(distance, Integer.MAX_VALUE);
distance[source] = 0;
for (int i = 0; i < n; i++) {
int current = findMin(distance, visited);
visited[current] = true;
for (int j = 0; j < n; j++) {
if (graph[current][j] != 0 && !visited[j]) {
int newDistance = distance[current] + graph[current][j];
if (newDistance < distance[j]) {
distance[j] = newDistance;
}
}
}
}
return distance;
}
private static int findMin(int[] distance, boolean[] visited) {
int min = Integer.MAX_VALUE;
int index = -1;
for (int i = 0; i < distance.length; i++) {
if (distance[i] < min && !visited[i]) {
min = distance[i];
index = i;
}
}
return index;
}
}
In this implementation, the shortestPath method takes a two-dimensional array graph representing the weighted graph and an integer source representing the source vertex, and returns an array representing the shortest distance from the source vertex to each vertex in the graph. The findMin method is a helper method used to find the unvisited vertex with the smallest tentative distance. The algorithm initializes the distance array to infinity for all vertices except the source vertex, sets the source vertex as visited, and updates the tentative distances of its neighboring vertices. The algorithm continues to update the tentative distances and visit the unvisited vertex with the smallest tentative distance until all vertices have been visited or the destination vertex is marked as visited. Finally, the shortest path can be traced back from the destination vertex to the source vertex using the distance array.