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

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

r""" 

Common Digraphs 

 

All digraphs in Sage can be built through the ``digraphs`` object. In order to 

build a circuit on 15 elements, one can do:: 

 

sage: g = digraphs.Circuit(15) 

 

To get a circulant graph on 10 vertices in which a vertex `i` has `i+2` and 

`i+3` as outneighbors:: 

 

sage: p = digraphs.Circulant(10,[2,3]) 

 

More interestingly, one can get the list of all digraphs that Sage knows how to 

build by typing ``digraphs.`` in Sage and then hitting tab. 

 

.. csv-table:: 

:class: contentstable 

:widths: 30, 70 

:delim: | 

 

:meth:`~DiGraphGenerators.ButterflyGraph` | Returns a n-dimensional butterfly graph. 

:meth:`~DiGraphGenerators.Circuit` | Returns the circuit on `n` vertices. 

:meth:`~DiGraphGenerators.Circulant` | Returns a circulant digraph on `n` vertices from a set of integers. 

:meth:`~DiGraphGenerators.Complete` | Return a complete digraph on `n` vertices. 

:meth:`~DiGraphGenerators.DeBruijn` | Returns the De Bruijn digraph with parameters `k,n`. 

:meth:`~DiGraphGenerators.GeneralizedDeBruijn` | Returns the generalized de Bruijn digraph of order `n` and degree `d`. 

:meth:`~DiGraphGenerators.ImaseItoh` | Returns the digraph of Imase and Itoh of order `n` and degree `d`. 

:meth:`~DiGraphGenerators.Kautz` | Returns the Kautz digraph of degree `d` and diameter `D`. 

:meth:`~DiGraphGenerators.Paley` | Return a Paley digraph on `q` vertices. 

:meth:`~DiGraphGenerators.Path` | Returns a directed path on `n` vertices. 

:meth:`~DiGraphGenerators.RandomDirectedGNC` | Returns a random GNC (growing network with copying) digraph with `n` vertices. 

:meth:`~DiGraphGenerators.RandomDirectedGNM` | Returns a random labelled digraph on `n` nodes and `m` arcs. 

:meth:`~DiGraphGenerators.RandomDirectedGNP` | Returns a random digraph on `n` nodes. 

:meth:`~DiGraphGenerators.RandomDirectedGN` | Returns a random GN (growing network) digraph with `n` vertices. 

:meth:`~DiGraphGenerators.RandomDirectedGNR` | Returns a random GNR (growing network with redirection) digraph. 

:meth:`~DiGraphGenerators.RandomSemiComplete` | Return a random semi-complete digraph of order `n`. 

:meth:`~DiGraphGenerators.RandomTournament` | Returns a random tournament on `n` vertices. 

:meth:`~DiGraphGenerators.TransitiveTournament`| Returns a transitive tournament on `n` vertices. 

:meth:`~DiGraphGenerators.tournaments_nauty` | Returns all tournaments on `n` vertices using Nauty. 

 

 

AUTHORS: 

 

- Robert L. Miller (2006) 

- Emily A. Kirkman (2006) 

- Michael C. Yurko (2009) 

- David Coudert (2012) 

 

Functions and methods 

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

 

""" 

 

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

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

# and Emily A. Kirkman 

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

# 

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

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

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

from __future__ import print_function, division 

from six.moves import range 

 

from math import sin, cos, pi 

from sage.misc.randstate import current_randstate 

from sage.graphs.digraph import DiGraph 

 

 

class DiGraphGenerators(): 

r""" 

A class consisting of constructors for several common digraphs, 

including orderly generation of isomorphism class representatives. 

 

A list of all graphs and graph structures in this database is 

available via tab completion. Type "digraphs." and then hit tab to 

see which graphs are available. 

 

The docstrings include educational information about each named 

digraph with the hopes that this class can be used as a reference. 

 

The constructors currently in this class include:: 

 

Random Directed Graphs: 

- RandomDirectedGN 

- RandomDirectedGNC 

- RandomDirectedGNP 

- RandomDirectedGNM 

- RandomDirectedGNR 

- RandomTournament 

- RandomSemiComplete 

 

Families of Graphs: 

- Complete 

- DeBruijn 

- GeneralizedDeBruijn 

- Kautz 

- Path 

- ImaseItoh 

- RandomTournament 

- TransitiveTournament 

- tournaments_nauty 

 

 

 

ORDERLY GENERATION: digraphs(vertices, property=lambda x: True, 

augment='edges', size=None) 

 

Accesses the generator of isomorphism class representatives. 

Iterates over distinct, exhaustive representatives. 

 

INPUT: 

 

 

- ``vertices`` - natural number or ``None`` to infinitely generate 

bigger and bigger digraphs. 

 

- ``property`` - any property to be tested on digraphs 

before generation. 

 

- ``augment`` - choices: 

 

- ``'vertices'`` - augments by adding a vertex, and 

edges incident to that vertex. In this case, all digraphs on *up to* 

n=vertices are generated. If for any digraph G satisfying the 

property, every subgraph, obtained from G by deleting one vertex 

and only edges incident to that vertex, satisfies the property, 

then this will generate all digraphs with that property. If this 

does not hold, then all the digraphs generated will satisfy the 

property, but there will be some missing. 

 

- ``'edges'`` - augments a fixed number of vertices by 

adding one edge In this case, all digraphs on *exactly* n=vertices 

are generated. If for any graph G satisfying the property, every 

subgraph, obtained from G by deleting one edge but not the vertices 

incident to that edge, satisfies the property, then this will 

generate all digraphs with that property. If this does not hold, 

then all the digraphs generated will satisfy the property, but 

there will be some missing. 

 

- ``implementation`` - which underlying implementation to use (see DiGraph?) 

 

- ``sparse`` - ignored if implementation is not ``c_graph`` 

 

EXAMPLES: Print digraphs on 2 or less vertices. 

 

:: 

 

sage: for D in digraphs(2, augment='vertices'): 

....: print(D) 

Digraph on 0 vertices 

Digraph on 1 vertex 

Digraph on 2 vertices 

Digraph on 2 vertices 

Digraph on 2 vertices 

 

Note that we can also get digraphs with underlying Cython implementation:: 

 

sage: for D in digraphs(2, augment='vertices', implementation='c_graph'): 

....: print(D) 

Digraph on 0 vertices 

Digraph on 1 vertex 

Digraph on 2 vertices 

Digraph on 2 vertices 

Digraph on 2 vertices 

 

Print digraphs on 3 vertices. 

 

:: 

 

sage: for D in digraphs(3): 

....: print(D) 

Digraph on 3 vertices 

Digraph on 3 vertices 

... 

Digraph on 3 vertices 

Digraph on 3 vertices 

 

Generate all digraphs with 4 vertices and 3 edges. 

 

:: 

 

sage: L = digraphs(4, size=3) 

sage: len(list(L)) 

13 

 

Generate all digraphs with 4 vertices and up to 3 edges. 

 

:: 

 

sage: L = list(digraphs(4, lambda G: G.size() <= 3)) 

sage: len(L) 

20 

sage: graphs_list.show_graphs(L) # long time 

 

Generate all digraphs with degree at most 2, up to 5 vertices. 

 

:: 

 

sage: property = lambda G: ( max([G.degree(v) for v in G] + [0]) <= 2 ) 

sage: L = list(digraphs(5, property, augment='vertices')) 

sage: len(L) 

75 

 

Generate digraphs on the fly: (see http://oeis.org/classic/A000273) 

 

:: 

 

sage: for i in range(5): 

....: print(len(list(digraphs(i)))) 

1 

1 

3 

16 

218 

 

REFERENCE: 

 

- Brendan D. McKay, Isomorph-Free Exhaustive generation. Journal 

of Algorithms Volume 26, Issue 2, February 1998, pages 306-324. 

""" 

 

