Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

# cython: binding=True 

""" 

Chromatic Polynomial 

  

AUTHORS: 

  

- Gordon Royle - original C implementation 

- Robert Miller - transplant 

  

REFERENCE: 

  

Ronald C Read, An improved method for computing the chromatic polynomials of 

sparse graphs. 

""" 

  

#***************************************************************************** 

# Copyright (C) 2008 Robert Miller 

# Copyright (C) 2008 Gordon Royle 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from cysignals.signals cimport sig_check 

  

from sage.libs.gmp.mpz cimport * 

from sage.rings.integer_ring import ZZ 

from sage.rings.integer cimport Integer 

from sage.ext.memory_allocator cimport MemoryAllocator 

from sage.misc.all import prod 

  

  

def chromatic_polynomial(G, return_tree_basis=False): 

""" 

Compute the chromatic polynomial of the graph G. 

  

The algorithm used is a recursive one, based on the following observations 

of Read: 

  

- The chromatic polynomial of a tree on n vertices is x(x-1)^(n-1). 

  

- If e is an edge of G, G' is the result of deleting the edge e, and G'' 

is the result of contracting e, then the chromatic polynomial of G is 

equal to that of G' minus that of G''. 

  

EXAMPLES:: 

  

sage: graphs.CycleGraph(4).chromatic_polynomial() 

x^4 - 4*x^3 + 6*x^2 - 3*x 

sage: graphs.CycleGraph(3).chromatic_polynomial() 

x^3 - 3*x^2 + 2*x 

sage: graphs.CubeGraph(3).chromatic_polynomial() 

x^8 - 12*x^7 + 66*x^6 - 214*x^5 + 441*x^4 - 572*x^3 + 423*x^2 - 133*x 

sage: graphs.PetersenGraph().chromatic_polynomial() 

x^10 - 15*x^9 + 105*x^8 - 455*x^7 + 1353*x^6 - 2861*x^5 + 4275*x^4 - 4305*x^3 + 2606*x^2 - 704*x 

sage: graphs.CompleteBipartiteGraph(3,3).chromatic_polynomial() 

x^6 - 9*x^5 + 36*x^4 - 75*x^3 + 78*x^2 - 31*x 

sage: for i in range(2,7): 

....: graphs.CompleteGraph(i).chromatic_polynomial().factor() 

(x - 1) * x 

(x - 2) * (x - 1) * x 

(x - 3) * (x - 2) * (x - 1) * x 

(x - 4) * (x - 3) * (x - 2) * (x - 1) * x 

(x - 5) * (x - 4) * (x - 3) * (x - 2) * (x - 1) * x 

sage: graphs.CycleGraph(5).chromatic_polynomial().factor() 

(x - 2) * (x - 1) * x * (x^2 - 2*x + 2) 

sage: graphs.OctahedralGraph().chromatic_polynomial().factor() 

(x - 2) * (x - 1) * x * (x^3 - 9*x^2 + 29*x - 32) 

sage: graphs.WheelGraph(5).chromatic_polynomial().factor() 

(x - 2) * (x - 1) * x * (x^2 - 5*x + 7) 

sage: graphs.WheelGraph(6).chromatic_polynomial().factor() 

(x - 3) * (x - 2) * (x - 1) * x * (x^2 - 4*x + 5) 

sage: C(x)=graphs.LCFGraph(24, [12,7,-7], 8).chromatic_polynomial() # long time (6s on sage.math, 2011) 

sage: C(2) # long time 

0 

  

By definition, the chromatic number of a graph G is the least integer k such that 

the chromatic polynomial of G is strictly positive at k:: 

  

sage: G = graphs.PetersenGraph() 

sage: P = G.chromatic_polynomial() 

sage: min(i for i in range(11) if P(i) > 0) == G.chromatic_number() 

True 

  

sage: G = graphs.RandomGNP(10,0.7) 

sage: P = G.chromatic_polynomial() 

sage: min(i for i in range(11) if P(i) > 0) == G.chromatic_number() 

True 

  

TESTS: 

  

Check that :trac:`21502` is solved:: 

  

sage: graphs.EmptyGraph().chromatic_polynomial() 

1 

""" 

