Debian Codegraph @

build/build.go

raw symbols SCIP
1 // Package build offers a tool for building virtual graphs.
2 //
3 // Virtual graphs
4 //
5 // In a virtual graph no vertices or edges are stored in memory,
6 // they are instead computed as needed. New virtual graphs are constructed
7 // by composing and filtering a set of standard graphs, or by writing
8 // functions that describe the edges of a graph.
9 // Multigraphs and graphs with self-loops are not suppported.
10 //
11 // Non-virtual graphs can be imported, and used as building blocks,
12 // by the Specific function. Virtual graphs don't need to be “exported‬”;
13 // they implement the Iterator interface and hence can be used directly
14 // by any algorithm in the graph package.
15 //
16 // Performance tips
17 //
18 // When possible, try to use predefined building blocks rather than
19 // filter functions. In particular, note that graphs built by the Generic
20 // function must visit all potenential neighbors during iteration.
21 //
22 // If space is readily available, you may use the Specific function
23 // to turn on caching for any component. This gives constant time
24 // performance for all basic operations on that component.
25 //
26 // Tutorial
27 //
28 // The Euclid and Maxflow examples show how to build graphs from
29 // standard components using composition and filtering. They also
30 // demonstrate how to apply a cost function to a virtual graph.
31 //
32 package build
33
34 import (
35 "github.com/yourbasic/graph"
36 )
37
38 // Virtual represents a virtual graph.
39 // In a virtual graph no vertices or edges are stored in memory,
40 // they are instead computed as needed. New virtual graphs are constructed
41 // by composing and filtering a set of standard graphs, or by writing
42 // functions that describe the edges of a graph.
43 type Virtual struct {
44 // The `order` field is, in fact, a constant function.
45 // It returns the number of vertices in the graph.
46 order int
47
48 // The `edge` and `cost` functions define a weighted graph without self-loops.
49 //
50 // • edge(v, w) returns true whenever (v, w) belongs to the graph;
51 // the value is disregarded when v == w.
52 //
53 // • cost(v, w) returns the cost of (v, w);
54 // the value is disregarded when edge(v, w) is false.
55 //
56 edge func(v, w int) bool
57 cost func(v, w int) int64
58
59 // The `degree` and `visit` functions can be used to improve performance.
60 // They MUST BE CONSISTENT with edge and cost. If not implemented,
61 // the `generic` or `generic0` implementation is used instead.
62 // The `Consistent` test function should be used to check compliance.
63 //
64 // • degree(v) returns the outdegree of vertex v.
65 //
66 // • visit(v) visits all neighbors w of v for which w ≥ a in
67 // NUMERICAL ORDER calling do(w, c) for edge (v, w) of cost c.
68 // If a call to do returns true, visit MUST ABORT the iteration
69 // and return true; if successful it should return false.
70 // Precondition: a ≥ 0.
71 //
72 degree func(v int) int
73 visit func(v int, a int, do func(w int, c int64) (skip bool)) (aborted bool)
74 }
75
76 // FilterFunc is a function that tells if there is a directed edge from v to w.
77 // The nil value represents an edge function that always returns true.
78 type FilterFunc func(v, w int) bool
79
80 // CostFunc is a function that computes the cost of an edge from v to w.
81 // The nil value represents a cost function that always returns 0.
82 type CostFunc func(v, w int) int64
83
84 // Cost returns a CostFunc that always returns n.
85 func Cost(n int64) CostFunc {
86 return func(int, int) int64 { return n }
87 }
88
89 func neverEdge(int, int) bool { return false }
90 func alwaysEdge(v, w int) bool { return v != w }
91
92 func zero(int, int) int64 { return 0 }
93
94 func degreeZero(int) int { return 0 }
95 func degreeOne(int) int { return 1 }
96
97 func noNeighbors(int, int, func(w int, c int64) bool) bool { return false }
98
99 const bitsPerWord = 32 << uint(^uint(0)>>63)
100
101 func min(m, n int) int {
102 if m > n {
103 return n
104 }
105 return m
106 }
107
108 func max(m, n int) int {
109 if m < n {
110 return n
111 }
112 return m
113 }
114
115 // null is the null graph; a graph with no vertices.
116 var null = new(Virtual)
117
118 // singleton returns a graph with one vertex.
119 func singleton() *Virtual {
120 return &Virtual{
121 order: 1,
122 edge: neverEdge,
123 cost: zero,
124 degree: degreeZero,
125 visit: noNeighbors,
126 }
127 }
128
129 // edge returns a graph with two edges (0, 1) and (1, 0).
130 func edge() *Virtual {
131 g := &Virtual{
132 order: 2,
133 cost: zero,
134 edge: alwaysEdge,
135 degree: degreeOne,
136 }
137 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
138 w := 1 - v
139 if w < a {
140 return
141 }
142 return do(w, 0)
143 }
144 return g
145 }
146
147 // line(n) returns the graph {0, 1}, {1, 2}, {2, 3},... , {n-2, n-1}.
148 func line(n int) *Virtual {
149 switch {
150 case n < 0:
151 return nil
152 case n == 0:
153 return null
154 case n == 1:
155 return singleton()
156 case n == 2:
157 return edge()
158 }
159 g := generic0(n, func(v, w int) (edge bool) {
160 switch v - w {
161 case -1, 1:
162 edge = true
163 }
164 return
165 })
166 g.degree = func(v int) int {
167 switch v {
168 case 0, n - 1:
169 return 1
170 default:
171 return 2
172 }
173 }
174 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
175 if w := v - 1; w >= a && do(w, 0) {
176 return true
177 }
178 if w := v + 1; w >= a && w < n && do(w, 0) {
179 return true
180 }
181 return
182 }
183 return g
184 }
185
186 // generic returns a standard implementation; cost and edge can't be nil.
187 func generic(n int, cost CostFunc, edge func(v, w int) bool) *Virtual {
188 switch {
189 case n < 0:
190 return nil
191 case n == 0:
192 return null
193 case n == 1:
194 return singleton()
195 }
196 g := &Virtual{
197 order: n,
198 edge: func(v, w int) bool { return v != w && edge(v, w) },
199 cost: cost,
200 }
201 g.degree = func(v int) (deg int) {
202 g.visit(v, 0, func(int, int64) (skip bool) {
203 deg++
204 return
205 })
206 return
207 }
208 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
209 for w := a; w < n; w++ {
210 if g.edge(v, w) && do(w, cost(v, w)) {
211 return true
212 }
213 }
214 return
215 }
216 return g
217 }
218
219 // generic0 returns a standard implementation; edge can't be nil.
220 func generic0(n int, edge func(v, w int) bool) *Virtual {
221 switch {
222 case n < 0:
223 return nil
224 case n == 0:
225 return null
226 case n == 1:
227 return singleton()
228 }
229 g := &Virtual{
230 order: n,
231 edge: func(v, w int) bool { return v != w && edge(v, w) },
232 cost: zero,
233 }
234 g.degree = func(v int) (deg int) {
235 g.visit(v, 0, func(int, int64) (skip bool) {
236 deg++
237 return
238 })
239 return
240 }
241 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
242 for w := a; w < n; w++ {
243 if g.edge(v, w) && do(w, 0) {
244 return true
245 }
246 }
247 return
248 }
249 return g
250 }
251
252 // Generic returns a virtual graph with n vertices; its edge set consists of
253 // all edges (v, w), v ≠ w, for which edge(v, w) returns true.
254 func Generic(n int, edge FilterFunc) *Virtual {
255 switch {
256 case n < 0:
257 return nil
258 case n == 0:
259 return null
260 case n == 1:
261 return singleton()
262 case edge == nil:
263 return Kn(n)
264 }
265 return generic0(n, edge)
266 }
267
268 // Specific returns a cached copy of g with constant time performance for
269 // all basic operations. It uses space proportional to the size of the graph.
270 //
271 // This function does not accept multigraphs and graphs with self-loops.
272 func Specific(g graph.Iterator) *Virtual {
273 h := graph.Sort(g)
274 stats := graph.Check(h)
275 if stats.Multi != 0 || stats.Loops != 0 {
276 panic("Virtual doesn't support multiple edges or self-loops")
277 }
278 res := &Virtual{
279 order: h.Order(),
280 edge: h.Edge,
281 visit: h.VisitFrom,
282 degree: h.Degree,
283 }
284 if stats.Weighted == 0 {
285 res.cost = zero
286 return res
287 }
288 res.cost = func(v, w int) (cost int64) {
289 if !h.Edge(v, w) {
290 return 0
291 }
292 h.VisitFrom(v, w, func(w int, c int64) (skip bool) {
293 cost = c
294 return true
295 })
296 return
297 }
298 return res
299 }
300
301 // Empty returns a virtual graph with n vertices and no edges.
302 func Empty(n int) *Virtual {
303 switch {
304 case n < 0:
305 return nil
306 case n == 0:
307 return null
308 case n == 1:
309 return singleton()
310 }
311 return &Virtual{
312 order: n,
313 edge: neverEdge,
314 cost: zero,
315 degree: degreeZero,
316 visit: noNeighbors,
317 }
318 }
319
320 // Kn returns a complete simple graph with n vertices.
321 func Kn(n int) *Virtual {
322 switch {
323 case n < 0:
324 return nil
325 case n == 0:
326 return null
327 case n == 1:
328 return singleton()
329 }
330 g := &Virtual{
331 order: n,
332 edge: alwaysEdge,
333 cost: zero,
334 degree: func(v int) int { return n - 1 },
335 }
336 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
337 for w := a; w < g.order; w++ {
338 if v != w && do(w, 0) {
339 return true
340 }
341 }
342 return
343 }
344 return g
345 }
346
347 // Complement returns the complement graph of g.
348 // This graph has the same vertices as g,
349 // but its edge set consists of the edges not present in g.
350 // The edges of the complement graph will have zero cost.
351 func (g *Virtual) Complement() *Virtual {
352 n := g.order
353 switch n {
354 case 0:
355 return null
356 case 1:
357 return singleton()
358 }
359 res := generic0(n, func(v, w int) (edge bool) {
360 return v != w && !g.edge(v, w)
361 })
362 res.degree = func(v int) int { return n - 1 - g.degree(v) }
363 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
364 prev := a
365 if g.visit(v, a, func(w0 int, _ int64) (skip bool) {
366 for w := prev; w < w0; w++ {
367 if v != w && do(w, 0) {
368 return true
369 }
370 }
371 prev = w0 + 1
372 return
373 }) {
374 return true
375 }
376 for w := prev; w < n; w++ {
377 if v != w && do(w, 0) {
378 return true
379 }
380 }
381 return
382 }
383 return res
384 }
385
386 // Keep returns a graph containing all edges (v, w) of g for which edge(v, w) is true.
387 func (g *Virtual) Keep(edge FilterFunc) *Virtual {
388 n := g.order
389 switch {
390 case n == 0:
391 return null
392 case n == 1:
393 return singleton()
394 case edge == nil:
395 return g
396 }
397 res := generic(g.order, g.cost, func(v, w int) bool {
398 return edge(v, w) && g.edge(v, w)
399 })
400 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
401 return g.visit(v, a, func(w int, c int64) bool {
402 return edge(v, w) && do(w, c)
403 })
404 }
405 return res
406 }
407
408 // AddCost returns a copy of g with a new cost assigned to all edges.
409 func (g *Virtual) AddCost(c int64) *Virtual {
410 res := *g
411 res.cost = Cost(c)
412 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
413 return g.visit(v, a, func(w int, _ int64) bool {
414 return do(w, c)
415 })
416 }
417 return &res
418 }
419
420 // AddCostFunc returns a copy of g with a new cost function assigned.
421 func (g *Virtual) AddCostFunc(c CostFunc) *Virtual {
422 if c == nil {
423 h := g.AddCost(0)
424 return h
425 }
426 res := *g
427 res.cost = c
428 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
429 return g.visit(v, a, func(w int, _ int64) bool {
430 return do(w, c(v, w))
431 })
432 }
433 return &res
434 }
435
436 // Order returns the number of vertices in the graph.
437 func (g *Virtual) Order() int {
438 return g.order
439 }
440
441 // Degree returns the number of outward directed edges from v.
442 func (g *Virtual) Degree(v int) int {
443 if v < 0 || v >= g.order {
444 panic("vertex out of range")
445 }
446 return g.degree(v)
447 }
448
449 // Edge tells if there is an edge from v to w.
450 func (g *Virtual) Edge(v, w int) bool {
451 if v < 0 || v >= g.order || w < 0 || w >= g.order {
452 return false
453 }
454 return g.edge(v, w)
455 }
456
457 // Cost returns the cost of an edge from v to w, or 0 if no such edge exists.
458 func (g *Virtual) Cost(v, w int) int64 {
459 if v < 0 || v >= g.order || w < 0 || w >= g.order {
460 return 0
461 }
462 if g.edge(v, w) {
463 return g.cost(v, w)
464 }
465 return 0
466 }
467
468 // Visit calls the do function for each neighbor w of v,
469 // with c equal to the cost of the edge from v to w.
470 // The neighbors are visited in increasing numerical order.
471 // If do returns true, Visit returns immediately,
472 // skipping any remaining neighbors, and returns true.
473 func (g *Virtual) Visit(v int, do func(w int, c int64) bool) bool {
474 if v < 0 || v >= g.order {
475 panic("vertex out of range")
476 }
477 return g.visit(v, 0, do)
478 }
479
480 // VisitFrom calls the do function starting from the first neighbor w
481 // for which w ≥ a, with c equal to the cost of the edge from v to w.
482 // The neighbors are then visited in increasing numerical order.
483 // If do returns true, VisitFrom returns immediately,
484 // skipping any remaining neighbors, and returns true.
485 func (g *Virtual) VisitFrom(v int, a int, do func(w int, c int64) bool) bool {
486 n := g.order
487 switch {
488 case v < 0 || v >= n:
489 panic("vertex out of range")
490 case a < 0:
491 a = 0
492 case a > n:
493 a = n
494 }
495 return g.visit(v, a, do)
496 }
497
498 // String returns a string representation of the graph.
499 func (g *Virtual) String() string {
500 return graph.String(g)
501 }