def ButterflyGraph(self, n, vertices='strings'): 

""" 

Returns a n-dimensional butterfly graph. The vertices consist of 

pairs (v,i), where v is an n-dimensional tuple (vector) with binary 

entries (or a string representation of such) and i is an integer in 

[0..n]. A directed edge goes from (v,i) to (w,i+1) if v and w are 

identical except for possibly v[i] != w[i]. 

 

A butterfly graph has `(2^n)(n+1)` vertices and 

`n2^{n+1}` edges. 

 

INPUT: 

 

 

- ``vertices`` - 'strings' (default) or 'vectors', 

specifying whether the vertices are zero-one strings or actually 

tuples over GF(2). 

 

 

EXAMPLES:: 

 

sage: digraphs.ButterflyGraph(2).edges(labels=False) 

[(('00', 0), ('00', 1)), 

(('00', 0), ('10', 1)), 

(('00', 1), ('00', 2)), 

(('00', 1), ('01', 2)), 

(('01', 0), ('01', 1)), 

(('01', 0), ('11', 1)), 

(('01', 1), ('00', 2)), 

(('01', 1), ('01', 2)), 

(('10', 0), ('00', 1)), 

(('10', 0), ('10', 1)), 

(('10', 1), ('10', 2)), 

(('10', 1), ('11', 2)), 

(('11', 0), ('01', 1)), 

(('11', 0), ('11', 1)), 

(('11', 1), ('10', 2)), 

(('11', 1), ('11', 2))] 

sage: digraphs.ButterflyGraph(2,vertices='vectors').edges(labels=False) 

[(((0, 0), 0), ((0, 0), 1)), 

(((0, 0), 0), ((1, 0), 1)), 

(((0, 0), 1), ((0, 0), 2)), 

(((0, 0), 1), ((0, 1), 2)), 

(((0, 1), 0), ((0, 1), 1)), 

(((0, 1), 0), ((1, 1), 1)), 

(((0, 1), 1), ((0, 0), 2)), 

(((0, 1), 1), ((0, 1), 2)), 

(((1, 0), 0), ((0, 0), 1)), 

(((1, 0), 0), ((1, 0), 1)), 

(((1, 0), 1), ((1, 0), 2)), 

(((1, 0), 1), ((1, 1), 2)), 

(((1, 1), 0), ((0, 1), 1)), 

(((1, 1), 0), ((1, 1), 1)), 

(((1, 1), 1), ((1, 0), 2)), 

(((1, 1), 1), ((1, 1), 2))] 

""" 

# We could switch to Sage integers to handle arbitrary n. 

if vertices=='strings': 

if n>=31: 

raise NotImplementedError("vertices='strings' is only valid for n<=30.") 

from sage.graphs.generic_graph_pyx import int_to_binary_string 

butterfly = {} 

for v in range(2 ** n): 

for i in range(n): 

w = v 

w ^= (1 << i) # push 1 to the left by i and xor with w 

bv = int_to_binary_string(v) 

bw = int_to_binary_string(w) 

# pad and reverse the strings 

padded_bv = ('0'*(n-len(bv))+bv)[::-1] 

padded_bw = ('0'*(n-len(bw))+bw)[::-1] 

butterfly[(padded_bv,i)]=[(padded_bv,i+1), (padded_bw,i+1)] 

elif vertices=='vectors': 

from sage.modules.free_module import VectorSpace 

from sage.rings.finite_rings.finite_field_constructor import FiniteField 

from copy import copy 

butterfly = {} 

for v in VectorSpace(FiniteField(2),n): 

for i in range(n): 

w=copy(v) 

w[i] += 1 # Flip the ith bit 

# We must call tuple since vectors are mutable. To obtain 

# a vector from the tuple t, just call vector(t). 

butterfly[(tuple(v),i)]=[(tuple(v),i+1), (tuple(w),i+1)] 

else: 

raise NotImplementedError("vertices must be 'strings' or 'vectors'.") 

return DiGraph(butterfly) 

 

def Path(self,n): 

r""" 

Returns a directed path on `n` vertices. 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices in the path. 

 

EXAMPLES:: 

 

sage: g = digraphs.Path(5) 

sage: g.vertices() 

[0, 1, 2, 3, 4] 

sage: g.size() 

4 

sage: g.automorphism_group().cardinality() 

1 

""" 

g = DiGraph(n) 

g.name("Path") 

 

if n: 

g.add_path(list(range(n))) 

 

g.set_pos({i:(i,0) for i in range(n)}) 

return g 

 

def Paley(self, q): 

r""" 

Return a Paley digraph on `q` vertices. 

 

Parameter `q` must be the power of a prime number and congruent to 3 mod 

4. 

 

.. SEEALSO:: 

 

- :wikipedia:`Paley_graph` 

- :meth:`~sage.graphs.graph_generators.GraphGenerators.PaleyGraph` 

 

EXAMPLES: 

 

A Paley digraph has `n * (n-1) / 2` edges, its underlying graph is a 

clique, and so it is a tournament:: 

 

sage: g = digraphs.Paley(7); g 

Paley digraph with parameter 7: Digraph on 7 vertices 

sage: g.size() == g.order() * (g.order() - 1) / 2 

True 

sage: g.to_undirected().is_clique() 

True 

 

A Paley digraph is always self-complementary:: 

 

sage: g.complement().is_isomorphic(g) 

True 

 

TESTS: 

 

Wrong parameter:: 

 

sage: digraphs.Paley(6) 

Traceback (most recent call last): 

... 

ValueError: parameter q must be a prime power 

sage: digraphs.Paley(5) 

Traceback (most recent call last): 

... 

ValueError: parameter q must be congruent to 3 mod 4 

""" 

from sage.rings.finite_rings.integer_mod import mod 

from sage.rings.finite_rings.finite_field_constructor import FiniteField 

from sage.arith.all import is_prime_power 

if not is_prime_power(q): 

raise ValueError("parameter q must be a prime power") 

if not mod(q, 4) == 3: 

raise ValueError("parameter q must be congruent to 3 mod 4") 

g = DiGraph([FiniteField(q,'a'), lambda i,j: (i!=j) and (j-i).is_square()], 

loops=False, name="Paley digraph with parameter {}".format(q)) 

return g 

 

def TransitiveTournament(self, n): 