if not G: 

R = ZZ['x'] 

return R.one() 

if not G.is_connected(): 

return prod([chromatic_polynomial(g) for g in G.connected_components_subgraphs()]) 

R = ZZ['x'] 

x = R.gen() 

if G.is_tree(): 

return x * (x - 1) ** (G.num_verts() - 1) 

  

cdef int nverts, nedges, i, j, u, v, top, bot, num_chords, next_v 

cdef int *queue 

cdef int *chords1 

cdef int *chords2 

cdef int *bfs_reorder 

cdef int *parent 

cdef mpz_t m, coeff 

cdef mpz_t *tot 

cdef mpz_t *coeffs 

G = G.relabel(inplace=False) 

G.remove_multiple_edges() 

G.remove_loops() 

nverts = G.num_verts() 

nedges = G.num_edges() 

  

cdef MemoryAllocator mem = MemoryAllocator() 

queue = <int *> mem.allocarray(nverts, sizeof(int)) 

chords1 = <int *> mem.allocarray((nedges - nverts + 1), sizeof(int)) 

chords2 = <int *> mem.allocarray((nedges - nverts + 1), sizeof(int)) 

parent = <int *> mem.allocarray(nverts, sizeof(int)) 

bfs_reorder = <int *> mem.allocarray(nverts, sizeof(int)) 

tot = <mpz_t *> mem.allocarray((nverts+1), sizeof(mpz_t)) 

coeffs = <mpz_t *> mem.allocarray((nverts+1), sizeof(mpz_t)) 

num_chords = 0 

  

# Breadth first search from 0: 

bfs_reorder[0] = 0 

mpz_init(tot[0]) # sets to 0 

for i from 0 < i < nverts: 

bfs_reorder[i] = -1 

mpz_init(tot[i]) # sets to 0 

mpz_init(tot[nverts]) # sets to 0 

queue[0] = 0 

top = 1 

bot = 0 

next_v = 1 

while top > bot: 

v = queue[bot] 

bot += 1 

for u in G.neighbor_iterator(v): 

if bfs_reorder[u] == -1: # if u is not yet in tree 

bfs_reorder[u] = next_v 

next_v += 1 

queue[top] = u 

top += 1 

parent[bfs_reorder[u]] = bfs_reorder[v] 

else: 

if bfs_reorder[u] > bfs_reorder[v]: 

chords1[num_chords] = bfs_reorder[u] 

chords2[num_chords] = bfs_reorder[v] 

else: 

continue 

i = num_chords 

num_chords += 1 

# bubble sort the chords 

while i > 0: 

if chords1[i-1] > chords1[i]: 

break 

if chords1[i-1] == chords1[i] and chords2[i-1] > chords2[i]: 

break 

j = chords1[i-1] 

chords1[i-1] = chords1[i] 

chords1[i] = j 

j = chords2[i-1] 

chords2[i-1] = chords2[i] 

chords2[i] = j 

i -= 1 

try: 

contract_and_count(chords1, chords2, num_chords, nverts, tot, parent) 

except BaseException: 

for i in range(nverts): 

mpz_clear(tot[i]) 

raise 

for i from 0 <= i <= nverts: 

mpz_init(coeffs[i]) # also sets them to 0 

mpz_init(coeff) 

mpz_init_set_si(m, -1) 

# start with the zero polynomial: f(x) = 0 

for i from nverts >= i > 0: 

if not mpz_sgn(tot[i]): 

continue 

mpz_neg(m, m) 

  

# do this: 

# f += tot[i]*m*x*(x-1)**(i-1) 

mpz_addmul(coeffs[i], m, tot[i]) 

mpz_set_si(coeff, 1) 

for j from 1 <= j < i: 

# an iterative method for binomial coefficients... 

mpz_mul_si(coeff, coeff, j-i) 

mpz_divexact_ui(coeff, coeff, j) 

