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

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

r""" 

Independent sets 

  

This module implements the :class:`IndependentSets` class which can be used to : 

  

- List the independent sets (or cliques) of a graph 

- Count them (which is obviously faster) 

- Test whether a set of vertices is an independent set 

  

It can also be restricted to focus on (inclusionwise) maximal independent 

sets. See the documentation of :class:`IndependentSets` for actual examples. 

  

Classes and methods 

------------------- 

  

""" 

from __future__ import print_function 

  

include "sage/data_structures/binary_matrix.pxi" 

from sage.misc.cachefunc import cached_method 

from sage.graphs.base.static_dense_graph cimport dense_graph_init 

  

  

cdef inline int ismaximal(binary_matrix_t g, int n, bitset_t s): 

cdef int i 

for i in range(n): 

if (not bitset_in(s,i)) and bitset_are_disjoint(g.rows[i], s): 

return False 

  

return True 

  

cdef class IndependentSets: 

r""" 

The set of independent sets of a graph. 

  

For more information on independent sets, see 

:wikipedia:`Independent_set_(graph_theory)`. 

  

INPUT: 

  

- ``G`` -- a graph 

  

- ``maximal`` (boolean) -- whether to only consider (inclusionwise) maximal 

independent sets. Set to ``False`` by default. 

  

- ``complement`` (boolean) -- whether to consider the graph's complement 

(i.e. cliques instead of independent sets). Set to ``False`` by default. 

  

ALGORITHM: 

  

The enumeration of independent sets is done naively : given an independent 

set, this implementation considers all ways to add a new vertex to it 

(while keeping it an independent set), and then creates new independent 

sets from all those that were created this way. 

  

The implementation, however, is not recursive. 

  

.. NOTE:: 

  

This implementation of the enumeration of *maximal* independent sets is 

not much faster than NetworkX', which is surprising as it is written in 

Cython. This being said, the algorithm from NetworkX appears to be 

sligthly different from this one, and that would be a good thing to 

explore if one wants to improve the implementation. 

  

A simple generalization can also be done without too much modifications: 

iteration through independent sets with given size bounds 

(minimum and maximum number of vertices allowed). 

  

EXAMPLES: 

  

Listing all independent sets of the Claw graph:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: g = graphs.ClawGraph() 

sage: I = IndependentSets(g) 

sage: list(I) 

[[0], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3], []] 

  

Count them:: 

  

sage: I.cardinality() 

9 

  

List only the maximal independent sets:: 

  

sage: Im = IndependentSets(g, maximal = True) 

sage: list(Im) 

[[0], [1, 2, 3]] 

  

And count them:: 

  

sage: Im.cardinality() 

2 

  

One can easily count the number of independent sets of each 

cardinality:: 

  

sage: g = graphs.PetersenGraph() 

sage: number_of = [0] * g.order() 

sage: for x in IndependentSets(g): 

....: number_of[len(x)] += 1 

sage: number_of 

[1, 10, 30, 30, 5, 0, 0, 0, 0, 0] 

  

It is also possible to define an iterator over all independent sets of a 

given cardinality. Note, however, that Sage will generate them *all*, to 

return only those that satisfy the cardinality constraints. Getting the list 

of independent sets of size 4 in this way can thus take a very long time:: 

  

sage: is4 = (x for x in IndependentSets(g) if len(x) == 4) 

sage: list(is4) 

[[0, 2, 8, 9], [0, 3, 6, 7], [1, 3, 5, 9], [1, 4, 7, 8], [2, 4, 5, 6]] 

  

Given a subset of the vertices, it is possible to test whether it is an 

independent set:: 

  

sage: g = graphs.DurerGraph() 

sage: I = IndependentSets(g) 

sage: [0,2] in I 

True 

sage: [0,3,5] in I 

False 

  

If an element of the subset is not a vertex, then an error is raised:: 

  

sage: [0, 'a', 'b', 'c'] in I 

Traceback (most recent call last): 

... 

ValueError: a is not a vertex of the graph. 

""" 

def __init__(self, G, maximal = False, complement = False): 

r""" 

Constructor for this class 

  

TESTS:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: IndependentSets(graphs.PetersenGraph()) 

<sage.graphs.independent_sets.IndependentSets object... 

  

Compute the number of matchings, and check with Sage's implementation:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: from sage.graphs.matchpoly import matching_polynomial 

sage: def check_matching(G): 

....: number_of_matchings = sum(map(abs,matching_polynomial(G).coefficients(sparse=False))) 

....: if number_of_matchings != IndependentSets(G.line_graph()).cardinality(): 

....: print("Ooooch !") 

sage: for i in range(30): 

....: check_matching(graphs.RandomGNP(11,.3)) 

  

Compare the result with the output of :meth:`subgraph_search`:: 

  

sage: from sage.sets.set import Set 

sage: def check_with_subgraph_search(G): 

....: IS = set(map(Set,list(IndependentSets(G)))) 

....: if not all(G.subgraph(l).is_independent_set() for l in IS): 

....: print("Gloops") 

....: alpha = max(map(len,IS)) 

....: IS2 = [Set([x]) for x in range(G.order())] + [Set([])] 

....: for n in range(2,alpha+1): 

....: IS2.extend(map(Set,list(G.subgraph_search_iterator(Graph(n), induced = True)))) 

....: if len(IS) != len(set(IS2)): 

....: print("Oops") 

....: print(len(IS), len(set(IS2))) 

sage: for i in range(5): 

....: check_with_subgraph_search(graphs.RandomGNP(11,.3)) 

  

Empty graph:: 

  

sage: IS0 = IndependentSets(graphs.EmptyGraph()) 

sage: list(IS0) 

[[]] 

sage: IS0.cardinality() 

1 

""" 