r""" 

Returns a transitive tournament on `n` vertices. 

 

In this tournament there is an edge from `i` to `j` if `i<j`. 

 

See :wikipedia:`Tournament_(graph_theory)` 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices in the tournament. 

 

EXAMPLES:: 

 

sage: g = digraphs.TransitiveTournament(5) 

sage: g.vertices() 

[0, 1, 2, 3, 4] 

sage: g.size() 

10 

sage: g.automorphism_group().cardinality() 

1 

 

.. SEEALSO:: 

 

- :wikipedia:`Tournament_(graph_theory)` 

- :meth:`~sage.graphs.digraph.DiGraph.is_tournament` 

- :meth:`~sage.graphs.digraph.DiGraph.is_transitive` 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.RandomTournament` 

 

TESTS:: 

 

sage: digraphs.TransitiveTournament(-1) 

Traceback (most recent call last): 

... 

ValueError: The number of vertices cannot be strictly negative! 

""" 

g = DiGraph(n) 

g.name("Transitive Tournament") 

 

for i in range(n-1): 

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

g.add_edge(i, j) 

 

if n: 

from sage.graphs.graph_plot import _circle_embedding 

_circle_embedding(g, list(range(n))) 

 

return g 

 

def RandomTournament(self, n): 

r""" 

Returns a random tournament on `n` vertices. 

 

For every pair of vertices, the tournament has an edge from 

`i` to `j` with probability `1/2`, otherwise it has an edge 

from `j` to `i`. 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices. 

 

EXAMPLES:: 

 

sage: T = digraphs.RandomTournament(10); T 

Random Tournament: Digraph on 10 vertices 

sage: T.size() == binomial(10, 2) 

True 

sage: T.is_tournament() 

True 

sage: digraphs.RandomTournament(-1) 

Traceback (most recent call last): 

... 

ValueError: The number of vertices cannot be strictly negative! 

 

.. SEEALSO:: 

 

- :wikipedia:`Tournament_(graph_theory)` 

- :meth:`~sage.graphs.digraph.DiGraph.is_tournament` 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.TransitiveTournament` 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.Complete` 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.RandomSemiComplete` 

""" 

from sage.misc.prandom import random 

g = DiGraph(n) 

g.name("Random Tournament") 

 

for i in range(n-1): 

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

if random() <= .5: 

g.add_edge(i, j) 

else: 

g.add_edge(j, i) 

 

if n: 

from sage.graphs.graph_plot import _circle_embedding 

_circle_embedding(g, list(range(n))) 

 

return g 

 

def tournaments_nauty(self, n, 

min_out_degree = None, max_out_degree = None, 

strongly_connected = False, debug=False, options=""): 

r""" 

Returns all tournaments on `n` vertices using Nauty. 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices. 

 

- ``min_out_degree``, ``max_out_degree`` (integers) -- if set to 

``None`` (default), then the min/max out-degree is not constrained. 

 

- ``debug`` (boolean) -- if ``True`` the first line of genbg's output to 

standard error is captured and the first call to the generator's 

``next()`` function will return this line as a string. A line leading 

with ">A" indicates a successful initiation of the program with some 

information on the arguments, while a line beginning with ">E" 

indicates an error with the input. 

 

- ``options`` (string) -- anything else that should be forwarded as 

input to Nauty's genbg. See its documentation for more information : 

`<http://cs.anu.edu.au/~bdm/nauty/>`_. 

 

 

.. NOTE:: 

 

To use this method you must first install the Nauty spkg. 

 

EXAMPLES:: 

 

sage: for g in digraphs.tournaments_nauty(4): 

....: print(g.edges(labels = False)) 

[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] 

[(1, 0), (1, 3), (2, 0), (2, 1), (3, 0), (3, 2)] 

[(0, 2), (1, 0), (2, 1), (3, 0), (3, 1), (3, 2)] 

[(0, 2), (0, 3), (1, 0), (2, 1), (3, 1), (3, 2)] 

sage: tournaments = digraphs.tournaments_nauty 

sage: [len(list(tournaments(x))) for x in range(1,8)] 

[1, 1, 2, 4, 12, 56, 456] 

sage: [len(list(tournaments(x, strongly_connected = True))) for x in range(1,9)] 

[1, 0, 1, 1, 6, 35, 353, 6008] 

""" 

import subprocess 

 

nauty_input = options 

 

if min_out_degree is None: 

min_out_degree = 0 

if max_out_degree is None: 

max_out_degree = n-1 

 

nauty_input += " -d"+str(min_out_degree) 

nauty_input += " -D"+str(max_out_degree) 

 

if strongly_connected: 

nauty_input += " -c" 

 

nauty_input += " "+str(n) +" " 

 

sp = subprocess.Popen("gentourng {0}".format(nauty_input), shell=True, 

stdin=subprocess.PIPE, stdout=subprocess.PIPE, 

stderr=subprocess.PIPE, close_fds=True) 

 

if debug: 

yield sp.stderr.readline() 

 

gen = sp.stdout 

while True: 

try: 

s = next(gen) 

except StopIteration: 

# Exhausted list of graphs from nauty geng 

return 

 

G = DiGraph(n) 

i = 0 

j = 1 

for b in s[:-1]: 

if b == '0': 

G.add_edge(i,j) 

else: 

G.add_edge(j,i) 

 

if j == n-1: 

i += 1 

j = i+1 

else: 

j += 1 

 

yield G 

 

 

def Complete(self, n, loops=False): 

r""" 

Return the complete digraph on `n` vertices. 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices. 

 

- ``loops`` (boolean) -- whether to add loops or not, i.e., edges from 

`u` to itself. 

 

.. SEEALSO:: 

 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.RandomSemiComplete` 

 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.RandomTournament` 

 

EXAMPLES:: 

 

sage: n = 10 

sage: G = digraphs.Complete(n); G 

Complete digraph: Digraph on 10 vertices 

sage: G.size() == n*(n-1) 

True 

sage: G = digraphs.Complete(n, loops=True); G 

Complete digraph with loops: Looped digraph on 10 vertices 

sage: G.size() == n*n 

True 

sage: digraphs.Complete(-1) 

Traceback (most recent call last): 

... 

ValueError: The number of vertices cannot be strictly negative! 

""" 

G = DiGraph(n, name="Complete digraph"+(" with loops" if loops else ''), loops=loops) 

 

if loops: 

G.add_edges((u,u) for u in range(n)) 

 

G.add_edges((u,v) for u in range(n) for v in range(n) if u!=v) 

 

if n: 

from sage.graphs.graph_plot import _circle_embedding 

_circle_embedding(G, list(range(n))) 

 

return G 

 

def Circuit(self,n): 

r""" 

Returns the circuit on `n` vertices 

 

The circuit is an oriented ``CycleGraph`` 

 

EXAMPLES: 

 

A circuit is the smallest strongly connected digraph:: 

 

sage: circuit = digraphs.Circuit(15) 

sage: len(circuit.strongly_connected_components()) == 1 

True 

""" 

g = DiGraph(n) 

g.name("Circuit") 

 

