-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticulationPoint.java
More file actions
51 lines (41 loc) · 1.58 KB
/
ArticulationPoint.java
File metadata and controls
51 lines (41 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package graph;
import java.util.ArrayList;
/**
* A vertex in an undirected connected graph is an articulation point if removing it disconnects the graph.
*/
public class ArticulationPoint {
// Time Complexity - O(V + E).
public static void getAP(ArrayList<ArrayList<Integer>> graph, int V){
int[] dt = new int[V];
int[] low = new int[V];
int time = 0;
boolean[] isVisited = new boolean[V];
boolean[] ap = new boolean[V];
for (int i = 0; i < V; i++) {
if(!isVisited[i]) dfs(graph, i, isVisited, dt, low, time, -1, ap);
}
System.out.println("Articulation Points: ");
for (int i = 0; i < V; i++) {
if(ap[i]) System.out.print(i + " ");
}
System.out.println();
}
private static void dfs(ArrayList<ArrayList<Integer>> graph, int curr, boolean[] isVisited, int[] dt, int[] low, int time, int parent, boolean[] ap) {
isVisited[curr] = true;
dt[curr] = low[curr] = ++time;
int children = 0;
for (int neighbour : graph.get(curr)) {
if(parent == neighbour){
continue;
}else if(isVisited[neighbour]){
low[curr] = Math.min(low[curr], dt[neighbour]);
} else{
dfs(graph, neighbour, isVisited, dt, low, time, curr, ap);
low[curr] = Math.min(low[curr], low[neighbour]);
if(dt[curr] <= low[neighbour] && parent != -1) ap[curr] = true;
children++;
}
}
if(parent == -1 && children > 1) ap[curr] = true;
}
}