-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmatching.go
64 lines (57 loc) · 1.27 KB
/
matching.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
package hungarianAlgorithm
type matching struct {
n int
ij []int
ji []int
}
// Returns an empty matching of the given size
func makeMatching(n int) matching {
ij := make([]int, n)
ji := make([]int, n)
for i := 0; i < n; i++ {
ij[i] = -1
ji[i] = -1
}
return matching{n, ij, ji}
}
// Greedily build a matching
func (m *matching) initialize(isTight func(int, int) bool) {
for i := 0; i < m.n; i++ {
for j := 0; j < m.n; j++ {
if isTight(i, j) && (m.ji[j] == -1) {
m.ij[i] = j
m.ji[j] = i
break
}
}
}
}
// Returns whether the matching is perfect and a free vertex (when the matching is not perfect)
func (m *matching) isPerfect() (bool, int) {
for i := 0; i < m.n; i++ {
if m.ij[i] == -1 {
return false, i
}
}
return true, -1
}
// Returns whether the vertex of the right set is matched, when true is also
// returns the match
func (m *matching) isMatched(j int) (bool, int) {
i := m.ji[j]
return (i != -1), i
}
// Returns an array `a` representing the edges of the matching in the form `(i, a[i])`.
func (m *matching) format() []int {
return m.ij
}
// Augments the matching using the given augmenting path.
func (m *matching) augment(p []int) {
for idx, j := range p {
if idx%2 == 0 {
i := p[idx+1]
m.ij[i] = j
m.ji[j] = i
}
}
}