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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::cmp::min;
use std::collections::{HashMap, HashSet};
use crate::dfs::{Dfs, Status, Visit};
use crate::{Graph, Partition};
struct NodeState {
on_stack: bool,
index: usize,
lowlink: usize,
}
impl NodeState {
fn new(index: usize) -> NodeState {
NodeState {
on_stack: true,
index: index,
lowlink: index,
}
}
}
pub(crate) struct Tarjan<'a, G: Graph + ?Sized> {
g: &'a G,
dfs: Dfs<'a, G>,
stack: Vec<G::Node>,
node_states: HashMap<G::Node, NodeState>,
next_index: usize,
}
impl<'a, G: Graph + ?Sized> Tarjan<'a, G> {
pub fn from_graph(g: &'a G) -> Self {
Tarjan {
g,
dfs: g.dfs(),
stack: Vec::new(),
node_states: HashMap::new(),
next_index: 0,
}
}
pub fn run(mut self) -> Partition<G> {
let mut ret = Vec::new();
for visit in self.dfs {
match visit {
Visit::Retreat { u, parent } => {
let lowlink = self.node_states[&u].lowlink;
let index = self.node_states[&u].index;
if let Some(p) = parent {
self.node_states
.entry(p)
.and_modify(|s| s.lowlink = min(s.lowlink, lowlink));
}
if lowlink == index {
let mut scc = HashSet::new();
loop {
let v = self.stack.pop().unwrap();
self.node_states.entry(v).and_modify(|s| s.on_stack = false);
scc.insert(v.clone());
if v == u {
break;
}
}
ret.push(scc);
}
}
Visit::Root(u) => {
self.stack.push(u.clone());
self.node_states.insert(u, NodeState::new(self.next_index));
self.next_index += 1;
}
Visit::Edge { src, dst, status } => {
if status == Status::New {
self.stack.push(dst.clone());
self.node_states
.insert(dst, NodeState::new(self.next_index));
self.next_index += 1;
} else if self.node_states[&dst].on_stack {
let index = self.node_states[&dst].index;
self.node_states
.entry(src)
.and_modify(|s| s.lowlink = min(s.lowlink, index));
}
}
}
}
ret.reverse();
Partition::new(self.g, ret)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{arb_dag, arb_graph, graph};
use crate::Graph;
macro_rules! tarjan_test {
($name:ident, $graph:expr, $expected:expr) => {
#[test]
fn $name() {
let g = graph($graph);
let d = g.tarjan();
let expected: Vec<_> = $expected
.into_iter()
.map(|scc| scc.into_iter().cloned().collect::<HashSet<u32>>())
.collect();
assert_eq!(d.sets, expected);
}
};
}
tarjan_test!(triangle, "0-1, 1-2, 2-0", [[0, 1, 2]]);
tarjan_test!(
two_triangles,
"0-1, 1-2, 2-0, 2-3, 3-4, 4-5, 5-3",
[[0, 1, 2], [3, 4, 5]]
);
tarjan_test!(diamond, "0-1, 0-2, 1-3, 2-3", [[0], [2], [1], [3]]);
proptest! {
#[test]
fn tarjan_dag_proptest(ref g in arb_dag()) {
let sccs = g.tarjan();
for s in sccs.parts() {
assert_eq!(s.len(), 1);
}
}
#[test]
fn tarjan_graph_proptest(ref g in arb_graph()) {
let sccs = g.tarjan();
for s in sccs.parts() {
for t in sccs.parts() {
if s == t && s.len() > 1 {
for u in s {
for v in s {
assert!(g.has_path(u, v));
}
}
} else if s != t {
for u in s {
for v in t {
assert!(!(g.has_path(u, v) && g.has_path(v, u)));
}
}
}
}
}
}
#[test]
fn tarjan_scc_dag(ref g in arb_graph()) {
let sccs = g.tarjan();
assert!(sccs.top_sort().is_some());
}
}
}