|
| 1 | +package Graph; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | + |
| 6 | +public class BFSTraversal { |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | + |
| 10 | + // int totalVertices = 5; // test graph 1 |
| 11 | + // int totalVertices = 7; // test graph 2 |
| 12 | + int totalVertices = 9; // test graph 3 |
| 13 | + |
| 14 | + ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>(totalVertices); |
| 15 | + for (int i = 0; i < totalVertices; i++) { |
| 16 | + adjList.add(new ArrayList<Integer>(i)); |
| 17 | + } |
| 18 | + |
| 19 | + // note: be careful to adjust totalVertices per test cases of Graph |
| 20 | + |
| 21 | + /* test graph 1 - connected graph */ |
| 22 | + |
| 23 | + // addEdge(adjList, 0, 1); |
| 24 | + // addEdge(adjList, 0, 2); |
| 25 | + // addEdge(adjList, 1, 2); |
| 26 | + // addEdge(adjList, 1, 3); |
| 27 | + // addEdge(adjList, 2, 3); |
| 28 | + // addEdge(adjList, 2, 4); |
| 29 | + // addEdge(adjList, 3, 4); |
| 30 | + |
| 31 | + // int sourceVertex = 0; |
| 32 | + // boolean[] visited = new boolean[totalVertices]; |
| 33 | + // breadthFirstSearchTraverse(adjList, sourceVertex, visited); |
| 34 | + // System.out.println(); |
| 35 | + |
| 36 | + /* test graph 2 - disconnected graph with two components */ |
| 37 | + |
| 38 | + // addEdge(adjList, 0, 1); |
| 39 | + // addEdge(adjList, 0, 2); |
| 40 | + // addEdge(adjList, 1, 3); |
| 41 | + // addEdge(adjList, 2, 3); |
| 42 | + // addEdge(adjList, 4, 5); |
| 43 | + // addEdge(adjList, 4, 6); |
| 44 | + // addEdge(adjList, 5, 6); |
| 45 | + // |
| 46 | + // bfsDisconnectedGraph(adjList); |
| 47 | + |
| 48 | + /* test graph 3 - disconnected graph with three components */ |
| 49 | + |
| 50 | + addEdge(adjList, 0, 1); |
| 51 | + addEdge(adjList, 0, 2); |
| 52 | + addEdge(adjList, 1, 2); |
| 53 | + addEdge(adjList, 3, 4); |
| 54 | + addEdge(adjList, 5, 6); |
| 55 | + addEdge(adjList, 5, 7); |
| 56 | + addEdge(adjList, 7, 8); |
| 57 | + |
| 58 | + bfsDisconnectedGraph(adjList); |
| 59 | + } |
| 60 | + |
| 61 | + private static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) { |
| 62 | + adj.get(u).add(v); |
| 63 | + adj.get(v).add(u); |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * Provided adjacency list representation of Graph, doing BFS traversal. |
| 68 | + * |
| 69 | + * It can only deal with connected graph. |
| 70 | + */ |
| 71 | + |
| 72 | + // O(V+E) Time |
| 73 | + public static void breadthFirstSearchTraverse(ArrayList<ArrayList<Integer>> adj, int src, boolean[] visited) { |
| 74 | + |
| 75 | + LinkedList<Integer> queue = new LinkedList<>(); |
| 76 | + |
| 77 | + queue.addLast(src); |
| 78 | + visited[src] = true; |
| 79 | + |
| 80 | + while (!queue.isEmpty()) { |
| 81 | + int removedVertex = queue.removeFirst(); |
| 82 | + System.out.print(removedVertex + " "); |
| 83 | + |
| 84 | + for (Integer neighbor : adj.get(removedVertex)) { |
| 85 | + if (visited[neighbor] == false) { |
| 86 | + queue.addLast(neighbor); |
| 87 | + visited[neighbor] = true; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /* |
| 94 | + * if the graph is either disconnected or connected |
| 95 | + * |
| 96 | + * flag counts no. of islands in graph |
| 97 | + * |
| 98 | + * O(V+E) Time, V is no. of Vertices, E is no. of Edges |
| 99 | + * |
| 100 | + */ |
| 101 | + public static void bfsDisconnectedGraph(ArrayList<ArrayList<Integer>> adj) { |
| 102 | + boolean[] visited = new boolean[adj.size()]; |
| 103 | + int flag = 0; // to count connected components |
| 104 | + |
| 105 | + for (int i = 0; i < adj.size(); i++) { |
| 106 | + if (visited[i] == false) { |
| 107 | + flag++; |
| 108 | + breadthFirstSearchTraverse(adj, i, visited); |
| 109 | + } |
| 110 | + } |
| 111 | + System.out.println("\n\nno. of connected components/ Island in Graph: " + flag); |
| 112 | + } |
| 113 | +} |
0 commit comments