if n==0: 

return g 

elif n == 1: 

g.allow_loops(True) 

g.add_edge(0,0) 

return g 

else: 

g.add_edges([(i,i+1) for i in range(n-1)]) 

g.add_edge(n-1,0) 

return g 

 

def Circulant(self,n,integers): 

r""" 

Returns a circulant digraph on `n` vertices from a set of integers. 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices. 

 

- ``integers`` -- the list of integers such that there is an edge from 

`i` to `j` if and only if ``(j-i)%n in integers``. 

 

EXAMPLES:: 

 

sage: digraphs.Circulant(13,[3,5,7]) 

Circulant graph ([3, 5, 7]): Digraph on 13 vertices 

 

TESTS:: 

 

sage: digraphs.Circulant(13,[3,5,7,"hey"]) 

Traceback (most recent call last): 

... 

ValueError: The list must contain only relative integers. 

sage: digraphs.Circulant(3,[3,5,7,3.4]) 

Traceback (most recent call last): 

... 

ValueError: The list must contain only relative integers. 

""" 

from sage.graphs.graph_plot import _circle_embedding 

from sage.rings.integer_ring import ZZ 

 

# Bad input and loops 

loops = False 

for i in integers: 

if not i in ZZ: 

raise ValueError("The list must contain only relative integers.") 

if (i%n) == 0: 

loops = True 

 

G=DiGraph(n, name="Circulant graph ("+str(integers)+")", loops=loops) 

 

_circle_embedding(G, list(range(n))) 

for v in range(n): 

G.add_edges([(v,(v+j)%n) for j in integers]) 

 

return G 

 

def DeBruijn(self, k, n, vertices = 'strings'): 

r""" 

Returns the De Bruijn digraph with parameters `k,n`. 

 

The De Bruijn digraph with parameters `k,n` is built upon a set of 

vertices equal to the set of words of length `n` from a dictionary of 

`k` letters. 

 

In this digraph, there is an arc `w_1w_2` if `w_2` can be obtained from 

`w_1` by removing the leftmost letter and adding a new letter at its 

right end. For more information, see the 

:wikipedia:`Wikipedia article on De Bruijn graph <De_Bruijn_graph>`. 

 

INPUT: 

 

- ``k`` -- Two possibilities for this parameter : 

- An integer equal to the cardinality of the alphabet to use, that 

is the degree of the digraph to be produced. 

- An iterable object to be used as the set of letters. The degree 

of the resulting digraph is the cardinality of the set of 

letters. 

 

- ``n`` -- An integer equal to the length of words in the De Bruijn 

digraph when ``vertices == 'strings'``, and also to the diameter of 

the digraph. 

 

- ``vertices`` -- 'strings' (default) or 'integers', specifying whether 

the vertices are words build upon an alphabet or integers. 

 

EXAMPLES: 

 

de Bruijn digraph of degree 2 and diameter 2:: 

 

sage: db = digraphs.DeBruijn(2, 2); db 

De Bruijn digraph (k=2, n=2): Looped digraph on 4 vertices 

sage: db.order(), db.size() 

(4, 8) 

sage: db.diameter() 

2 

 

Building a de Bruijn digraph on a different alphabet:: 

 

sage: g = digraphs.DeBruijn(['a', 'b'], 2) 

sage: g.vertices() 

['aa', 'ab', 'ba', 'bb'] 

sage: g.is_isomorphic(db) 

True 

sage: g = digraphs.DeBruijn(['AA', 'BB'], 2) 

sage: g.vertices() 

['AA,AA', 'AA,BB', 'BB,AA', 'BB,BB'] 

sage: g.is_isomorphic(db) 

True 

 

TESTS: 

 

Alphabet of null size or words of length zero:: 

 

sage: digraphs.DeBruijn(5, 0) 

De Bruijn digraph (k=5, n=0): Looped multi-digraph on 1 vertex 

sage: digraphs.DeBruijn(0, 0) 

De Bruijn digraph (k=0, n=0): Looped multi-digraph on 0 vertices 

 

:trac:`22355`:: 

 

sage: db = digraphs.DeBruijn(2, 2, vertices='strings') 

sage: db.vertices() 

['00', '01', '10', '11'] 

sage: h = digraphs.DeBruijn(2, 2, vertices='integers') 

sage: h.vertices() 

[0, 1, 2, 3] 

sage: db.is_isomorphic(h) 

True 

sage: digraphs.DeBruijn(0, 0, vertices='integers') 

De Bruijn digraph (k=0, n=0): Looped multi-digraph on 0 vertices 

sage: digraphs.DeBruijn(2, 2, vertices='circles') 

Traceback (most recent call last): 

... 

ValueError: unknown type for vertices 

""" 

from sage.combinat.words.words import Words 

from sage.rings.integer import Integer 

 

if vertices == 'strings': 

W = Words(list(range(k)) if isinstance(k, Integer) else k, n) 

A = Words(list(range(k)) if isinstance(k, Integer) else k, 1) 

g = DiGraph(loops=True) 

 

if n == 0 : 

g.allow_multiple_edges(True) 

v = W[0] 

for a in A: 

g.add_edge(v.string_rep(), v.string_rep(), a.string_rep()) 

else: 

for w in W: 

ww = w[1:] 

for a in A: 

g.add_edge(w.string_rep(), (ww*a).string_rep(), a.string_rep()) 

 

elif vertices == 'integers': 

d = k if isinstance(k, Integer) else len(list(k)) 

if d == 0: 

g = DiGraph(loops=True, multiedges=True) 

else: 

g = digraphs.GeneralizedDeBruijn(d**n, d) 

 

else: 

raise ValueError('unknown type for vertices') 

 

g.name( "De Bruijn digraph (k=%s, n=%s)"%(k,n) ) 

return g 

 

def GeneralizedDeBruijn(self, n, d): 

r""" 

Returns the generalized de Bruijn digraph of order `n` and degree `d`. 

 

The generalized de Bruijn digraph has been defined in [RPK80]_ 

[RPK83]_. It has vertex set `V=\{0, 1,..., n-1\}` and there is an arc 

from vertex `u \in V` to all vertices `v \in V` such that 

`v \equiv (u*d + a) \mod{n}` with `0 \leq a < d`. 

 

When `n = d^{D}`, the generalized de Bruijn digraph is isomorphic to the 

de Bruijn digraph of degree `d` and diameter `D`. 

 

INPUT: 

 

- ``n`` -- is the number of vertices of the digraph 

 

- ``d`` -- is the degree of the digraph 

 

.. SEEALSO:: 

 

* :meth:`sage.graphs.generic_graph.GenericGraph.is_circulant` -- 

checks whether a (di)graph is circulant, and/or returns all 

possible sets of parameters. 

 

EXAMPLES:: 

 

sage: GB = digraphs.GeneralizedDeBruijn(8, 2) 

sage: GB.is_isomorphic(digraphs.DeBruijn(2, 3), certificate = True) 

(True, {0: '000', 1: '001', 2: '010', 3: '011', 4: '100', 5: '101', 6: '110', 7: '111'}) 

 

TESTS: 

 

An exception is raised when the degree is less than one:: 

 

sage: G = digraphs.GeneralizedDeBruijn(2, 0) 

Traceback (most recent call last): 

... 

ValueError: The generalized de Bruijn digraph is defined for degree at least one. 

 

An exception is raised when the order of the graph is less than one:: 

 

sage: G = digraphs.GeneralizedDeBruijn(0, 2) 

Traceback (most recent call last): 

... 

ValueError: The generalized de Bruijn digraph is defined for at least one vertex. 

 

 

REFERENCES: 

 

.. [RPK80] \S. M. Reddy, D. K. Pradhan, and J. Kuhl. Directed graphs with 

minimal diameter and maximal connectivity, School Eng., Oakland Univ., 

Rochester MI, Tech. Rep., July 1980. 

 

.. [RPK83] \S. Reddy, P. Raghavan, and J. Kuhl. A Class of Graphs for 

Processor Interconnection. *IEEE International Conference on Parallel 

Processing*, pages 154-157, Los Alamitos, Ca., USA, August 1983. 

""" 

