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

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

# -*- coding: utf-8 -*- 

r""" 

Intersection graphs 

 

The methods defined here appear in :mod:`sage.graphs.graph_generators`. 

""" 

 

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

# Copyright (C) 2006 Robert L. Miller <rlmillster@gmail.com> 

# Copyright (C) 2006 Emily A. Kirkman 

# Copyright (C) 2009 Michael C. Yurko <myurko@gmail.com> 

# 

# 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 __future__ import absolute_import, print_function 

from six.moves import range 

from six import itervalues 

 

# import from Sage library 

from sage.graphs.graph import Graph 

 

 

def IntervalGraph(intervals, points_ordered=False): 

r""" 

Return the graph corresponding to the given intervals. 

 

An interval graph is built from a list `(a_i,b_i)_{1\leq i \leq n}` of 

intervals : to each interval of the list is associated one vertex, two 

vertices being adjacent if the two corresponding (closed) intervals 

intersect. 

 

INPUT: 

 

- ``intervals`` -- the list of pairs `(a_i,b_i)` defining the graph. 

 

- ``points_ordered`` -- states whether every interval `(a_i,b_i)` of 

`intervals` satisfies `a_i<b_i`. If satisfied then setting 

``points_ordered`` to ``True`` will speed up the creation of the graph. 

 

.. NOTE:: 

 

* The vertices are named 0, 1, 2, and so on. The intervals used 

to create the graph are saved with the graph and can be recovered 

using ``get_vertex()`` or ``get_vertices()``. 

 

EXAMPLES: 

 

The following line creates the sequence of intervals 

`(i, i+2)` for i in `[0, ..., 8]`:: 

 

sage: intervals = [(i,i+2) for i in range(9)] 

 

In the corresponding graph :: 

 

sage: g = graphs.IntervalGraph(intervals) 

sage: g.get_vertex(3) 

(3, 5) 

sage: neigh = g.neighbors(3) 

sage: for v in neigh: print(g.get_vertex(v)) 

(1, 3) 

(2, 4) 

(4, 6) 

(5, 7) 

 

The is_interval() method verifies that this graph is an interval graph. :: 

 

sage: g.is_interval() 

True 

 

The intervals in the list need not be distinct. :: 

 

sage: intervals = [ (1,2), (1,2), (1,2), (2,3), (3,4) ] 

sage: g = graphs.IntervalGraph(intervals,True) 

sage: g.clique_maximum() 

[0, 1, 2, 3] 

sage: g.get_vertices() 

{0: (1, 2), 1: (1, 2), 2: (1, 2), 3: (2, 3), 4: (3, 4)} 

 

The endpoints of the intervals are not ordered we get the same graph 

(except for the vertex labels). :: 

 

sage: rev_intervals = [ (2,1), (2,1), (2,1), (3,2), (4,3) ] 

sage: h = graphs.IntervalGraph(rev_intervals,False) 

sage: h.get_vertices() 

{0: (2, 1), 1: (2, 1), 2: (2, 1), 3: (3, 2), 4: (4, 3)} 

sage: g.edges() == h.edges() 

True 

""" 

intervals = list(intervals) 

n = len(intervals) 

g = Graph(n) 

 

if points_ordered: 

for i in range(n-1): 

li,ri = intervals[i] 

for j in range(i+1,n): 

lj,rj = intervals[j] 

if ri < lj or rj < li: 

continue 

g.add_edge(i,j) 

else: 

for i in range(n-1): 

I = intervals[i] 

for j in range(i+1,n): 

J = intervals[j] 

if max(I) < min(J) or max(J) < min(I): 

continue 

g.add_edge(i,j) 

 

rep = dict(zip(range(n), intervals)) 

g.set_vertices(rep) 

 

return g 

 

 

def PermutationGraph(second_permutation, first_permutation = None): 

