Challenge - 5 Problems
DAG Shortest Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of shortest path distances from source in DAG
What is the output of the following C code that finds shortest paths from source 0 in a DAG using topological order?
DSA C
#include <stdio.h> #include <limits.h> #define V 6 void shortestPath(int graph[V][V], int src) { int dist[V]; int visited[V] = {0}; int stack[V], top = -1; // Function to perform DFS and fill stack void dfs(int v) { visited[v] = 1; for (int i = 0; i < V; i++) { if (graph[v][i] != INT_MAX && !visited[i]) { dfs(i); } } stack[++top] = v; } for (int i = 0; i < V; i++) { if (!visited[i]) dfs(i); } for (int i = 0; i < V; i++) dist[i] = INT_MAX; dist[src] = 0; while (top >= 0) { int u = stack[top--]; if (dist[u] != INT_MAX) { for (int v = 0; v < V; v++) { if (graph[u][v] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } } } for (int i = 0; i < V; i++) { if (dist[i] == INT_MAX) printf("INF "); else printf("%d ", dist[i]); } printf("\n"); } int main() { int graph[V][V] = { {INT_MAX, 5, 3, INT_MAX, INT_MAX, INT_MAX}, {INT_MAX, INT_MAX, 2, 6, INT_MAX, INT_MAX}, {INT_MAX, INT_MAX, INT_MAX, 7, 4, 2}, {INT_MAX, INT_MAX, INT_MAX, INT_MAX, -1, 1}, {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, -2}, {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX} }; shortestPath(graph, 0); return 0; }
Attempts:
2 left
💡 Hint
Trace the topological order and update distances step-by-step from source 0.
✗ Incorrect
The code performs a DFS to get topological order, then relaxes edges in that order. The shortest distances from node 0 to others are: 0 (to itself), 5 (via edge 0->1), 3 (0->2), 10 (0->2->3), 7 (0->2->4), 5 (0->2->5).
🧠 Conceptual
intermediate1:30remaining
Why use topological order for shortest path in DAG?
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 after all vertices that lead into it, so when we update distances, all shortest paths to predecessors are already known. This works only in DAGs because they have no cycles.
🔧 Debug
advanced2:00remaining
Identify the bug causing incorrect shortest path output
The following code attempts to find shortest paths in a DAG but produces wrong results. What is the bug?
DSA C
void shortestPath(int graph[V][V], int src) { int dist[V]; int visited[V] = {0}; int stack[V], top = -1; void dfs(int v) { visited[v] = 1; for (int i = 0; i < V; i++) { if (graph[v][i] != INT_MAX && !visited[i]) { dfs(i); } } stack[++top] = v; } for (int i = 0; i < V; i++) { if (!visited[i]) dfs(i); } for (int i = 0; i < V; i++) dist[i] = INT_MAX; dist[src] = 0; while (top >= 0) { int u = stack[top--]; for (int v = 0; v < V; v++) { if (graph[u][v] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } } for (int i = 0; i < V; i++) { if (dist[i] == INT_MAX) printf("INF "); else printf("%d ", dist[i]); } printf("\n"); }
Attempts:
2 left
💡 Hint
Check the relaxation step carefully for invalid distance updates.
✗ Incorrect
If dist[u] is INT_MAX (meaning unreachable), adding graph[u][v] causes overflow or wrong values. The code must check dist[u] != INT_MAX before relaxing edges.
📝 Syntax
advanced1:00remaining
Identify the syntax error in the topological sort DFS function
Which option contains the syntax error in this DFS function used for topological sorting?
DSA C
void dfs(int v) { visited[v] = 1 for (int i = 0; i < V; i++) { if (graph[v][i] != INT_MAX && !visited[i]) { dfs(i); } } stack[++top] = v; }
Attempts:
2 left
💡 Hint
Look carefully at each line for missing punctuation.
✗ Incorrect
The line 'visited[v] = 1' is missing a semicolon at the end, causing a syntax error.
🚀 Application
expert3:00remaining
Number of shortest paths from source to target in DAG
Given a DAG with weighted edges (all weights positive), how can you modify the shortest path algorithm using topological order to also count the number of distinct shortest paths from source to each vertex?
Attempts:
2 left
💡 Hint
Think about how to track number of ways while updating shortest distances.
✗ Incorrect
By maintaining a count array, we track how many shortest paths reach each vertex. When a shorter path is found, reset count; when an equal path is found, add counts. This works efficiently with topological order.