A DAG (Directed Acyclic Graph) is a graph with directed edges and no cycles, meaning you cannot start at one node and follow a path that loops back to it.
Click to reveal answer
intermediate
Why can we use topological order to find shortest paths in a DAG?
Because a DAG has no cycles, we can order its nodes so that all edges go from earlier to later nodes. This order lets us relax edges once in a single pass to find shortest paths efficiently.
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 the destination node can be improved by going through the source node of that edge, and updating it if yes.
Click to reveal answer
beginner
In the shortest path algorithm for DAGs, what initial distance values do we assign to nodes?
We set the distance to the source node as 0 and all other nodes as infinity (or a very large number) to indicate they are not yet reachable.
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
What property of a DAG allows us to use topological sorting for shortest path calculation?
AIt is undirected
BIt has no cycles
CAll edges have equal weight
DIt has only one node
✗ Incorrect
A DAG has no cycles, which allows a linear ordering of nodes (topological order) to process edges efficiently.
What is the first step in finding shortest paths in a DAG using topological order?
AFind the topological order of nodes
BInitialize all distances to zero
CRelax all edges randomly
DConvert the graph to undirected
✗ 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?
ADistance to the destination node if a shorter path is found
BThe weight of the edge
CThe topological order
DThe number of nodes
✗ Incorrect
Relaxation updates the shortest known distance to a node if a better path is found.
What initial distance value is assigned to nodes other than the source in shortest path DAG algorithm?
ANegative one
BZero
CRandom value
DInfinity or a very large number
✗ Incorrect
Nodes other than the source start with infinity to indicate they are not reachable yet.
What is the overall time complexity of shortest path in DAG using topological order?
AO(V^2)
BO(E^2)
CO(V + E)
DO(log V)
✗ Incorrect
Each node and edge is processed once, so the time complexity is linear in the size of the graph.
Explain how topological order helps in finding shortest paths in a DAG.
Think about how ordering nodes prevents revisiting and cycles.
You got /4 concepts.
Describe the steps to find shortest paths from a source node in a DAG using topological sorting.
Start from ordering, 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 finds the longest path instead of the shortest.
B. It guarantees the graph has cycles.
C. It sorts vertices by their weights.
D. It allows processing vertices in a linear order without revisiting nodes.
Solution
Step 1: Understand topological order in DAG
Topological order arranges vertices so all edges go from earlier to later vertices, ensuring no cycles.
Step 2: Use topological order for shortest path
Processing vertices in this order allows updating shortest paths without revisiting nodes, making the algorithm efficient.
Final Answer:
It allows processing vertices in a linear order without revisiting nodes. -> Option D
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
2. Which Rust code snippet correctly initializes a vector to store shortest distances with infinity for all vertices except the source (index 0)?
easy
A. let dist = vec![i32::MAX; n]; dist[0] = 0;
B. let dist = vec![0; n]; dist[0] = i32::MAX;
C. let dist = vec![None; n]; dist[0] = Some(0);
D. let dist = vec![0; n];
Solution
Step 1: Initialize distances with maximum value
Using vec![i32::MAX; n] sets all distances to infinity (max int), representing unknown paths.
Step 2: Set source distance to zero
dist[0] = 0 sets the source vertex distance to zero, as it is the start point.
Final Answer:
let dist = vec![i32::MAX; n]; dist[0] = 0; -> Option A
Quick Check:
Initialize with max, source zero [OK]
Hint: Use vec![i32::MAX; n] and set source to 0 [OK]
Common Mistakes:
Setting source distance to max instead of zero
Using zero for all distances initially
Using Option types unnecessarily
3. Given the following Rust code snippet for shortest path in a DAG, what is the final distance vector printed?
let n = 4;
let edges = vec![
(0, 1, 1),
(0, 2, 4),
(1, 2, 2),
(1, 3, 6),
(2, 3, 3),
];
let mut dist = vec![i32::MAX; n];
dist[0] = 0;
let topo_order = vec![0, 1, 2, 3];
for &u in &topo_order {
for &(src, dst, w) in &edges {
if src == u && dist[u] != i32::MAX && dist[u] + w < dist[dst] {
dist[dst] = dist[u] + w;
}
}
}
println!("{:?}", dist);
From 1: dist[2] = min(4, 1+2=3) = 3, dist[3] = 1+6=7. From 2: dist[3] = min(7, 3+3=6) = 6.
Step 3: Process vertex 3
No outgoing edges, distances remain.
Final Answer:
[0, 1, 3, 6] -> Option B
Quick Check:
Relax edges in topo order = [0,1,3,6] [OK]
Hint: Relax edges in topological order to update distances [OK]
Common Mistakes:
Not updating dist[2] to 3 via vertex 1
Ignoring better path to dist[3]
Printing distances before updates
4. Identify the bug in this Rust code snippet for shortest path in a DAG using topological order:
let n = 3;
let edges = vec![(0, 1, 5), (1, 2, 10)];
let mut dist = vec![i32::MAX; n];
dist[0] = 0;
let topo_order = vec![0, 1, 2];
for u in topo_order {
for &(src, dst, w) in &edges {
if src == u && dist[dst] != i32::MAX && dist[u] + w < dist[dst] {
dist[dst] = dist[u] + w;
}
}
}
println!("{:?}", dist);
medium
A. The condition 'dist[dst] != i32::MAX' prevents updating distances correctly.
B. The topological order vector is incorrect.
C. The initial distance vector should be zeros.
D. Edges are not represented as tuples.
Solution
Step 1: Analyze the if condition in edge relaxation
The condition 'dist[dst] != i32::MAX' wrongly blocks updating dist[dst] if it is still infinity, so no update happens initially.
Step 2: Correct the condition to allow updates
Remove 'dist[dst] != i32::MAX' so that if dist[u] + w is less, dist[dst] updates even if dist[dst] was infinity.
Final Answer:
The condition 'dist[dst] != i32::MAX' prevents updating distances correctly. -> Option A
Quick Check:
Wrong condition blocks updates [OK]
Hint: Allow updates even if dist[dst] is infinity [OK]
Common Mistakes:
Adding unnecessary condition on dist[dst]
Incorrect topological order
Initializing distances with zeros
5. You have a DAG with 5 vertices and edges with weights: (0->1:2), (0->2:3), (1->3:4), (2->3:1), (3->4:5). Using topological order [0,1,2,3,4], what is the shortest path distance from vertex 0 to vertex 4?