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, and updating it if yes.
Click to reveal answer
intermediate
How do you find the shortest path from a source node in a DAG using topological order?
First, find the topological order of nodes. Initialize distances with infinity except source as 0. Then, for each node in order, relax all outgoing edges to update shortest distances.
Click to reveal answer
intermediate
What is the time complexity of shortest path in DAG using topological order?
It 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 must a graph have to use topological order for shortest path?
AIt must be a tree
BIt must be undirected
CIt must contain cycles
DIt must be a Directed Acyclic Graph (DAG)
✗ Incorrect
Topological order only exists for DAGs because cycles prevent a linear ordering.
What is the initial distance value for all nodes except the source in shortest path using topological order?
A0
B-1
CInfinity
D1
✗ Incorrect
Distances start as infinity to represent unknown paths, except the source which is 0.
What does relaxing an edge help achieve in shortest path algorithms?
AUpdate the shortest distance if a better path is found
BFind the longest path
CRemove cycles
DSort the nodes
✗ Incorrect
Relaxation updates the shortest known distance to a node if a better path is found.
Which data structure is commonly used to find topological order?
AStack
BQueue
CHeap
DHash Map
✗ Incorrect
A stack is used in DFS-based topological sort to store nodes in reverse finishing order.
What is the time complexity of shortest path in DAG using topological order?
AO(V^2)
BO(V + E)
CO(V!)
DO(E log V)
✗ Incorrect
Each vertex and edge is processed once, so the complexity is O(V + E).
Explain how topological order helps in finding the shortest path in a DAG.
Think about how ordering nodes prevents revisiting and cycles.
You got /4 concepts.
Describe the steps to compute 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 allows processing nodes in a linear order without revisiting them.
B. It guarantees the graph has cycles.
C. It sorts nodes based on their 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 nodes in a linear order without revisiting them. -> Option A
Quick Check:
Topological order = linear processing without revisits [OK]
Hint: Topological order processes nodes once for shortest path [OK]
Common Mistakes:
Thinking topological order sorts by weights
Assuming DAG has cycles
Confusing shortest path with longest path
2. Which of the following JavaScript code snippets correctly initializes the distance array for shortest path calculation in a DAG with 5 nodes, starting from node 0?
easy
A. const dist = Array(5).fill(-1); dist[0] = 0;
B. const dist = new Array(5).fill(0); dist[0] = Infinity;
C. const dist = Array(5).fill(null); dist[0] = 0;
D. const dist = Array(5).fill(Infinity); dist[0] = 0;
Solution
Step 1: Initialize distances for shortest path
Distances to all nodes except start should be Infinity to represent unknown paths.
Step 2: Set start node distance to zero
Distance to start node (0) is zero because it is the source.
Final Answer:
const dist = Array(5).fill(Infinity); dist[0] = 0; -> Option D
Quick Check:
Start distance 0, others Infinity [OK]
Hint: Use Infinity for unknown distances, zero for start [OK]
Common Mistakes:
Setting start node distance to Infinity
Using 0 for all distances initially
Using null or -1 which are invalid for distances
3. Consider the following DAG represented as adjacency list with weights:
If you want to find the shortest path from node 0 to node 5 using topological order, which sequence of distances will you get before processing node 3?
hard
A. [0, 3, 2, 5, Infinity, Infinity]
B. [0, 3, 2, 3, Infinity, Infinity]
C. [0, 3, 2, 3, 2, Infinity]
D. [0, 3, 2, 1, Infinity, Infinity]
Solution
Step 1: Initialize distances and process nodes 0,1,2
Start dist = [0, Infinity, Infinity, Infinity, Infinity, Infinity]. After 0: dist[1]=3, dist[2]=2. After 1: dist[3]=3+4=7. After 2: dist[3]=min(7,2+1=3)=3.
Step 2: Before processing node 3
At this point, node 3's distance has been finalized by its predecessors to 3, but outgoing edges from 3 have not been relaxed yet, so dist[4] and dist[5] remain Infinity. Distances: [0, 3, 2, 3, Infinity, Infinity].
Final Answer:
[0, 3, 2, 3, Infinity, Infinity] -> Option B
Quick Check:
Distances before relaxing node 3 edges [OK]
Hint: Check distances before relaxing edges from current node [OK]
Common Mistakes:
Updating distances of neighbors too early
Confusing order of relaxation
Miscomputing minimum distances
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.