r""" 

Build a permutation graph from one permutation or from two lists. 

 

Definition: 

 

If `\sigma` is a permutation of `\{ 1, 2, \ldots, n \}`, then the 

permutation graph of `\sigma` is the graph on vertex set 

`\{ 1, 2, \ldots, n \}` in which two vertices `i` and `j` satisfying 

`i < j` are connected by an edge if and only if 

`\sigma^{-1}(i) > \sigma^{-1}(j)`. A visual way to construct this 

graph is as follows: 

 

Take two horizontal lines in the euclidean plane, and mark points 

`1, ..., n` from left to right on the first of them. On the second 

one, still from left to right, mark `n` points 

`\sigma(1), \sigma(2), \ldots, \sigma(n)`. 

Now, link by a segment the two points marked with `1`, then link 

together the points marked with `2`, and so on. The permutation 

graph of `\sigma` is the intersection graph of those segments: there 

exists a vertex in this graph for each element from `1` to `n`, two 

vertices `i, j` being adjacent if the segments `i` and `j` cross 

each other. 

 

The set of edges of the permutation graph can thus be identified with 

the set of inversions of the inverse of the given permutation 

`\sigma`. 

 

A more general notion of permutation graph can be defined as 

follows: If `S` is a set, and `(a_1, a_2, \ldots, a_n)` and 

`(b_1, b_2, \ldots, b_n)` are two lists of elements of `S`, each of 

which lists contains every element of `S` exactly once, then the 

permutation graph defined by these two lists is the graph on the 

vertex set `S` in which two vertices `i` and `j` are connected by an 

edge if and only if the order in which these vertices appear in the 

list `(a_1, a_2, \ldots, a_n)` is the opposite of the order in which 

they appear in the list `(b_1, b_2, \ldots, b_n)`. When 

`(a_1, a_2, \ldots, a_n) = (1, 2, \ldots, n)`, this graph is the 

permutation graph of the permutation 

`(b_1, b_2, \ldots, b_n) \in S_n`. Notice that `S` does not have to 

be a set of integers here, but can be a set of strings, tuples, or 

anything else. We can still use the above visual description to 

construct the permutation graph, but now we have to mark points 

`a_1, a_2, \ldots, a_n` from left to right on the first horizontal 

line and points `b_1, b_2, \ldots, b_n` from left to right on the 

second horizontal line. 

 

INPUT: 

 

- ``second_permutation`` -- the unique permutation/list defining the graph, 

or the second of the two (if the graph is to be built from two 

permutations/lists). 

 

- ``first_permutation`` (optional) -- the first of the two 

permutations/lists from which the graph should be built, if it is to be 

built from two permutations/lists. 

 

When ``first_permutation is None`` (default), it is set to be equal to 

``sorted(second_permutation)``, which yields the expected ordering when 

the elements of the graph are integers. 

 

.. SEEALSO:: 

 

- Recognition of Permutation graphs in the :mod:`comparability module 

<sage.graphs.comparability>`. 

 

- Drawings of permutation graphs as intersection graphs of segments is 

possible through the 

:meth:`~sage.combinat.permutation.Permutation.show` method of 

:class:`~sage.combinat.permutation.Permutation` objects. 

 

The correct argument to use in this case is ``show(representation = 

"braid")``. 

 

- :meth:`~sage.combinat.permutation.Permutation.inversions` 

 

EXAMPLES:: 

 

sage: p = Permutations(5).random_element() 

sage: PG = graphs.PermutationGraph(p) 

sage: edges = PG.edges(labels=False) 

sage: set(edges) == set(p.inverse().inversions()) 

True 

 

sage: PG = graphs.PermutationGraph([3,4,5,1,2]) 

sage: sorted(PG.edges()) 

[(1, 3, None), 

(1, 4, None), 

(1, 5, None), 

(2, 3, None), 

(2, 4, None), 

(2, 5, None)] 

sage: PG = graphs.PermutationGraph([3,4,5,1,2], [1,4,2,5,3]) 

sage: sorted(PG.edges()) 

[(1, 3, None), 

(1, 4, None), 

(1, 5, None), 

(2, 3, None), 

(2, 5, None), 

(3, 4, None), 

(3, 5, None)] 

sage: PG = graphs.PermutationGraph([1,4,2,5,3], [3,4,5,1,2]) 

sage: sorted(PG.edges()) 

[(1, 3, None), 

(1, 4, None), 

(1, 5, None), 

(2, 3, None), 

(2, 5, None), 

(3, 4, None), 

(3, 5, None)] 

 

sage: PG = graphs.PermutationGraph(Permutation([1,3,2]), Permutation([1,2,3])) 

sage: sorted(PG.edges()) 

[(2, 3, None)] 

 

sage: graphs.PermutationGraph([]).edges() 

[] 

sage: graphs.PermutationGraph([], []).edges() 

[] 

 

sage: PG = graphs.PermutationGraph("graph", "phrag") 

sage: sorted(PG.edges()) 

[('a', 'g', None), 

('a', 'h', None), 

('a', 'p', None), 

('g', 'h', None), 

('g', 'p', None), 

('g', 'r', None), 

('h', 'r', None), 

('p', 'r', None)] 

 

TESTS:: 

 

sage: graphs.PermutationGraph([1, 2, 3], [4, 5, 6]) 

Traceback (most recent call last): 

... 

ValueError: The two permutations do not contain the same set of elements ... 

""" 

