-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathTarjan's Algorithm
93 lines (79 loc) · 1.73 KB
/
Tarjan's Algorithm
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
//Tarjan's Algorithm
import java.util.*;
public class TarjanSCC
{
static HashMap<Integer,List<Integer>> adj=new HashMap<>();
static int Disc[]=new int[8];
static int Low[]=new int[8];
static boolean inStack[]=new boolean[8];
static Stack<Integer> stack=new Stack<>();
static int time = 0;
static void DFS(int u)
{
Disc[u] = time;
Low[u] = time;
time++;
stack.push(u);
inStack[u] = true;
List<Integer> temp=adj.get(u); // get the list of edges from the node.
if(temp==null)
return;
for(int v: temp)
{
if(Disc[v]==-1) //If v is not visited
{
DFS(v);
Low[u] = Math.min(Low[u],Low[v]);
}
//Differentiate back-edge and cross-edge
else if(inStack[v]) //Back-edge case
Low[u] = Math.min(Low[u],Disc[v]);
}
if(Low[u]==Disc[u]) //If u is head node of SCC
{
System.out.print("SCC is: ");
while(stack.peek()!=u)
{
System.out.print(stack.peek()+" ");
inStack[stack.peek()] = false;
stack.pop();
}
System.out.println(stack.peek());
inStack[stack.peek()] = false;
stack.pop();
}
}
static void findSCCs_Tarjan(int n)
{
for(int i=1;i<=n;i++)
{
Disc[i]=-1;
Low[i]=-1;
inStack[i]=false;
}
for(int i=1;i<=n;++i)
{
if(Disc[i]==-1)
DFS(i); // call DFS for each undiscovered node.
}
}
public static void main(String args[])
{
adj.put(1,new ArrayList<Integer>());
adj.get(1).add(3);
adj.put(2,new ArrayList<Integer>());
adj.get(2).add(1);
adj.put(3,new ArrayList<Integer>());
adj.get(3).add(2);
adj.get(3).add(4);
adj.put(4,new ArrayList<Integer>());
adj.get(4).add(5);
adj.put(5,new ArrayList<Integer>());
adj.get(5).add(6);
adj.put(6,new ArrayList<Integer>());
adj.get(6).add(7);
adj.put(7,new ArrayList<Integer>());
adj.get(7).add(4);
findSCCs_Tarjan(7);
}
}