if n < 1: 

raise ValueError("The generalized de Bruijn digraph is defined for at least one vertex.") 

if d < 1: 

raise ValueError("The generalized de Bruijn digraph is defined for degree at least one.") 

 

GB = DiGraph(loops = True) 

GB.allow_multiple_edges(True) 

for u in range(n): 

for a in range(u*d, u*d+d): 

GB.add_edge(u, a%n) 

 

GB.name( "Generalized de Bruijn digraph (n=%s, d=%s)"%(n,d) ) 

return GB 

 

 

def ImaseItoh(self, n, d): 

r""" 

Returns the digraph of Imase and Itoh of order `n` and degree `d`. 

 

The digraph of Imase and Itoh has been defined in [II83]_. It has vertex 

set `V=\{0, 1,..., n-1\}` and there is an arc from vertex `u \in V` to 

all vertices `v \in V` such that `v \equiv (-u*d-a-1) \mod{n}` with 

`0 \leq a < d`. 

 

When `n = d^{D}`, the digraph of Imase and Itoh is isomorphic to the de 

Bruijn digraph of degree `d` and diameter `D`. When `n = d^{D-1}(d+1)`, 

the digraph of Imase and Itoh is isomorphic to the Kautz digraph 

[Kautz68]_ of degree `d` and diameter `D`. 

 

INPUT: 

 

- ``n`` -- is the number of vertices of the digraph 

 

- ``d`` -- is the degree of the digraph 

 

EXAMPLES:: 

 

sage: II = digraphs.ImaseItoh(8, 2) 

sage: II.is_isomorphic(digraphs.DeBruijn(2, 3), certificate = True) 

(True, {0: '010', 1: '011', 2: '000', 3: '001', 4: '110', 5: '111', 6: '100', 7: '101'}) 

 

sage: II = digraphs.ImaseItoh(12, 2) 

sage: II.is_isomorphic(digraphs.Kautz(2, 3), certificate = True) 

(True, {0: '010', 1: '012', 2: '021', 3: '020', 4: '202', 5: '201', 6: '210', 7: '212', 8: '121', 9: '120', 10: '102', 11: '101'}) 

 

 

TESTS: 

 

An exception is raised when the degree is less than one:: 

 

sage: G = digraphs.ImaseItoh(2, 0) 

Traceback (most recent call last): 

... 

ValueError: The digraph of Imase and Itoh is defined for degree at least one. 

 

An exception is raised when the order of the graph is less than two:: 

 

sage: G = digraphs.ImaseItoh(1, 2) 

Traceback (most recent call last): 

... 

ValueError: The digraph of Imase and Itoh is defined for at least two vertices. 

 

 

REFERENCE: 

 

.. [II83] \M. Imase and M. Itoh. A design for directed graphs with 

minimum diameter, *IEEE Trans. Comput.*, vol. C-32, pp. 782-784, 1983. 

""" 

if n < 2: 

raise ValueError("The digraph of Imase and Itoh is defined for at least two vertices.") 

if d < 1: 

raise ValueError("The digraph of Imase and Itoh is defined for degree at least one.") 

 

II = DiGraph(loops = True) 

II.allow_multiple_edges(True) 

for u in range(n): 

for a in range(-u*d-d, -u*d): 

II.add_edge(u, a % n) 

 

II.name( "Imase and Itoh digraph (n=%s, d=%s)"%(n,d) ) 

return II 

 

 

def Kautz(self, k, D, vertices = 'strings'): 

r""" 

Returns the Kautz digraph of degree `d` and diameter `D`. 

 

The Kautz digraph has been defined in [Kautz68]_. The Kautz digraph of 

degree `d` and diameter `D` has `d^{D-1}(d+1)` vertices. This digraph is 

build upon a set of vertices equal to the set of words of length `D` 

from an alphabet of `d+1` letters such that consecutive letters are 

differents. There is an arc from vertex `u` to vertex `v` if `v` can be 

obtained from `u` by removing the leftmost letter and adding a new 

letter, distinct from the rightmost letter of `u`, at the right end. 

 

The Kautz digraph of degree `d` and diameter `D` is isomorphic to the 

digraph of Imase and Itoh [II83]_ of degree `d` and order 

`d^{D-1}(d+1)`. 

 

See also the 

:wikipedia:`Wikipedia article on Kautz Graphs <Kautz_graph>`. 

 

INPUT: 

 

- ``k`` -- Two possibilities for this parameter : 

- An integer equal to the degree of the digraph to be produced, that 

is the cardinality minus one of the alphabet to use. 

- An iterable object to be used as the set of letters. The degree of 

the resulting digraph is the cardinality of the set of letters 

minus one. 

 

- ``D`` -- An integer equal to the diameter of the digraph, and also to 

the length of a vertex label when ``vertices == 'strings'``. 

 

- ``vertices`` -- 'strings' (default) or 'integers', specifying whether 

the vertices are words build upon an alphabet or integers. 

 

 

EXAMPLES:: 

 

sage: K = digraphs.Kautz(2, 3) 

sage: K.is_isomorphic(digraphs.ImaseItoh(12, 2), certificate = True) 

(True, 

{'010': 0, 

'012': 1, 

'020': 3, 

'021': 2, 

'101': 11, 

'102': 10, 

'120': 9, 

'121': 8, 

'201': 5, 

'202': 4, 

'210': 6, 

'212': 7}) 

 

sage: K = digraphs.Kautz([1,'a','B'], 2) 

sage: K.edges() 

[('1B', 'B1', '1'), ('1B', 'Ba', 'a'), ('1a', 'a1', '1'), ('1a', 'aB', 'B'), ('B1', '1B', 'B'), ('B1', '1a', 'a'), ('Ba', 'a1', '1'), ('Ba', 'aB', 'B'), ('a1', '1B', 'B'), ('a1', '1a', 'a'), ('aB', 'B1', '1'), ('aB', 'Ba', 'a')] 

 

sage: K = digraphs.Kautz([1,'aA','BB'], 2) 

sage: K.edges() 

[('1,BB', 'BB,1', '1'), ('1,BB', 'BB,aA', 'aA'), ('1,aA', 'aA,1', '1'), ('1,aA', 'aA,BB', 'BB'), ('BB,1', '1,BB', 'BB'), ('BB,1', '1,aA', 'aA'), ('BB,aA', 'aA,1', '1'), ('BB,aA', 'aA,BB', 'BB'), ('aA,1', '1,BB', 'BB'), ('aA,1', '1,aA', 'aA'), ('aA,BB', 'BB,1', '1'), ('aA,BB', 'BB,aA', 'aA')] 

 

 

TESTS: 

 

An exception is raised when the degree is less than one:: 

 

sage: G = digraphs.Kautz(0, 2) 

Traceback (most recent call last): 

... 

ValueError: Kautz digraphs are defined for degree at least one 

 

sage: G = digraphs.Kautz(['a'], 2) 

Traceback (most recent call last): 

... 

ValueError: Kautz digraphs are defined for degree at least one 

 

An exception is raised when the diameter of the graph is less than one:: 

 

sage: G = digraphs.Kautz(2, 0) 

Traceback (most recent call last): 

... 

ValueError: Kautz digraphs are defined for diameter at least one 

 

:trac:`22355`:: 

 

sage: K = digraphs.Kautz(2, 2, vertices='strings') 

sage: K.vertices() 

['01', '02', '10', '12', '20', '21'] 

sage: h = digraphs.Kautz(2, 2, vertices='integers') 

sage: h.vertices() 

[0, 1, 2, 3, 4, 5] 

sage: h.is_isomorphic(K) 

True 

sage: h = digraphs.Kautz([1,'aA','BB'], 2, vertices='integers') 

sage: h.is_isomorphic(K) 

True 

sage: h.vertices() 

[0, 1, 2, 3, 4, 5] 

sage: digraphs.Kautz(2, 2, vertices='circles') 

Traceback (most recent call last): 

... 

ValueError: unknown type for vertices 

 

REFERENCE: 

 

.. [Kautz68] \W. H. Kautz. Bounds on directed (d, k) graphs. Theory of 

cellular logic networks and machines, AFCRL-68-0668, SRI Project 7258, 

Final Rep., pp. 20-28, 1968. 

""" 

