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 a DAG
What is the output of the shortest path distances array after running the given code on the DAG?
DSA Python
def shortest_path_dag(graph, start): from collections import deque n = len(graph) indegree = [0]*n for u in range(n): for v, w in graph[u]: indegree[v] += 1 queue = deque([start]) dist = [float('inf')] * n dist[start] = 0 while queue: u = queue.popleft() for v, w in graph[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w indegree[v] -= 1 if indegree[v] == 0: queue.append(v) return dist graph = [ [(1, 2), (2, 4)], [(2, 1), (3, 5)], [(4, 3)], [(5, 1)], [(3, 2), (5, 5)], [] ] print(shortest_path_dag(graph, 0))
Attempts:
2 left
💡 Hint
Trace the graph edges and update distances step-by-step using topological order.
✗ Incorrect
The code uses a queue to process nodes in topological order by indegree zero. Distances are updated when a shorter path is found. The final distances from node 0 to others are [0, 2, 3, 7, 6, 8].
🧠 Conceptual
intermediateUnderstanding topological order in shortest path calculation
Why is topological order important when finding the shortest path in a Directed Acyclic Graph (DAG)?
Attempts:
2 left
💡 Hint
Think about dependencies between nodes and when distances can be finalized.
✗ Incorrect
Topological order processes nodes only after all nodes leading into them are processed, ensuring all shortest distances to predecessors are known before updating the current node.
🔧 Debug
advancedIdentify the error in shortest path code for DAG
What error will the following code produce when run on a DAG with multiple disconnected components?
DSA Python
def shortest_path_dag(graph, start): from collections import deque n = len(graph) indegree = [0]*n for u in range(n): for v, w in graph[u]: indegree[v] += 1 queue = deque([start]) dist = [float('inf')] * n dist[start] = 0 while queue: u = queue.popleft() for v, w in graph[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w indegree[v] -= 1 if indegree[v] == 0: queue.append(v) return dist # graph with disconnected node 6 graph = [ [(1, 2), (2, 4)], [(2, 1), (3, 7)], [(4, 3)], [(5, 1)], [(3, 2), (5, 5)], [], [] ] print(shortest_path_dag(graph, 0))
Attempts:
2 left
💡 Hint
Consider what happens to nodes not reachable from the start node.
✗ Incorrect
The code does not process disconnected nodes, so their distances remain infinity. No error occurs because graph[6] is an empty list and indegree list matches graph size.
❓ Predict Output
advancedOutput of shortest path distances with negative edge weights in DAG
What is the output of the shortest path distances array after running the code on the DAG with negative edge weights?
DSA Python
def shortest_path_dag(graph, start): from collections import deque n = len(graph) indegree = [0]*n for u in range(n): for v, w in graph[u]: indegree[v] += 1 queue = deque([start]) dist = [float('inf')] * n dist[start] = 0 while queue: u = queue.popleft() for v, w in graph[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w indegree[v] -= 1 if indegree[v] == 0: queue.append(v) return dist graph = [ [(1, 3), (2, 6)], [(2, -2), (3, 1)], [(3, 1)], [] ] print(shortest_path_dag(graph, 0))
Attempts:
2 left
💡 Hint
Negative edges are allowed in DAG shortest path if no cycles exist.
✗ Incorrect
The shortest path distances are updated correctly even with negative edges because the graph is acyclic. Distances are: 0 to 0 is 0, to 1 is 3, to 2 is 1 (3 + -2), to 3 is 2 (1 + 1).
🚀 Application
expertNumber of nodes reachable from start in shortest path DAG algorithm
After running the shortest path algorithm on the given DAG, how many nodes have a finite shortest path distance from the start node 0?
DSA Python
def shortest_path_dag(graph, start): from collections import deque n = len(graph) indegree = [0]*n for u in range(n): for v, w in graph[u]: indegree[v] += 1 queue = deque([start]) dist = [float('inf')] * n dist[start] = 0 while queue: u = queue.popleft() for v, w in graph[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w indegree[v] -= 1 if indegree[v] == 0: queue.append(v) return dist graph = [ [(1, 5)], [(2, 3)], [(3, 1)], [(4, 2)], [], [(6, 1)], [] ] distances = shortest_path_dag(graph, 0) count = sum(1 for d in distances if d != float('inf')) print(count)
Attempts:
2 left
💡 Hint
Count nodes reachable by following edges from start node 0.
✗ Incorrect
Nodes 0,1,2,3,4 are reachable with finite distances. Nodes 5 and 6 are disconnected from 0, so their distances remain infinity.
