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 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// Copyright ©2012 The bíogo 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 graph
import (
"math"
"sync"
)
// Worked from http://www.cs.tau.ac.il/~zwick/grad-algo-08/gmc.pdf, but the wiki page
// http://en.wikipedia.org/wiki/Karger%27s_algorithm#Karger.E2.80.93Stein_algorithm is very good.
// FIXME Use Index() instead of ID() on edges and nodes - this requires a change to node.go
const sqrt2 = 1.4142135623730950488016887242096980785696718753769480
func RandMinCut(g *Undirected, iter int) (c []Edge, w float64) {
ka := newKarger(g)
w = math.Inf(1)
for i := 0; i < iter; i++ {
ka.fastMinCut()
if ka.w < w {
w = ka.w
c = ka.c
}
}
return c, w
}
func (ka *karger) fastMinCut() {
if ka.order <= 6 {
ka.compact(2)
return
}
t := int(math.Ceil(float64(ka.order)/sqrt2 + 1))
sub := []*karger{ka, ka.clone()}
for _, ks := range sub {
ks.contract(t)
ks.fastMinCut()
}
if sub[1].w < sub[0].w {
*ka = *sub[1]
}
}
// parallelised within the recursion tree
func RandMinCutPar(g *Undirected, iter, threads int) (c []Edge, w float64) {
k := newKarger(g)
k.split = threads
if k.split == 0 {
k.split = -1
}
w = math.Inf(1)
for i := 0; i < iter; i++ {
k.fastMinCutPar()
if k.w < w {
w = k.w
c = k.c
}
}
return c, w
}
func (ka *karger) fastMinCutPar() {
if ka.order <= 6 {
ka.compact(2)
return
}
t := int(math.Ceil(float64(ka.order)/sqrt2 + 1))
var wg *sync.WaitGroup
if ka.count < ka.split {
wg = &sync.WaitGroup{}
}
ka.count++
sub := []*karger{ka, ka.clone()}
for _, ks := range sub {
if wg != nil {
wg.Add(1)
go func(ks *karger) {
defer wg.Done()
ks.contract(t)
ks.fastMinCutPar()
}(ks)
} else {
ks.contract(t)
ks.fastMinCutPar()
}
}
if wg != nil {
wg.Wait()
}
if sub[1].w < sub[0].w {
*ka = *sub[1]
}
}
type karger struct {
g *Undirected
order int
ind []super
sel Selector
c []Edge
w float64
count int
split int
}
type super struct {
label int
nodes []int
}
func newKarger(g *Undirected) *karger {
ka := karger{
g: g,
order: g.Order(),
ind: make([]super, g.NextNodeID()),
sel: make(Selector, g.Size()),
}
for i := range ka.ind {
ka.ind[i].label = -1
ka.ind[i].nodes = nil
}
for _, n := range ka.g.Nodes() {
id := n.ID()
ka.ind[id].label = id
}
for i, e := range ka.g.Edges() {
ka.sel[i] = WeightedItem{Index: e.ID(), Weight: e.Weight()}
}
ka.sel.Init()
return &ka
}
func (ka *karger) clone() *karger {
c := karger{
g: ka.g,
ind: make([]super, ka.g.NextNodeID()),
sel: make(Selector, ka.g.Size()),
order: ka.order,
count: ka.count,
}
copy(c.sel, ka.sel)
for i, n := range ka.ind {
s := &c.ind[i]
s.label = n.label
if n.nodes != nil {
s.nodes = make([]int, len(n.nodes))
copy(s.nodes, n.nodes)
}
}
return &c
}
func (ka *karger) contract(k int) {
for ka.order > k {
id, err := ka.sel.Select()
if err != nil {
break
}
e := ka.g.Edge(id)
if ka.loop(e) {
continue
}
hid, tid := e.Head().ID(), e.Tail().ID()
hl, tl := ka.ind[hid].label, ka.ind[tid].label
if len(ka.ind[hl].nodes) < len(ka.ind[tl].nodes) {
hid, tid = tid, hid
hl, tl = tl, hl
}
if ka.ind[hl].nodes == nil {
ka.ind[hl].nodes = []int{hid}
}
if ka.ind[tl].nodes == nil {
ka.ind[hl].nodes = append(ka.ind[hl].nodes, tid)
} else {
ka.ind[hl].nodes = append(ka.ind[hl].nodes, ka.ind[tl].nodes...)
ka.ind[tl].nodes = nil
}
for _, i := range ka.ind[hl].nodes {
ka.ind[i].label = ka.ind[hid].label
}
ka.order--
}
}
func (ka *karger) compact(k int) {
ka.contract(k)
ka.c, ka.w = []Edge{}, 0
for _, e := range ka.g.Edges() {
if ka.loop(e) {
continue
}
ka.c = append(ka.c, e)
ka.w += e.Weight()
}
}
func (ka *karger) loop(e Edge) bool {
return ka.ind[e.Head().ID()].label == ka.ind[e.Tail().ID()].label
}
|