Debian Codegraph @

build/circulant.go

raw symbols SCIP
1 package build
2
3 import "sort"
4
5 // Circulant returns a virtual circulant graph with n vertices
6 // in which vertex i is adjacent to vertex (i + j) mod n and vertex (i - j) mod n
7 // for each j in the list s.
8 func Circulant(n int, s ...int) *Virtual {
9 switch {
10 case n < 0:
11 return nil
12 case n == 0:
13 return null
14 case n == 1:
15 return singleton()
16 }
17
18 s1 := make([]int, 0, 2*len(s))
19 for _, j := range s {
20 j %= n
21 if j < 0 {
22 j += n
23 }
24 s1 = append(s1, j)
25 s1 = append(s1, n-j)
26 }
27 sort.Ints(s1)
28 var t []int
29 prev := -1
30 for _, j := range s1 {
31 // Remove zeroes and duplicates.
32 if j == prev || j == 0 || j == n {
33 continue
34 }
35 prev = j
36 t = append(t, j)
37 }
38 deg := len(t)
39 switch {
40 case deg == 0:
41 return Empty(n)
42 case deg == 1 && t[0] == 1:
43 return Cycle(n)
44 case deg == n-1:
45 return Kn(n)
46 }
47
48 g := generic0(n, func(v, w int) (edge bool) {
49 d := v - w
50 if d < 0 {
51 d = -d
52 }
53 i := sort.SearchInts(t, d)
54 return i < deg && t[i] == d
55 })
56
57 g.degree = func(v int) int { return deg }
58
59 g.visit = func(v int, a int, do func(w int, c int64) bool) (aborted bool) {
60 start := sort.SearchInts(t, n-v+a)
61 for i := start; i < deg; i++ {
62 if do(v+t[i]-n, 0) {
63 return true
64 }
65 }
66 start = sort.SearchInts(t, a-v)
67 stop := sort.SearchInts(t, n-v)
68 for i := start; i < stop; i++ {
69 if do(v+t[i], 0) {
70 return true
71 }
72 }
73 return
74 }
75 return g
76 }