Shortest path in DAG

Asked atLinkedin
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given a Directed Acyclic Graph of N vertices from 0 to N-1 and M edges and a 2D Integer array edges, where there is a directed edge from vertex edge[i][0] to vertex edge[i][1] with a distance of edge[i][2] for all i.

Find the shortest path from source vertex to all the vertices and if it is impossible to reach any vertex, then return -1 for that vertex. The source vertex is assumed to be 0.

Input: N = 4, M = 2 edge = [[0,1,2],[0,2,1]]

Output: 0 2 1 -1

Explanation: Shortest path from 0 to 1 is 0->1 with edge weight 2. Shortest path from 0 to 2 is 0->2 with edge weight 1. There is no way we can reach 3, so it's -1 for 3.

Input: N = 6, M = 7 edge = [[0,1,2],[0,4,1],[4,5,4],[4,2,2],[1,2,3],[2,3,6],[5,3,1]]

Output: 0 2 3 6 1 5

Explanation: Shortest path from 0 to 1 is 0->1 with edge weight 2. Shortest path from 0 to 2 is 0->4->2 with edge weight 1+2=3. Shortest path from 0 to 3 is 0->4->5->3 with edge weight 1+4+1=6. Shortest path from 0 to 4 is 0->4 with edge weight 1. Shortest path from 0 to 5 is 0->4->5 with edge weight 1+4=5.

Input: N = 3, M = 3 edge = [[0, 1, 4], [0, 2, 2], [1, 2, 5]]

  • 1 ≤ N,M ≤ 5*104
  • 0 ≤ edge[i][0],edge[i][1] < N-1
  • 1 ≤ edge[i][2] < 104

cpp

class Solution {
    public:
    vector < int > shortestPath(int N, int M, vector < vector < int >> & edges) {
    }
};

java

class Solution {
  public int[] shortestPath(int N, int M, int[][] edges) {
 
  }
}

python

class Solution:
    def shortestPath(self, N, M, edges):

javascript

class Solution {
    shortestPath(N, M, edges) {
  
    }
}

csharp

class Solution
{
    public List<int> ShortestPath(int N, int M, List<List<int>> edges) {

    }
}

go

func shortestPath(N int, M int, edges [][]int) []int {
	
}
Stuck? Show a way to structure it+
  1. 01Confirm the graph is acyclic and choose a source
  2. 02Produce a topological order
  3. 03Relax outgoing weighted edges only from reachable nodes
  4. 04Convert remaining infinity values to the requested unreachable marker

Reference answer

Then expect these follow-ups

  • Why does topological order make one pass sufficient?

    Tests: correctness reasoning

  • How would you reconstruct paths?

    Tests: implementation extension

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question