Added the program of Longest Path in DAG under Dynamic Programming #1753
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Longest Path in DAG
Definition:
This program finds the longest path from a given source vertex in a Directed Acyclic Graph (DAG) with weighted edges. It accomplishes this by performing a topological sort on the vertices and then using dynamic programming to calculate the longest path distances.
Problem Statement:
Given a weighted directed acyclic graph (DAG) and a source vertex, find the cost of the longest path from the source vertex to all other vertices present in the graph. If the vertex can’t be reached from the given source vertex, print its distance as infinity.
Algorithm to Find the Longest Path in a Directed Acyclic Graph (DAG)
Topological Sort
Perform a topological sort of the DAG. This will allow us to process each vertex in a linear order, ensuring that each vertex is processed after all its dependencies.
Initialize Distances
Create a distance array to store the longest path distances from the source. Set the distance of the source vertex to 0 and all other vertices to negative infinity (
-∞
), representing unvisited vertices.Process Vertices in Topological Order
For each vertex in the topologically sorted order:
-∞
), update the distances of its adjacent vertices.Update Distances of Adjacent Vertices
For each adjacent vertex of the current vertex, calculate the longest path distance from the source. If this new distance is greater than the current distance of the adjacent vertex, update it.
Output the Longest Path Distances
Print the longest distance to each vertex from the source. If a vertex's distance remains
-∞
, it means the vertex is unreachable from the source.Time Complexity:
O(V + E)
, whereV
is the number of vertices andE
is the number of edges. This is achieved by first performing a topological sort using Depth-First Search (DFS), which takesO(V + E)
time, and then processing each vertex in topological order to update the distances of its adjacent vertices, also inO(V + E)
time.Example
Sample Input:
Sample Output:
Explanation of Sample:
The maximum distance from the source node 0 to each node is as follows:
Node 0: The maximum distance from node 0 to itself is 0.
(The distance of a node from itself is always 0).
Node 1: The maximum distance from node 0 to node 1 is 3, achieved via the path
0 -> 1
.Node 2: The maximum distance from node 0 to node 2 is 10, achieved via the path
0 -> 2
.Node 3: The maximum distance from node 0 to node 3 is 15, achieved via the path
0 -> 2 -> 3
.Node 4: The maximum distance from node 0 to node 4 is 54, achieved via the path
0 -> 1 -> 4
.Thus, we should print the distances in order as follows: 0 3 10 15 54.