if D < 1: 

raise ValueError("Kautz digraphs are defined for diameter at least one") 

 

from sage.combinat.words.words import Words 

from sage.rings.integer import Integer 

 

if vertices == 'strings': 

 

my_alphabet = Words([str(i) for i in range(k+1)] if isinstance(k, Integer) else k, 1) 

if my_alphabet.alphabet().cardinality() < 2: 

raise ValueError("Kautz digraphs are defined for degree at least one") 

 

# We start building the set of vertices 

V = [i for i in my_alphabet] 

for i in range(D-1): 

VV = [] 

for w in V: 

VV += [w*a for a in my_alphabet if not w.has_suffix(a) ] 

V = VV 

 

# We now build the set of arcs 

G = DiGraph() 

for u in V: 

for a in my_alphabet: 

if not u.has_suffix(a): 

G.add_edge(u.string_rep(), (u[1:]*a).string_rep(), a.string_rep()) 

 

elif vertices == 'integers': 

d = k if isinstance(k, Integer) else (len(list(k))-1) 

if d < 1: 

raise ValueError("Kautz digraphs are defined for degree at least one") 

G = digraphs.ImaseItoh( (d+1)*(d**(D-1)), d) 

 

else: 

raise ValueError('unknown type for vertices') 

 

G.name( "Kautz digraph (k={}, D={})".format(k, D) ) 

return G 

 

def RandomDirectedGN(self, n, kernel=lambda x:x, seed=None): 

""" 

Returns a random GN (growing network) digraph with n vertices. 

 

The digraph is constructed by adding vertices with a link to one 

previously added vertex. The vertex to link to is chosen with a 

preferential attachment model, i.e. probability is proportional to 

degree. The default attachment kernel is a linear function of 

degree. The digraph is always a tree, so in particular it is a 

directed acyclic graph. 

 

INPUT: 

 

 

- ``n`` - number of vertices. 

 

- ``kernel`` - the attachment kernel 

 

- ``seed`` - for the random number generator 

 

 

EXAMPLES:: 

 

sage: D = digraphs.RandomDirectedGN(25) 

sage: D.edges(labels=False) 

[(1, 0), (2, 0), (3, 1), (4, 0), (5, 0), (6, 1), (7, 0), (8, 3), (9, 0), (10, 8), (11, 3), (12, 9), (13, 8), (14, 0), (15, 11), (16, 11), (17, 5), (18, 11), (19, 6), (20, 5), (21, 14), (22, 5), (23, 18), (24, 11)] 

sage: D.show() # long time 

 

REFERENCE: 

 

- [1] Krapivsky, P.L. and Redner, S. Organization of Growing 

Random Networks, Phys. Rev. E vol. 63 (2001), p. 066123. 

""" 

if seed is None: 

seed = current_randstate().long_seed() 

import networkx 

return DiGraph(networkx.gn_graph(n, kernel, seed=seed)) 

 

def RandomDirectedGNC(self, n, seed=None): 

""" 

Returns a random GNC (growing network with copying) digraph with n 

vertices. 

 

The digraph is constructed by adding vertices with a link to one 

previously added vertex. The vertex to link to is chosen with a 

preferential attachment model, i.e. probability is proportional to 

degree. The new vertex is also linked to all of the previously 

added vertex's successors. 

 

INPUT: 

 

 

- ``n`` - number of vertices. 

 

- ``seed`` - for the random number generator 

 

 

EXAMPLES:: 

 

sage: D = digraphs.RandomDirectedGNC(25) 

sage: D.edges(labels=False) 

[(1, 0), (2, 0), (2, 1), (3, 0), (4, 0), (4, 1), (5, 0), (5, 1), (5, 2), (6, 0), (6, 1), (7, 0), (7, 1), (7, 4), (8, 0), (9, 0), (9, 8), (10, 0), (10, 1), (10, 2), (10, 5), (11, 0), (11, 8), (11, 9), (12, 0), (12, 8), (12, 9), (13, 0), (13, 1), (14, 0), (14, 8), (14, 9), (14, 12), (15, 0), (15, 8), (15, 9), (15, 12), (16, 0), (16, 1), (16, 4), (16, 7), (17, 0), (17, 8), (17, 9), (17, 12), (18, 0), (18, 8), (19, 0), (19, 1), (19, 4), (19, 7), (20, 0), (20, 1), (20, 4), (20, 7), (20, 16), (21, 0), (21, 8), (22, 0), (22, 1), (22, 4), (22, 7), (22, 19), (23, 0), (23, 8), (23, 9), (23, 12), (23, 14), (24, 0), (24, 8), (24, 9), (24, 12), (24, 15)] 

sage: D.show() # long time 

 

REFERENCE: 

 

- [1] Krapivsky, P.L. and Redner, S. Network Growth by 

Copying, Phys. Rev. E vol. 71 (2005), p. 036118. 

""" 

