Debian Codegraph @

build/cartesian.go

raw symbols SCIP
1 package build
2
3 import "strconv"
4
5 // Cartesian returns the cartesian product of g1 and g2:
6 // a graph whose vertices correspond to ordered pairs (v1, v2),
7 // where v1 and v2 are vertices in g1 and g2, respectively.
8 // The vertices (v1, v2) and (w1, w2) are connected by an edge if
9 // v1 = w1 and {v2, w2} ∊ g2 or v2 = w2 and {v1, w1} ∊ g1.
10 //
11 // In the new graph, vertex (v1, v2) gets index nā‹…v1 + v2, where n = g2.Order(),
12 // and index i corresponds to the vertice (i/n, i%n).
13 func (g1 *Virtual) Cartesian(g2 *Virtual) *Virtual {
14 m, n := g1.Order(), g2.Order()
15 switch {
16 case m == 0 || n == 0:
17 return null
18 case m*n/m != n:
19 panic("too large m=" + strconv.Itoa(m) + " n=" + strconv.Itoa(n))
20 }
21 order := m * n
22 g := generic0(order, func(v, w int) (edge bool) {
23 v1, v2 := v/n, v%n
24 w1, w2 := w/n, w%n
25 return v1 == w1 && g2.Edge(v2, w2) || v2 == w2 && g1.Edge(v1, w1)
26 })
27
28 g.degree = func(v int) (deg int) {
29 return g1.degree(v/n) + g2.degree(v%n)
30 }
31
32 g.visit = func(v int, a int, do func(w int, _ int64) bool) (aborted bool) {
33 v1, v2 := v/n, v%n
34 a1 := a / n
35 if a1 < v1 {
36 if more := false; g1.visit(v1, a1, func(w1 int, _ int64) (skip bool) {
37 if w1 >= v1 {
38 more, skip = true, true
39 return
40 }
41 w := n*w1 + v2 // v2 == w2
42 return w >= a && do(w, 0)
43 }) && !more {
44 return true
45 }
46 }
47 if a1 <= v1 {
48 a2 := 0
49 if a1 == v1 {
50 a2 = a % n
51 }
52 if g2.visit(v2, a2, func(w2 int, _ int64) (skip bool) {
53 return do(n*v1+w2, 0) // v1 == w1
54 }) {
55 return true
56 }
57 }
58 return g1.visit(v1, max(a1, v1+1), func(w1 int, _ int64) (skip bool) {
59 w := n*w1 + v2 // v2 == w2
60 return w >= a && do(w, 0)
61 })
62 }
63 return g
64 }