Shortest path in DAG

Graphs Hard Problems Hard
  • A practical application of this programming problem lies in routing and navigation software, such as Google Maps or Waze
  • The "Directed Acyclic Graph" represents the map's network of roads, with the "vertices" representing intersections and the "edges" representing the roads themselves
  • The algorithm to find the "shortest path" from a source (user's starting point) to all vertices (possible destinations) is a fundamental operation of these services
  • This would allow them to provide real-time directions and traffic updates, indicating the fastest route to the destination
  • If a path can't be found (represented by '-1'), it means that destination is unreachable from the current location

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.

Examples:

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]]

Constraints

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

Hints

  • "Perform Depth-First Search (DFS) to determine the order. If a back-edge is detected during DFS traversal, it means a cycle exists, and the order is invalid."

Company Tags

Oracle Boston Consulting Group Databricks Twilio Texas Instruments Philips Healthcare Uber Optum McKinsey & Company Broadcom Lyft Snowflake Cerner Bungie Zoho Ernst & Young Activision Blizzard Electronic Arts Intel Roblox Morgan Stanley Seagate Technology Shopify Freshworks Medtronic Google Microsoft Amazon Meta Apple Netflix Adobe