A DAG is a graph with directed edges and no cycles, meaning you cannot start at one vertex and follow a path that loops back to it.
Click to reveal answer
intermediate
Why do we use topological order to find the shortest path in a DAG?
Topological order arranges vertices so all edges go from earlier to later vertices, allowing us to relax edges in one pass without revisiting nodes.
Click to reveal answer
beginner
What does 'relaxing an edge' mean in shortest path algorithms?
Relaxing an edge means checking if the current known distance to a vertex can be improved by going through another vertex connected by that edge, and updating it if yes.
Click to reveal answer
beginner
In the shortest path algorithm for DAG, what initial distance values do we assign?
We set the distance to the source vertex as 0 and all other vertices 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 the shortest path 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 vertex and edge is processed once.
Click to reveal answer
What property must a graph have to use topological order for shortest path?
AIt must be a Directed Acyclic Graph (DAG)
BIt must be undirected
CIt must have 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?
AFind the topological order of vertices
BRelax all edges randomly
CInitialize all distances to zero
DRun Dijkstra's algorithm
✗ Incorrect
We first find the topological order to process vertices in a sequence that respects edge directions.
When relaxing edges in topological order, how many times is each edge processed?
ANever
BTwice
CMultiple times until no changes
DOnce
✗ Incorrect
Each edge is relaxed exactly once in topological order, making the algorithm efficient.
What value do we assign to the source vertex distance before starting relaxation?
A-1
BInfinity
C0
DRandom number
✗ Incorrect
The source vertex distance is set to 0 because the distance from source to itself is zero.
Why can't we use topological order shortest path algorithm on graphs with cycles?
ABecause cycles make distances negative
BBecause topological order does not exist for graphs with cycles
CBecause cycles increase the number of vertices
DBecause cycles make the graph undirected
✗ Incorrect
Graphs with cycles do not have a topological order, so this method cannot be applied.
Explain how topological order helps in finding the shortest path in a DAG.
Think about how ordering vertices prevents revisiting.
You got /4 concepts.
Describe the steps to find the shortest path from a source vertex in a DAG using topological order.
Start from initialization and end with final distances.
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 no cycles.
C. It finds the longest path instead of the shortest path.
D. It works only for undirected graphs.
Solution
Step 1: Understand topological order in DAG
Topological order arranges vertices so that all edges go from earlier to later vertices, allowing linear processing.
Step 2: Use topological order for shortest path
Processing vertices in this order ensures each vertex is relaxed once, avoiding revisits and cycles.
Final Answer:
It allows processing vertices in a linear order without revisiting nodes. -> Option A
Quick Check:
Topological order = linear processing without revisits [OK]
Hint: Topological order processes nodes once in DAG [OK]
Common Mistakes:
Thinking it works for graphs with cycles
Confusing shortest path with longest path
Assuming it works for undirected graphs
2. Which of the following C++ code snippets correctly initializes the distance array for shortest path calculation in a DAG with n vertices, assuming source vertex is 0?
easy
A. std::vector<int> dist(n, 0); dist[0] = INT_MAX;
B. std::vector<int> dist(n, INT_MAX); dist[0] = 0;
C. int dist[n]; for(int i=0; i
D. std::vector<int> dist(n); dist[0] = 0;
Solution
Step 1: Initialize distances with maximum value
Distances should start as infinity (INT_MAX) except source which is 0.
Step 2: Check code correctness
std::vector<int> dist(n, INT_MAX); dist[0] = 0; correctly sets all distances to INT_MAX and source distance to 0.
Final Answer:
std::vector<int> dist(n, INT_MAX); dist[0] = 0; -> Option B
Quick Check:
Distance init = INT_MAX except source 0 [OK]
Hint: Initialize all distances to INT_MAX except source 0 [OK]
Common Mistakes:
Setting source distance to INT_MAX
Initializing distances to zero
Not using vector or uninitialized array
3. Consider the following DAG with edges and weights: 0 -> 1 (2), 0 -> 2 (4), 1 -> 2 (1), 1 -> 3 (7), 2 -> 3 (3) Using topological order starting from vertex 0, 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
5. Given a DAG with vertices 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, what is the shortest path distance from vertex 0 to vertex 4?
hard
A. 13
B. 11
C. 9
D. 14
Solution
Step 1: Determine topological order and initialize distances