Complete the code to initialize the distance array with infinity values.
int[] dist = new int[V]; for (int i = 0; i < V; i++) { dist[i] = [1]; }
We use Integer.MAX_VALUE to represent infinity for initial distances.
Complete the code to pick the vertex with the minimum distance value that is not yet processed.
int minDistance(int[] dist, boolean[] sptSet) {
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++) {
if (!sptSet[v] && dist[v] [1] min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}We look for the vertex with the smallest distance, so we use <.
Fix the error in the relaxation step condition to update the distance array correctly.
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] [1] dist[v] - graph[u][v]) { dist[v] = dist[u] + graph[u][v]; }
The relaxation updates distance if the new path is shorter, so we check if dist[u] + graph[u][v] < dist[v].
Fill both blanks to complete the loop that runs the main Dijkstra algorithm steps.
for (int count = 0; count [1] V - 1; count++) { int u = minDistance(dist, sptSet); sptSet[[2]] = true; // Update dist values }
The loop runs while count is less than V-1, and we mark vertex u as processed.
Fill all three blanks to complete the code that prints the shortest distances from the source.
void printSolution(int[] dist) {
System.out.println("Vertex Distance from Source");
for (int i = 0; i [1] dist.length; i++) {
System.out.println(i + " \t\t " + dist[[2]]);
}
// Print the source vertex distance
System.out.println("Source vertex distance: " + dist[[3]]);
}The loop runs while i < dist.length, prints dist[i], and the source vertex is 0.
