From d8f3bab5a12f72a89e2056427fe91ec69dc8e62e Mon Sep 17 00:00:00 2001 From: Sujit Gunjal Date: Mon, 4 Nov 2024 18:54:04 +0000 Subject: [PATCH] Added feature to find Longest path in DAG --- java/LongestPathInDAG.md | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/java/LongestPathInDAG.md b/java/LongestPathInDAG.md index 0bd875089..3b4cb8a67 100644 --- a/java/LongestPathInDAG.md +++ b/java/LongestPathInDAG.md @@ -35,6 +35,7 @@ To solve this problem, we can use a combination of topological sorting and dynam ```java import java.util.*; +// Edge class to represent a directed edge with a weight class Edge { int dest, weight; @@ -44,10 +45,12 @@ class Edge { } } +// Graph class to represent the DAG and implement the longest path algorithm class Graph { private final int V; // Number of vertices private final List> adj; // Adjacency list for each vertex + // Constructor for initializing the graph public Graph(int V) { this.V = V; adj = new ArrayList<>(V); @@ -56,10 +59,12 @@ class Graph { } } + // Adds an edge to the graph public void addEdge(int u, int v, int weight) { adj.get(u).add(new Edge(v, weight)); } + // Performs topological sort and stores the result in a stack private void topologicalSort(int v, boolean[] visited, Stack stack) { visited[v] = true; for (Edge edge : adj.get(v)) { @@ -70,23 +75,28 @@ class Graph { stack.push(v); } - public void findLongestPath(int source) { + // Method to find the longest path from a given source vertex + public int[] findLongestPath(int source) { Stack stack = new Stack<>(); boolean[] visited = new boolean[V]; + // Perform topological sort on all vertices for (int i = 0; i < V; i++) { if (!visited[i]) { topologicalSort(i, visited, stack); } } + // Initialize distances to all vertices as negative infinity except source int[] dist = new int[V]; Arrays.fill(dist, Integer.MIN_VALUE); dist[source] = 0; + // Process vertices in topological order and update distances while (!stack.isEmpty()) { int u = stack.pop(); + // Update distances to all adjacent vertices of u if (dist[u] != Integer.MIN_VALUE) { for (Edge edge : adj.get(u)) { if (dist[edge.dest] < dist[u] + edge.weight) { @@ -96,24 +106,31 @@ class Graph { } } - for (int i = 0; i < V; i++) { - if (dist[i] == Integer.MIN_VALUE) { - System.out.println("Vertex " + i + ": Infinity"); - } else { - System.out.println("Vertex " + i + ": " + dist[i]); - } - } + return dist; // Returns the distances from the source to each vertex } +} +// Testing class with basic test cases to verify the code functionality +public class LongestPathInDAGTest { public static void main(String[] args) { - Graph graph = new Graph(4); // Example with 4 vertices + Graph graph = new Graph(4); graph.addEdge(0, 1, 5); graph.addEdge(0, 2, 3); graph.addEdge(1, 3, 6); graph.addEdge(2, 3, 7); graph.addEdge(1, 2, 2); - + + // Run longest path algorithm from vertex 0 + int[] distances = graph.findLongestPath(0); + + // Display results for each vertex System.out.println("Longest paths from vertex 0:"); - graph.findLongestPath(0); + for (int i = 0; i < distances.length; i++) { + if (distances[i] == Integer.MIN_VALUE) { + System.out.println("Vertex " + i + ": Infinity"); + } else { + System.out.println("Vertex " + i + ": " + distances[i]); + } + } } }