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

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

# cython: binding=True 

r""" 

Weakly chordal graphs 

  

This module deals with everything related to weakly chordal graphs. It currently 

contains the following functions: 

  

.. csv-table:: 

:class: contentstable 

:widths: 30, 70 

:delim: | 

  

:meth:`~sage.graphs.weakly_chordal.is_long_hole_free` | Tests whether ``g`` contains an induced cycle of length at least 5. 

:meth:`~sage.graphs.weakly_chordal.is_long_antihole_free` | Tests whether ``g`` contains an induced anticycle of length at least 5. 

:meth:`~sage.graphs.weakly_chordal.is_weakly_chordal` | Tests whether ``g`` is weakly chordal. 

  

Author: 

  

- Birk Eisermann (initial implementation) 

- Nathann Cohen (some doc and optimization) 

  

REFERENCES: 

  

.. [NikolopoulosPalios07] Nikolopoulos, S.D. and Palios, L. 

Detecting holes and antiholes in graphs 

Algorithmica, 2007 

Vol. 47, number 2, pages 119--138 

http://www.cs.uoi.gr/~stavros/C-Papers/C-2004-SODA.pdf 

  

  

  

Methods 

------- 

""" 

  

############################################################################## 

# Copyright (C) 2012 Birk Eisermann <eisermbi@fastmail.fm> 

# Distributed under the terms of the GNU General Public License (GPL) 

# The full text of the GPL is available at: 

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

############################################################################## 

  

include "sage/data_structures/bitset.pxi" 

  

cdef inline int has_edge(bitset_t bs, int u, int v, int n): 

return bitset_in(bs, u*n+v) 

  

  

def is_long_hole_free(g, certificate=False): 

r""" 

Tests whether ``g`` contains an induced cycle of length at least 5. 

  

INPUT: 

  

- ``certificate`` -- boolean (default: ``False``) 

  

Whether to return a certificate. When ``certificate = True``, then 

the function returns 

  

* ``(True, [])`` if ``g`` does not contain such a cycle. 

For this case, it is not known how to provide a certificate. 

* ``(False, Hole)`` if ``g`` contains an induced cycle of length at 

least 5. ``Hole`` returns this cycle. 

  

If ``certificate = False``, the function returns just ``True`` or 

``False`` accordingly. 

  

ALGORITHM: 

  

This algorithm tries to find a cycle in the graph of all induced `P_4` of 

`g`, where two copies `P` and `P'` of `P_4` are adjacent if there exists a 

(not necessarily induced) copy of `P_5=u_1u_2u_3u_4u_5` such that 

`P=u_1u_2u_3u_4` and `P'=u_2u_3u_4u_5`. 

  

This is done through a depth-first-search. For efficiency, the auxiliary 

graph is constructed on-the-fly and never stored in memory. 

  

The run time of this algorithm is `O(m^2)` [NikolopoulosPalios07]_ ( where 

`m` is the number of edges of the graph ) . 

  

EXAMPLES: 

  

The Petersen Graph contains a hole:: 

  

sage: g = graphs.PetersenGraph() 

sage: g.is_long_hole_free() 

False 

  

The following graph contains a hole, which we want to display:: 

  

sage: g = graphs.FlowerSnark() 

sage: r,h = g.is_long_hole_free(certificate=True) 

sage: r 

False 

sage: Graph(h).is_isomorphic(graphs.CycleGraph(h.order())) 

True 

  

TESTS: 

  

Another graph with vertices 2, ..., 8, 10:: 

  

sage: g = Graph({2:[3,8],3:[2,4],4:[3,8,10],5:[6,10],6:[5,7],7:[6,8],8:[2,4,7,10],10:[4,5,8]}) 

sage: r,hole = g.is_long_hole_free(certificate=True) 

sage: r 

False 

sage: hole 

Subgraph of (): Graph on 5 vertices 

sage: hole.is_isomorphic(graphs.CycleGraph(hole.order())) 

True 

  

sage: graphs.EmptyGraph().is_long_hole_free() 

True 

""" 