if seed is None: 

seed = current_randstate().long_seed() 

import networkx 

return DiGraph(networkx.gnc_graph(n, seed=seed)) 

 

def RandomDirectedGNP(self, n, p, loops = False, seed = None): 

r""" 

Returns a random digraph on `n` nodes. Each edge is inserted 

independently with probability `p`. 

 

INPUT: 

 

- ``n`` -- number of nodes of the digraph 

 

- ``p`` -- probability of an edge 

 

- ``loops`` -- is a boolean set to True if the random digraph may have 

loops, and False (default) otherwise. 

 

- ``seed`` -- integer seed for random number generator (default=None). 

 

REFERENCES: 

 

.. [1] \P. Erdos and A. Renyi, On Random Graphs, Publ. Math. 6, 290 

(1959). 

 

.. [2] \E. N. Gilbert, Random Graphs, Ann. Math. Stat., 30, 1141 (1959). 

 

 

PLOTTING: When plotting, this graph will use the default spring-layout 

algorithm, unless a position dictionary is specified. 

 

EXAMPLES:: 

 

sage: set_random_seed(0) 

sage: D = digraphs.RandomDirectedGNP(10, .2) 

sage: D.num_verts() 

10 

sage: D.edges(labels=False) 

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

""" 

from sage.graphs.graph_generators_pyx import RandomGNP 

if 0.0 > p or 1.0 < p: 

raise ValueError("The probability p must be in [0..1].") 

 

if seed is None: 

seed = current_randstate().long_seed() 

 

return RandomGNP(n, p, directed = True, loops = loops) 

 

def RandomDirectedGNM(self, n, m, loops = False): 

r""" 

Returns a random labelled digraph on `n` nodes and `m` arcs. 

 

INPUT: 

 

- ``n`` (integer) -- number of vertices. 

 

- ``m`` (integer) -- number of edges. 

 

- ``loops`` (boolean) -- whether to allow loops (set to ``False`` by 

default). 

 

PLOTTING: When plotting, this graph will use the default spring-layout 

algorithm, unless a position dictionary is specified. 

 

EXAMPLES:: 

 

sage: D = digraphs.RandomDirectedGNM(10, 5) 

sage: D.num_verts() 

10 

sage: D.edges(labels=False) 

[(0, 3), (1, 5), (5, 1), (7, 0), (8, 5)] 

 

With loops:: 

 

sage: D = digraphs.RandomDirectedGNM(10, 100, loops = True) 

sage: D.num_verts() 

10 

sage: D.loops() 

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

 

TESTS:: 

 

sage: digraphs.RandomDirectedGNM(10,-3) 

Traceback (most recent call last): 

... 

ValueError: The number of edges must satisfy 0<= m <= n(n-1) when no loops are allowed, and 0<= m <= n^2 otherwise. 

 

sage: digraphs.RandomDirectedGNM(10,100) 

Traceback (most recent call last): 

... 

ValueError: The number of edges must satisfy 0<= m <= n(n-1) when no loops are allowed, and 0<= m <= n^2 otherwise. 

""" 

n, m = int(n), int(m) 

 

# The random graph is built by drawing randomly and uniformly two 

# integers u,v, and adding the corresponding edge if it does not exist, 

# as many times as necessary. 

 

# When the graph is dense, we actually compute its complement. This will 

# prevent us from drawing the same pair u,v too many times. 

 

from sage.misc.prandom import _pyrand 

rand = _pyrand() 

D = DiGraph(n, loops = loops) 

 

# Ensuring the parameters n,m make sense. 

# 

# If the graph is dense, we actually want to build its complement. We 

# update m accordingly. 

 

good_input = True 

is_dense = False 

 

if m < 0: 

good_input = False 

 

if loops: 

if m > n*n: 

good_input = False 

elif 2*m > n*n: 

is_dense = True 

m = n*n - m 

 

else: 

if m > n*(n-1): 

good_input = False 

elif m > (n * (n - 1)) // 2: 

is_dense = True 

m = n*(n-1) - m 

 

if not good_input: 

raise ValueError("The number of edges must satisfy 0<= m <= n(n-1) when no loops are allowed, and 0<= m <= n^2 otherwise.") 

 

# When the given number of edges defines a density larger than 1/2, it 

# should be faster to compute the complement of the graph (less edges to 

# generate), then to return its complement. This being said, the 

# .complement() method for sparse graphs is very slow at the moment. 

 

# Similarly, it is faster to test whether a pair belongs to a dictionary 

# than to test the adjacency of two vertices in a graph. For these 

# reasons, the following code mainly works on dictionaries. 

 

adj = dict( (i, dict()) for i in range(n) ) 

 

# We fill the dictionary structure, but add the corresponding edge in 

# the graph only if is_dense is False. If it is true, we will add the 

# edges in a second phase. 

 

 

while m > 0: 

 

# It is better to obtain random numbers this way than by calling the 

# randint or randrange method. This, because they are very expensive 

# when trying to compute MANY random integers, and because the 

# following lines is precisely what they do anyway, after checking 

# their parameters are correct. 

 

u=int(rand.random()*n) 

v=int(rand.random()*n) 

 

if (u != v or loops) and (not v in adj[u]): 

adj[u][v] = 1 

m -= 1 

if not is_dense: 

D.add_edge(u,v) 

 

# If is_dense is True, it means the graph has not been built. We fill D 

# with the complement of the edges stored in the adj dictionary 

 

if is_dense: 

for u in range(n): 

for v in range(n): 

if ((u != v) or loops) and (not (v in adj[u])): 

D.add_edge(u,v) 

 

return D 

 

def RandomDirectedGNR(self, n, p, seed=None): 

""" 

Returns a random GNR (growing network with redirection) digraph 

with n vertices and redirection probability p. 

 

The digraph is constructed by adding vertices with a link to one 

previously added vertex. The vertex to link to is chosen uniformly. 

With probability p, the arc is instead redirected to the successor 

vertex. The digraph is always a tree. 

 

INPUT: 

 

 

- ``n`` - number of vertices. 

 

- ``p`` - redirection probability 

 

- ``seed`` - for the random number generator. 

 

 

EXAMPLES:: 

 

sage: D = digraphs.RandomDirectedGNR(25, .2) 

sage: D.edges(labels=False) 

[(1, 0), (2, 0), (2, 1), (3, 0), (4, 0), (4, 1), (5, 0), (5, 1), (5, 2), (6, 0), (6, 1), (7, 0), (7, 1), (7, 4), (8, 0), (9, 0), (9, 8), (10, 0), (10, 1), (10, 2), (10, 5), (11, 0), (11, 8), (11, 9), (12, 0), (12, 8), (12, 9), (13, 0), (13, 1), (14, 0), (14, 8), (14, 9), (14, 12), (15, 0), (15, 8), (15, 9), (15, 12), (16, 0), (16, 1), (16, 4), (16, 7), (17, 0), (17, 8), (17, 9), (17, 12), (18, 0), (18, 8), (19, 0), (19, 1), (19, 4), (19, 7), (20, 0), (20, 1), (20, 4), (20, 7), (20, 16), (21, 0), (21, 8), (22, 0), (22, 1), (22, 4), (22, 7), (22, 19), (23, 0), (23, 8), (23, 9), (23, 12), (23, 14), (24, 0), (24, 8), (24, 9), (24, 12), (24, 15)] 

sage: D.show() # long time 

 

REFERENCE: 

 

- [1] Krapivsky, P.L. and Redner, S. Organization of Growing 

Random Networks, Phys. Rev. E vol. 63 (2001), p. 066123. 

""" 

