- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArticulationPointInGraph.java
More file actions
Latest commit
98 lines (86 loc) · 4.49 KB
/
Copy pathArticulationPointInGraph.java
File metadata and controls
98 lines (86 loc) · 4.49 KB
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
packageGraph;
importjava.util.ArrayList;
importjava.util.Scanner;
publicclassArticulationPointInGraph {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.println("Enter the number of test cases:");
intnumberOfTestcases = sc.nextInt();
while (numberOfTestcases-- > 0){
ArrayList<ArrayList<Integer>> adjacencyList = newArrayList<>();
// Taking input for number of nodes
System.out.println("Enter the number of nodes:");
intnumberOfNodes = sc.nextInt();
// initializing adjacencyList with new ArrayList<>()
for(inti=1 ; i<=numberOfNodes+1 ; i++){
adjacencyList.add(newArrayList<>());
}
// taking input of adjacent nodes of particular node
for(inti=1 ; i<=numberOfNodes ; i++){
System.out.println("Enter number of adjacent nodes to the node : " + i);
intcountOfAdjacentNodes = sc.nextInt();
// taking input of the particular adjacent nodes to the node i
System.out.println("Enter adjacent nodes:");
for(intj=0 ; j<countOfAdjacentNodes ; j++){
// taking input of adjacent node
intadjacentNode = sc.nextInt();
// adding the adjacent node to the particular node i
adjacencyList.get(i).add(adjacentNode);
}
}
// calling the method
printArticulationPoint(adjacencyList, numberOfNodes);
}
}
publicstaticvoidprintArticulationPoint(ArrayList<ArrayList<Integer>> adjacencyList, intnumberOfNodes){
int[] visited = newint[numberOfNodes+1]; // visiting array for tall the nodes
int[] timeOfInsertion = newint[numberOfNodes+1]; // time of insertion for all the nodes
int[] minTimeOfInsertion = newint[numberOfNodes+1]; // minimum time of insertion for all the nodes
int[] isArticulation = newint[numberOfNodes+1]; // array for marking the node as articulation point
inttimer = 0; // timer variable for time of insertion
// calling the DFS function for all the nodes
for(intnode=1 ; node<=numberOfNodes ; node++){
// if the node is not visited
if(visited[node] == 0){
dfs(node, -1, adjacencyList, timeOfInsertion, minTimeOfInsertion, isArticulation, visited, timer);
}
}
// printing the articulation point
System.out.println("Articulation points in the given graph is :");
for(inti=1 ; i<=numberOfNodes ; i++){
if(isArticulation[i] == 1){
System.out.print(i + " ");
}
}
System.out.println();
}
publicstaticvoiddfs(intsource, intparent, ArrayList<ArrayList<Integer>> adjacencyList, int[] timeOfInsertion, int[] minTimeOfInsertion, int[] isArticulation, int[] visited, inttimer){
visited[source] = 1;
timeOfInsertion[source] = minTimeOfInsertion[source] = timer++;
intchild = 0;
for(IntegeradjacentNode : adjacencyList.get(source)){
// if the adjacent node is itself the parent node
if(adjacentNode == parent){
continue;
}
// if the adjacent node is not visited the call the DFS
if(visited[adjacentNode] == 0){
// call the DFS for the adjacent node
dfs(adjacentNode, source, adjacencyList, timeOfInsertion, minTimeOfInsertion, isArticulation, visited, timer);
// update the minimum time of insertion array for all the adjacent node
minTimeOfInsertion[source] = Math.min(minTimeOfInsertion[source], minTimeOfInsertion[adjacentNode]);
// condition for articulation point
if(minTimeOfInsertion[adjacentNode] >= timeOfInsertion[source] && parent != -1){
isArticulation[source] = 1;
}
child++; // count the individual child of the parent node
}else{
minTimeOfInsertion[source] = Math.min(minTimeOfInsertion[source], timeOfInsertion[adjacentNode]);
}
}
// if the node has multiple children the make it as articulation point
if(parent != -1 && child > 1){
isArticulation[source] = 1;
}
}
}