forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_flow_lt.go
211 lines (178 loc) · 5.44 KB
/
control_flow_lt.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flow
import "gonum.org/v1/gonum/graph"
// Dominators returns a dominator tree for all nodes in the flow graph
// g starting from the given root node.
func Dominators(root graph.Node, g graph.Directed) DominatorTree {
// The algorithm used here is essentially the Lengauer and Tarjan
// algorithm described in https://doi.org/10.1145%2F357062.357071
lt := lengauerTarjan{
indexOf: make(map[int64]int),
}
// step 1.
lt.dfs(g, root)
for i := len(lt.nodes) - 1; i > 0; i-- {
w := lt.nodes[i]
// step 2.
for _, v := range w.pred {
u := lt.eval(v)
if u.semi < w.semi {
w.semi = u.semi
}
}
lt.nodes[w.semi].bucket[w] = struct{}{}
lt.link(w.parent, w)
// step 3.
for v := range w.parent.bucket {
delete(w.parent.bucket, v)
u := lt.eval(v)
if u.semi < v.semi {
v.dom = u
} else {
v.dom = w.parent
}
}
}
// step 4.
for _, w := range lt.nodes[1:] {
if w.dom.node.ID() != lt.nodes[w.semi].node.ID() {
w.dom = w.dom.dom
}
}
// Construct the public-facing dominator tree structure.
dominatorOf := make(map[int64]graph.Node)
dominatedBy := make(map[int64][]graph.Node)
for _, w := range lt.nodes[1:] {
dominatorOf[w.node.ID()] = w.dom.node
did := w.dom.node.ID()
dominatedBy[did] = append(dominatedBy[did], w.node)
}
return DominatorTree{root: root, dominatorOf: dominatorOf, dominatedBy: dominatedBy}
}
// lengauerTarjan holds global state of the Lengauer-Tarjan algorithm.
// This is a mapping between nodes and the postordering of the nodes.
type lengauerTarjan struct {
// nodes is the nodes traversed during the
// Lengauer-Tarjan depth-first-search.
nodes []*ltNode
// indexOf contains a mapping between
// the id-dense representation of the
// graph and the potentially id-sparse
// nodes held in nodes.
//
// This corresponds to the vertex
// number of the node in the Lengauer-
// Tarjan algorithm.
indexOf map[int64]int
}
// ltNode is a graph node with accounting for the Lengauer-Tarjan
// algorithm.
//
// For the purposes of documentation the ltNode is given the name w.
type ltNode struct {
node graph.Node
// parent is vertex which is the parent of w
// in the spanning tree generated by the search.
parent *ltNode
// pred is the set of vertices v such that (v, w)
// is an edge of the graph.
pred []*ltNode
// semi is a number defined as follows:
// (i) After w is numbered but before its semidominator
// is computed, semi is the number of w.
// (ii) After the semidominator of w is computed, semi
// is the number of the semidominator of w.
semi int
// bucket is the set of vertices whose
// semidominator is w.
bucket map[*ltNode]struct{}
// dom is vertex defined as follows:
// (i) After step 3, if the semidominator of w is its
// immediate dominator, then dom is the immediate
// dominator of w. Otherwise dom is a vertex v
// whose number is smaller than w and whose immediate
// dominator is also w's immediate dominator.
// (ii) After step 4, dom is the immediate dominator of w.
dom *ltNode
// In general ancestor is nil only if w is a tree root
// in the forest; otherwise ancestor is an ancestor
// of w in the forest.
ancestor *ltNode
// Initially label is w. It is adjusted during
// the algorithm to maintain invariant (3) in the
// Lengauer and Tarjan paper.
label *ltNode
}
// dfs is the Lengauer-Tarjan DFS procedure.
func (lt *lengauerTarjan) dfs(g graph.Directed, v graph.Node) {
i := len(lt.nodes)
lt.indexOf[v.ID()] = i
ltv := <Node{
node: v,
semi: i,
bucket: make(map[*ltNode]struct{}),
}
ltv.label = ltv
lt.nodes = append(lt.nodes, ltv)
to := g.From(v.ID())
for to.Next() {
w := to.Node()
wid := w.ID()
idx, ok := lt.indexOf[wid]
if !ok {
lt.dfs(g, w)
// We place this below the recursive call
// in contrast to the original algorithm
// since w needs to be initialised, and
// this happens in the child call to dfs.
idx, ok = lt.indexOf[wid]
if !ok {
panic("path: unintialized node")
}
lt.nodes[idx].parent = ltv
}
ltw := lt.nodes[idx]
ltw.pred = append(ltw.pred, ltv)
}
}
// compress is the Lengauer-Tarjan COMPRESS procedure.
func (lt *lengauerTarjan) compress(v *ltNode) {
if v.ancestor.ancestor != nil {
lt.compress(v.ancestor)
if v.ancestor.label.semi < v.label.semi {
v.label = v.ancestor.label
}
v.ancestor = v.ancestor.ancestor
}
}
// eval is the Lengauer-Tarjan EVAL function.
func (lt *lengauerTarjan) eval(v *ltNode) *ltNode {
if v.ancestor == nil {
return v
}
lt.compress(v)
return v.label
}
// link is the Lengauer-Tarjan LINK procedure.
func (*lengauerTarjan) link(v, w *ltNode) {
w.ancestor = v
}
// DominatorTree is a flow graph dominator tree.
type DominatorTree struct {
root graph.Node
dominatorOf map[int64]graph.Node
dominatedBy map[int64][]graph.Node
}
// Root returns the root of the tree.
func (d DominatorTree) Root() graph.Node { return d.root }
// DominatorOf returns the immediate dominator of the node with the given ID.
func (d DominatorTree) DominatorOf(id int64) graph.Node {
return d.dominatorOf[id]
}
// DominatedBy returns a slice of all nodes immediately dominated by the node
// with the given ID. Elements of the slice are retained by the DominatorTree.
func (d DominatorTree) DominatedBy(id int64) []graph.Node {
return d.dominatedBy[id]
}