|
| 1 | +/*Problem Description |
| 2 | +A vertex is said to be an articulation point in a graph if removal of the vertex and associated edges disconnects the graph. |
| 3 | +You are given the nodes and edges . Find the articulation point. |
| 4 | +*/ |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | +import java.lang.*; |
| 8 | +import java.io.*; |
| 9 | + |
| 10 | +public class Articulation_Point { |
| 11 | + // Initialize all the required variables with their type. |
| 12 | + private static int[] low = new int[10001]; |
| 13 | + private static int[] visited = new int[10001]; |
| 14 | + private static int[] disc = new int[10001] ; |
| 15 | + private static int timer = 1; |
| 16 | + private static Set<Integer> hashset ; |
| 17 | + static ArrayList<Integer> adj[]; |
| 18 | + // This class will help us to create a graph and know the articulation point. |
| 19 | + static class Graph |
| 20 | + { |
| 21 | + static int v ; |
| 22 | + // Creating graph |
| 23 | + Graph(int v) |
| 24 | + { |
| 25 | + Graph.v = v; |
| 26 | + adj = new ArrayList[v+1]; |
| 27 | + for(int i = 0 ; i < v+1 ; i++) |
| 28 | + { |
| 29 | + adj[i] = new ArrayList<Integer>(); |
| 30 | + } |
| 31 | + } |
| 32 | + // adding nodes with each other |
| 33 | + static void addEdge(int u , int v) |
| 34 | + { |
| 35 | + adj[u].add(v); |
| 36 | + adj[v].add(u); |
| 37 | + } |
| 38 | + // The method will help us to know the articulation point |
| 39 | + /* |
| 40 | + Code flow : |
| 41 | + step 1 : We need to calculate the lowest discovery number. |
| 42 | + step 2 : The algorithm then searches for its adjacent vertices. |
| 43 | + step 3 : the algorithms take a pair of vertices and checks whether its an articulation point or not. |
| 44 | + step 4 : There’s one special case when the root is an articulation point |
| 45 | + */ |
| 46 | + static void dfs(int node , int par) |
| 47 | + { |
| 48 | + visited[node] = 1 ; |
| 49 | + low[node] = disc[node] = timer++; |
| 50 | + int children = 0 ; |
| 51 | + for(int child : adj[node] ) |
| 52 | + { |
| 53 | + if(child == par) continue; |
| 54 | + if(visited[child] == 0 ) |
| 55 | + { |
| 56 | + children++; |
| 57 | + dfs(child , node ); |
| 58 | + if(disc[node] <= low[child] && par != -1) |
| 59 | + { |
| 60 | + hashset.add(node ); |
| 61 | + } |
| 62 | + low[node] = Math.min(low[node] , low[child] ); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + low[node] = Math.min(low[node], disc[child] ); |
| 67 | + } |
| 68 | + } |
| 69 | + // if root is articulation point |
| 70 | + if(par == 0 && children > 1) |
| 71 | + { |
| 72 | + hashset.add(node); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + public static void main(String[] args) throws IOException |
| 77 | + { |
| 78 | + Scanner sc = new Scanner(System.in); |
| 79 | + System.out.println("Enter number of testcase"); |
| 80 | + int test = sc.nextInt(); |
| 81 | + while(test-- > 0) |
| 82 | + { |
| 83 | + // Clear all the values inside the array to start new fresh array for next test case |
| 84 | + Arrays.fill(disc, 0); |
| 85 | + Arrays.fill(visited, 0); |
| 86 | + Arrays.fill(low, 0); |
| 87 | + timer = 0 ; |
| 88 | + // hashset for storing the articulation point |
| 89 | + hashset = new HashSet<Integer>(); |
| 90 | + // No of nodes |
| 91 | + System.out.println("Enter number of nodes"); |
| 92 | + int n = sc.nextInt(); |
| 93 | + // No of edges |
| 94 | + System.out.println("Enter number of edges"); |
| 95 | + int m = sc.nextInt(); |
| 96 | + // Create a graph with 10001 entries so it does not overflow |
| 97 | + Graph g = new Graph(10001); |
| 98 | + // Add the nodes with each other |
| 99 | + System.out.println("Enter values of edges"); |
| 100 | + for(int i = 1 ; i <= m ; i++) |
| 101 | + { |
| 102 | + int a = sc.nextInt(); |
| 103 | + int b = sc.nextInt(); |
| 104 | + g.addEdge(a,b); |
| 105 | + } |
| 106 | + // visit every element of nodes and accordingly call dfs function |
| 107 | + for(int i = 1 ; i < n + 1 ; i++ ) |
| 108 | + { |
| 109 | + if(visited[i] == 0 ) |
| 110 | + g.dfs(i , -1 ); |
| 111 | + } |
| 112 | + // Print all the articulation point |
| 113 | + for(Integer val : hashset) |
| 114 | + { |
| 115 | + System.out.print(val + " "); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/* Input |
| 122 | +Enter number of testcase |
| 123 | +1 |
| 124 | +Enter number of nodes |
| 125 | +4 |
| 126 | +Enter number of edges |
| 127 | +5 |
| 128 | +Enter values of edges |
| 129 | +1 0 |
| 130 | +0 2 |
| 131 | +2 1 |
| 132 | +0 3 |
| 133 | +3 4 |
| 134 | +
|
| 135 | +Output |
| 136 | +0 3 |
| 137 | +
|
| 138 | +Explanation : |
| 139 | +From all the nodes we find that articulation point is 0 and 3.This will seperate the graph and can make many unconnect graph |
| 140 | +Time Complexity : O(V + E) |
| 141 | +Space Complexity : O(V + E) |
| 142 | +*/ |
0 commit comments