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 relax edges during shortest path calculation.
for (Pair neighbor : adj.get(u)) { int v = neighbor.getKey(); int weight = neighbor.getValue(); if (dist[u] != Integer.MAX_VALUE && dist[u] + [1] < dist[v]) { dist[v] = dist[u] + weight; } }
The edge weight is added to the current distance to relax the edge.
Fix the error in the topological sort DFS call to correctly process all vertices.
for (int i = 0; i < V; i++) { if (!visited[i]) { [1](i, visited, stack, adj); } }
The DFS function for topological sorting is called topologicalSortDFS.
Fill both blanks to complete the loop that processes vertices in topological order.
while (!stack.isEmpty()) { int u = stack.[1](); for (Pair neighbor : adj.get(u)) { int v = neighbor.[2](); int weight = neighbor.getValue(); if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) { dist[v] = dist[u] + weight; } } }
We pop vertices from the stack and get the key (vertex) from the neighbor pair.
Fill all three blanks to complete the initialization and update of the distance array.
int[] dist = new int[V]; for (int i = 0; i < V; i++) { dist[i] = [1]; } dist[[2]] = 0; // Relax edges in topological order while (!stack.isEmpty()) { int u = stack.pop(); if (dist[u] != Integer.MAX_VALUE) { for (Pair neighbor : adj.get(u)) { int v = neighbor.getKey(); int weight = neighbor.getValue(); if (dist[u] + weight < dist[v]) { dist[v] = dist[u] + [3]; } } } }
Initialize distances with infinity, set source distance to 0, and add edge weight when relaxing.
