Skip to content

Commit

Permalink
Added feature to find Longest path in DAG
Browse files Browse the repository at this point in the history
  • Loading branch information
sujitgunjal committed Nov 4, 2024
1 parent c84a2c0 commit d8f3bab
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions java/LongestPathInDAG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<List<Edge>> adj; // Adjacency list for each vertex

// Constructor for initializing the graph
public Graph(int V) {
this.V = V;
adj = new ArrayList<>(V);
Expand All @@ -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<Integer> stack) {
visited[v] = true;
for (Edge edge : adj.get(v)) {
Expand All @@ -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<Integer> 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) {
Expand All @@ -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]);
}
}
}
}

0 comments on commit d8f3bab

Please sign in to comment.