A DAG is a graph with directed edges and no cycles, meaning you cannot start at one node and follow a path that loops back to the same node.
Click to reveal answer
intermediate
Why do we use topological order for shortest path in a DAG?
Topological order arranges nodes so all edges go from earlier to later nodes, allowing us to relax edges in one pass without revisiting nodes, making shortest path calculation efficient.
Click to reveal answer
beginner
What does 'relaxing an edge' mean in shortest path algorithms?
Relaxing an edge means checking if the current known shortest distance to a node can be improved by going through another node connected by that edge, and updating it if yes.
Click to reveal answer
intermediate
What is the time complexity of finding shortest paths in a DAG using topological order?
The time complexity is O(V + E), where V is the number of vertices and E is the number of edges, because each node and edge is processed once.
Click to reveal answer
beginner
What happens if the graph has cycles when using topological order for shortest path?
Topological order does not exist for graphs with cycles, so this method cannot be used. The algorithm requires a DAG to work correctly.
Click to reveal answer
What is the first step in finding the shortest path in a DAG using topological order?
AFind the topological order of the graph
BInitialize all distances to infinity
CRelax all edges randomly
DCheck for cycles in the graph
✗ Incorrect
We first find the topological order to process nodes in a sequence that respects edge directions.
When relaxing edges in shortest path for DAG, what do we update?
AThe topological order of nodes
BThe number of edges in the graph
CThe distance to the destination node if a shorter path is found
DThe adjacency list
✗ Incorrect
Relaxing updates the shortest known distance to a node if a better path is found.
Why can't we use Dijkstra's algorithm directly on a DAG for shortest path?
ABecause DAGs have cycles
BBecause DAG edges have negative weights
CBecause Dijkstra's algorithm only works on undirected graphs
DBecause topological order is faster and simpler for DAGs
✗ Incorrect
Topological order allows a simpler and faster method for shortest path in DAGs compared to Dijkstra.
What is the initial distance assigned to the source node in shortest path calculation?
AZero
BNegative infinity
CInfinity
DOne
✗ Incorrect
The source node distance is set to zero because the distance from source to itself is zero.
If a node is unreachable from the source in a DAG, what is its shortest path distance after the algorithm?
AZero
BInfinity
CNegative one
DOne
✗ Incorrect
Unreachable nodes keep their distance as infinity, meaning no path exists from the source.
Explain how topological order helps in finding the shortest path in a DAG.
Think about how ordering nodes avoids revisiting and simplifies edge relaxation.
You got /4 concepts.
Describe the steps to find shortest paths from a source node in a DAG using topological order.
Start from ordering nodes, then update distances step-by-step.
You got /5 concepts.
Practice
(1/5)
1. What is the main advantage of using topological order to find the shortest path in a Directed Acyclic Graph (DAG)?
easy
A. It allows processing vertices in a linear order without revisiting nodes.
B. It guarantees the graph has cycles.
C. It sorts the graph by edge weights.
D. It finds the longest path instead of the shortest.
Solution
Step 1: Understand topological order in DAG
Topological order arranges nodes so all edges go from earlier to later nodes, ensuring no cycles.
Step 2: Use topological order for shortest path
Processing nodes in this order means each node is visited once, and distances can be updated efficiently without revisiting.
Final Answer:
It allows processing vertices in a linear order without revisiting nodes. -> Option A
Quick Check:
Topological order = linear processing [OK]
Hint: Topological order processes nodes once in DAG [OK]
Common Mistakes:
Thinking topological order applies to graphs with cycles
Confusing shortest path with longest path
Assuming topological order sorts by edge weights
2. Which of the following Python code snippets correctly initializes the distance array for shortest path calculation in a DAG with 5 nodes, starting from node 0?
easy
A. dist = [float('inf')] * 5; dist[5] = 0
B. dist = [0] * 5; dist[0] = float('inf')
C. dist = [None] * 5; dist[0] = 0
D. dist = [float('inf')] * 5; dist[0] = 0
Solution
Step 1: Initialize distances with infinity
We set all distances to infinity to represent unknown paths initially.
Step 2: Set start node distance to zero
The distance to the start node (0) is zero because it is the source.
Hint: Start distance zero, others infinity for shortest path [OK]
Common Mistakes:
Setting start node distance to infinity
Using invalid index dist[5] (out of range)
Initializing distances with zeros
3. Given the DAG edges with weights: 0->1 (2), 0->2 (4), 1->2 (1), 1->3 (7), 2->3 (3), and start node 0, what is the shortest distance to node 3 after running shortest path using topological order?
medium
A. 6
B. 7
C. 9
D. 10
Solution
Step 1: Calculate distances step-by-step
Start dist: [0, inf, inf, inf]. From 0: dist[1]=2, dist[2]=4. From 1: dist[2]=min(4,2+1=3)=3, dist[3]=2+7=9. From 2: dist[3]=min(9,3+3=6)=6.
Step 2: Final shortest distance to node 3
The shortest distance to node 3 is 6 after updates.
Final Answer:
6 -> Option A
Quick Check:
Shortest path to 3 = 6 [OK]
Hint: Update distances using topological order edges [OK]
Common Mistakes:
Not updating dist[2] to 3 via node 1
Taking longer path 0->1->3 as shortest
Ignoring edge weights when updating distances
4. Consider this code snippet for shortest path in DAG:
dist = [float('inf')] * 4
dist[0] = 0
topo_order = [0, 1, 2, 3]
graph = {0: [(1, 1), (2, 4)], 1: [(2, 2), (3, 6)], 2: [(3, 3)], 3: []}
for u in topo_order:
for v, w in graph[u]:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
print(dist)
What is the output?
medium
A. [0, 1, 2, 5]
B. [0, 1, 4, 7]
C. [0, 1, 3, 6]
D. [0, 1, 3, 9]
Solution
Step 1: Initialize distances and process node 0
dist = [0, inf, inf, inf]. From 0: dist[1]=1, dist[2]=4.
Step 2: Process node 1 and update distances
From 1: dist[2]=min(4,1+2=3)=3, dist[3]=1+6=7.
Step 3: Process node 2 and update distance
From 2: dist[3]=min(7,3+3=6)=6.
Step 4: Final distances array
dist = [0, 1, 3, 6]
Final Answer:
[0, 1, 3, 6] -> Option C
Quick Check:
Distances updated correctly using topological order [OK]
Hint: Update dist[v] only if new path is shorter [OK]
Common Mistakes:
Not updating dist[2] to 3 via node 1
Using wrong edge weights
Printing dist before updates complete
5. You have a DAG with nodes 0 to 4 and edges: 0->1 (3), 0->2 (6), 1->2 (4), 1->3 (4), 2->3 (8), 3->4 (2). Using topological order [0,1,2,3,4], what is the shortest path distance from node 0 to node 4?
hard
A. 15
B. 9
C. 13
D. 11
Solution
Step 1: Initialize distances and update from node 0