-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdetKDecomp.go
242 lines (197 loc) · 6.29 KB
/
detKDecomp.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"log"
"reflect"
"github.com/cem-okulmus/BalancedGo/lib"
"github.com/cem-okulmus/disjoint"
)
// DetKDecomp computes for a graph and some width K a HD of width K if it exists
type DetKDecomp struct {
K int
Graph lib.Graph
BalFactor int
SubEdge bool
cache lib.Cache
}
// SetWidth sets the current width parameter of the algorithm
func (d *DetKDecomp) SetWidth(K int) {
d.cache.Reset() // reset the cache as the new width might invalidate any old results
d.K = K
}
func (d *DetKDecomp) findHD(currentGraph lib.Graph) lib.Decomp {
d.cache.Init()
return d.findDecomp(currentGraph, []int{}, 0)
}
// FindDecomp finds a decomp
func (d *DetKDecomp) FindDecomp() lib.Decomp {
return d.findHD(d.Graph)
}
// Name returns the name of the algorithm
func (d *DetKDecomp) Name() string {
if d.SubEdge {
return "DetK with local BIP"
}
return "DetK"
}
// FindDecompGraph finds a decomp, for an explicit graph
func (d *DetKDecomp) FindDecompGraph(G lib.Graph) lib.Decomp {
return d.findHD(G)
}
func connectingSep(sep []int, conn []int, comp []int) bool {
if !lib.Subset(conn, sep) {
return false
}
if len(lib.Inter(sep, comp)) == 0 {
return false
}
return true
}
//Note: as implemented this breaks Special Condition (bag must be limited by oldSep)
func baseCaseDetK(H lib.Graph) lib.Decomp {
// log.Printf("Base case reached. Number of Special Edges %d\n", len(Sp))
var children lib.Node
switch len(H.Special) {
case 0:
return lib.Decomp{Graph: H, Root: lib.Node{Bag: H.Vertices(), Cover: H.Edges}}
case 1:
sp1 := H.Special[0]
children = lib.Node{Bag: sp1.Vertices(), Cover: sp1}
}
if H.Edges.Len() == 0 {
return lib.Decomp{Graph: H, Root: children}
}
return lib.Decomp{Graph: H, Root: lib.Node{Bag: H.Vertices(), Cover: H.Edges, Children: []lib.Node{children}}}
}
func (d *DetKDecomp) findDecomp(H lib.Graph, oldSep []int, recDepth int) lib.Decomp {
recDepth = recDepth + 1 // increase the recursive depth
verticesCurrent := append(H.Vertices())
verticesExtended := append(verticesCurrent, oldSep...)
conn := lib.Inter(oldSep, verticesCurrent)
compVertices := lib.Diff(verticesCurrent, oldSep)
bound := lib.FilterVertices(d.Graph.Edges, conn)
// log.Printf("\n\nD Current oldSep: %v, Conn: %v\n", lib.PrintVertices(oldSep), lib.PrintVertices(conn))
// log.Printf("D Current SubGraph: %v ( %v hash) \n", H, H.Edges.Hash())
// log.Printf("D Current SubGraph: %v ( %v edges) (hash: %v )\n", H, H.Edges.Len(), H.Edges.Hash())
// log.Println("D Hedges ", H)
// log.Println("D Comp Vertices: ", lib.PrintVertices(compVertices))
// Base case if H <= K
if H.Edges.Len() == 0 && len(H.Special) <= 1 {
return baseCaseDetK(H)
}
gen := lib.NewCover(d.K, conn, bound, H.Edges.Vertices())
var Vertices = make(map[int]*disjoint.Element)
OUTER:
for gen.HasNext {
out := gen.NextSubset()
if out == -1 {
if gen.HasNext {
log.Panicln(" -1 but hasNext not false!")
}
continue
}
var sep lib.Edges
sep = lib.GetSubset(bound, gen.Subset)
// if !Subset(conn, sep.Vertices()) {
// log.Panicln("Cover messed up! 137")
// }
// log.Println("Next Cover ", sep)
addEdges := false
//check if sep "makes some progress" into separating H
if len(lib.Inter(sep.Vertices(), compVertices)) == 0 {
addEdges = true
}
if !addEdges || d.K-sep.Len() > 0 {
iAdd := 0
addingEdges:
for !addEdges || iAdd < H.Edges.Len() {
var sepActual lib.Edges
if addEdges {
sepActual = lib.NewEdges(append(sep.Slice(), H.Edges.Slice()[iAdd]))
} else {
sepActual = sep
}
// sepActualOrigin := sepActual
var sepSub *lib.SepSub
var sepConst []lib.Edge
var sepChanging []lib.Edge
if d.SubEdge {
for i, v := range gen.Subset {
if gen.InComp[v] {
sepChanging = append(sepChanging, sep.Slice()[i])
} else {
sepConst = append(sepConst, sep.Slice()[i])
}
}
if addEdges {
sepChanging = append(sepChanging, H.Edges.Slice()[iAdd])
}
}
subEdges:
for true {
// log.Println("Sep chosen ", sepActual, " out ", out)
comps, _, _ := H.GetComponents(sepActual, Vertices)
//check cache for previous encounters
if d.cache.CheckNegative(sepActual, comps) {
// log.Println("Skipping sep", sepActual, "due to cache.")
if addEdges {
iAdd++
continue addingEdges
} else {
continue OUTER
}
}
// log.Printf("Comps of Sep: %v, len: %v\n", comps, len(comps))
var subtrees []lib.Node
bag := lib.Inter(sepActual.Vertices(), verticesExtended)
for i := range comps {
decomp := d.findDecomp(comps[i], bag, recDepth)
if reflect.DeepEqual(decomp, lib.Decomp{}) {
d.cache.AddNegative(sepActual, comps[i])
// log.Printf("detK REJECTING %v: couldn't decompose %v \n",
// lib.Graph{Edges: sepActual}, comps[i])
// log.Printf("\n\nCurrent oldSep: %v\n", lib.PrintVertices(oldSep))
// log.Printf("Current SubGraph: %v ( %v edges) %v\n", H, H.Edges.Len(), H.Edges.Hash())
if d.SubEdge {
if sepSub == nil {
sepSub = lib.GetSepSub(d.Graph.Edges, lib.NewEdges(sepChanging), d.K)
}
nextBalsepFound := false
for !nextBalsepFound {
if sepSub.HasNext() {
sepActual = sepSub.GetCurrent()
sepActual = lib.NewEdges(append(sepActual.Slice(), sepConst...))
if connectingSep(sepActual.Vertices(), conn, compVertices) {
nextBalsepFound = true
}
} else {
// log.Printf("No SubSep found for %v \n", Graph{Edges: sepActualOrigin})
if addEdges {
iAdd++
continue addingEdges
} else {
continue OUTER
}
}
}
// log.Printf("Sub Sep chosen: %vof %v \n", lib.Graph{Edges: sepActual},
// lib.Graph{Edges: sepActualOrigin})
continue subEdges
}
if addEdges {
iAdd++
continue addingEdges
} else {
continue OUTER
}
}
//d.Cache.AddPositive(sepActual, comps[i])
// log.Printf("Produced Decomp: %v\n", decomp)
subtrees = append(subtrees, decomp.Root)
}
return lib.Decomp{Graph: H, Root: lib.Node{Bag: bag, Cover: sepActual, Children: subtrees}}
}
}
}
}
return lib.Decomp{} // Reject if no separator could be found
}