forked from HyosangKang/mwpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
131 lines (122 loc) · 2.64 KB
/
tree.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
package mwpm
type Tree struct {
g *WeightedGraph
roots map[*Node]struct{}
nodes []*Node
tight map[*Node]*Node
}
func NewTree(wg *WeightedGraph) *Tree {
t := &Tree{
g: wg,
roots: make(map[*Node]struct{}),
nodes: make([]*Node, wg.N()),
tight: make(map[*Node]*Node),
}
for i := int64(0); i < int64(wg.N()); i++ {
nid := i
n := &Node{label: 1}
n.temp = nid
t.nodes[nid] = n
}
return t
}
func (t *Tree) Blossoms() []*Node {
var nodes []*Node
unique := make(map[*Node]struct{})
for n := range t.roots {
for _, m := range n.Descendents() {
b := m.Blossom()
if _, ok := unique[b]; !ok {
nodes = append(nodes, b)
unique[b] = struct{}{}
}
}
}
return nodes
}
// returns the pair of nodes that connects the cycle.
// For example, it returns [[2 1] [1 0] [0 4] [4 3] [3 2]] if the tree looks like:
// p o [2]
// / \
// [1] o o [3]
// | |
// [0] o o [4]
// n -- m
func (t *Tree) MakeCycle(n, m *Node) [][2]*Node {
ansn := n.Anscesters()
ansm := m.Anscesters()
var i, j int
for i = 0; i < len(ansn); i++ {
for j = 0; j < len(ansm); j++ {
if ansn[i] == ansm[j] {
goto FOUND
}
}
}
FOUND:
var cycle [][2]*Node
for k := i; k > 0; k-- {
u := ansn[k].PopChild(ansn[k-1])
ub := u.Blossom()
cycle = append(cycle, [2]*Node{ub.parent, u})
ub.parent = nil
}
cycle = append(cycle, [2]*Node{n, m})
for k := 0; k < j; k++ {
u := ansm[k+1].PopChild(ansm[k])
ub := u.Blossom()
cycle = append(cycle, [2]*Node{u, ub.parent})
ub.parent = nil
}
return cycle
}
// set the node n as tight within the blossom b
func (t *Tree) SetTight(s [2]*Node, b *Node) {
// fmt.Printf("setting tight edge %d:%d\n", s[0].temp, s[1].temp)
t.tight[s[0]], t.tight[s[1]] = s[1], s[0]
for _, l := range s {
for l.pp != b {
lb := l.pp
for i, c := range lb.cycle {
if c[0].BlossomWithin(lb) == l.BlossomWithin(lb) {
lb.cycle = append(lb.cycle[i:], lb.cycle[:i]...)
break
}
}
for i := 1; i < len(lb.cycle); i += 2 {
t.SetTight(lb.cycle[i], lb)
}
l = l.pp
}
}
}
func (t *Tree) TightFrom(n *Node) *Node {
for _, u := range n.All() {
if v, ok := t.tight[u]; ok {
if v.Blossom() != n {
return v
}
}
}
panic("No tight match found")
return nil
}
// remove the tight edge within the blossom b
func (t *Tree) RemoveTight(s [2]*Node) {
for _, u := range s {
if _, ok := t.tight[u]; ok {
delete(t.tight, u)
t.RemoveTightWithin(u.Blossom())
}
}
}
func (t *Tree) RemoveTightWithin(b *Node) {
for _, c := range b.cycle {
for _, u := range c {
if u.BlossomWithin(b).pp == b {
delete(t.tight, u)
}
}
t.RemoveTightWithin(c[0].BlossomWithin(b))
}
}