Debian Codegraph @

build/intersect.go

raw symbols SCIP
1 package build
2
3 // Intersect returns the graph intersection g1 ∩ g2,
4 // which consists of the intersection of the two vertex sets
5 // and the intersection of the two edge sets of g1 and g2.
6 // The edges of the new graph will have zero cost.
7 func (g1 *Virtual) Intersect(g2 *Virtual) *Virtual {
8 n := min(g1.order, g2.order)
9 switch n {
10 case 0:
11 return null
12 case 1:
13 return singleton()
14 }
15
16 var res *Virtual
17 if g1.order == g2.order {
18 res = generic0(n, func(v, w int) bool {
19 return g1.edge(v, w) && g2.edge(v, w)
20 })
21 } else {
22 res = generic0(n, func(v, w int) bool {
23 return v < n && w < n && g1.edge(v, w) && g2.edge(v, w)
24 })
25 }
26
27 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
28 w, n := findBoth(v, a, g1, g2)
29 for n != 0 {
30 for u := w; u < w+n; u++ {
31 if do(u, 0) {
32 return true
33 }
34 }
35 w, n = findBoth(v, w+n, g1, g2)
36 }
37 return
38 }
39 return res
40 }
41
42 // findBoth returns the smallest n consecutive neighbors w, w+1, ..., w+n-1
43 // of v in g1 ∩ g2 for which w >= a.
44 func findBoth(v int, a int, g1, g2 *Virtual) (w int, n int) {
45 w1, n1 := g1.find(v, a)
46 w2, n2 := g2.find(v, a)
47 for n1 != 0 && n2 != 0 {
48 switch {
49 case w1+n1 <= w2:
50 w1, n1 = g1.find(v, w2)
51 case w2+n2 <= w1:
52 w2, n2 = g2.find(v, w1)
53 case w1 < w2:
54 return w2, min(w1+n1-w2, n2)
55 default:
56 return w1, min(w2+n2-w1, n1)
57 }
58 }
59 return
60 }
61
62 // find returns the smallest n consecutive neighbors w, w+1, ..., w+n-1
63 // of v for which w >= a.
64 func (g *Virtual) find(v int, a int) (w int, n int) {
65 prev := -1
66 g.visit(v, a, func(w0 int, c0 int64) (skip bool) {
67 switch prev {
68 case -1:
69 w, n = w0, 1
70 prev = w0
71 return
72 case w0 - 1:
73 n += 1
74 prev = w0
75 return
76 }
77 return true
78 })
79 return
80 }