|
| 1 | +// Java program to check if a given directed graph |
| 2 | +// is strongly connected or not with BFS use |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class Graph { |
| 6 | + int V; // No. of vertices |
| 7 | + // An array of adjacency lists |
| 8 | + ArrayList<ArrayList<Integer> > adj; |
| 9 | + |
| 10 | + // Constructor and Destructor |
| 11 | + Graph(int V) |
| 12 | + { |
| 13 | + this.V = V; |
| 14 | + adj = new ArrayList<>(); |
| 15 | + for (int i = 0; i < V; i++) { |
| 16 | + adj.add(new ArrayList<>()); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + // Method to add an edge |
| 21 | + void addEdge(int v, int w) |
| 22 | + { |
| 23 | + adj.get(v).add(w); // Add w to v’s list. |
| 24 | + } |
| 25 | + |
| 26 | + // A recursive function to print DFS starting from v |
| 27 | + void BFSUtil(int v, boolean visited[]) |
| 28 | + { |
| 29 | + |
| 30 | + // Create a queue for BFS |
| 31 | + Queue<Integer> queue = new ArrayDeque<>(); |
| 32 | + |
| 33 | + // Mark the current node as visited and enqueue it |
| 34 | + visited[v] = true; |
| 35 | + queue.add(v); |
| 36 | + |
| 37 | + while (!queue.isEmpty()) |
| 38 | + { |
| 39 | + |
| 40 | + // Dequeue a vertex from queue |
| 41 | + v = queue.peek(); |
| 42 | + queue.remove(); |
| 43 | + |
| 44 | + // Get all adjacent vertices of the dequeued |
| 45 | + // vertex s If a adjacent has not been visited, |
| 46 | + // then mark it visited and enqueue it |
| 47 | + for (Integer i : adj.get(v)) { |
| 48 | + if (!visited[i]) { |
| 49 | + visited[i] = true; |
| 50 | + queue.add(i); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // The main function that returns true if the |
| 57 | + // graph is strongly connected, otherwise false |
| 58 | + boolean isSC() |
| 59 | + { |
| 60 | + // Step 1: Mark all the vertices as not |
| 61 | + // visited (For first BFS) |
| 62 | + boolean[] visited = new boolean[V]; |
| 63 | + for (int i = 0; i < V; i++) |
| 64 | + visited[i] = false; |
| 65 | + |
| 66 | + // Step 2: Do BFS traversal starting |
| 67 | + // from first vertex. |
| 68 | + BFSUtil(0, visited); |
| 69 | + |
| 70 | + // If BFS traversal doesn’t visit all |
| 71 | + // vertices, then return false. |
| 72 | + for (int i = 0; i < V; i++) |
| 73 | + if (visited[i] == false) |
| 74 | + return false; |
| 75 | + |
| 76 | + // Step 3: Create a reversed graph |
| 77 | + Graph gr = getTranspose(); |
| 78 | + |
| 79 | + // Step 4: Mark all the vertices as not |
| 80 | + // visited (For second BFS) |
| 81 | + for (int i = 0; i < V; i++) |
| 82 | + visited[i] = false; |
| 83 | + |
| 84 | + // Step 5: Do BFS for reversed graph |
| 85 | + // starting from first vertex. |
| 86 | + // Starting Vertex must be same starting |
| 87 | + // point of first DFS |
| 88 | + gr.BFSUtil(0, visited); |
| 89 | + |
| 90 | + // If all vertices are not visited in |
| 91 | + // second DFS, then return false |
| 92 | + for (int i = 0; i < V; i++) |
| 93 | + if (visited[i] == false) |
| 94 | + return false; |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + // Function that returns reverse (or transpose) |
| 100 | + // of this graph |
| 101 | + Graph getTranspose() |
| 102 | + { |
| 103 | + Graph g = new Graph(V); |
| 104 | + for (int v = 0; v < V; v++) { |
| 105 | + // Recur for all the vertices adjacent to this |
| 106 | + // vertex |
| 107 | + for (Integer i : adj.get(v)) |
| 108 | + g.adj.get(i).add(v); |
| 109 | + } |
| 110 | + return g; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +public class GFG { |
| 115 | + |
| 116 | + // Driver program to test above functions |
| 117 | + public static void main(String[] args) |
| 118 | + { |
| 119 | + Graph g1 = new Graph(5); |
| 120 | + g1.addEdge(0, 1); |
| 121 | + g1.addEdge(1, 2); |
| 122 | + g1.addEdge(2, 3); |
| 123 | + g1.addEdge(3, 0); |
| 124 | + g1.addEdge(2, 4); |
| 125 | + g1.addEdge(4, 2); |
| 126 | + if (g1.isSC()) |
| 127 | + System.out.println("Yes"); |
| 128 | + else |
| 129 | + System.out.println("No"); |
| 130 | + |
| 131 | + Graph g2 = new Graph(4); |
| 132 | + g2.addEdge(0, 1); |
| 133 | + g2.addEdge(1, 2); |
| 134 | + g2.addEdge(2, 3); |
| 135 | + if (g2.isSC()) |
| 136 | + System.out.println("Yes"); |
| 137 | + else |
| 138 | + System.out.println("No"); |
| 139 | + } |
| 140 | +} |
0 commit comments