This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bipartitegraphmatching.go
161 lines (131 loc) · 3.46 KB
/
bipartitegraphmatching.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
package bipartitegraph
import . "github.com/onsi/gomega/matchers/support/goraph/node"
import . "github.com/onsi/gomega/matchers/support/goraph/edge"
import "github.com/onsi/gomega/matchers/support/goraph/util"
func (bg *BipartiteGraph) LargestMatching() (matching EdgeSet) {
paths := bg.maximalDisjointSLAPCollection(matching)
for len(paths) > 0 {
for _, path := range paths {
matching = matching.SymmetricDifference(path)
}
paths = bg.maximalDisjointSLAPCollection(matching)
}
return
}
func (bg *BipartiteGraph) maximalDisjointSLAPCollection(matching EdgeSet) (result []EdgeSet) {
guideLayers := bg.createSLAPGuideLayers(matching)
if len(guideLayers) == 0 {
return
}
used := make(map[Node]bool)
for _, u := range guideLayers[len(guideLayers)-1] {
slap, found := bg.findDisjointSLAP(u, matching, guideLayers, used)
if found {
for _, edge := range slap {
used[edge.Node1] = true
used[edge.Node2] = true
}
result = append(result, slap)
}
}
return
}
func (bg *BipartiteGraph) findDisjointSLAP(
start Node,
matching EdgeSet,
guideLayers []NodeOrderedSet,
used map[Node]bool,
) ([]Edge, bool) {
return bg.findDisjointSLAPHelper(start, EdgeSet{}, len(guideLayers)-1, matching, guideLayers, used)
}
func (bg *BipartiteGraph) findDisjointSLAPHelper(
currentNode Node,
currentSLAP EdgeSet,
currentLevel int,
matching EdgeSet,
guideLayers []NodeOrderedSet,
used map[Node]bool,
) (EdgeSet, bool) {
used[currentNode] = true
if currentLevel == 0 {
return currentSLAP, true
}
for _, nextNode := range guideLayers[currentLevel-1] {
if used[nextNode] {
continue
}
edge, found := bg.Edges.FindByNodes(currentNode, nextNode)
if !found {
continue
}
if matching.Contains(edge) == util.Odd(currentLevel) {
continue
}
currentSLAP = append(currentSLAP, edge)
slap, found := bg.findDisjointSLAPHelper(nextNode, currentSLAP, currentLevel-1, matching, guideLayers, used)
if found {
return slap, true
}
currentSLAP = currentSLAP[:len(currentSLAP)-1]
}
used[currentNode] = false
return nil, false
}
func (bg *BipartiteGraph) createSLAPGuideLayers(matching EdgeSet) (guideLayers []NodeOrderedSet) {
used := make(map[Node]bool)
currentLayer := NodeOrderedSet{}
for _, node := range bg.Left {
if matching.Free(node) {
used[node] = true
currentLayer = append(currentLayer, node)
}
}
if len(currentLayer) == 0 {
return []NodeOrderedSet{}
} else {
guideLayers = append(guideLayers, currentLayer)
}
done := false
for !done {
lastLayer := currentLayer
currentLayer = NodeOrderedSet{}
if util.Odd(len(guideLayers)) {
for _, leftNode := range lastLayer {
for _, rightNode := range bg.Right {
if used[rightNode] {
continue
}
edge, found := bg.Edges.FindByNodes(leftNode, rightNode)
if !found || matching.Contains(edge) {
continue
}
currentLayer = append(currentLayer, rightNode)
used[rightNode] = true
if matching.Free(rightNode) {
done = true
}
}
}
} else {
for _, rightNode := range lastLayer {
for _, leftNode := range bg.Left {
if used[leftNode] {
continue
}
edge, found := bg.Edges.FindByNodes(leftNode, rightNode)
if !found || !matching.Contains(edge) {
continue
}
currentLayer = append(currentLayer, leftNode)
used[leftNode] = true
}
}
}
if len(currentLayer) == 0 {
return []NodeOrderedSet{}
} else {
guideLayers = append(guideLayers, currentLayer)
}
}
return
}