Skip to content

Commit f8adab2

Browse files
author
Tushar Roy
committed
Updating articulation point to correct logic
1 parent 370bf9f commit f8adab2

1 file changed

Lines changed: 98 additions & 53 deletions

File tree

‎src/com/interview/graph/ArticulationPoint.java‎

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,123 @@
66
import java.util.Set;
77

88
/**
9-
http://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/
9+
* Date 08/22/2015
10+
* @author Tushar Roy
11+
*
12+
* Find articulation points in connected undirected graph.
13+
* Articulation points are vertices such that removing any one of them disconnects the graph.
14+
*
15+
* We need to do DFS of this graph and keep visitedTime and lowTime for each vertex.
16+
* lowTime is keeps track of back edges.
17+
*
18+
* If any one of following condition meets then vertex is articulation point.
19+
*
20+
* 1) If vertex is root of DFS and has atlesat 2 independent children.(By independent it means they are
21+
* not connected to each other except via this vertex). This condition is needed because if we
22+
* started from corner vertex it will meet condition 2 but still is not an articulation point. To filter
23+
* out those vertices we need this condition.
24+
*
25+
* 2) It is not root of DFS and if visitedTime of vertex <= lowTime of any adjacent vertex then its articulation point.
26+
*
27+
* Time complexity is O(E + V)
28+
* Space complexity is O(V)
29+
*
30+
* References:
31+
* https://en.wikipedia.org/wiki/Biconnected_component
32+
* http://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/
1033
*/
1134
public class ArticulationPoint<T> {
1235

1336
private int time;
14-
private Vertex<T> rootVertex;
1537

16-
public Set<Vertex<T>> ap(Graph<T> graph) {
17-
18-
Map<Vertex<T>, Boolean> visited = new HashMap<Vertex<T>, Boolean>();
19-
Set<Vertex<T>> articulationPoints = new HashSet<Vertex<T>>();
38+
public Set<Vertex<T>> findarticulationPoints(Graph<T> graph) {
39+
time = 0;
40+
Set<Vertex<T>> visited = new HashSet<>();
41+
Set<Vertex<T>> articulationPoints = new HashSet<>();
2042
Vertex<T> startVertex = graph.getAllVertex().iterator().next();
21-
Map<Vertex<T>, Integer> discoveryTime = new HashMap<Vertex<T>, Integer>();
22-
Map<Vertex<T>, Integer> minTime = new HashMap<Vertex<T>, Integer>();
23-
24-
rootVertex = startVertex;
25-
DFS(visited,articulationPoints,startVertex,discoveryTime,minTime);
26-
43+
44+
Map<Vertex<T>, Integer> visitedTime = new HashMap<>();
45+
Map<Vertex<T>, Integer> lowTime = new HashMap<>();
46+
Map<Vertex<T>, Vertex<T>> parent = new HashMap<>();
47+
48+
DFS(visited,articulationPoints,startVertex, visitedTime, lowTime, parent);
2749
return articulationPoints;
2850
}
2951

30-
private void DFS(Map<Vertex<T>, Boolean> visited,
31-
Set<Vertex<T>> articulationPoints, Vertex<T> startVertex,
32-
Map<Vertex<T>, Integer> discoveryTime,
33-
Map<Vertex<T>, Integer> minTime) {
34-
visited.put(startVertex, true);
35-
discoveryTime.put(startVertex, time++);
36-
minTime.put(startVertex, time);
37-
int count =0;
38-
for(Vertex<T> adj : startVertex.getAdjacentVertexes()){
39-
if(!visited.containsKey(adj)){
40-
DFS(visited,articulationPoints,adj,discoveryTime,minTime);
41-
count++;
42-
if(startVertex.equals(rootVertex)){
43-
if(count >= 2){
44-
articulationPoints.add(startVertex);
45-
}
46-
}
47-
else{
48-
int lowTime = getTime(minTime,adj);
49-
if(getTime(discoveryTime,startVertex) <= lowTime){
50-
articulationPoints.add(startVertex);
51-
}else{
52-
minTime.put(startVertex,lowTime < getTime(minTime,startVertex) ? lowTime : getTime(minTime,startVertex));
53-
}
54-
}
55-
}else{
56-
if(getTime(minTime,startVertex) > getTime(discoveryTime,adj)){
57-
minTime.put(startVertex, getTime(discoveryTime,adj));
52+
private void DFS(Set<Vertex<T>> visited,
53+
Set<Vertex<T>> articulationPoints, Vertex<T> vertex,
54+
Map<Vertex<T>, Integer> visitedTime,
55+
Map<Vertex<T>, Integer> lowTime, Map<Vertex<T>, Vertex<T>> parent) {
56+
visited.add(vertex);
57+
visitedTime.put(vertex, time);
58+
lowTime.put(vertex, time);
59+
time++;
60+
int childCount =0;
61+
boolean isArticulationPoint = false;
62+
for(Vertex<T> adj : vertex.getAdjacentVertexes()){
63+
if(adj.equals(parent.get(vertex))) {
64+
continue;
65+
}
66+
if(!visited.contains(adj)) {
67+
parent.put(adj, vertex);
68+
childCount++;
69+
DFS(visited, articulationPoints, adj, visitedTime, lowTime, parent);
70+
71+
if(visitedTime.get(vertex) <= lowTime.get(adj)) {
72+
isArticulationPoint = true;
73+
} else {
74+
//lowTime[vertex] = min(lowTime[vertex], lowTime[adj]);
75+
lowTime.compute(vertex, (currentVertex, time) ->
76+
Math.min(time, lowTime.get(adj))
77+
);
5878
}
79+
80+
} else {
81+
//lowTime[vertex] = min(lowTime[vertex], visitedTime[adj]);
82+
lowTime.compute(vertex, (currentVertex, time) ->
83+
Math.min(time, visitedTime.get(adj))
84+
);
5985
}
6086
}
87+
88+
//checks if either condition 1 or condition 2 meets). If yes then it is articulation point.
89+
if((parent.get(vertex) == null && childCount >= 2) || parent.get(vertex) != null && isArticulationPoint ) {
90+
articulationPoints.add(vertex);
91+
}
6192

6293
}
63-
64-
private int getTime(Map<Vertex<T>,Integer> time,Vertex<T> vertex){
65-
return time.containsKey(vertex) ? time.get(vertex) : 1000;
66-
}
6794

6895
public static void main(String args[]){
69-
Graph<Integer> graph = new Graph<Integer>(false);
70-
graph.addEdge(1,2);
96+
Graph<Integer> graph = new Graph<>(false);
97+
graph.addEdge(1, 2);
7198
graph.addEdge(2, 3);
7299
graph.addEdge(1, 3);
73-
graph.addEdge(2, 4);
74-
graph.addEdge(4,5);
75-
graph.addEdge(5,6);
76-
graph.addEdge(6,4);
77-
graph.addEdge(6,2);
100+
graph.addEdge(1, 4);
101+
graph.addEdge(4, 5);
102+
graph.addEdge(5, 6);
103+
graph.addEdge(6, 7);
104+
graph.addEdge(7, 5);
105+
graph.addEdge(6, 8);
106+
107+
//bigger example
108+
/*
109+
graph.addEdge(0, 1);
110+
graph.addEdge(0, 2);
111+
graph.addEdge(0, 3);
112+
graph.addEdge(0, 4);
113+
graph.addEdge(4, 2);
114+
graph.addEdge(3, 5);
115+
graph.addEdge(4, 6);
116+
graph.addEdge(6, 3);
117+
graph.addEdge(6, 7);
118+
graph.addEdge(6, 8);
119+
graph.addEdge(7, 9);
120+
graph.addEdge(9, 10);
121+
graph.addEdge(8, 10);*/
122+
78123
ArticulationPoint<Integer> ap = new ArticulationPoint<Integer>();
79-
Set<Vertex<Integer>> aPoints = ap.ap(graph);
80-
System.out.println(aPoints);
124+
Set<Vertex<Integer>> aPoints = ap.findarticulationPoints(graph);
125+
aPoints.forEach(point -> System.out.println(point));
81126
}
82127

83128
}

0 commit comments

Comments
 (0)