if first_permutation is None: 

first_permutation = sorted(second_permutation) 

else: 

if set(second_permutation) != set(first_permutation): 

raise ValueError("The two permutations do not contain the same "+ 

"set of elements ! It is going to be pretty "+ 

"hard to define a permutation graph from that !") 

 

vertex_to_index = {} 

for i, v in enumerate(first_permutation): 

vertex_to_index[v] = i+1 

 

from sage.combinat.permutation import Permutation 

p2 = Permutation([vertex_to_index[x] for x in second_permutation]) 

p2 = p2.inverse() 

 

g = Graph(name="Permutation graph for "+str(second_permutation)) 

g.add_vertices(second_permutation) 

 

for u,v in p2.inversions(): 

g.add_edge(first_permutation[u-1], first_permutation[v-1]) 

 

return g 

 

def ToleranceGraph(tolrep): 

r""" 

Returns the graph generated by the tolerance representation ``tolrep``. 

 

The tolerance representation ``tolrep`` is described by the list 

`((l_0,r_0,t_0), (l_1,r_1,t_1), ..., (l_k,r_k,t_k))` where `I_i = (l_i,r_i)` 

denotes a closed interval on the real line with `l_i < r_i` and `t_i` a 

positive value, called tolerance. This representation generates the 

tolerance graph with the vertex set {0,1, ..., k} and the edge set `{(i,j): 

|I_i \cap I_j| \ge \min{t_i, t_j}}` where `|I_i \cap I_j|` denotes the 

length of the intersection of `I_i` and `I_j`. 

 

INPUT: 

 

- ``tolrep`` -- list of triples `(l_i,r_i,t_i)` where `(l_i,r_i)` denotes a 

closed interval on the real line and `t_i` a positive value. 

 

.. NOTE:: 

 

The vertices are named 0, 1, ..., k. The tolerance representation used 

to create the graph is saved with the graph and can be recovered using 

``get_vertex()`` or ``get_vertices()``. 

 

EXAMPLES: 

 

The following code creates a tolerance representation ``tolrep``, generates 

its tolerance graph ``g``, and applies some checks:: 

 

sage: tolrep = [(1,4,3),(1,2,1),(2,3,1),(0,3,3)] 

sage: g = graphs.ToleranceGraph(tolrep) 

sage: g.get_vertex(3) 

(0, 3, 3) 

sage: neigh = g.neighbors(3) 

sage: for v in neigh: print(g.get_vertex(v)) 

(1, 2, 1) 

(2, 3, 1) 

sage: g.is_interval() 

False 

sage: g.is_weakly_chordal() 

True 

 

The intervals in the list need not be distinct :: 

 

sage: tolrep2 = [(0,4,5),(1,2,1),(2,3,1),(0,4,5)] 

sage: g2 = graphs.ToleranceGraph(tolrep2) 

sage: g2.get_vertices() 

{0: (0, 4, 5), 1: (1, 2, 1), 2: (2, 3, 1), 3: (0, 4, 5)} 

sage: g2.is_isomorphic(g) 

True 

 

Real values are also allowed :: 

 

sage: tolrep = [(0.1,3.3,4.4),(1.1,2.5,1.1),(1.4,4.4,3.3)] 

sage: g = graphs.ToleranceGraph(tolrep) 

sage: g.is_isomorphic(graphs.PathGraph(3)) 

True 

 

TESTS: 

 

Giving negative third value:: 

 

sage: tolrep = [(0.1,3.3,-4.4),(1.1,2.5,1.1),(1.4,4.4,3.3)] 

sage: g = graphs.ToleranceGraph(tolrep) 

Traceback (most recent call last): 

... 

ValueError: Invalid tolerance representation at position 0; third value must be positive! 

""" 

