Challenge - 5 Problems
Shortest Path DAG Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of shortest path distances in DAG
What is the output of the shortest path distances array after running the given code on the DAG with source vertex 0?
DSA Javascript
const graph = [
[{to: 1, weight: 2}, {to: 2, weight: 4}],
[{to: 2, weight: 1}, {to: 3, weight: 7}],
[{to: 4, weight: 3}],
[{to: 5, weight: 1}],
[{to: 3, weight: 2}, {to: 5, weight: 5}],
[]
];
function topologicalSort(graph) {
const visited = new Array(graph.length).fill(false);
const stack = [];
function dfs(node) {
visited[node] = true;
for (const edge of graph[node]) {
if (!visited[edge.to]) dfs(edge.to);
}
stack.push(node);
}
for (let i = 0; i < graph.length; i++) {
if (!visited[i]) dfs(i);
}
return stack.reverse();
}
function shortestPathDAG(graph, source) {
const order = topologicalSort(graph);
const dist = new Array(graph.length).fill(Infinity);
dist[source] = 0;
for (const u of order) {
if (dist[u] !== Infinity) {
for (const edge of graph[u]) {
if (dist[edge.to] > dist[u] + edge.weight) {
dist[edge.to] = dist[u] + edge.weight;
}
}
}
}
return dist;
}
console.log(shortestPathDAG(graph, 0));Attempts:
2 left
💡 Hint
Trace the topological order and update distances step-by-step.
✗ Incorrect
The topological order is [0,1,2,4,3,5]. Starting from source 0 with distance 0, distances update as follows: 0->1=2, 0->2=4, then 1->2=3 (updated to 3 since 2+1=3 < 4), 1->3=9, 2->4=6, 4->3=8 (updated to 7 since 6+2=8 > 7), 4->5=11, 3->5=8 (updated to 8 since 7+1=8 < 11). Final distances: [0,2,3,7,6,8].
🧠 Conceptual
intermediateWhy topological order is used in shortest path for DAG?
Why do we use topological order to find the shortest path in a Directed Acyclic Graph (DAG)?
Attempts:
2 left
💡 Hint
Think about how processing order affects distance updates in graphs without cycles.
✗ Incorrect
Topological order processes vertices so that all edges go from earlier to later vertices. This guarantees that when we update distances for a vertex, all possible shorter paths from predecessors are already computed, avoiding revisits and cycles.
🔧 Debug
advancedIdentify the error in shortest path code for DAG
What error will occur when running this code snippet for shortest path in a DAG?
DSA Javascript
function shortestPathDAG(graph, source) {
const order = topologicalSort(graph);
const dist = new Array(graph.length).fill(Infinity);
dist[source] = 0;
for (const u of order) {
for (const edge of graph[u]) {
if (dist[edge.to] > dist[u] + edge.weight) {
dist[edge.to] = dist[u] + edge.weight;
}
}
}
return dist;
}
// graph and topologicalSort defined correctly
console.log(shortestPathDAG(graph, 0));Attempts:
2 left
💡 Hint
Check if the code verifies if the current distance is valid before updating neighbors.
✗ Incorrect
The code does not check if dist[u] is Infinity before relaxing edges. This causes incorrect updates because adding weight to Infinity results in Infinity, but the comparison may incorrectly update distances.
❓ Predict Output
advancedOutput of shortest path distances with unreachable nodes
Given the graph below and source vertex 0, what is the output of the shortest path distances array?
DSA Javascript
const graph = [
[{to: 1, weight: 3}],
[{to: 2, weight: 4}],
[],
[{to: 4, weight: 2}],
[]
];
function topologicalSort(graph) {
const visited = new Array(graph.length).fill(false);
const stack = [];
function dfs(node) {
visited[node] = true;
for (const edge of graph[node]) {
if (!visited[edge.to]) dfs(edge.to);
}
stack.push(node);
}
for (let i = 0; i < graph.length; i++) {
if (!visited[i]) dfs(i);
}
return stack.reverse();
}
function shortestPathDAG(graph, source) {
const order = topologicalSort(graph);
const dist = new Array(graph.length).fill(Infinity);
dist[source] = 0;
for (const u of order) {
if (dist[u] !== Infinity) {
for (const edge of graph[u]) {
if (dist[edge.to] > dist[u] + edge.weight) {
dist[edge.to] = dist[u] + edge.weight;
}
}
}
}
return dist;
}
console.log(shortestPathDAG(graph, 0));Attempts:
2 left
💡 Hint
Nodes 3 and 4 are disconnected from source 0; their distances remain Infinity.
✗ Incorrect
Nodes 3 and 4 are not reachable from source 0. Distances for unreachable nodes remain Infinity. Distances for reachable nodes 1 and 2 are updated correctly: 0->1=3, 1->2=7.
🧠 Conceptual
expertTime complexity of shortest path in DAG using topological order
What is the time complexity of finding the shortest path in a DAG using topological order and edge relaxation?
Attempts:
2 left
💡 Hint
Consider the cost of topological sort and single pass edge relaxation.
✗ Incorrect
Topological sort takes O(V + E). Relaxing edges once in topological order also takes O(E). Total time is O(V + E). No priority queue is used.
