-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarjansAlgorithm.java
More file actions
44 lines (37 loc) · 1.42 KB
/
TarjansAlgorithm.java
File metadata and controls
44 lines (37 loc) · 1.42 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
package graph;
import java.util.ArrayList;
/**
* <b>Bridge</b> is an edge whose deletion increases the graph's number of connected components.
* Low is the lowest discovery time of all of my neighbours.
* DT is the discovery time from src to that vertex.
*/
public class TarjansAlgorithm {
// Time Complexity = O(V + E)
public static void getBridge(ArrayList<ArrayList<Integer>> graph, int V) {
int[] dt = new int[V];
int[] low = new int[V];
int time = 0;
boolean[] isVisited = new boolean[V];
for (int i = 0; i < V; i++) {
if (!isVisited[i]) dfs(graph, i, isVisited, dt, low, time, -1);
}
}
private static void dfs(ArrayList<ArrayList<Integer>> graph, int curr, boolean[] isVisited, int[] dt, int[] low, int time, int parent) {
isVisited[curr] = true;
dt[curr] = low[curr] = ++time;
for (int dest : graph.get(curr)) {
if (!isVisited[dest]) {
dfs(graph, dest, isVisited, dt, low, time, curr);
low[curr] = Math.min(low[curr], low[dest]);
if (dt[curr] < low[dest]) {
System.out.println("Bridge is " + curr + " -> " + dest);
}
} else if (dest == parent) {
continue;
} else {
// If the neighbour is visited!
low[curr] = Math.min(low[curr], dt[dest]);
}
}
}
}