A Directed Acyclic Graph (DAG) is a graph with directed edges and no cycles, meaning you cannot start at one node and follow a path that leads 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 that all edges go from earlier to later nodes. This order helps us relax edges only once, making shortest path calculation efficient in DAGs.
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
In the context of shortest path in DAG, what initial distance values do we assign before processing?
We assign 0 to the source node and infinity (or a very large number) to all other nodes to indicate they are initially unreachable.
Click to reveal answer
What property must a graph have to use topological order for shortest path calculation?
AIt must be a Directed Acyclic Graph (DAG)
BIt must be undirected
CIt must contain cycles
DIt must be a tree
✗ Incorrect
Only DAGs have a valid topological order because they have no cycles.
What is the first step in finding shortest paths in a DAG using topological order?
ARelax all edges randomly
BFind the topological order of the graph
CInitialize all distances to zero
DRun Dijkstra's algorithm
✗ 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, how many times is each edge relaxed?
AOnce
BMultiple times
CNever
DDepends on the graph size
✗ Incorrect
Each edge is relaxed exactly once in topological order, making the algorithm efficient.
What initial distance is assigned to the source node before starting the algorithm?
A-1
BInfinity
C0
DNumber of nodes
✗ Incorrect
The source node distance is set to 0 because the distance from source to itself is zero.
What happens if the graph contains a cycle when trying to find shortest path using topological order?
AEdges are ignored
BAlgorithm runs normally
CShortest path is found faster
DTopological order does not exist, so the method fails
✗ Incorrect
Topological order requires no cycles; if cycles exist, the method cannot be applied.
Explain how topological order helps in finding the shortest path in a DAG.
Think about how ordering nodes prevents revisiting edges multiple times.
You got /4 concepts.
Describe the steps to find shortest paths in a DAG using topological order starting from a source node.
Start with distance setup, then order, then edge relaxation.
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 guarantees the graph has cycles.
B. It allows processing vertices in a linear order without revisiting nodes.
C. It sorts vertices by their weights.
D. It finds the longest path instead of the shortest.
Solution
Step 1: Understand topological order in DAG
Topological order arranges vertices so that every directed edge goes from an earlier vertex to a later one, ensuring no cycles.
Step 2: Use topological order for shortest path
Processing vertices in this order allows updating shortest distances without revisiting nodes, making the algorithm efficient.
Final Answer:
It allows processing vertices in a linear order without revisiting nodes. -> Option B
Quick Check:
Topological order = linear processing [OK]
Hint: Topological order processes nodes once in DAG [OK]
Common Mistakes:
Thinking DAG has cycles
Confusing topological order with sorting by weights
Assuming it finds longest path by default
2. Which of the following C code snippets correctly initializes the distance array for shortest path calculation in a DAG with 5 vertices?
easy
A. int dist[5] = {0, 0, 0, 0, 0};
B. int dist[5] = {-1, -1, -1, -1, -1};
C. int dist[5]; for(int i=0; i<5; i++) dist[i] = INT_MAX;
D. int dist[5] = {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN};
Solution
Step 1: Understand initialization for shortest path
Distances should start as very large values (infinity) to represent unknown paths.
Step 2: Identify correct initialization
Using INT_MAX sets all distances to a large number, suitable for shortest path updates.
Final Answer:
int dist[5]; for(int i=0; i<5; i++) dist[i] = INT_MAX; -> Option C
Quick Check:
Initialize distances to INT_MAX [OK]
Hint: Initialize distances with INT_MAX for shortest path [OK]
Common Mistakes:
Initializing distances to zero (wrong for shortest path)
Using INT_MIN which is for max path, not min
Using -1 which can cause incorrect comparisons
3. Consider the following DAG with edges and weights: 0 -> 1 (2), 0 -> 2 (4), 1 -> 2 (1), 1 -> 3 (7), 2 -> 3 (3) If we run shortest path from vertex 0 using topological order, what is the shortest distance to vertex 3?
medium
A. 6
B. 7
C. 9
D. 10
Solution
Step 1: Find topological order and initialize distances
Topological order can be [0,1,2,3]. Initialize dist[0]=0, others = infinity.
Step 2: Relax edges in topological order
From 0: dist[1]=2, dist[2]=4. From 1: dist[2]=min(4,2+1)=3, dist[3]=2+7=9. From 2: dist[3]=min(9,3+3)=6.
Final Answer:
6 -> Option A
Quick Check:
Shortest path to 3 = 6 [OK]
Hint: Relax edges in topological order to update distances [OK]
Common Mistakes:
Not updating dist[2] to 3 from 1->2 edge
Ignoring better path from 2 to 3
Using wrong topological order
4. Given this code snippet for shortest path in DAG, what is the error?
for (int i = 0; i < V; i++) {
for (auto edge : adj[i]) {
int v = edge.first;
int weight = edge.second;
if (dist[i] + weight < dist[v])
dist[v] = dist[i] + weight;
}
}
Assume dist is initialized and adj is adjacency list.
medium
A. The outer loop should follow topological order, not 0 to V-1.
B. The weight variable is not assigned correctly.
C. The condition should be dist[v] + weight < dist[i].
D. The adjacency list should be reversed.
Solution
Step 1: Check loop order for shortest path in DAG
The outer loop must process vertices in topological order to ensure correct relaxation.
Step 2: Identify the error in loop order
Looping from 0 to V-1 may not respect topological order, causing incorrect distance updates.
Final Answer:
The outer loop should follow topological order, not 0 to V-1. -> Option A
Quick Check:
Use topological order for outer loop [OK]
Hint: Always loop vertices in topological order for DAG shortest path [OK]
Common Mistakes:
Ignoring topological order in loops
Mixing up edge weight usage
Reversing adjacency list unnecessarily
5. You have a DAG with 6 vertices and edges: 0->1(5), 0->2(3), 1->3(6), 1->2(2), 2->4(4), 2->5(2), 2->3(7), 3->4(-1), 4->5(-2) Using shortest path with topological order from vertex 0, what is the shortest distance to vertex 5?
hard
A. 7
B. 8
C. 4
D. 5
Solution
Step 1: Determine topological order and initialize distances
One topological order: [0,1,2,3,4,5]. Initialize dist[0]=0, others infinity.
Step 2: Relax edges in order
From 0: dist[1]=5, dist[2]=3. From 1: dist[3]=min(inf,5+6=11)=11, dist[2]=min(3,5+2=7)=3 (unchanged). From 2: dist[4]=min(inf,3+4=7)=7, dist[5]=min(inf,3+2=5)=5, dist[3]=min(11,3+7=10)=10. From 3: dist[4]=min(7,10+(-1)=9)=7 (unchanged). From 4: dist[5]=min(5,7+(-2)=5)=5 (unchanged).