g._scream_if_not_simple() 

  

if g.order() == 0: 

return (True, []) if certificate else True 

  

cdef int a,b,c,i,u,v,d 

  

if g.is_immutable(): 

g = g.copy(immutable=False) 

  

# relabel the graph on 0...n-1 

cdef dict label_id = g.relabel(return_map = True) 

cdef dict id_label = {idd:label for label, idd in label_id.iteritems()} 

  

# A dense copy of our graph 

cdef bitset_t dense_graph 

cdef int n = g.order() 

bitset_init(dense_graph, n*n) 

bitset_set_first_n(dense_graph, 0) 

for u,v in g.edges(labels = False): 

bitset_add(dense_graph,u*n+v) 

bitset_add(dense_graph,v*n+u) 

  

InPath = {} #vertices of the current path with their position (InPath[v] = i) 

VisitedP3 = {} #stores triples (u,v,w) which represent visited paths of length 3 

  

  

def process(a,b,c,i): 

InPath[c] = i # c is the (i+1)-th vertex at position i 

VisitedP3[a,b,c] = True 

VisitedP3[c,b,a] = True 

  

for d in g.neighbor_iterator(c): 

if not has_edge(dense_graph,d,a,n) and not has_edge(dense_graph,d,b,n): 

# a-b-c-d form an induced path P_4 

  

if d in InPath: 

# d is already contained in InPath 

# HOLE FOUND !!! 

if certificate: 

j = InPath[d] 

C = [v for v,vj in InPath.iteritems() if vj >= j] 

C.sort(key = lambda x: InPath[x]) 

C_index = {u:i for i,u in enumerate(C)} 

  

# At this step C[0]C[1]..... is a cycle such that any 4 

# consecutive vertices induce a P4. C may not be an 

# induced cycle, so we extract one from it. 

  

# To do so, we look for the *shortest* edge C[i]C[j] 

# between two nonconsecutive vertices of C, where the 

# length is the difference |i-j|. 

# 

# C[i]...C[j] is necessarily an induced cycle.⇧ 

  

gg = g.subgraph(C) 

gg.delete_edges(zip(C[:-1],C[1:])) 

  

abs = lambda x : x if x>0 else -x 

dist = lambda X : abs(C_index[X[0]]-C_index[X[1]]) 

  

u,v = min(gg.edges(labels = False), key = dist) 

u,v = C_index[u], C_index[v] 

  

# Return the answer, and relabel it on-the-fly with the 

# vertices' real name 

return False, [id_label[x] 

for x in C[min(u, v): max(u, v) + 1]] 

  

else: 

return False, None 

  

elif (b,c,d) not in VisitedP3: 

# search for another P_4 

res, hole_vertices = process(b,c,d,i+1) 

if not res: 

return False, hole_vertices 

  

del InPath[c] 

return True, [] 

  

  

# main algorithm 

# For all triples u,v,w of vertices such that uvw is a P_3 

for u in g: 

InPath[u] = 0 # u is the first vertex at position 0 

for vv,ww in g.edge_iterator(labels = False): 

for v,w in [(vv,ww),(ww,vv)]: 

if has_edge(dense_graph,u,v,n) and u!=w and not has_edge(dense_graph,u,w,n) and (u,v,w) not in VisitedP3: 

InPath[v] = 1 # v is the second vertex at position 1 

res,hole = process(u, v, w, 2) 

if not res: 

# We relabel the graph before returning the result 

g.relabel(id_label) 

# Free the dense graph 

bitset_free(dense_graph) 

  

if certificate: 

return False, g.subgraph(hole) 

else: 

return False 

del InPath[v] 

del InPath[u] 

  

# We relabel the graph before returning the result 

g.relabel(id_label) 

# Free the dense graph 

bitset_free(dense_graph) 

  

if certificate: 

return True, [] 

else: 

return True 

  

