Skip to content

Commit

Permalink
Merge pull request #1815 from Kumar-laxmi/pilot
Browse files Browse the repository at this point in the history
Push Kosaraju's Algorithm in Java
  • Loading branch information
Kumar-laxmi authored Oct 7, 2024
2 parents be1377a + 0ae5fb8 commit ddbfcef
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
File renamed without changes.
113 changes: 113 additions & 0 deletions Java/Graphs/Kosaraju_Algorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import java.util.ArrayList;
import java.util.List;

class GFG {

// dfs Function to reach destination
boolean dfs(int curr, int des, List<List<Integer>> adj,
List<Integer> vis) {

// If curr node is destination return true
if (curr == des) {
return true;
}
vis.set(curr, 1);
for (int x : adj.get(curr)) {
if (vis.get(x) == 0) {
if (dfs(x, des, adj, vis)) {
return true;
}
}
}
return false;
}

// To tell whether there is path from source to
// destination
boolean isPath(int src, int des, List<List<Integer>> adj) {
List<Integer> vis = new ArrayList<>(adj.size() + 1);
for (int i = 0; i <= adj.size(); i++) {
vis.add(0);
}
return dfs(src, des, adj, vis);
}

// Function to return all the strongly connected
// component of a graph.
List<List<Integer>> findSCC(int n, List<List<Integer>> a) {
// Stores all the strongly connected components.
List<List<Integer>> ans = new ArrayList<>();

// Stores whether a vertex is a part of any Strongly
// Connected Component
List<Integer> is_scc = new ArrayList<>(n + 1);
for (int i = 0; i <= n; i++) {
is_scc.add(0);
}

List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i <= n; i++) {
adj.add(new ArrayList<>());
}

for (List<Integer> edge : a) {
adj.get(edge.get(0)).add(edge.get(1));
}

// Traversing all the vertices
for (int i = 1; i <= n; i++) {

if (is_scc.get(i) == 0) {

// If a vertex i is not a part of any SCC
// insert it into a new SCC list and check
// for other vertices whether they can be
// the part of this list.
List<Integer> scc = new ArrayList<>();
scc.add(i);

for (int j = i + 1; j <= n; j++) {

// If there is a path from vertex i to
// vertex j and vice versa, put vertex j
// into the current SCC list.
if (is_scc.get(j) == 0 && isPath(i, j, adj)
&& isPath(j, i, adj)) {
is_scc.set(j, 1);
scc.add(j);
}
}

// Insert the SCC containing vertex i into
// the final list.
ans.add(scc);
}
}
return ans;
}
}

public class Main {

public static void main(String[] args) {

GFG obj = new GFG();
int V = 5;
List<List<Integer>> edges = new ArrayList<>();
edges.add(new ArrayList<>(List.of(1, 3)));
edges.add(new ArrayList<>(List.of(1, 4)));
edges.add(new ArrayList<>(List.of(2, 1)));
edges.add(new ArrayList<>(List.of(3, 2)));
edges.add(new ArrayList<>(List.of(4, 5)));
List<List<Integer>> ans = obj.findSCC(V, edges);
System.out.println("Strongly Connected Components are:");
for (List<Integer> x : ans) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
}
}

// This code is contributed by shivamgupta310570
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
| 10. | Ford-Fulkerson Algorithm | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Graphs/FordFulkerson.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Ford-Fulkerson_Algorithm.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/FordFulkerson.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Graphs/Ford-Fulkerson_Algorithm.py) |
| 11. | Hamiltonian Path | [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/Hamiltonian_path.java) |
| 12. | Hopcroft-Karp Algorithm | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Graphs/Hopcroft-Karp%20algorithm.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Hopcroft-Karp%20algorithm.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/Hopcroft-Karp.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Graphs/Hopcroft-Karp%20algorithm.py) |
| 13. | Kosaraju's Algorithm | [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Kosaraju_Algorithm.cpp) |
| 13. | Kosaraju's Algorithm | [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Kosaraju_Algorithm.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/Kosaraju_Algorithm.java) |
| 14. | Kruskal's Algorithm | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Graphs/Kruskals.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Kruskals.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/Kruskals.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Graphs/Kruskals.py) |
| 15. | Multi Source Shortest Path | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Graphs/Multisource_Shortest_Path.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Multisource_Shortest_Path.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/Multisource_Shortest_Path.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Graphs/MultiSource_Shortest_Path.py) |
| 16. | Multistage Graph (Shortest Path) | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Graphs/multistage_graph.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/multistage_graph.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/MultistageGraph.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Graphs/multistage_graph.py) |
Expand Down

0 comments on commit ddbfcef

Please sign in to comment.