From 1fc96de64dd88c0a67e55cee89c86c0f001b51ad Mon Sep 17 00:00:00 2001 From: Taranpreet Singh <47025523+taran-1407@users.noreply.github.com> Date: Fri, 2 Oct 2020 17:35:57 +0530 Subject: [PATCH] Created ArticulationPoint.java Created ArticulationPoint.java, expects graph to be generated in accordance with GraphBuilder makeGraph method. --- ArticulationPoint/ArticulationPoint.java | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ArticulationPoint/ArticulationPoint.java diff --git a/ArticulationPoint/ArticulationPoint.java b/ArticulationPoint/ArticulationPoint.java new file mode 100644 index 0000000..a28f680 --- /dev/null +++ b/ArticulationPoint/ArticulationPoint.java @@ -0,0 +1,37 @@ +class ArticulationPoints{ + private static int time; + private static int[] disc, low; + private static boolean[] visited; + + public static boolean[] articulationPoints(int[][] graph){ + int NumberOfNodes = graph.length; + + time = -1; + disc = new int[NumberOfNodes]; + low = new int[NumberOfNodes]; + visited = new boolean[NumberOfNodes]; + java.util.Arrays.fill(low, 2*NumberOfNodes); + + boolean[] isAP = new boolean[NumberOfNodes]; + for(int i = 0; i< NumberOfNodes; i++)if(!visited[i] && graph[i] != null)apUtil(graph, isAP, i, -1); + return isAP; + } + + private static void apUtil(int[][] g, boolean[] isAP, int u, int p){ + visited[u] = true; + disc[u] = low[u] = ++time; + + int childCount = 0; + for(int v:g[u]){ + if(!visited[v]){ + apUtil(g, isAP, v, u); + + low[u] = Math.min(low[u], low[v]); + childCount++; + + if(p != -1 && low[v] >= disc[u])isAP[u] = true; + }else if(v != p)low[u] = Math.min(low[u], disc[v]); + } + if(p == -1 && childCount > 1)isAP[u] = true; + } +}