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
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
/**
* Graph algorithms
*/
package chalice;
import scala.collection.mutable;
import scala.collection.immutable;
// Directed simple graph on T. Payload of nodes (of type T) should remain immutable while in graph.
class DiGraph[T] {
private class Node[T](t: T) {
val data: T = t;
var children = mutable.Set.empty[Node[T]];
// temporary variables
var index = -1;
var lowlink = -1;
var onstack = false;
}
private var rep = mutable.Map.empty[T, Node[T]];
def addNode(t: T) = {
if (hasNode(t))
false;
else {
rep(t) = new Node(t);
true;
}
}
def addEdge(a: T, b: T) = {
assert (hasNode(a));
assert (hasNode(b));
if (rep(a).children.contains(rep(b)))
false
else {
rep(a).children += rep(b);
true;
}
}
def hasNode(a: T) = rep contains a;
def nodes: immutable.Set[T] =
immutable.Set() ++ rep.keys
def children(t: T): immutable.Set[T] = {
assert (rep contains t);
immutable.Set() ++ (rep(t).children map {x => x.data})
}
// Compute condensation of the digraph.
// The resulting digraph has no self loops
def computeSCC(): (DiGraph[List[T]],mutable.Map[T,List[T]]) = {
// Tarjan's algorithm for finding strongly connected components
// http://algowiki.net/wiki/index.php/Tarjan%27s_algorithm
rep.values foreach {x => assert(x.index == -1 && x.lowlink == -1 && !x.onstack)};
// morphism
val result = new DiGraph[List[T]];
val map = mutable.Map.empty[T,List[T]];
// algorithm
var index = 0;
var S:List[Node[T]] = Nil;
def tarjan(v: Node[T]) {
v.index = index;
v.lowlink = index;
index = index + 1;
S = v :: S; v.onstack = true;
for (w <- v.children) {
if (w.index == -1) {
tarjan(w);
v.lowlink = Math.min(v.lowlink, w.lowlink);
} else if (w.onstack)
v.lowlink = Math.min(v.lowlink, w.index);
}
if (v.lowlink == v.index) {
var scc:List[T] = Nil;
var w: Node[T] = null;
while (v != w) {
w = S.head;
S = S.tail; w.onstack = false;
scc = w.data :: scc;
}
result.addNode(scc);
scc foreach {t => map(t) = scc}
}
}
// compute SCCs
rep.values foreach {n => if(n.index == -1) tarjan(n)};
// compute SCCs edges
rep.values foreach {n => n.children foreach {m =>
if (map(n.data) != map(m.data))
result.addEdge(map(n.data), map(m.data))
}
}
// clean-up
rep.values foreach {x => x.index = -1; x.lowlink = -1; x.onstack = false};
(result,map);
}
}
object Test {
def main(args: Array[String]) {
val g = new DiGraph[Int];
assert(g.addNode(0));
assert(!g.addNode(0));
assert(g.addNode(1));
assert(g.addNode(2));
assert(g.addNode(3));
assert(g.addNode(4));
assert(g.addNode(5));
g.addEdge(0,1);
g.addEdge(0,1);
g.addEdge(0,2);
assert(g.children(0) == Set(1,2));
g.addEdge(1,1);
g.addEdge(2,3);
g.addEdge(3,4);
g.addEdge(3,5);
g.addEdge(5,4);
g.addEdge(4,2);
g.computeSCC;
val (d, h) = g.computeSCC;
assert(h.get(0).get == List(0));
assert(h.get(1).get == List(1));
assert(h.get(2).get.size == 4);
assert(h.get(3).get.size == 4);
assert(h.get(4).get.size == 4);
assert(h.get(5).get.size == 4);
assert(d.children(h.get(0).get) == Set(h.get(1).get, h.get(2).get));
assert(d.children(h.get(1).get) == Set());
assert(d.children(h.get(2).get) == Set());
}
}
|