diff --git a/C++/Graphs/Kosaraju_Algorithm.cpp "b/C++/Graphs/\tKosaraju's Algorithm.cpp" similarity index 100% rename from C++/Graphs/Kosaraju_Algorithm.cpp rename to "C++/Graphs/\tKosaraju's Algorithm.cpp" diff --git a/Java/Graphs/Kosaraju_Algorithm.java b/Java/Graphs/Kosaraju_Algorithm.java new file mode 100644 index 000000000..9ddcf8192 --- /dev/null +++ b/Java/Graphs/Kosaraju_Algorithm.java @@ -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> adj, + List 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> adj) { + List 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> findSCC(int n, List> a) { + // Stores all the strongly connected components. + List> ans = new ArrayList<>(); + + // Stores whether a vertex is a part of any Strongly + // Connected Component + List is_scc = new ArrayList<>(n + 1); + for (int i = 0; i <= n; i++) { + is_scc.add(0); + } + + List> adj = new ArrayList<>(); + for (int i = 0; i <= n; i++) { + adj.add(new ArrayList<>()); + } + + for (List 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 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> 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> ans = obj.findSCC(V, edges); + System.out.println("Strongly Connected Components are:"); + for (List x : ans) { + for (int y : x) { + System.out.print(y + " "); + } + System.out.println(); + } + } +} + +// This code is contributed by shivamgupta310570 diff --git a/README.md b/README.md index 84072bd45..f93ba17d8 100644 --- a/README.md +++ b/README.md @@ -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) |