Challenge - 5 Problems
DAG Shortest Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of shortest path distances in DAG
What is the output of the following C++ code that finds shortest paths from node 0 in a DAG using topological order?
DSA C++
/* Graph edges with weights: 0 -> 1 (2), 0 -> 2 (4), 1 -> 2 (1), 1 -> 3 (7), 2 -> 4 (3), 3 -> 5 (1), 4 -> 3 (1), 4 -> 5 (5) */ #include <iostream> #include <vector> #include <stack> #include <limits> using namespace std; void topologicalSort(int node, vector<vector<pair<int,int>>>& graph, vector<bool>& visited, stack<int>& st) { visited[node] = true; for(auto& edge : graph[node]) { int v = edge.first; if(!visited[v]) topologicalSort(v, graph, visited, st); } st.push(node); } int main() { int V = 6; vector<vector<pair<int,int>>> graph(V); graph[0].push_back({1,2}); graph[0].push_back({2,4}); graph[1].push_back({2,1}); graph[1].push_back({3,7}); graph[2].push_back({4,3}); graph[3].push_back({5,1}); graph[4].push_back({3,1}); graph[4].push_back({5,5}); vector<bool> visited(V, false); stack<int> st; for(int i=0; i<V; i++) { if(!visited[i]) topologicalSort(i, graph, visited, st); } vector<int> dist(V, numeric_limits<int>::max()); dist[0] = 0; while(!st.empty()) { int u = st.top(); st.pop(); if(dist[u] != numeric_limits<int>::max()) { for(auto& edge : graph[u]) { int v = edge.first; int w = edge.second; if(dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } } for(int i=0; i<V; i++) { if(dist[i] == numeric_limits<int>::max()) cout << "INF "; else cout << dist[i] << " "; } cout << endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the topological order and update distances step-by-step from node 0.
✗ Incorrect
The code performs a topological sort and then relaxes edges in that order. Starting from node 0 with distance 0, distances to reachable nodes are updated to shortest possible values. The final distances are: 0 (start), 2 (0->1), 3 (0->1->2), 7 (0->1->2->4->3 or 0->1->3), 6 (0->1->2->4), 8 (0->1->2->4->3->5).
🧠 Conceptual
intermediateWhy 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 nodes in order affects distance updates.
✗ Incorrect
Topological order processes nodes so that all edges go from earlier to later nodes. This means when we process a node, all nodes that can reach it have already been processed, so distances are finalized and can be used to update the current node's neighbors correctly.
🔧 Debug
advancedIdentify the bug in shortest path code for DAG
What error will the following code produce when finding shortest paths in a DAG using topological order?
DSA C++
#include <iostream> #include <vector> #include <stack> #include <limits> using namespace std; void topologicalSort(int node, vector<vector<pair<int,int>>>& graph, vector<bool>& visited, stack<int>& st) { visited[node] = true; for(auto edge : graph[node]) { int v = edge.first; // <-- fixed line if(!visited[v]) topologicalSort(v, graph, visited, st); } st.push(node); } int main() { int V = 3; vector<vector<pair<int,int>>> graph(V); graph[0].push_back({1,5}); graph[1].push_back({2,3}); vector<bool> visited(V, false); stack<int> st; for(int i=0; i<V; i++) { if(!visited[i]) topologicalSort(i, graph, visited, st); } vector<int> dist(V, numeric_limits<int>::max()); dist[0] = 0; while(!st.empty()) { int u = st.top(); st.pop(); if(dist[u] != numeric_limits<int>::max()) { for(auto edge : graph[u]) { int v = edge.first; int w = edge.second; if(dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } } for(int i=0; i<V; i++) { if(dist[i] == numeric_limits<int>::max()) cout << "INF "; else cout << dist[i] << " "; } cout << endl; return 0;
Attempts:
2 left
💡 Hint
Check how edge elements are accessed in topologicalSort function.
✗ Incorrect
In topologicalSort, the code uses edge.second as the next node, but edge.first is the node index and edge.second is the weight. Accessing edge.second as node index causes wrong memory access or runtime error.
❓ Predict Output
advancedOutput of shortest path distances with unreachable nodes
What is the output of the following code that finds shortest paths from node 0 in a DAG with some unreachable nodes?
DSA C++
#include <iostream> #include <vector> #include <stack> #include <limits> using namespace std; void topologicalSort(int node, vector<vector<pair<int,int>>>& graph, vector<bool>& visited, stack<int>& st) { visited[node] = true; for(auto& edge : graph[node]) { int v = edge.first; if(!visited[v]) topologicalSort(v, graph, visited, st); } st.push(node); } int main() { int V = 5; vector<vector<pair<int,int>>> graph(V); graph[0].push_back({1,3}); graph[1].push_back({2,4}); // Node 3 and 4 are disconnected vector<bool> visited(V, false); stack<int> st; for(int i=0; i<V; i++) { if(!visited[i]) topologicalSort(i, graph, visited, st); } vector<int> dist(V, numeric_limits<int>::max()); dist[0] = 0; while(!st.empty()) { int u = st.top(); st.pop(); if(dist[u] != numeric_limits<int>::max()) { for(auto& edge : graph[u]) { int v = edge.first; int w = edge.second; if(dist[u] + w < dist[v]) { dist[v] = dist[u] + w; } } } } for(int i=0; i<V; i++) { if(dist[i] == numeric_limits<int>::max()) cout << "INF "; else cout << dist[i] << " "; } cout << endl; return 0;
Attempts:
2 left
💡 Hint
Nodes 3 and 4 are disconnected, so their distances remain infinite.
✗ Incorrect
Nodes 3 and 4 are unreachable from node 0, so their distances remain infinity (INF). Nodes 1 and 2 have distances 3 and 7 respectively from node 0.
🧠 Conceptual
expertTime complexity of shortest path in DAG using topological order
What is the time complexity of finding shortest paths in a DAG using topological order and edge relaxation?
Attempts:
2 left
💡 Hint
Consider the cost of topological sort plus edge relaxation.
✗ Incorrect
Topological sort takes O(V + E) time. Relaxing edges once in topological order also takes O(E). So total time is O(V + E).
