Skip to content

Commit 8b561fc

Browse files
Added Code
1 parent ff05116 commit 8b561fc

File tree

2 files changed

+352
-0
lines changed

2 files changed

+352
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import java.util.*;
2+
3+
4+
public class Articulation_Point_and_Bridges {
5+
6+
class Pair{
7+
int a,b;
8+
9+
Pair(int a, int b){
10+
this.a = a;
11+
this.b = b;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
// TODO Auto-generated method stub
17+
return "{"+String.valueOf(this.a)+", "+String.valueOf(this.b)+"}";
18+
}
19+
}
20+
21+
class Graph{
22+
23+
24+
Set<Integer> articulation_points;
25+
ArrayList<ArrayList<Integer>> arr;
26+
ArrayList<Pair> bridges;
27+
int V;
28+
29+
Graph(int V){
30+
this.V =V;
31+
this.arr = new ArrayList<>();
32+
articulation_points = new HashSet<>();
33+
bridges = new ArrayList<>();
34+
for(int i=0;i<V;i++) {
35+
arr.add(new ArrayList<>());
36+
}
37+
}
38+
39+
public void add_edge(int x, int y) {
40+
this.arr.get(x).add(y);
41+
this.arr.get(y).add(x);
42+
}
43+
44+
public void display() {
45+
46+
for(int i=0; i<V; i++) {
47+
System.out.print(String.valueOf(i));
48+
49+
for(int j=0;j<this.arr.get(i).size(); j++) {
50+
System.out.print(" -> "+this.arr.get(i).get(j));
51+
}
52+
System.out.println();
53+
}
54+
}
55+
56+
57+
public void points_bridges() {
58+
59+
int[] discoverTime = new int[this.V];
60+
int[] lowestTime = new int[this.V];
61+
62+
int time = 1;
63+
for(int node=0; node<this.V; node++) {
64+
if(discoverTime[node]==0) {
65+
//System.out.println("Time: "+time);
66+
dfs_helper(node, -1,discoverTime,lowestTime,time);
67+
}
68+
}
69+
70+
//display_arr(discoverTime);
71+
//display_arr(lowestTime);
72+
}
73+
74+
public void dfs_helper(int curr, int parent,int[] discoverTime, int[] lowestTime, int time) {
75+
76+
int noOfChild = 0;
77+
discoverTime[curr] = lowestTime[curr] = time;
78+
time+=1;
79+
80+
for(int child : this.arr.get(curr)) {
81+
82+
if(discoverTime[child]==0) {
83+
noOfChild+=1;
84+
85+
dfs_helper(child,curr, discoverTime, lowestTime, time);
86+
lowestTime[curr] = Math.min(lowestTime[curr], lowestTime[child]);
87+
88+
89+
if(parent!=-1 && lowestTime[child]>=discoverTime[curr]) {
90+
// AP exist
91+
//System.out.println("C: "+curr);
92+
this.articulation_points.add(curr);
93+
}
94+
95+
if(lowestTime[child]>discoverTime[curr]) {
96+
// Bridge exist
97+
this.bridges.add(new Pair(curr, child));
98+
}
99+
100+
101+
} else if(discoverTime[child]>0 && child != parent) {
102+
lowestTime[curr] = Math.min(lowestTime[curr], discoverTime[child]);
103+
}
104+
}
105+
106+
107+
// Separate case for src node
108+
if(parent==-1 && noOfChild>=2) {
109+
this.articulation_points.add(curr);
110+
}
111+
}
112+
113+
public void display_arr(int[] arr) {
114+
for(int i=0; i<arr.length; i++) {
115+
System.out.print(arr[i]+", ");
116+
}
117+
System.out.println();
118+
}
119+
}
120+
121+
public static void main(String[] args) {
122+
// TODO Auto-generated method stub
123+
124+
Graph g1 = new Articulation_Point_and_Bridges().new Graph(5);
125+
g1.add_edge(0, 1);
126+
g1.add_edge(1, 2);
127+
g1.add_edge(2, 0);
128+
g1.add_edge(2, 3);
129+
g1.add_edge(3, 4);
130+
131+
g1.points_bridges();
132+
System.out.println("Points: "+g1.articulation_points);
133+
System.out.println("Bridges: "+g1.bridges);
134+
135+
System.out.println("**************************************");
136+
137+
Graph g2 = new Articulation_Point_and_Bridges().new Graph(5);
138+
g2.add_edge(1, 0);
139+
g2.add_edge(0, 2);
140+
g2.add_edge(2, 1);
141+
g2.add_edge(0, 3);
142+
g2.add_edge(3, 4);
143+
144+
g2.points_bridges();
145+
System.out.println("Points: "+g2.articulation_points);
146+
System.out.println("Bridges: "+g2.bridges);
147+
148+
System.out.println("**************************************");
149+
150+
Graph g3 = new Articulation_Point_and_Bridges().new Graph(4);
151+
g3.add_edge(0, 1);
152+
g3.add_edge(1, 2);
153+
g3.add_edge(2, 3);
154+
155+
g3.points_bridges();
156+
System.out.println("Points: "+g3.articulation_points);
157+
System.out.println("Bridges: "+g3.bridges);
158+
159+
System.out.println("**************************************");
160+
161+
Graph g4 = new Articulation_Point_and_Bridges().new Graph(7);
162+
g4.add_edge(0, 1);
163+
g4.add_edge(1, 2);
164+
g4.add_edge(2, 0);
165+
g4.add_edge(1, 3);
166+
g4.add_edge(1, 4);
167+
g4.add_edge(1, 6);
168+
g4.add_edge(3, 5);
169+
g4.add_edge(4, 5);
170+
171+
g4.points_bridges();
172+
System.out.println("Points: "+g4.articulation_points);
173+
System.out.println("Bridges: "+g4.bridges);
174+
}
175+
176+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import java.util.*;
2+
3+
4+
public class Articulation_Point_and_Bridges {
5+
6+
class Pair{
7+
int a,b;
8+
9+
Pair(int a, int b){
10+
this.a = a;
11+
this.b = b;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
// TODO Auto-generated method stub
17+
return "{"+String.valueOf(this.a)+", "+String.valueOf(this.b)+"}";
18+
}
19+
}
20+
21+
class Graph{
22+
23+
24+
Set<Integer> articulation_points;
25+
ArrayList<ArrayList<Integer>> arr;
26+
ArrayList<Pair> bridges;
27+
int V;
28+
29+
Graph(int V){
30+
this.V =V;
31+
this.arr = new ArrayList<>();
32+
articulation_points = new HashSet<>();
33+
bridges = new ArrayList<>();
34+
for(int i=0;i<V;i++) {
35+
arr.add(new ArrayList<>());
36+
}
37+
}
38+
39+
public void add_edge(int x, int y) {
40+
this.arr.get(x).add(y);
41+
this.arr.get(y).add(x);
42+
}
43+
44+
public void display() {
45+
46+
for(int i=0; i<V; i++) {
47+
System.out.print(String.valueOf(i));
48+
49+
for(int j=0;j<this.arr.get(i).size(); j++) {
50+
System.out.print(" -> "+this.arr.get(i).get(j));
51+
}
52+
System.out.println();
53+
}
54+
}
55+
56+
57+
public void points_bridges() {
58+
59+
int[] discoverTime = new int[this.V];
60+
int[] lowestTime = new int[this.V];
61+
62+
int time = 1;
63+
for(int node=0; node<this.V; node++) {
64+
if(discoverTime[node]==0) {
65+
//System.out.println("Time: "+time);
66+
dfs_helper(node, -1,discoverTime,lowestTime,time);
67+
}
68+
}
69+
70+
//display_arr(discoverTime);
71+
//display_arr(lowestTime);
72+
}
73+
74+
public void dfs_helper(int curr, int parent,int[] discoverTime, int[] lowestTime, int time) {
75+
76+
int noOfChild = 0;
77+
discoverTime[curr] = lowestTime[curr] = time;
78+
time+=1;
79+
80+
for(int child : this.arr.get(curr)) {
81+
82+
if(discoverTime[child]==0) {
83+
noOfChild+=1;
84+
85+
dfs_helper(child,curr, discoverTime, lowestTime, time);
86+
lowestTime[curr] = Math.min(lowestTime[curr], lowestTime[child]);
87+
88+
89+
if(parent!=-1 && lowestTime[child]>=discoverTime[curr]) {
90+
// AP exist
91+
//System.out.println("C: "+curr);
92+
this.articulation_points.add(curr);
93+
}
94+
95+
if(lowestTime[child]>discoverTime[curr]) {
96+
// Bridge exist
97+
this.bridges.add(new Pair(curr, child));
98+
}
99+
100+
101+
} else if(discoverTime[child]>0 && child != parent) {
102+
lowestTime[curr] = Math.min(lowestTime[curr], discoverTime[child]);
103+
}
104+
}
105+
106+
107+
// Separate case for src node
108+
if(parent==-1 && noOfChild>=2) {
109+
this.articulation_points.add(curr);
110+
}
111+
}
112+
113+
public void display_arr(int[] arr) {
114+
for(int i=0; i<arr.length; i++) {
115+
System.out.print(arr[i]+", ");
116+
}
117+
System.out.println();
118+
}
119+
}
120+
121+
public static void main(String[] args) {
122+
// TODO Auto-generated method stub
123+
124+
Graph g1 = new Articulation_Point_and_Bridges().new Graph(5);
125+
g1.add_edge(0, 1);
126+
g1.add_edge(1, 2);
127+
g1.add_edge(2, 0);
128+
g1.add_edge(2, 3);
129+
g1.add_edge(3, 4);
130+
131+
g1.points_bridges();
132+
System.out.println("Points: "+g1.articulation_points);
133+
System.out.println("Bridges: "+g1.bridges);
134+
135+
System.out.println("**************************************");
136+
137+
Graph g2 = new Articulation_Point_and_Bridges().new Graph(5);
138+
g2.add_edge(1, 0);
139+
g2.add_edge(0, 2);
140+
g2.add_edge(2, 1);
141+
g2.add_edge(0, 3);
142+
g2.add_edge(3, 4);
143+
144+
g2.points_bridges();
145+
System.out.println("Points: "+g2.articulation_points);
146+
System.out.println("Bridges: "+g2.bridges);
147+
148+
System.out.println("**************************************");
149+
150+
Graph g3 = new Articulation_Point_and_Bridges().new Graph(4);
151+
g3.add_edge(0, 1);
152+
g3.add_edge(1, 2);
153+
g3.add_edge(2, 3);
154+
155+
g3.points_bridges();
156+
System.out.println("Points: "+g3.articulation_points);
157+
System.out.println("Bridges: "+g3.bridges);
158+
159+
System.out.println("**************************************");
160+
161+
Graph g4 = new Articulation_Point_and_Bridges().new Graph(7);
162+
g4.add_edge(0, 1);
163+
g4.add_edge(1, 2);
164+
g4.add_edge(2, 0);
165+
g4.add_edge(1, 3);
166+
g4.add_edge(1, 4);
167+
g4.add_edge(1, 6);
168+
g4.add_edge(3, 5);
169+
g4.add_edge(4, 5);
170+
171+
g4.points_bridges();
172+
System.out.println("Points: "+g4.articulation_points);
173+
System.out.println("Bridges: "+g4.bridges);
174+
}
175+
176+
}

0 commit comments

Comments
 (0)