def is_long_antihole_free(g, certificate = False): 

r""" 

Tests whether the given graph contains an induced subgraph that is 

isomorphic to the complement of a cycle of length at least 5. 

  

INPUT: 

  

- ``certificate`` -- boolean (default: ``False``) 

  

Whether to return a certificate. When ``certificate = True``, then 

the function returns 

  

* ``(False, Antihole)`` if ``g`` contains an induced complement 

of a cycle of length at least 5 returned as ``Antihole``. 

* ``(True, [])`` if ``g`` does not contain an induced complement of 

a cycle of length at least 5. 

For this case it is not known how to provide a certificate. 

  

When ``certificate = False``, the function returns just ``True`` or 

``False`` accordingly. 

  

ALGORITHM: 

  

This algorithm tries to find a cycle in the graph of all induced 

`\overline{P_4}` of `g`, where two copies `\overline{P}` and `\overline{P'}` 

of `\overline{P_4}` are adjacent if there exists a (not necessarily induced) 

copy of `\overline{P_5}=u_1u_2u_3u_4u_5` such that 

`\overline{P}=u_1u_2u_3u_4` and `\overline{P'}=u_2u_3u_4u_5`. 

  

This is done through a depth-first-search. For efficiency, the auxiliary 

graph is constructed on-the-fly and never stored in memory. 

  

The run time of this algorithm is `O(m^2)` [NikolopoulosPalios07]_ ( where 

`m` is the number of edges of the graph ) . 

  

EXAMPLES: 

  

The Petersen Graph contains an antihole:: 

  

sage: g = graphs.PetersenGraph() 

sage: g.is_long_antihole_free() 

False 

  

The complement of a cycle is an antihole:: 

  

sage: g = graphs.CycleGraph(6).complement() 

sage: r,a = g.is_long_antihole_free(certificate=True) 

sage: r 

False 

sage: a.complement().is_isomorphic( graphs.CycleGraph(6) ) 

True 

  

TESTS: 

  

Further tests:: 

  

sage: g = Graph({0:[6,7],1:[7,8],2:[8,9],3:[9,10],4:[10,11],5:[11,6],6:[0,5,7],7:[0,1,6],8:[1,2,9],9:[2,3,8],10:[3,4,11],11:[4,5,10]}).complement() 

sage: r,a = g.is_long_antihole_free(certificate=True) 

sage: r 

False 

sage: a.complement().is_isomorphic( graphs.CycleGraph(9) ) 

True 

  

sage: graphs.EmptyGraph().is_long_hole_free() 

True 

""" 

g._scream_if_not_simple() 

  

if g.order() == 0: 

return (True, []) if certificate else True 

  

cdef int a,b,c,i,u,v,d 

  

if g.is_immutable(): 

g = g.copy(immutable=False) 

  

# relabel the graph on 0...n-1 

cdef dict label_id = g.relabel(return_map = True) 

cdef dict id_label = {idd:label for label, idd in label_id.iteritems()} 

  

# A dense copy of our graph 

cdef bitset_t dense_graph 

cdef int n = g.order() 

bitset_init(dense_graph, n*n) 

bitset_set_first_n(dense_graph, 0) 

for u,v in g.edges(labels = False): 

bitset_add(dense_graph,u*n+v) 

bitset_add(dense_graph,v*n+u) 

  

InPath = {} #vertices of the current path with their position (InPath[v] = i) 

VisitedP3 = {} #stores triples (u,v,w) which represent visited paths of length 3 

  

  

def process(a,b,c,k): 

InPath[c] = k # c is the (i+1)-th vertex at position i 

VisitedP3[a,c,b] = True 

VisitedP3[c,a,b] = True 

for d in g.neighbor_iterator(b): 

if has_edge(dense_graph,d,a,n) and not has_edge(dense_graph,d,c,n): 

if d in InPath: 

if certificate: #calculation of induced cycle in complement 

j = InPath[d] 

  

C = [v for v,vj in InPath.iteritems() if vj >= j] 