cdef int i 

  

# Map from Vertex to Integer, and from Integer to Vertex 

self.vertices = G.vertices() 

self.n = G.order() 

self.maximal = maximal 

self.vertex_to_int = dense_graph_init(self.g, G, translation = True) 

  

# If we must consider the graph's complement instead 

if complement: 

binary_matrix_complement(self.g) 

for i in range(self.n): 

binary_matrix_set0(self.g,i,i) 

  

self.count_only = 0 

  

def __iter__(self): 

r""" 

Returns an iterator over the independent sets of self. 

  

TESTS:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: I = IndependentSets(graphs.PetersenGraph()) 

sage: iter1 = iter(I) 

sage: iter2 = iter(I) 

sage: next(iter1) # indirect doctest 

[0] 

sage: next(iter2) # indirect doctest 

[0] 

sage: next(iter2) 

[0, 2] 

sage: next(iter1) 

[0, 2] 

""" 

if self.n == 0: 

yield [] 

return 

  

cdef int i = 0 

  

cdef bitset_t current_set 

cdef bitset_t tmp 

bitset_init(current_set,self.n) 

bitset_set_first_n(current_set,0) 

bitset_add(current_set,0) 

bitset_init(tmp,self.n) 

  

cdef uint64_t count = 0 

cdef list ans 

cdef int j 

  

# At every moment of the algorithm current_set represents an independent 

# set, except for the ith bit. All bits >i are zero. 

  

while True: 

  

# If i is in current_set 

if bitset_in(current_set,i): 

  

# We have found an independent set ! 

if bitset_are_disjoint(self.g.rows[i], current_set): 

  

# Saving that set 

bitset_copy(tmp, current_set) 

  

# Preparing for the next set, except if we set the last bit. 

if i < self.n-1: 

  

# Adding (i+1)th bit 

bitset_add(current_set,i+1) 

i += 1 

else: 

bitset_discard(current_set,i) 

  

# Returning the result if necessary ... 

if self.maximal and not ismaximal(self.g,self.n, tmp): 

continue 

  

count += 1 

  

if not self.count_only: 

yield [self.vertices[j] for j in range(i+1) if bitset_in(tmp,j)] 

continue 

  

else: 

# Removing the ith bit 

bitset_discard(current_set, i) 

  

# Preparing for the next set ! 

if i < self.n-1: 

bitset_add(current_set, i+1) 

i += 1 

  

# Not already included in the set 

else: 

if i == 0: 

break 

  

# Going backward, we explored all we could there ! 

if bitset_in(current_set,i-1): 

bitset_discard(current_set, i-1) 

bitset_add(current_set,i) 

else: 

i -= 1 

if i == -1: 

break 

  

if not self.maximal: 

count += 1 

if not self.count_only: 

yield [] 

  

if self.count_only: 

yield count 

  

bitset_free(current_set) 

bitset_free(tmp) 

  

def __dealloc__(self): 

r""" 

Frees everything we ever allocated 

""" 

if self.g.rows != NULL: 

binary_matrix_free(self.g) 

  

@cached_method 

def cardinality(self): 

r""" 

Computes and returns the number of independent sets 

  

TESTS:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: IndependentSets(graphs.PetersenGraph()).cardinality() 

76 

  

Only maximal ones:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: IndependentSets(graphs.PetersenGraph(), maximal = True).cardinality() 

15 

""" 

if self.n == 0: 

return 1 

  

self.count_only = 1 

  

for i in self: 

pass 

  

self.count_only = 0 

  

from sage.rings.integer import Integer 

return Integer(i) 

  

def __contains__(self, S): 

r""" 

Checks whether the set is an independent set (possibly maximal) 

  

INPUT: 

  

- ``S`` -- a set of vertices to be tested. 

  

TESTS: 

  

All independent sets of PetersenGraph are... independent sets:: 

  

sage: from sage.graphs.independent_sets import IndependentSets 

sage: G = graphs.PetersenGraph() 

sage: IS = IndependentSets(graphs.PetersenGraph()) 

sage: all(s in IS for s in IS) 

True 

  

And only them are:: 

  

sage: IS2 = [x for x in subsets(G.vertices()) if x in IS] 

sage: sorted(IS) == sorted(IS2) 

True 

  

Same with maximal independent sets:: 

  

sage: IS = IndependentSets(graphs.PetersenGraph(), maximal = True) 

sage: S = Subsets(G.vertices()) 

sage: all(s in IS for s in IS) 

True 

sage: IS2 = [x for x in subsets(G.vertices()) if x in IS] 

sage: sorted(IS) == sorted(IS2) 

True 

""" 

# Set of vertices as a bitset 

cdef bitset_t s 

  

bitset_init(s,self.n) 

bitset_set_first_n(s,0) 

  

cdef int i 

for I in S: 

try: 

i = self.vertex_to_int[I] 

except KeyError: 

raise ValueError(str(I)+" is not a vertex of the graph.") 

  

# Adding the new vertex to s 

bitset_add(s, i) 

  

# Checking that the set s is independent 

if not bitset_are_disjoint(self.g.rows[i], s): 

return False 

  

if self.maximal and not ismaximal(self.g, self.n,s): 

return False 

  

return True