0
0
DSA Typescriptprogramming~10 mins

Shortest Path in DAG Using Topological Order in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Shortest Path in DAG Using Topological Order
Build graph with edges and weights
Find topological order
Initialize distances (start=0, others=∞)
For each node in topo order
Relax edges from current node
Update distances if shorter path found
Done
Shortest distances from start to all nodes
We first build the graph, find a topological order, then relax edges in that order to find shortest paths.
Execution Sample
DSA Typescript
const graph = new Map<number, [number, number][]>();
graph.set(0, [[1, 2], [2, 4]]);
graph.set(1, [[2, 1], [3, 7]]);
graph.set(2, [[4, 3]]);
graph.set(3, [[5, 1]]);
graph.set(4, [[3, 2], [5, 5]]);

// Find shortest paths from node 0
This code sets up a weighted directed graph and prepares to find shortest paths from node 0.
Execution Table
StepOperationCurrent NodeDistances BeforeEdge RelaxedDistances AfterVisual State
1Topological Sort----Topo order: [0,1,2,4,3,5]
2Initialize distances---0:0,1:∞,2:∞,3:∞,4:∞,5:∞Distances: [0,∞,∞,∞,∞,∞]
3Relax edges from node 00[0,∞,∞,∞,∞,∞]0->1 (2)[0,2,∞,∞,∞,∞]Updated dist[1]=2
4Relax edges from node 00[0,2,∞,∞,∞,∞]0->2 (4)[0,2,4,∞,∞,∞]Updated dist[2]=4
5Relax edges from node 11[0,2,4,∞,∞,∞]1->2 (1)[0,2,3,∞,∞,∞]Updated dist[2]=3 (2+1)
6Relax edges from node 11[0,2,3,∞,∞,∞]1->3 (7)[0,2,3,9,∞,∞]Updated dist[3]=9 (2+7)
7Relax edges from node 22[0,2,3,9,∞,∞]2->4 (3)[0,2,3,9,6,∞]Updated dist[4]=6 (3+3)
8Relax edges from node 44[0,2,3,9,6,∞]4->3 (2)[0,2,3,8,6,∞]Updated dist[3]=8 (6+2)
9Relax edges from node 44[0,2,3,8,6,∞]4->5 (5)[0,2,3,8,6,11]Updated dist[5]=11 (6+5)
10Relax edges from node 33[0,2,3,8,6,11]3->5 (1)[0,2,3,8,6,9]Updated dist[5]=9 (8+1)
11Relax edges from node 55[0,2,3,8,6,9]-[0,2,3,8,6,9]No edges to relax
12End----Final distances: [0,2,3,8,6,9]
💡 All nodes processed in topological order; shortest distances finalized.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 10Final
distances[0,∞,∞,∞,∞,∞][0,2,∞,∞,∞,∞][0,2,3,9,∞,∞][0,2,3,9,6,∞][0,2,3,8,6,9][0,2,3,8,6,9]
current_node-01235
topo_order-----[0,1,2,4,3,5]
Key Moments - 3 Insights
Why do we relax edges in topological order?
Relaxing edges in topological order ensures that when we process a node, all shortest paths to it are already found, as shown in steps 3 to 10 in the execution_table.
Why do some distances update multiple times?
Distances update when a shorter path is found later in the order, like dist[2] updated at step 5 after step 4, because a better path was discovered.
Why is the initial distance to start node zero and others infinity?
Start node distance is zero because it's the source; others are infinity to represent unknown paths, as initialized in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the distance to node 2 after relaxing edge 1->2?
A4
B
C3
D2
💡 Hint
Check the 'Distances After' column at step 5 in execution_table.
At which step does the distance to node 5 first get updated?
AStep 10
BStep 9
CStep 8
DStep 11
💡 Hint
Look for the first 'Updated dist[5]' in the 'Visual State' column.
If the edge 4->3 had weight 10 instead of 2, how would the distance to node 3 change after step 8?
AIt would remain 9
BIt would become 16
CIt would become 8
DIt would become 6
💡 Hint
Check step 6 distance to node 3 and compare with step 8 update logic.
Concept Snapshot
Shortest Path in DAG Using Topological Order:
- Build graph with weighted edges
- Find topological order of nodes
- Initialize distances: start=0, others=∞
- Relax edges in topo order to update shortest distances
- Final distances give shortest path from start to all nodes
Full Transcript
We start by building a weighted directed graph. Then, we find a topological order of the nodes, which means an order where all edges go from earlier to later nodes. We initialize distances with zero for the start node and infinity for others. Then, we process each node in topological order, relaxing edges by checking if going through the current node offers a shorter path to neighbors. We update distances accordingly. This process ensures shortest paths are found efficiently in a DAG. The execution table shows each step with distances before and after relaxing edges, and the variable tracker shows how distances and current nodes change over time.