if seed is None: 

seed = current_randstate().long_seed() 

import networkx 

return DiGraph(networkx.gnc_graph(n, seed=seed)) 

 

def RandomSemiComplete(self, n): 

r""" 

Return a random semi-complete digraph on `n` vertices. 

 

A directed graph `G=(V,E)` is *semi-complete* if for any pair of 

vertices `u` and `v`, there is *at least* one arc between them. 

 

To generate randomly a semi-complete digraph, we have to ensure, for any 

pair of distinct vertices `u` and `v`, that with probability `1/3` we 

have only arc `uv`, with probability `1/3` we have only arc `vu`, and 

with probability `1/3` we have both arc `uv` and arc `vu`. We do so by 

selecting a random integer `coin` in `[1,3]`. When `coin==1` we select 

only arc `uv`, when `coin==3` we select only arc `vu`, and when 

`coin==2` we select both arcs. In other words, we select arc `uv` when 

`coin\leq 2` and arc `vu` when `coin\geq 2`. 

 

INPUT: 

 

- ``n`` (integer) -- the number of nodes 

 

.. SEEALSO:: 

 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.Complete` 

 

- :meth:`~sage.graphs.digraph_generators.DiGraphGenerators.RandomTournament` 

 

EXAMPLES:: 

 

sage: SC = digraphs.RandomSemiComplete(10); SC 

Random Semi-Complete digraph: Digraph on 10 vertices 

sage: SC.size() >= binomial(10, 2) 

True 

sage: digraphs.RandomSemiComplete(-1) 

Traceback (most recent call last): 

... 

ValueError: The number of vertices cannot be strictly negative! 

""" 

G = DiGraph(n, name="Random Semi-Complete digraph") 

 

# For each pair u,v we choose a randon number ``coin`` in [1,3]. 

# We select edge `(u,v)` if `coin==1` or `coin==2`. 

# We select edge `(v,u)` if `coin==2` or `coin==3`. 

import itertools 

from sage.misc.prandom import randint 

for u,v in itertools.combinations(range(n), 2): 

coin = randint(1,3) 

if coin<=2: 

G.add_edge(u,v) 

if coin>=2: 

G.add_edge(v,u) 

 

if n: 

from sage.graphs.graph_plot import _circle_embedding 

_circle_embedding(G, list(range(n))) 

 

return G 

 

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

# DiGraph Iterators 

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

 

def __call__(self, vertices=None, property=lambda x: True, augment='edges', 

size=None, implementation='c_graph', sparse=True, copy=True): 

""" 

Accesses the generator of isomorphism class representatives. 

Iterates over distinct, exhaustive representatives. 

 

INPUT: 

 

- ``vertices`` - natural number or ``None`` to generate all digraphs 

 

- ``property`` - any property to be tested on digraphs 

before generation. 

 

- ``augment`` - choices: 

 

- ``'vertices'`` - augments by adding a vertex, and 

edges incident to that vertex. In this case, all digraphs on up to 

n=vertices are generated. If for any digraph G satisfying the 

property, every subgraph, obtained from G by deleting one vertex 

and only edges incident to that vertex, satisfies the property, 

then this will generate all digraphs with that property. If this 

does not hold, then all the digraphs generated will satisfy the 

property, but there will be some missing. 

 

- ``'edges'`` - augments a fixed number of vertices by 

adding one edge In this case, all digraphs on exactly n=vertices 

are generated. If for any graph G satisfying the property, every 

subgraph, obtained from G by deleting one edge but not the vertices 

incident to that edge, satisfies the property, then this will 

generate all digraphs with that property. If this does not hold, 

then all the digraphs generated will satisfy the property, but 

there will be some missing. 

 

- ``implementation`` - which underlying implementation to use (see DiGraph?) 

 

- ``sparse`` - ignored if implementation is not ``c_graph`` 

 

- ``copy`` (boolean) -- If set to ``True`` (default) 

this method makes copies of the digraphs before returning 

them. If set to ``False`` the method returns the digraph it 

is working on. The second alternative is faster, but modifying 

any of the digraph instances returned by the method may break 

the function's behaviour, as it is using these digraphs to 

compute the next ones: only use ``copy = False`` when 

you stick to *reading* the digraphs returned. 

 

EXAMPLES: Print digraphs on 2 or less vertices. 

 

:: 

 

sage: for D in digraphs(2, augment='vertices'): 

....: print(D) 

Digraph on 0 vertices 

Digraph on 1 vertex 

Digraph on 2 vertices 

Digraph on 2 vertices 

Digraph on 2 vertices 

 

Print digraphs on 3 vertices. 

 

:: 

 

sage: for D in digraphs(3): 

....: print(D) 

Digraph on 3 vertices 

Digraph on 3 vertices 

... 

Digraph on 3 vertices 

Digraph on 3 vertices 

 

For more examples, see the class level documentation, or type 

 

:: 

 

sage: digraphs? # not tested 

 

REFERENCE: 

 

- Brendan D. McKay, Isomorph-Free Exhaustive generation. 

Journal of Algorithms Volume 26, Issue 2, February 1998, 

pages 306-324. 

""" 

from copy import copy as copyfun 

if size is not None: 

extra_property = lambda x: x.size() == size 

else: 

extra_property = lambda x: True 

if augment == 'vertices': 

if vertices is None: 

raise NotImplementedError 

 

from sage.graphs.graph_generators import canaug_traverse_vert 

g = DiGraph(implementation=implementation, sparse=sparse) 

for gg in canaug_traverse_vert(g, [], vertices, property, dig=True, implementation=implementation, sparse=sparse): 

if extra_property(gg): 

yield copyfun(gg) if copy else gg 

 

elif augment == 'edges': 

 

if vertices is None: 

vertices = 0 

while True: 

for g in self(vertices, implementation=implementation, sparse=sparse, copy=copy): 

yield g 

vertices += 1 

 

from sage.graphs.graph_generators import canaug_traverse_edge 

g = DiGraph(vertices, implementation=implementation, sparse=sparse) 

gens = [] 

for i in range(vertices-1): 

gen = list(range(i)) 

gen.append(i+1); gen.append(i) 

gen += list(range(i + 2, vertices)) 

gens.append(gen) 

for gg in canaug_traverse_edge(g, gens, property, dig=True, implementation=implementation, sparse=sparse): 

if extra_property(gg): 

yield copyfun(gg) if copy else gg 

else: 

raise NotImplementedError() 

 

 

# Easy access to the graph generators from the command line: 

digraphs = DiGraphGenerators()