Debian Codegraph @

build/tensor.go

raw symbols SCIP
1 package build
2
3 import "strconv"
4
5 // Tensor returns the tensor 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 whenever
9 // both of the edges {v1, w1} and {v2, w2} exist in the original graphs.
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) Tensor(g2 *Virtual) *Virtual {
14 m, n := g1.Order(), g2.Order()
15 switch {
16 case m < 0 || n < 0:
17 return nil
18 case m == 0 || n == 0:
19 return null
20 case m*n/m != n:
21 panic("too large m=" + strconv.Itoa(m) + " n=" + strconv.Itoa(n))
22 }
23
24 g := generic0(m*n, func(v, w int) (edge bool) {
25 v1, v2 := v/n, v%n
26 w1, w2 := w/n, w%n
27 return g1.Edge(v1, w1) && g2.Edge(v2, w2)
28 })
29
30 g.degree = func(v int) (deg int) {
31 v1, v2 := v/n, v%n
32 return g1.degree(v1) * g2.degree(v2)
33 }
34
35 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
36 v1, v2 := v/n, v%n
37 a1, a2 := a/n, a%n
38 return g1.visit(v1, a1, func(w1 int, c int64) (skip bool) {
39 if w1 == a1 {
40 return g2.visit(v2, a2, func(w2 int, c int64) (skip bool) {
41 return do(n*w1+w2, 0)
42 })
43 }
44 return g2.visit(v2, 0, func(w2 int, c int64) (skip bool) {
45 return do(n*w1+w2, 0)
46 })
47 })
48 }
49 return g
50 }