Debian Codegraph @

build/connect.go

raw symbols SCIP
1 package build
2
3 // Connect connects g1 and g2 by making v1 in g1 a common vertex
4 // with 0 in g2. The vertices in g2 are renumbered: 0 becomes v1
5 // in the new graph; and w, with w > 0, becomes w + g1.Order() - 1.
6 //
7 func (g1 *Virtual) Connect(v1 int, g2 *Virtual) *Virtual {
8 if g1.order == 0 || g2.order == 0 || v1 < 0 || v1 >= g1.order {
9 return nil
10 }
11
12 n := g1.order + g2.order - 1
13 t := g1.order - 1 // transpose
14 if n == 1 {
15 return singleton()
16 }
17
18 newCost := func(v, w int) int64 {
19 switch {
20 case v <= t && w <= t:
21 return g1.cost(v, w)
22 case v > t && w > t:
23 return g2.cost(v-t, w-t)
24 case v == v1:
25 return g2.cost(0, w-t)
26 case w == v1:
27 return g2.cost(v-t, 0)
28 default:
29 return 0
30 }
31 }
32
33 res := generic(n, newCost, func(v, w int) bool {
34 switch {
35 case v <= t && w <= t:
36 return g1.edge(v, w)
37 case v > t && w > t:
38 return g2.edge(v-t, w-t)
39 case v == v1:
40 return g2.edge(0, w-t)
41 case w == v1:
42 return g2.edge(v-t, 0)
43 default:
44 return false
45 }
46 })
47
48 res.degree = func(v int) (deg int) {
49 switch {
50 case v == v1:
51 return g1.degree(v) + g2.degree(0)
52 case v <= t:
53 return g1.degree(v)
54 default:
55 return g2.degree(v - t)
56 }
57 }
58
59 res.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
60 switch {
61 case v > t:
62 return g2.visit(v-t, max(0, a-t), func(w int, c int64) (skip bool) {
63 if w == 0 {
64 return v1 >= a && do(v1, c)
65 }
66 return do(w+t, c)
67 })
68 case g1.visit(v, a, do):
69 return true
70 case v == v1:
71 return g2.visit(0, max(0, a-t), func(w int, c int64) (skip bool) {
72 return do(w+t, c)
73 })
74 default:
75 return
76 }
77 }
78 return res
79 }