- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBridgesInGraph.java
More file actions
Latest commit
90 lines (77 loc) · 3.65 KB
/
Copy pathBridgesInGraph.java
File metadata and controls
90 lines (77 loc) · 3.65 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
packageGraph;
importjava.util.ArrayList;
importjava.util.Scanner;
/**
* Given an undirected graph, and we have to print the
* edges which are bridges means after removing that
* edge the graph will be divided into more connected
* components.
* Input :
* Adjacency List :
* 1 - {2, 4}
* 2 - {1, 3}
* 3 - {1, 2}
* 4 - {1, 5}
* 5 - {4}
* Output :
* Edge is : 1->4
* Edge is : 4->5
*/
publicclassBridgesInGraph {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
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();
for (intj = 0; j < countOfAdjacentNodes; j++) {
System.out.println("Enter the adjacent node : ");
intadjacentNode = sc.nextInt();
// adding the adjacent node in to the adjacency list
adjacencyList.get(i).add(adjacentNode);
}
}
boolean[] visited = newboolean[numberOfNodes+1]; // visited array for storing if the node is visited or not
int[] timeOfInsertion = newint[numberOfNodes+1]; // for storing time of insertion of the node
int[] minimumInsertionTime = newint[numberOfNodes+1]; // for storing minimum time of insertion of the node
inttimer = 0; // variable for time of insertion
// Running dfs for all the nodes
for(intnode=1 ; node<=numberOfNodes ; node++){
// if the node is not visited then visit it
if(!visited[node]){
dfs(node, -1, timeOfInsertion, minimumInsertionTime, adjacencyList, visited, timer);
}
}
}
publicstaticvoiddfs(intnode, intparentNode, int[] timeOfInsertion, int[] minimumInsertionTime, ArrayList<ArrayList<Integer>> adjacencyList, boolean[] visited, inttimer){
visited[node] = true; // put true e.g. node is visited
timeOfInsertion[node] = minimumInsertionTime[node] = timer++; // store the time
// visit the adjacent nodes of the current node
for(intadjacentNode : adjacencyList.get(node)){
// if the adjacent node == parent node then no need to visit it
if(adjacentNode == parentNode){
continue;
}
// if the adjacent node is not visited then run the dfs
if(!visited[adjacentNode]){
dfs(adjacentNode, node, timeOfInsertion, minimumInsertionTime, adjacencyList, visited, timer);
// store the minimum time of insertion
minimumInsertionTime[node] = Math.min(minimumInsertionTime[node], minimumInsertionTime[adjacentNode]);
// if the minimum of insertion > time of insertion then it's an edge
if(minimumInsertionTime[adjacentNode] > timeOfInsertion[node]){
System.out.println("Edge is : " + adjacentNode + " -> " + node);
}
}else{
minimumInsertionTime[node] = Math.min(minimumInsertionTime[node], timeOfInsertion[adjacentNode]);
}
}
}
}