-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCut Bridge or Cut Edges
50 lines (43 loc) · 1.6 KB
/
Cut Bridge or Cut Edges
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
public class Main {
static int[] low,dfsnum;
static int num=1;
static int noOfVertices;
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,0,1,0},
{0,0,0,0,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];
System.out.println("Cut Edges / Cut Bridges are: ");
for (int i = 0; i < noOfVertices; i++) {
if (dfsnum[i] == 0) {
cutEdges(adjMatrix,i,i);
}
}
}
static void cutEdges(int[][] adjMatrix, int vertex,int pre){
low[vertex]=dfsnum[vertex]=num++;
for(int v=0;v<noOfVertices;v++){
if(v==pre) continue;
if(adjMatrix[vertex][v]==1) {
if(dfsnum[v]!=0){
low[vertex]=Math.min(low[vertex],dfsnum[v]);
}
else{
cutEdges(adjMatrix,v,vertex);
low[vertex] = Math.min(low[vertex],low[v]);
if(low[v]>dfsnum[vertex]){
System.out.println((char)('A'+vertex) +" ----- "+(char)('A'+v));
}
}
}
}
}
}