• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 04, 2023 |810 Views

PROBLEM OF THE DAY: 03/08/2023 | Shortest path in Directed Acyclic Graph

Description
Discussion

In this video, we are given a Directed Acyclic Graph of N vertices from 0 to N-1 and a 2D Integer array(or vector) edges[ ][ ] of length M, where there is a directed edge from edge[i][0] to edge[i][1] with a distance of edge[i][2] for all i. We have to find the shortest path from src(0) vertex to all the vertices and if it is impossible to reach any vertex, then return -1 for that vertex.

For example:

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

Output:
0 2 1 -1

Explanation:
The shortest path from 0 to 1 is 0->1 with edge weight 2. 
The 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.

Give the problem a try before going through the video. All the best!!!

Problem Link: https://practice.geeksforgeeks.org/problems/shortest-path-in-undirected-graph/1