n = len(tolrep) 

 

for i in range(n): 

if tolrep[i][2] <= 0: 

raise ValueError("Invalid tolerance representation at position "+str(i)+"; third value must be positive!") 

 

g = Graph(n) 

 

for i in range(n-1): 

li,ri,ti = tolrep[i] 

for j in range(i+1,n): 

lj,rj,tj = tolrep[j] 

if min(ri,rj) - max(li,lj) >= min(ti,tj): 

g.add_edge(i,j) 

 

rep = dict( zip(range(n),tolrep) ) 

g.set_vertices(rep) 

 

return g 

 

def OrthogonalArrayBlockGraph(k,n,OA=None): 

r""" 

Returns the graph of an `OA(k,n)`. 

 

The intersection graph of the blocks of a transversal design with parameters 

`(k,n)`, or `TD(k,n)` for short, is a strongly regular graph (unless it is a 

complete graph). Its parameters `(v,k',\lambda,\mu)` are determined by the 

parameters `k,n` via: 

 

.. MATH:: 

 

v=n^2, k'=k(n-1), \lambda=(k-1)(k-2)+n-2, \mu=k(k-1) 

 

As transversal designs and orthogonal arrays (OA for short) are equivalent 

objects, this graph can also be built from the blocks of an `OA(k,n)`, two 

of them being adjacent if one of their coordinates match. 

 

For more information on these graphs, see `Andries Brouwer's page 

on Orthogonal Array graphs <www.win.tue.nl/~aeb/graphs/OA.html>`_. 

 

.. WARNING:: 

 

- Brouwer's website uses the notation `OA(n,k)` instead of `OA(k,n)` 

 

- For given parameters `k` and `n` there can be many `OA(k,n)` : the 

graphs returned are not uniquely defined by their parameters (see the 

examples below). 

 

- If the function is called only with the parameter ``k`` and ``n`` the 

results might be different with two versions of Sage, or even worse : 

some could not be available anymore. 

 

.. SEEALSO:: 

 

:mod:`sage.combinat.designs.orthogonal_arrays` 

 

INPUT: 

 

- ``k,n`` (integers) 

 

- ``OA`` -- An orthogonal array. If set to ``None`` (default) then 

:func:`~sage.combinat.designs.orthogonal_arrays.orthogonal_array` is 

called to compute an `OA(k,n)`. 

 

EXAMPLES:: 

 

sage: G = graphs.OrthogonalArrayBlockGraph(5,5); G 

OA(5,5): Graph on 25 vertices 

sage: G.is_strongly_regular(parameters=True) 

(25, 20, 15, 20) 

sage: G = graphs.OrthogonalArrayBlockGraph(4,10); G 

OA(4,10): Graph on 100 vertices 

sage: G.is_strongly_regular(parameters=True) 

(100, 36, 14, 12) 

 

Two graphs built from different orthogonal arrays are also different:: 

 

sage: k=4;n=10 

sage: OAa = designs.orthogonal_arrays.build(k,n) 

sage: OAb = [[(x+1)%n for x in R] for R in OAa] 

sage: set(map(tuple,OAa)) == set(map(tuple,OAb)) 

False 

sage: Ga = graphs.OrthogonalArrayBlockGraph(k,n,OAa) 

sage: Gb = graphs.OrthogonalArrayBlockGraph(k,n,OAb) 

sage: Ga == Gb 

False 

 

As ``OAb`` was obtained from ``OAa`` by a relabelling the two graphs are 

isomorphic:: 

 

sage: Ga.is_isomorphic(Gb) 

True 

 

But there are examples of `OA(k,n)` for which the resulting graphs are not 

isomorphic:: 

 

sage: oa0 = [[0, 0, 1], [0, 1, 3], [0, 2, 0], [0, 3, 2], 

....: [1, 0, 3], [1, 1, 1], [1, 2, 2], [1, 3, 0], 

....: [2, 0, 0], [2, 1, 2], [2, 2, 1], [2, 3, 3], 

....: [3, 0, 2], [3, 1, 0], [3, 2, 3], [3, 3, 1]] 

sage: oa1 = [[0, 0, 1], [0, 1, 0], [0, 2, 3], [0, 3, 2], 

....: [1, 0, 3], [1, 1, 2], [1, 2, 0], [1, 3, 1], 

....: [2, 0, 0], [2, 1, 1], [2, 2, 2], [2, 3, 3], 

....: [3, 0, 2], [3, 1, 3], [3, 2, 1], [3, 3, 0]] 

sage: g0 = graphs.OrthogonalArrayBlockGraph(3,4,oa0) 

sage: g1 = graphs.OrthogonalArrayBlockGraph(3,4,oa1) 

sage: g0.is_isomorphic(g1) 

False 

 

But nevertheless isospectral:: 

 

sage: g0.spectrum() 

[9, 1, 1, 1, 1, 1, 1, 1, 1, 1, -3, -3, -3, -3, -3, -3] 

sage: g1.spectrum() 

[9, 1, 1, 1, 1, 1, 1, 1, 1, 1, -3, -3, -3, -3, -3, -3] 

 

Note that the graph ``g0`` is actually isomorphic to the affine polar graph 

`VO^+(4,2)`:: 

 

sage: graphs.AffineOrthogonalPolarGraph(4,2,'+').is_isomorphic(g0) 

True 

 

TESTS:: 

 

sage: G = graphs.OrthogonalArrayBlockGraph(4,6) 

Traceback (most recent call last): 

... 

NotImplementedError: I don't know how to build an OA(4,6)! 

sage: G = graphs.OrthogonalArrayBlockGraph(8,2) 

Traceback (most recent call last): 

... 

ValueError: There is no OA(8,2). Beware, Brouwer's website uses OA(n,k) instead of OA(k,n) ! 

""" 

