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 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 that all edges go from earlier to later nodes. This order helps find shortest paths efficiently by relaxing edges only once in the correct sequence.
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 node can be improved by going through another node, and updating the distance if it is shorter.
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 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
What property must a graph have to use topological order for shortest path?
AIt must be a complete graph
BIt must be an undirected graph
CIt must be a Directed Acyclic Graph (DAG)
DIt must contain cycles
✗ Incorrect
Topological order only exists for Directed Acyclic Graphs (DAGs), so shortest path using topological order requires the graph to be a DAG.
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 nodes
CInitialize all distances to zero
DRun Dijkstra's algorithm
✗ Incorrect
The first step is to find the topological order of the nodes to process them in the correct sequence.
When relaxing edges in topological order, how many times is each edge processed?
AOnce
BTwice
CMultiple times until no changes
DNever
✗ Incorrect
Each edge is relaxed exactly once in topological order, making the algorithm efficient.
What initial distance value is assigned to nodes other than the source?
ARandom value
BZero
CNegative one
DInfinity (a very large number)
✗ Incorrect
Nodes other than the source start with distance infinity to indicate they are not reachable yet.
What is the main advantage of using topological order for shortest path in DAGs?
AIt allows processing nodes in a sequence that guarantees correct shortest paths with one pass
BIt works for graphs with cycles
CIt requires no initialization
DIt uses more memory
✗ Incorrect
Topological order ensures nodes are processed so that all edges go forward, allowing shortest paths to be found in one pass.
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 find shortest paths 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 cycles.
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 means each vertex is processed after all its predecessors, so shortest paths can be updated without revisiting nodes.
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 correct sequence [OK]
Common Mistakes:
Thinking topological order sorts by weights
Assuming DAG has cycles
Confusing shortest path with longest path
2. Which of the following Java code snippets correctly initializes the distance array for shortest path calculation in a DAG with 5 vertices, assuming source vertex is 0?
easy
A. int[] dist = new int[5]; Arrays.fill(dist, Integer.MIN_VALUE); dist[0] = 0;
B. int[] dist = new int[5]; Arrays.fill(dist, 0); dist[0] = Integer.MAX_VALUE;
C. int[] dist = new int[5]; for(int i=0; i<5; i++) dist[i] = 0; dist[0] = 0;
D. int[] dist = new int[5]; for(int i=0; i<5; i++) dist[i] = Integer.MAX_VALUE; dist[0] = 0;
Solution
Step 1: Initialize distances to max value
Distances to all vertices except source should be set to Integer.MAX_VALUE to represent infinity.
Step 2: Set source distance to zero
Distance to source vertex 0 must be zero as it is the starting point.
Final Answer:
int[] dist = new int[5]; for(int i=0; i<5; i++) dist[i] = Integer.MAX_VALUE; dist[0] = 0; -> Option D
Quick Check:
Initialize all to max, source to zero [OK]
Hint: Set all distances max except source zero [OK]
Common Mistakes:
Setting all distances to zero
Setting source distance to max value
Using Integer.MIN_VALUE instead of max
3. Consider the following DAG with edges and weights: 0 -> 1 (2), 0 -> 2 (4), 1 -> 2 (1), 1 -> 3 (7), 2 -> 4 (3), 3 -> 4 (1) Using topological order [0,1,2,3,4], what is the shortest distance array from source 0 after processing?
No error; code correctly relaxes edges in topological order. -> Option B
Quick Check:
Relax edges with correct condition and loops [OK]
Hint: Relax edges with dist[u]+weight < dist[v] in topo order [OK]
Common Mistakes:
Confusing graph[u] with graph[e.dest]
Using wrong comparison operator
Forgetting to initialize dist array
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) If you run shortest path using topological order starting from vertex 0, what is the shortest distance to vertex 5?