-
Notifications
You must be signed in to change notification settings - Fork 0
/
clique.go
207 lines (192 loc) · 4.83 KB
/
clique.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package graph
import "math/rand"
import "github.com/Tom-Johnston/gigraph/sortints"
//cliqueData is a wrapper used in the CliqueNumber.
type cliqueData struct {
R []int
P []int
X []int
}
//AllMaximalCliques returns every maximal clique in g.
//This uses the Bron–Kerbosch algorithm with pivots chosen to reduce the number of branches at each point and currently doesn't use a vertex ordering for the first pass.
func AllMaximalCliques(g Graph, c chan []int) {
n := g.N()
R := make([]int, 0)
P := make([]int, n)
for i := range P {
P[i] = i
}
X := make([]int, 0)
var cd cliqueData
toCheck := make([]cliqueData, 1)
toCheck[0] = cliqueData{R, P, X}
for len(toCheck) > 0 {
cd, toCheck = toCheck[len(toCheck)-1], toCheck[:len(toCheck)-1]
P = cd.P
R = cd.R
X = cd.X
if len(P) == 0 && len(X) == 0 {
c <- R
continue
}
//Choose a pivot vertex
pivotVertex := -1
bestPivotSize := -1
pivotSize := 0
for _, v := range P {
pivotSize = 0
for _, u := range P {
//TODO Would this be nicer to be the intersection of the neighbourhoods so it is quicker for low degree vertices.
if u != v && g.IsEdge(u, v) {
pivotSize++
}
}
if pivotSize > bestPivotSize {
bestPivotSize = pivotSize
pivotVertex = v
}
}
for _, v := range X {
pivotSize = 0
for _, u := range P {
//TODO As above
if u != v && g.IsEdge(u, v) {
pivotSize++
}
}
if pivotSize > bestPivotSize {
bestPivotSize = pivotSize
pivotVertex = v
}
}
for i := len(P) - 1; i >= 0; i-- {
v := P[i]
if v != pivotVertex && g.IsEdge(v, pivotVertex) {
continue
}
tmpR := make([]int, len(R)+1)
tmpP := make([]int, 0, len(P))
tmpX := make([]int, 0, len(X)+1)
copy(tmpR, R)
tmpR[len(tmpR)-1] = v
for _, u := range P {
if u != v && g.IsEdge(u, v) {
tmpP = append(tmpP, u)
}
}
for _, u := range X {
if u != v && g.IsEdge(u, v) {
tmpX = append(tmpX, u)
}
}
toCheck = append(toCheck, cliqueData{tmpR, tmpP, tmpX})
P[i] = P[len(P)-1]
P = P[:len(P)-1]
X = append(X, v)
}
}
close(c)
return
}
//CliqueNumber returns the size of the largest clique in g.
//This effectively finds all maximal cliques by using the Bron–Kerbosch algorithm with pivots chosen to reduce the number of branches at each point. This doesn't currently use a vertex ordering for the first pass.
func CliqueNumber(g Graph) int {
n := g.N()
cliqueNumber := 0
R := make([]int, 0)
P := make([]int, n)
for i := range P {
P[i] = i
}
X := make([]int, 0)
var cd cliqueData
toCheck := make([]cliqueData, 1)
toCheck[0] = cliqueData{R, P, X}
for len(toCheck) > 0 {
cd, toCheck = toCheck[len(toCheck)-1], toCheck[:len(toCheck)-1]
P = cd.P
R = cd.R
X = cd.X
if len(P) == 0 && len(X) == 0 {
if cliqueNumber < len(R) {
cliqueNumber = len(R)
}
continue
}
//Choose a pivot vertex
pivotVertex := -1
bestPivotSize := -1
pivotSize := 0
for _, v := range P {
pivotSize = 0
for _, u := range P {
//TODO Would this be nicer to be the intersection of the neighbourhoods so it is quicker for low degree vertices.
if u != v && g.IsEdge(u, v) {
pivotSize++
}
}
if pivotSize > bestPivotSize {
bestPivotSize = pivotSize
pivotVertex = v
}
}
for _, v := range X {
pivotSize = 0
for _, u := range P {
//TODO As above
if u != v && g.IsEdge(u, v) {
pivotSize++
}
}
if pivotSize > bestPivotSize {
bestPivotSize = pivotSize
pivotVertex = v
}
}
for i := len(P) - 1; i >= 0; i-- {
v := P[i]
if v != pivotVertex && g.IsEdge(v, pivotVertex) {
continue
}
tmpR := make([]int, len(R)+1)
tmpP := make([]int, 0, len(P))
tmpX := make([]int, 0, len(X)+1)
copy(tmpR, R)
tmpR[len(tmpR)-1] = v
for _, u := range P {
if u != v && g.IsEdge(u, v) {
tmpP = append(tmpP, u)
}
}
for _, u := range X {
if u != v && g.IsEdge(u, v) {
tmpX = append(tmpX, u)
}
}
toCheck = append(toCheck, cliqueData{tmpR, tmpP, tmpX})
P[i] = P[len(P)-1]
P = P[:len(P)-1]
X = append(X, v)
}
}
return cliqueNumber
}
//IndependenceNumber returns the size of the largest independent set in g.
//The independence number of g is the clique number of the complement of g and this is how it is calculated here.
func IndependenceNumber(g Graph) int {
h := Complement(g)
return CliqueNumber(h)
}
//RandomMaximalClique builds a random maximal clique by iteratively choosing an allowed vertex uniformly at random and adding it to the clique.
func RandomMaximalClique(g Graph, seed int64) []int {
r := rand.New(rand.NewSource(seed))
clique := make([]int, 0)
options := sortints.Range(0, g.N(), 1)
for len(options) > 0 {
index := r.Intn(len(options))
option := options[index]
clique = append(clique, option)
options = sortints.Intersection(options, g.Neighbours(option))
}
return clique
}