📖 Scenario: You are working on a project to find the shortest travel time between cities connected by one-way roads. The roads form a Directed Acyclic Graph (DAG), meaning there are no loops. You want to use a smart method that processes cities in an order so that when you calculate the shortest path to a city, you have already calculated the shortest paths to all cities that lead to it.
🎯 Goal: Build a program that finds the shortest travel time from a starting city to all other cities using topological order in a DAG.
📋 What You'll Learn
Create a graph as an adjacency list with exact edges and weights
Create a variable for the number of cities
Implement topological sorting using DFS
Calculate shortest paths from the start city using the topological order
Print the shortest distances array
💡 Why This Matters
🌍 Real World
Finding shortest paths in a DAG is useful in project scheduling, task ordering, and route planning where cycles do not exist.
💼 Career
Understanding topological sorting and shortest path algorithms is important for software engineers working on optimization, graph problems, and dependency resolution.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a variable called graph as an array of arrays representing the adjacency list for 6 cities (0 to 5). Use these exact edges with weights: from city 0 to 1 with weight 5, from 0 to 2 with weight 3, from 1 to 3 with weight 6, from 1 to 2 with weight 2, from 2 to 4 with weight 4, from 2 to 5 with weight 2, from 2 to 3 with weight 7, from 3 to 4 with weight -1, from 4 to 5 with weight -2.
DSA Javascript
Hint
Use an array where each element is a list of pairs [neighbor, weight]. Follow the exact edges and weights given.
2
Set the number of cities and create a visited array
Create a variable called numCities and set it to 6. Then create a variable called visited as an array of length numCities filled with false.
DSA Javascript
Hint
Use new Array(numCities).fill(false) to create the visited array.
3
Implement topological sort and shortest path calculation
Write a function called topologicalSort that takes a city node and performs DFS to fill an array stack with cities in topological order. Then create a variable distances as an array of length numCities filled with Infinity. Set distances[0] to 0 as the start city. Use the stack to relax edges and update distances with shortest paths.
DSA Javascript
Hint
Use DFS to fill stack in topological order. Then pop from stack to relax edges and update distances.
4
Print the shortest distances array
Write a console.log statement to print the distances array.
DSA Javascript
Hint
Use console.log(distances) to print the final shortest distances array.
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.