|
| 1 | +// Implementation of Kosaraju's algorithm to print all Strongly Connected Components |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class Graph |
| 6 | +{ |
| 7 | +// Adjacency List representation of Graph |
| 8 | + int size; |
| 9 | + LinkedList<Integer> adjacencyList[]; |
| 10 | + |
| 11 | + Graph(int n) |
| 12 | + { |
| 13 | + size = n; |
| 14 | + adjacencyList = new LinkedList[n]; |
| 15 | + for (int i=0; i<n; ++i) |
| 16 | + adjacencyList[i] = new LinkedList(); |
| 17 | + } |
| 18 | + |
| 19 | + void addEdge(int v, int w) |
| 20 | + { |
| 21 | + adjacencyList[v].add(w); |
| 22 | + } |
| 23 | +} |
| 24 | +public class Kosaraju |
| 25 | +{ |
| 26 | + Graph graph; |
| 27 | + Kosaraju(Graph graph){ |
| 28 | + this.graph = graph; |
| 29 | + } |
| 30 | + // A recursive function to print DFS starting from v |
| 31 | + void DFS(Graph g, int v,boolean visited[]) |
| 32 | + { |
| 33 | + visited[v] = true; |
| 34 | + System.out.print(v + " "); |
| 35 | + |
| 36 | + int n; |
| 37 | + |
| 38 | + Iterator<Integer> i = g.adjacencyList[v].iterator(); |
| 39 | + while (i.hasNext()) |
| 40 | + { |
| 41 | + n = i.next(); |
| 42 | + if (!visited[n]) |
| 43 | + DFS(g, n,visited); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Function that returns reverse of this graph |
| 48 | + Graph reverse() |
| 49 | + { |
| 50 | + Graph rev_graph = new Graph(graph.size); |
| 51 | + for (int v = 0; v < graph.size; v++) |
| 52 | + { |
| 53 | + Iterator<Integer> i = graph.adjacencyList[v].listIterator(); |
| 54 | + while(i.hasNext()) |
| 55 | + rev_graph.adjacencyList[i.next()].add(v); |
| 56 | + } |
| 57 | + return rev_graph; |
| 58 | + } |
| 59 | + |
| 60 | + void fillOrder(int v, boolean visited[], Stack stack) |
| 61 | + { |
| 62 | + visited[v] = true; |
| 63 | + |
| 64 | + Iterator<Integer> i = graph.adjacencyList[v].iterator(); |
| 65 | + while (i.hasNext()) |
| 66 | + { |
| 67 | + int n = i.next(); |
| 68 | + if(!visited[n]) |
| 69 | + fillOrder(n, visited, stack); |
| 70 | + } |
| 71 | + |
| 72 | + stack.push(v); |
| 73 | + } |
| 74 | + |
| 75 | + void printSCCs() |
| 76 | + { |
| 77 | + Stack stack = new Stack(); |
| 78 | + |
| 79 | + boolean visited[] = new boolean[graph.size]; |
| 80 | + for(int i = 0; i < graph.size; i++) |
| 81 | + visited[i] = false; |
| 82 | + |
| 83 | + for (int i = 0; i < graph.size; i++) |
| 84 | + if (visited[i] == false) |
| 85 | + fillOrder( i, visited, stack); |
| 86 | + |
| 87 | + Graph rev_graph = reverse(); |
| 88 | + |
| 89 | + for (int i = 0; i < graph.size; i++) |
| 90 | + visited[i] = false; |
| 91 | + |
| 92 | + while (stack.empty() == false) |
| 93 | + { |
| 94 | + int v = (int)stack.pop(); |
| 95 | + if (visited[v] == false) |
| 96 | + { |
| 97 | + DFS(rev_graph, v, visited); |
| 98 | + System.out.println(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void main(String args[]) |
| 104 | + { |
| 105 | + |
| 106 | + Graph graph = new Graph(9); |
| 107 | + graph.addEdge(0, 3); |
| 108 | + graph.addEdge(1, 7); |
| 109 | + graph.addEdge(2, 5); |
| 110 | + graph.addEdge(3, 6); |
| 111 | + graph.addEdge(4, 1); |
| 112 | + graph.addEdge(5, 8); |
| 113 | + graph.addEdge(6, 0); |
| 114 | + graph.addEdge(7, 4); |
| 115 | + graph.addEdge(7, 5); |
| 116 | + graph.addEdge(8, 6); |
| 117 | + graph.addEdge(8, 2); |
| 118 | + |
| 119 | + System.out.println("Strongly connected components are:\n"); |
| 120 | + Kosaraju scc = new Kosaraju(graph); |
| 121 | + scc.printSCCs(); |
| 122 | + } |
| 123 | +} |
0 commit comments