-
Notifications
You must be signed in to change notification settings - Fork 0
/
TarjanAlgorithmSCC.java
82 lines (70 loc) · 2.13 KB
/
TarjanAlgorithmSCC.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.ttahb.graph;
import java.util.*;
/**
* Find strongly connected components using the Tarjans algorithm in a directed graph.
* @author - Vijay Bhatt, bhattvijay69@hotmail.com
*/
public class TarjanAlgorithmSCC {
private int n;
private List<List<Integer>> adjList;
private boolean[] visited;
private int[] ids,lowLinks;
private int id = 0;
/** Using java stack */
private Deque<Integer> stack;
private Map<Integer,List<Integer>> sccMap;
private int sccCount = 0;
public TarjanAlgorithmSCC(int n){
if(n==0)
throw new IllegalArgumentException();
this.adjList = getEmptyGraph(n);
this.n = n;
}
public Map<Integer,List<Integer>> findSCC(){
visited = new boolean[n];
ids = new int[n];
lowLinks = new int[n];
stack = new ArrayDeque<>();
sccMap = new HashMap<>();
for(int i=0; i < n; i++){
if(!visited[i])
dfs(i);
}
return this.sccMap;
}
private void dfs(int at) {
visited[at] = true;
stack.addFirst(at);
ids[at] = lowLinks[at] = ++id;
for(int to:adjList.get(at)){
if(!visited[to]){
dfs(to);
lowLinks[at] = Math.min(lowLinks[to], lowLinks[at]);
} else {
if(stack.contains(to)){
lowLinks[at] = Math.min(lowLinks[to],lowLinks[at]);
}
}
}
// Found Strongly connected component.
if(lowLinks[at] == ids[at]){
List<Integer> scc = new ArrayList<>();
int current;
do {
current = stack.pop();
scc.add(current);
} while(current != at);
sccMap.put(sccCount++,scc);
}
}
/** Adds a directed edge u to v */
public void addDirectedEdge(int u, int v){
this.adjList.get(u).add(v);
}
private List<List<Integer>> getEmptyGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for(int i=0;i<n;i++)
graph.add(new ArrayList<>());
return graph;
}
}