-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathBridgeUndirectedGraph.java
156 lines (127 loc) · 3.14 KB
/
BridgeUndirectedGraph.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package algo.graph;
import java.io.*;
import java.util.*;
/*
Bridges in an undirected graph:
An edge in an undirected connected graph is a bridge iff removing it disconnects the graph.
Time Complexity of below solution: O(V+E)
Examples: wired computer network, an articulation point indicates the critical computers and a bridge indicates the critical wires or connections.
Java version this problem was tested on: java version "1.8.0_51"
Sample way of running this problem:
1): Enter the maximum number of vertices in the graph
7
Enter an edge or type quit to exit
0
1
Enter an edge or type quit to exit
0
2
Enter an edge or type quit to exit
1
2
Enter an edge or type quit to exit
1
6
Enter an edge or type quit to exit
1
3
Enter an edge or type quit to exit
1
4
Enter an edge or type quit to exit
3
5
Enter an edge or type quit to exit
4
5
Enter an edge or type quit to exit
quit
Finding Bridges
1 6
*/
class Node{
int data;
public Node(int data)
{
this.data = data;
}
}
public class BridgeUndirectedGraph{
LinkedList<Node> ld[];
int temp;
public BridgeUndirectedGraph(int v)
{
ld = new LinkedList[v];
for(int i=0;i<v;i++)
{
ld[i] = new LinkedList<Node>();
}
}
public void addEdge(int a,int b)
{
ld[a].add(new Node(b));
ld[b].add(new Node(a));
}
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the maximum number of vertices in the graph");
int V = Integer.parseInt(br.readLine());
BridgeUndirectedGraph obj = new BridgeUndirectedGraph(V);
System.out.println("Enter an edge or type quit to exit");
String str = br.readLine();
while(!str.equalsIgnoreCase("quit")){
int b = Integer.parseInt(br.readLine());
obj.addEdge(Integer.parseInt(str),b);
System.out.println("Enter an edge or type quit to exit");
str = br.readLine();
}
System.out.println("Bridges found: ");
obj.findBridges(V);
}
public void findBridges(int vertices)
{
boolean visited[] = new boolean[vertices];
int parent[] = new int[vertices];
int low[] = new int[vertices];
int disc[] = new int[vertices];
for(int i=0;i<vertices;i++)
{
if(visited[i] != true)
{
bridgeUtil(i,visited,parent,low,disc);
}
}
}
public int min(int a,int b)
{
return (a<b)?a:b;
}
public void bridgeUtil(int u,boolean visited[],int parent[],int low[],int disc[])
{
low[u] = disc[u] = ++temp;
visited[u] = true;
LinkedList<Node> l = ld[u];
Iterator<Node> itr = l.iterator();
while(itr.hasNext())
{
int v = itr.next().data;
if(visited[v] != true)
{
parent[v] = u;
bridgeUtil(v,visited,parent,low,disc);
// Check if the subtree rooted with v has a
// connection to one of the ancestors of u
low[u] = min(low[u], low[v]);
// If the lowest vertex reachable from subtree
// under v is below u in DFS tree, then u-v is
// a bridge
if(low[v] > disc[u])
System.out.println(u + " " + v);
}else if(parent[u] != v) // Update low value of u for parent function calls.
{
low[u] = min(low[u],disc[v]);
}
}
}
}