-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathArticulationPoints1.java
62 lines (54 loc) · 2.22 KB
/
ArticulationPoints1.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
/*https://practice.geeksforgeeks.org/problems/articulation-point-1/0/*/
class Solution
{
int timer;
boolean[] visited, isArtPoint;
int[] discovery, parent, low;
//Function to return Breadth First Traversal of given graph.
public ArrayList<Integer> articulationPoints(int V,ArrayList<ArrayList<Integer>> adj)
{
// Code here
timer = 0;
visited = new boolean[V];
isArtPoint = new boolean[V];
discovery = new int[V];
parent = new int[V];
low = new int[V];
int v;
ArrayList<Integer> articulationPoints = new ArrayList<Integer>();
for (v = 0; v < V; ++v)
parent[v] = -1;
for (v = 0; v < V; ++v)
if (!visited[v])
dfs(adj,v);
for (v = 0; v < V; ++v)
if (isArtPoint[v])
articulationPoints.add(v);
if (articulationPoints.size() == 0) articulationPoints.add(-1);
return articulationPoints;
}
public void dfs(ArrayList<ArrayList<Integer>> graph, int source)
{
int children = 0;
visited[source] = true;
discovery[source] = low[source] = ++timer;
for (Integer adjNode : graph.get(source))
{
if (!visited[adjNode]) //tree edge
{
++children;
parent[adjNode] = source;
dfs(graph, adjNode);
low[source] = Math.min(low[source],low[adjNode]); //update the low value from the immediate child of dfs tree
if (parent[source] != -1 && low[adjNode] >= discovery[source]) //articulation point rule
isArtPoint[source] = true;
}
else if (adjNode != parent[source]) //back edge //this condition ensures that we are not updating the low value for its immediate parent from which the dfs call started
{
low[source] = Math.min(low[source],discovery[adjNode]); //update the low value of the previously visited adjacent node i.e. the destination node of a back edge
}
}
if (parent[source] == -1 && children > 1) //articulation point rule
isArtPoint[source] = true;
}
}