# coeffs[i-j] += tot[i]*m*coeff 

mpz_mul(coeff, coeff, m) 

mpz_addmul(coeffs[i-j], coeff, tot[i]) 

mpz_mul(coeff, coeff, m) 

coeffs_ZZ = [] 

cdef Integer c_ZZ 

for i from 0 <= i <= nverts: 

c_ZZ = Integer(0) 

mpz_set(c_ZZ.value, coeffs[i]) 

coeffs_ZZ.append(c_ZZ) 

f = R(coeffs_ZZ) 

  

for i from 0 <= i <= nverts: 

mpz_clear(tot[i]) 

mpz_clear(coeffs[i]) 

  

mpz_clear(coeff) 

mpz_clear(m) 

  

return f 

  

  

cdef int contract_and_count(int *chords1, int *chords2, int num_chords, int nverts, 

mpz_t *tot, int *parent) except -1: 

if num_chords == 0: 

mpz_add_ui(tot[nverts], tot[nverts], 1) 

return 0 

cdef MemoryAllocator mem = MemoryAllocator() 

cdef int *new_chords1 = <int *> mem.allocarray(num_chords, sizeof(int)) 

cdef int *new_chords2 = <int *> mem.allocarray(num_chords, sizeof(int)) 

cdef int *ins_list1 = <int *> mem.allocarray(num_chords, sizeof(int)) 

cdef int *ins_list2 = <int *> mem.allocarray(num_chords, sizeof(int)) 

cdef int i, j, k, x1, xj, z, num, insnum, parent_checked 

for i in range(num_chords): 

sig_check() 

  

# contract chord i, and recurse 

z = chords1[i] 

x1 = chords2[i] 

j = i + 1 

insnum = 0 

parent_checked = 0 

while j < num_chords and chords1[j] == z: 

xj = chords2[j] 

if parent[z] > xj: 

parent_checked = 1 

# now try adding {x1, parent[z]} to the list 

if not parent[x1] == parent[z]: 

if x1 > parent[z]: 

ins_list1[insnum] = x1 

ins_list2[insnum] = parent[z] 

else: 

ins_list1[insnum] = parent[z] 

ins_list2[insnum] = x1 

insnum += 1 

if not parent[x1] == xj: # then {x1, xj} isn't already a tree edge 

ins_list1[insnum] = x1 

ins_list2[insnum] = xj 

insnum += 1 

j += 1 

if not parent_checked: 

if not parent[x1] == parent[z]: 

if x1 > parent[z]: 

ins_list1[insnum] = x1 

ins_list2[insnum] = parent[z] 

else: 

ins_list1[insnum] = parent[z] 

ins_list2[insnum] = x1 

insnum += 1 

  

# now merge new_chords and ins_list 

num = 0 

k = 0 

while k < insnum and j < num_chords: 

if chords1[j] > ins_list1[k] or \ 

(chords1[j] == ins_list1[k] and chords2[j] > ins_list2[k]): 

new_chords1[num] = chords1[j] 

new_chords2[num] = chords2[j] 

num += 1 

j += 1 

elif chords1[j] < ins_list1[k] or \ 

(chords1[j] == ins_list1[k] and chords2[j] < ins_list2[k]): 

new_chords1[num] = ins_list1[k] 

new_chords2[num] = ins_list2[k] 

num += 1 

k += 1 

else: 

new_chords1[num] = chords1[j] 

new_chords2[num] = chords2[j] 

num += 1 

j += 1 

k += 1 

if j == num_chords: 

while k < insnum: 

new_chords1[num] = ins_list1[k] 

new_chords2[num] = ins_list2[k] 

num += 1 

k += 1 

elif k == insnum: 

while j < num_chords: 

new_chords1[num] = chords1[j] 

new_chords2[num] = chords2[j] 

num += 1 

j += 1 

contract_and_count(new_chords1, new_chords2, num, nverts - 1, tot, parent) 

mpz_add_ui(tot[nverts], tot[nverts], 1)