-
Notifications
You must be signed in to change notification settings - Fork 9
/
DominatorTree
52 lines (52 loc) · 1.68 KB
/
DominatorTree
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
class DominatorTree(Graph):
def __init__(self, root):
Graph.__init__(self)
self.v = []
self.e = []
vertex = {}
dom = {}
bucket = {}
for i, v in enumerate(root.dfs()): #A
vertex[i] = v
bucket[v] = []
self.v.append(v)
v._semi = i
v._ancestor = 0
v._label = v
for i in range(len(vertex)-1, 0, -1):
w = vertex[i]
for v in w.pres(): # Optimize by building in #A
u = self._EVAL(v)
if u._semi < w._semi:
w._semi = u._semi
bucket[vertex[w._semi]].append(w)
self._LINK(w._dfs_parent, w)
for v in list(bucket[w._dfs_parent]):
bucket[w._dfs_parent].remove(v)
u = self._EVAL(v)
if u._semi < v._semi:
dom[v] = u
else:
dom[v] = w._dfs_parent
for i in range(1,len(vertex)-1):
w = vertex[i]
if dom[w] != vertex[w._semi]:
dom[w] = dom[dom[w]]
for v in dom:
self.add_edge(dom[v],v)
def __str__(self):
return "VERTICES: %s\nEDGES: %s" % (self.v, self.e)
def _EVAL(self, v):
if v._ancestor == 0:
return v
else:
self._COMPRESS(v)
return v._label
def _COMPRESS(self, v):
if v._ancestor._ancestor != 0:
self._COMPRESS(v._ancestor)
if v._ancestor._label._semi < v._label._semi:
v._label = v._ancestor._label
v._ancestor = v._ancestor._ancestor
def _LINK(self, v, w):
w._ancestor = v