C.sort(key = lambda x: InPath[x]) 

C_index = {u:i for i,u in enumerate(C)} 

  

# At this step C[0]C[1]..... is an anticycle such that 

# any 4 consecutive vertices induce the complement of a 

# P_4. C may not be an induced anticycle, so we extract one 

# from it. 

  

# To do so, we look for the *shortest* nonedge C[i]C[j] 

# between two nonconsecutive vertices of C, where the 

# length is the difference |i-j|. 

# 

# C[i]...C[j] is necessarily an induced anticycle.⇧ 

  

gg = g.subgraph(C).complement() 

gg.delete_edges(zip(C[:-1],C[1:])) 

  

abs = lambda x : x if x>0 else -x 

dist = lambda X : abs(C_index[X[0]]-C_index[X[1]]) 

  

u,v = min(gg.edges(labels = False), key = dist) 

u,v = C_index[u], C_index[v] 

  

# Return the answer, and relabel it on-the-fly with the 

# vertices' real name 

return False, [id_label[x] 

for x in C[min(u, v): max(u, v) + 1]] 

  

else: 

return False, [] 

  

elif (b,d,c) not in VisitedP3: 

r,antihole = process(b,c,d,k+1) 

if not r: 

return False, antihole 

  

del InPath[c] 

return True, [] 

  

  

# main algorithm 

# For all triples u,v,w of vertices such that uvw is a complement of P_3 

for u in g: 

InPath[u] = 1 

for v,w in g.edge_iterator(labels = False): 

if not has_edge(dense_graph,u,v,n) and not has_edge(dense_graph,u,w,n) and (v,w,u) not in VisitedP3: 

InPath[v] = 0 

r,antihole = process(v, u, w, 2) 

if not r: 

# We relabel the graph before returning the result 

g.relabel(id_label) 

# Free the dense graph 

bitset_free(dense_graph) 

  

if certificate: 

return False, g.subgraph(antihole) 

else: 

return False 

del InPath[v] 

del InPath[u] 

  

# We relabel the graph before returning the result 

g.relabel(id_label) 

# Free the dense graph 

bitset_free(dense_graph) 

  

if certificate: 

return True, [] 

else: 

return True 

  

def is_weakly_chordal(g, certificate = False): 

r""" 

Tests whether the given graph is weakly chordal, i.e., the graph and its 

complement have no induced cycle of length at least 5. 

  

INPUT: 

  

- ``certificate`` -- Boolean value (default: ``False``) whether to 

return a certificate. If ``certificate = False``, return ``True`` or 

``False`` according to the graph. If ``certificate = True``, return 

  

* ``(False, forbidden_subgraph)`` when the graph contains a 

forbidden subgraph H, this graph is returned. 

* ``(True, [])`` when the graph is weakly chordal. 

For this case, it is not known how to provide a certificate. 

  

ALGORITHM: 

  

This algorithm checks whether the graph ``g`` or its complement 

contain an induced cycle of length at least 5. 

  

Using is_long_hole_free() and is_long_antihole_free() yields a run time 

of `O(m^2)` (where `m` is the number of edges of the graph). 

  

EXAMPLES: 

  

The Petersen Graph is not weakly chordal and contains a hole:: 

  

sage: g = graphs.PetersenGraph() 

sage: r,s = g.is_weakly_chordal(certificate = True) 

sage: r 

False 

sage: l = len(s.vertices()) 

sage: s.is_isomorphic( graphs.CycleGraph(l) ) 

True 

  

TESTS:: 

  

sage: graphs.EmptyGraph().is_weakly_chordal() 

True 

  

""" 

if g.order() == 0: 

return (True, []) if certificate else True 

  

if certificate: 

r,forbid_subgr = g.is_long_hole_free(certificate=True) 

if not r: 

return False, forbid_subgr 

  

return g.is_long_antihole_free(certificate=True) 

else: 

return g.is_long_hole_free() and g.is_long_antihole_free()