if n>1 and k>=n+2: 

raise ValueError("There is no OA({},{}). Beware, Brouwer's website uses OA(n,k) instead of OA(k,n) !".format(k,n)) 

 

from itertools import combinations 

 

if OA is None: 

from sage.combinat.designs.orthogonal_arrays import orthogonal_array 

OA = orthogonal_array(k,n) 

else: 

assert len(OA) == n**2 

assert n == 0 or k == len(OA[0]) 

 

OA = map(tuple,OA) 

 

d = [[[] for j in range(n)] for i in range(k)] 

for R in OA: 

for i,x in enumerate(R): 

d[i][x].append(R) 

 

g = Graph() 

for l in d: 

for ll in l: 

g.add_edges(combinations(ll,2)) 

 

g.name("OA({},{})".format(k,n)) 

 

return g 

 

def IntersectionGraph(S): 

r""" 

Returns the intersection graph of the family `S` 

 

The intersection graph of a family `S` is a graph `G` with `V(G)=S` such 

that two elements `s_1,s_2\in S` are adjacent in `G` if and only if `s_1\cap 

s_2\neq \emptyset`. 

 

INPUT: 

 

- ``S`` -- a list of sets/tuples/iterables 

 

.. NOTE:: 

 

The elements of `S` must be finite, hashable, and the elements of 

any `s\in S` must be hashable too. 

 

EXAMPLES:: 

 

sage: graphs.IntersectionGraph([(1,2,3),(3,4,5),(5,6,7)]) 

Intersection Graph: Graph on 3 vertices 

 

TESTS:: 

 

sage: graphs.IntersectionGraph([(1,2,[1])]) 

Traceback (most recent call last): 

... 

TypeError: The elements of S must be hashable, and this one is not: (1, 2, [1]) 

""" 

from itertools import combinations 

 

for s in S: 

try: 

hash(s) 

except TypeError: 

raise TypeError("The elements of S must be hashable, and this one is not: {}".format(s)) 

 

ground_set_to_sets = {} 

for s in S: 

for x in s: 

if x not in ground_set_to_sets: 

ground_set_to_sets[x] = [] 

ground_set_to_sets[x].append(s) 

 

g = Graph(name="Intersection Graph") 

g.add_vertices(S) 

for clique in itervalues(ground_set_to_sets): 

g.add_clique(set(clique)) 

 

return g