Challenge - 5 Problems
Shortest Path DAG Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of shortest path distances from source in DAG
What is the output of the following Java code that finds shortest paths from source vertex 0 in a DAG using topological order?
DSA Java
import java.util.*; class Graph { int V; List<List<int[]>> adj; Graph(int V) { this.V = V; adj = new ArrayList<>(); for (int i = 0; i < V; i++) { adj.add(new ArrayList<>()); } } void addEdge(int u, int v, int w) { adj.get(u).add(new int[]{v, w}); } void shortestPath(int src) { int[] dist = new int[V]; Arrays.fill(dist, Integer.MAX_VALUE); dist[src] = 0; boolean[] visited = new boolean[V]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < V; i++) { if (!visited[i]) topologicalSort(i, visited, stack); } while (!stack.isEmpty()) { int u = stack.pop(); if (dist[u] != Integer.MAX_VALUE) { for (int[] edge : adj.get(u)) { int v = edge[0]; int weight = edge[1]; if (dist[u] + weight < dist[v]) { dist[v] = dist[u] + weight; } } } } for (int i = 0; i < V; i++) { if (dist[i] == Integer.MAX_VALUE) { System.out.print("INF "); } else { System.out.print(dist[i] + " "); } } } void topologicalSort(int u, boolean[] visited, Stack<Integer> stack) { visited[u] = true; for (int[] edge : adj.get(u)) { int v = edge[0]; if (!visited[v]) topologicalSort(v, visited, stack); } stack.push(u); } } public class Main { public static void main(String[] args) { Graph g = new Graph(6); g.addEdge(0, 1, 5); g.addEdge(0, 2, 3); g.addEdge(1, 3, 6); g.addEdge(1, 2, 2); g.addEdge(3, 4, -1); g.addEdge(4, 5, -2); g.shortestPath(0); } }
Attempts:
2 left
💡 Hint
Trace the topological order and update distances step-by-step from source 0.
✗ Incorrect
The code performs a topological sort and then relaxes edges in that order. Starting from 0, distances update as: dist[0]=0, dist[1]=5, dist[2]=3, dist[3]=11, dist[4]=10, dist[5]=8. Negative weights on edges 3->4 and 4->5 reduce distances accordingly.
🧠 Conceptual
intermediateWhy does shortest path in DAG use topological order?
Why is topological order used to find shortest paths in a Directed Acyclic Graph (DAG)?
Attempts:
2 left
💡 Hint
Think about how processing order affects distance updates in graphs without cycles.
✗ Incorrect
Topological order processes each vertex only after all vertices leading to it are processed. This guarantees that when relaxing edges, the shortest distances to predecessors are already known, so no repeated updates or cycles occur.
🔧 Debug
advancedIdentify the bug causing incorrect shortest path output
The following code attempts to find shortest paths in a DAG but produces incorrect results. What is the bug?
DSA Java
void shortestPath(int src) { int[] dist = new int[V]; Arrays.fill(dist, Integer.MAX_VALUE); dist[src] = 0; Stack<Integer> stack = new Stack<>(); boolean[] visited = new boolean[V]; for (int i = 0; i < V; i++) { if (!visited[i]) topologicalSort(i, visited, stack); } while (!stack.isEmpty()) { int u = stack.pop(); for (int[] edge : adj.get(u)) { int v = edge[0]; int weight = edge[1]; if (dist[u] + weight < dist[v]) { dist[v] = dist[u] + weight; } } } }
Attempts:
2 left
💡 Hint
Check what happens if dist[u] is still at initial max value when relaxing edges.
✗ Incorrect
If dist[u] is Integer.MAX_VALUE, adding weight causes integer overflow and incorrect updates. The code must check dist[u] != Integer.MAX_VALUE before relaxing edges.
📝 Syntax
advancedIdentify the syntax error in this shortest path code snippet
Which option contains the syntax error in the following Java code snippet for shortest path in DAG?
DSA Java
for (int[] edge : adj.get(u)) { int v = edge[0]; int weight = edge[1]; if (dist[u] + weight < dist[v]) { dist[v] = dist[u] + weight } }
Attempts:
2 left
💡 Hint
Check line endings carefully in Java.
✗ Incorrect
Java statements must end with a semicolon. The line 'dist[v] = dist[u] + weight' is missing a semicolon, causing a syntax error.
🚀 Application
expertDetermine the number of vertices reachable from source after shortest path calculation
Given the DAG and source vertex 0, how many vertices have a finite shortest path distance (not INF) after running the shortest path algorithm?
DSA Java
Graph g = new Graph(7); g.addEdge(0, 1, 2); g.addEdge(0, 2, 4); g.addEdge(1, 2, 1); g.addEdge(1, 3, 7); g.addEdge(2, 4, 3); g.addEdge(4, 5, 1); g.addEdge(5, 6, 5); // Vertex 6 has no outgoing edges // Vertex 3 has no outgoing edges // Vertex 6 is reachable only through 5 // Vertex 3 is reachable through 1 // Vertex 0 is source // Vertex unreachable: none // Run shortestPath(0) and count vertices with dist != INF
Attempts:
2 left
💡 Hint
Check connectivity from source 0 to all vertices.
✗ Incorrect
All vertices 0 through 6 are reachable from source 0 through edges. So all have finite shortest path distances.
