-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCut Vertex or Articulation Point
59 lines (50 loc) · 1.8 KB
/
Cut Vertex or Articulation Point
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
52
53
54
55
56
57
58
59
public class Main {
static int[] low,dfsnum;
static int num=1;
static int noOfVertices;
static boolean[] art;
public static void main(String[] args) {
noOfVertices=7;
int[][] adjMatrix=new int[][]{
{0,1,0,1,0,0,0},
{1,0,1,0,0,0,0},
{0,1,0,1,0,0,1},
{1,0,1,0,1,1,0},
{0,0,0,1,0,1,0},
{0,0,0,1,1,0,0},
{0,0,1,0,0,0,0}
};
low=new int[noOfVertices];
dfsnum=new int[noOfVertices];
art=new boolean[noOfVertices];
cutVertices(adjMatrix,0,-1);
System.out.println("Cut vertex / articulation points are: ");
for(int i=0;i<noOfVertices;i++){
if(art[i]){
System.out.println((char)('A'+i));
}
}
}
static void cutVertices(int[][] adjMatrix, int vertex,int p){
low[vertex]=dfsnum[vertex]=num++;
int child=0;
for(int v=0;v<noOfVertices;v++){
if(adjMatrix[vertex][v]==1) {
if(dfsnum[v]!=0){
low[vertex]=Math.min(low[vertex],dfsnum[v]);
}
else{
child++;
cutVertices(adjMatrix,v,vertex);
low[vertex] = Math.min(low[vertex],low[v]);
if(low[v]>=dfsnum[vertex]){
art[vertex]=true;
}
}
}
}
if(p==-1 && child<2){
art[vertex]=false;
}
}
}