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

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

r""" 

Graphic Matroids 

 

Let `G = (V,E)` be a graph and let `C` be the collection of the edge sets 

of cycles in `G`. The corresponding graphic matroid `M(G)` has ground set `E` 

and circuits `C`. 

 

Construction 

============ 

 

The recommended way to create a graphic matroid is by using the 

:func:`Matroid() <sage.matroids.constructor.Matroid>` function, with a 

graph `G` as input. This function can accept many different kinds of input 

to get a graphic matroid if the ``graph`` keyword is used, similar to the 

:func:`Graph() <sage.graphs.graph.Graph>` constructor. However, 

invoking the class directly is possible too. To get access to it, type:: 

 

sage: from sage.matroids.advanced import * 

 

See also :mod:`sage.matroids.advanced`. 

 

Graphic matroids do not have a representation matrix or any of the 

functionality of regular matroids. It is possible to get an instance of the 

:class:`~sage.matroids.linear_matroid.RegularMatroid` class 

by using the ``regular`` keyword when constructing the matroid. 

It is also possible to cast a GraphicMatroid as a RegularMatroid with the 

:meth:`~sage.matroids.graphic_matroids.GraphicMatroid.regular_matroid` 

method:: 

 

sage: M1 = Matroid(graphs.DiamondGraph(), regular=True) 

sage: M2 = Matroid(graphs.DiamondGraph()) 

sage: M3 = M2.regular_matroid() 

 

Below are some examples of constructing a graphic matroid. 

 

:: 

 

sage: from sage.matroids.advanced import * 

sage: edgelist = [(0, 1, 'a'), (0, 2, 'b'), (1, 2, 'c')] 

sage: G = Graph(edgelist) 

sage: M1 = Matroid(G) 

sage: M2 = Matroid(graph=edgelist) 

sage: M3 = Matroid(graphs.CycleGraph(3)) 

sage: M1 == M3 

False 

sage: M1.is_isomorphic(M3) 

True 

sage: M1.equals(M2) 

True 

sage: M1 == M2 

True 

sage: isinstance(M1, GraphicMatroid) 

True 

sage: isinstance(M1, RegularMatroid) 

False 

 

Note that if there is not a complete set of unique edge labels, and there are 

no parallel edges, then vertex tuples will be used for the ground set. The user 

may wish to override this by specifying the ground set, as the vertex tuples will 

not be updated if the matroid is modified. 

 

sage: G = graphs.DiamondGraph() 

sage: M1 = Matroid(G) 

sage: N1 = M1.contract((0,1)) 

sage: N1.graph().edges_incident(0) 

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

sage: M2 = Matroid(range(G.num_edges()), G) 

sage: N2 = M2.contract(0) 

sage: N1.is_isomorphic(N2) 

True 

 

AUTHORS: 

 

- Zachary Gershkoff (2017-07-07): initial version 

 

Methods 

======= 

""" 

from __future__ import absolute_import 

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

# Copyright (C) 2017 Zachary Gershkoff <zgersh2@lsu.edu> 

# 

# 

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

# 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 .matroid import Matroid 

 

from sage.graphs.graph import Graph 

from copy import copy, deepcopy 

from .utilities import newlabel, split_vertex, sanitize_contractions_deletions 

from itertools import combinations 

from sage.rings.integer import Integer 

from sage.sets.disjoint_set import DisjointSet 

 

class GraphicMatroid(Matroid): 

r""" 

The graphic matroid class. 

 

INPUT: 

 

- ``G`` -- a Graph 

- ``groundset`` -- (optional) a list in 1-1 correspondence with 

``G.edge_iterator()`` 

 

OUTPUT: 

 

A ``GraphicMatroid`` instance where the ground set elements are 

the edges of ``G``. 

 

..NOTE:: 

 

If a disconnected graph is given as input, the instance of 

``GraphicMatroid`` will connect the graph components and store 

this as its graph. 

 

EXAMPLES:: 

 

sage: from sage.matroids.advanced import * 

sage: M = GraphicMatroid(graphs.BullGraph()); M 

Graphic matroid of rank 4 on 5 elements 

sage: N = GraphicMatroid(graphs.CompleteBipartiteGraph(3,3)); N 

Graphic matroid of rank 5 on 9 elements 

 

A disconnected input will get converted to a connected graph internally:: 

 

sage: G1 = graphs.CycleGraph(3); G2 = graphs.DiamondGraph() 

sage: G = G1.disjoint_union(G2) 

sage: len(G) 

7 

sage: G.is_connected() 

False 

sage: M = GraphicMatroid(G) 

sage: M 

Graphic matroid of rank 5 on 8 elements 

sage: H = M.graph() 

sage: H 

Looped multi-graph on 6 vertices 

sage: H.is_connected() 

True 

sage: M.is_connected() 

False 

 

You can still locate an edge using the vertices of the input graph:: 

 

sage: G1 = graphs.CycleGraph(3); G2 = graphs.DiamondGraph() 

sage: G = G1.disjoint_union(G2) 

sage: M = Matroid(G) 

sage: H = M.graph() 

sage: vm = M.vertex_map() 

sage: (u, v, l) = G.random_edge() 

sage: H.has_edge(vm[u], vm[v]) 

True 

""" 

 

# Necessary: 

 

def __init__(self, G, groundset=None): 

""" 

See class definition for full documentation. 

 

EXAMPLES:: 

 

sage: from sage.matroids.advanced import * 

sage: G1 = graphs.CycleGraph(3); G2 = graphs.DiamondGraph() 

sage: G = G1.disjoint_union(G2) 

sage: M = GraphicMatroid(G) 

sage: M 

Graphic matroid of rank 5 on 8 elements 

sage: M.graph() 

Looped multi-graph on 6 vertices 

sage: M.graph().is_connected() 

True 

sage: M.is_connected() 

False 

 

TESTS:: 

 

sage: TestSuite(M).run(verbose=True) 

running ._test_category() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

""" 

 

if groundset is None: 

#Try to construct a ground set based on the edge labels. 

#If that fails, use range() to come up with a groundset. 

groundset = G.edge_labels() 

 

groundset_set = frozenset(groundset) 

 

# if the provided ground set is incomplete, it gets overwriten 

# invalidate `None` as label 

if None in groundset_set or len(groundset_set) != G.num_edges(): 

groundset = range(G.num_edges()) 

groundset_set = frozenset(groundset) 

 

self._groundset = groundset_set 

 

# Map vertices on input graph to vertices in self._G 

self._vertex_map = {v: v for v in G.vertices()} 

comps = G.connected_components() 

while len(comps) > 1: 

comp = comps.pop() 

v1 = comps[-1][-1] 

v2 = comp[0] 

self._vertex_map[v2] = v1 

comps[-1].extend(comp) 

 

# Construct a graph and assign edge labels corresponding to the ground set 

edge_list = [] 

for i, e in enumerate(G.edge_iterator()): 

# the ordering from edge_labels() respects edge_iterator() and not edges() 

edge_list.append((self._vertex_map[e[0]], 

self._vertex_map[e[1]], groundset[i])) 

# If the matroid is empty, have the internal graph be a single vertex 

if edge_list: 

self._G = Graph(edge_list, loops=True, multiedges=True, weighted=True, 

data_structure='static_sparse') 

else: 

self._G = Graph(1, loops=True, multiedges=True, weighted=True, 

data_structure='static_sparse') 

# Map ground set elements to graph edges: 

# The the edge labels should already be the elements. 

self._groundset_edge_map = ({l: (u, v) for 

(u, v, l) in self._G.edges()}) 

 

def groundset(self): 

""" 

Return the ground set of the matroid as a frozenset. 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.DiamondGraph()) 

sage: sorted(M.groundset()) 

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

sage: G = graphs.CompleteGraph(3).disjoint_union(graphs.CompleteGraph(4)) 

sage: M = Matroid(range(G.num_edges()), G); sorted(M.groundset()) 

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

sage: M = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (0, 3, 'c')])) 

sage: sorted(M.groundset()) 

['a', 'b', 'c'] 

""" 

return self._groundset 

 

def _rank(self, X): 

""" 

Return the rank of a set ``X``. 

 

This method does no checking on ``X``, and 

``X`` may be assumed to have the same interface as ``frozenset``. 

 

INPUT: 

 

- ``X`` -- an object with Python's ``frozenset`` interface 

 

OUTPUT: 

 

The rank of `X` in the matroid. 

 

EXAMPLES:: 

 

sage: from sage.matroids.advanced import * 

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

sage: M = GraphicMatroid(Graph(edgelist, loops=True, multiedges=True)) 

sage: M.rank([0]) 

0 

sage: M.rank([1,2]) 

2 

sage: M.rank([1,2,4]) 

2 

sage: M.rank(M.groundset()) 

3 

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

sage: M = GraphicMatroid(Graph(edgelist, loops=True, multiedges=True)) 

sage: M.rank(M.groundset()) 

2 

sage: M.rank([0,3]) 

1 

 

""" 

edges = self.groundset_to_edges(X) 

vertices = set([u for (u, v, l) in edges]).union( 

[v for (u, v, l) in edges]) 

# This counts components: 

DS_vertices = DisjointSet(vertices) 

for (u, v, l) in edges: 

DS_vertices.union(u,v) 

return (len(vertices) - DS_vertices.number_of_subsets()) 

 

# Representation: 

 

def _repr_(self): 

""" 

Returns a string representation of the matroid. 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.CompleteGraph(5)) 

sage: M 

Graphic matroid of rank 4 on 10 elements 

sage: G = Graph([(0, 0), (0, 1), (0, 2), (1, 1), (2, 2)], loops=True) 

sage: M = Matroid(G) 

sage: M 

Graphic matroid of rank 2 on 5 elements 

""" 

self._mrank = str(self._rank(self._groundset)) 

self._elts = str(len(self._groundset)) 

 

return "Graphic matroid of rank " + self._mrank + " on " + self._elts + " elements" 

 

# Comparison: 

 

def _vertex_stars(self): 

""" 

Computes the set of edge labels around each vertex. 

 

Internal method for hashing purposes. 

 

OUTPUT: 

 

A ``frozenset`` of ``frozenset``s containing the edge labels around 

each vertex. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: sorted(M._vertex_stars()) 

[frozenset({0, 2, 3}), 

frozenset({1, 2, 4}), 

frozenset({3, 4}), 

frozenset({0, 1})] 

 

sage: N = Matroid(range(5), graphs.BullGraph()) 

sage: sorted(N._vertex_stars()) 

[frozenset({0, 2, 3}), 

frozenset({4}), 

frozenset({1, 2, 4}), 

frozenset({3}), 

frozenset({0, 1})] 

""" 

star_list = [] 

for v in self._G.vertices(): 

star = [l for (u, v, l) in self._G.edges_incident(v)] 

star_list.append(frozenset(star)) 

return frozenset(star_list) 

 

def __hash__(self): 

r""" 

Return an invariant of the matroid. 

 

This function is called when matroids are added to a set. It is very 

desirable to override it so it can distinguish matroids on the same 

groundset, which is a very typical use case! 

 

.. WARNING:: 

 

This method is linked to __richcmp__ (in Cython) and __cmp__ or 

__eq__/__ne__ (in Python). If you override one, you should (and in 

Cython: MUST) override the other! 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.CompleteGraph(3)) 

sage: N = Matroid(graphs.CycleGraph(3)) 

sage: O = Matroid(graphs.ButterflyGraph()) 

sage: hash(M) == hash(N) 

True 

sage: hash(O) == hash(N) 

False 

sage: P = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (1, 2, 'c')])) 

sage: hash(P) == hash(M) 

False 

""" 

return hash(self._vertex_stars()) 

 

def __eq__(self, other): 

""" 

Compare two matroids. 

 

For two graphic matroids to be equal, all attributes of the underlying 

graphs must be equal. 

 

INPUT: 

 

- ``other`` -- a matroid 

 

OUTPUT: 

 

``True`` if ``self`` and ``other`` have the same graph; ``False`` 

otherwise. 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.CompleteGraph(3)) 

sage: N = Matroid(graphs.CycleGraph(3)) 

sage: O = Matroid(graphs.ButterflyGraph()) 

sage: P = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (1, 2, 'c')])) 

sage: M == N 

True 

sage: M == O 

False 

sage: M == P 

False 

 

A more subtle example where the vertex labels differ:: 

 

sage: G1 = Graph([(0,1,0),(0,2,1),(1,2,2)]) 

sage: G2 = Graph([(3,4,3),(3,5,4),(4,5,5),(4,6,6),(5,6,7)]) 

sage: G = G1.disjoint_union(G2) 

sage: H = G2.disjoint_union(G1) 

sage: Matroid(G) == Matroid(H) 

False 

sage: Matroid(G).equals(Matroid(H)) 

True 

 

Same except for vertex labels:: 

 

sage: G1 = Graph([(0,1,0),(1,2,1),(2,0,2)]) 

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

sage: Matroid(G1) == Matroid(G2) 

False 

 

""" 

# Graph.__eq__() will ignore edge labels unless we turn on weighted() 

# This will be done in __init__() 

if not isinstance(other, GraphicMatroid): 

return False 

return (self._G == other._G) 

 

def __ne__(self, other): 

""" 

Compare two matroids. 

 

INPUT: 

 

- ``other`` -- a matroid 

 

OUTPUT: 

 

``False`` if ``self`` and ``other`` have the same graph; ``True`` 

otherwise. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(4), graphs.CycleGraph(4)) 

sage: N = Matroid(range(4), graphs.CompleteBipartiteGraph(2,2)) 

sage: O = Matroid(graphs.PetersenGraph()) 

sage: M != N 

True 

sage: M.equals(N) 

True 

sage: M != O 

True 

 

""" 

return (not self == other) 

 

# Copying, loading, saving: 

 

def __copy__(self): 

""" 

Create a shallow copy. 

 

Creating a ``GraphicMatroid`` instance will build a new graph, so 

the copies have no attributes in common. 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.PappusGraph()) 

sage: N = copy(M) 

sage: M == N 

True 

sage: M._G is N._G 

False 

""" 

N = GraphicMatroid(self._G) 

if getattr(self, '__custom_name') is not None: # because of name wrangling, this is not caught by the default copy 

N.rename(getattr(self, '__custom_name')) 

return N 

 

def __deepcopy__(self, memo={}): 

""" 

Create a deep copy. 

 

.. NOTE:: 

 

Since matroids are immutable, a shallow copy normally suffices. 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.PetersenGraph()) 

sage: N = deepcopy(M) 

sage: N == M 

True 

""" 

# The only real difference between this and __copy__() is the memo 

N = GraphicMatroid(deepcopy(self._G, memo)) 

if getattr(self, '__custom_name') is not None: # because of name wrangling, this is not caught by the default deepcopy 

N.rename(deepcopy(getattr(self, '__custom_name'), memo)) 

return N 

 

def __reduce__(self): 

""" 

Save the matroid for later reloading. 

 

EXAMPLES:: 

 

sage: M = Matroid(graphs.PetersenGraph()) 

sage: M == loads(dumps(M)) 

True 

sage: loads(dumps(M)) 

Graphic matroid of rank 9 on 15 elements 

""" 

from .unpickling import unpickle_graphic_matroid 

data = (self._G, getattr(self, '__custom_name')) 

version = 0 

return unpickle_graphic_matroid, (version, data) 

 

# Overrides: 

 

def _minor(self, contractions=frozenset([]), deletions=frozenset([])): 

""" 

Return a minor. 

 

INPUT: 

 

- ``contractions`` -- frozenset; subset of ``self.groundset()`` to be contracted 

- ``deletions`` -- frozenset; subset of ``self.groundset()`` to be deleted 

 

Assumptions: contractions are independent, deletions are coindependent, 

contractions and deletions are disjoint. 

 

OUTPUT: 

 

An instance of GraphicMatroid. 

 

EXAMPLES:: 

 

sage: M = matroids.CompleteGraphic(5) 

sage: M._minor(deletions=frozenset([0,1,2])) 

Graphic matroid of rank 4 on 7 elements 

sage: M._minor(contractions=frozenset([0,1,2])) 

Graphic matroid of rank 1 on 7 elements 

sage: M = Matroid(range(15), graphs.PetersenGraph()) 

sage: N = M._minor(deletions = frozenset([0, 3, 5, 9]), contractions = 

....: frozenset([1, 2, 11])); N 

Graphic matroid of rank 6 on 8 elements 

""" 

g = self.graph() 

cont_edges = self._groundset_to_edges(contractions) 

del_edges = self._groundset_to_edges(deletions) 

# deletions first so contractions don't mess up the vertices 

g.delete_edges(del_edges) 

g.contract_edges(cont_edges) 

 

return GraphicMatroid(g) 

 

def _has_minor(self, N, certificate=False): 

""" 

Check if the matroid has a minor isomorphic to M(H). 

 

INPUT: 

 

- ``N`` - a matroid 

- ``certificate`` - (default: ``False``) if ``True``, returns the certificate 

isomorphism from the minor of ``self`` to ``N`` 

 

OUTPUT: 

 

Boolean, or tuple if the ``certificate`` option is used. If ``certificate`` 

is ``True``, then the output will either be ``False, None`` or 

``True, (X, Y, dic) where ``N`` is isomorphic to ``self.minor(X, Y)``, 

and ``dic`` is an isomorphism between ``N`` and ``self.minor(X, Y)``. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(9), graphs.CompleteBipartiteGraph(3, 3)) 

sage: N = Matroid(range(3), graphs.CycleGraph(3)) 

sage: N1 = Matroid(range(3), graph=graphs.CycleGraph(3), 

....: regular=True) 

sage: _, cert = M._has_minor(N1, certificate=True) 

sage: Mp = M.minor(cert[0], cert[1]) 

sage: N.is_isomorphism(Mp, cert[2]) 

True 

sage: M._has_minor(N) 

True 

sage: M._has_minor(N1) 

True 

sage: _, cert = M._has_minor(N, certificate=True) 

sage: Mp = M.minor(cert[0], cert[1]) 

sage: N.is_isomorphism(Mp, cert[2]) 

True 

 

:: 

 

sage: M = matroids.CompleteGraphic(6) 

sage: N = Matroid(range(8), graphs.WheelGraph(5)) 

sage: M._has_minor(N) 

True 

sage: _, cert = M._has_minor(N, certificate=True) 

sage: Mp = M.minor(cert[0], cert[1]) 

sage: N.is_isomorphism(Mp, cert[2]) 

True 

sage: N.has_minor(M) 

False 

sage: N.has_minor(M, certificate=True) 

(False, None) 

 

If the matroids are not 3-connected, then the default matroid algorithms 

are used:: 

 

sage: M = matroids.CompleteGraphic(6) 

sage: N = Matroid(graphs.CycleGraph(4)) 

sage: M.has_minor(N) 

True 

sage: N.has_minor(M) 

False 

""" 

# The graph minor algorithm is faster but it doesn't make sense 

# to use it if M(H) is not 3-connected, because of all the possible 

# Whitney switches or 1-sums that will give the same matroid. 

if isinstance(N, GraphicMatroid) and N.is_3connected(): 

# Graph.minor() does not work with multigraphs 

G = self.graph() 

G.allow_loops(False) 

G.allow_multiple_edges(False) 

H = N.graph() 

H.allow_loops(False) 

H.allow_multiple_edges(False) 

 

try: 

# Graph.minor() returns a certificate if there is one 

# and a ValueError if there isn't. 

cert = G.minor(H) 

except ValueError: 

if certificate: 

return (False, None) 

else: 

return False 

 

if certificate: 

# This is where it gets complicated. 

# The Graph.minor() method gives a dictionary of vertices 

# as its certificate. There is currently no easy way to 

# determine the edges. 

# From the dictionary, we can get an idea of what the 

# contractions are, and what vertices are not used. 

# So we'll merge the appropriate vertices, delete the 

# unused vertices, and pass to Matroid._has_minor(). 

 

# Determine contractions: 

vertices_for_minor = cert.values() 

contractions = [] 

for vertex_list in vertices_for_minor: 

S = G.subgraph(vertex_list) 

X = S.edge_labels() 

contractions.extend(self.max_independent(X)) 

 

# determine deletions: 

from itertools import chain 

deletions = [] 

big_vertex_list = list(chain.from_iterable(vertices_for_minor)) 

for v in G.vertices(): 

if v not in big_vertex_list: 

deletions.extend([l for (u0, v0, l) in G.edges_incident(v)]) 

 

# take contractions and deletions with what we have so far 

# then use method from abstract matroid class 

conset, delset = sanitize_contractions_deletions(self, contractions, deletions) 

M = self.minor(contractions=conset, deletions=delset) 

(should_be_true, elements) = Matroid._has_minor(M, N, certificate=True) 

 

# elements is a tuple (contractions, deletions, dict) 

# There should be no more contractions 

delset = set(delset) 

delset.update(elements[1]) 

return (True, (conset, frozenset(delset), elements[2])) 

 

else: 

return True 

else: 

# otherwise send it to regular matroids 

M = self.regular_matroid() 

if isinstance(N, GraphicMatroid): 

N = N.regular_matroid() 

return M._has_minor(N, certificate=certificate) 

 

def _corank(self, X): 

""" 

Return the corank of the set `X` in the matroid. 

 

Internal version that does no input checking. 

 

INPUT: 

 

- ``X`` -- an iterable container of ground set elements 

 

OUTPUT: 

 

Integer. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(9), graphs.CompleteBipartiteGraph(3,3)) 

sage: M._corank([0,1,2]) 

2 

sage: M._corank([1,2,3]) 

3 

""" 

edges = self.groundset_to_edges(X) 

all_vertices = self._G.vertices() 

not_our_edges = self.groundset_to_edges(self._groundset.difference(X)) 

DS_vertices = DisjointSet(all_vertices) 

for (u, v, l) in not_our_edges: 

DS_vertices.union(u, v) 

return (len(X) - (DS_vertices.number_of_subsets() - Integer(1))) 

 

def _is_circuit(self, X): 

""" 

Test if input is a circuit. 

 

INPUT: 

 

- ``X`` -- an iterable container of ground set elements 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: M._is_circuit([0,1,2]) 

True 

sage: M._is_circuit([0,1,2,3]) 

False 

sage: M._is_circuit([0,1,3]) 

False 

""" 

g = self._subgraph_from_set(X) 

return g.is_cycle() 

 

def _closure(self, X): 

""" 

Return the closure of a set. 

 

INPUT: 

 

- ``X`` -- an iterable container of ground set elements 

 

OUTPUT: 

 

``frozenset`` instance containing a subset of the ground set. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: sorted(M._closure([0])) 

[0] 

sage: sorted(M._closure([0,1])) 

[0, 1, 2] 

sage: sorted(M._closure(M.groundset())) 

[0, 1, 2, 3, 4] 

 

TESTS: 

 

Make sure the closure gets loops:: 

 

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

sage: M = Matroid(range(6), Graph(edgelist, loops=True, multiedges=True)) 

sage: M.graph().edges() 

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

sage: sorted(M._closure([4])) 

[0, 4, 5] 

 

""" 

X = set(X) 

Y = self.groundset().difference(X) 

edgelist = self._groundset_to_edges(Y) 

g = self._subgraph_from_set(X) 

V = g.vertices() 

components = g.connected_components_number() 

for e in edgelist: 

# a non-loop edge is in the closure iff both its vertices are 

# in the induced subgraph, and the edge doesn't connect components 

if e[0] in V and e[1] in V: 

g.add_edge(e) 

if g.connected_components_number() >= components: 

X.add(e[2]) 

else: 

g.delete_edge(e) 

# add all loops 

X.update(set([l for (u, v, l) in self._G.loops()])) 

return frozenset(X) 

 

def _max_independent(self, X): 

""" 

Compute a maximal independent subset. 

 

INPUT: 

 

- ``X`` -- An object with Python's ``frozenset`` interface containing 

a subset of ``self.groundset()`` 

 

OUTPUT: 

 

``frozenset`` instance containing a subset of the ground set. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: sorted(M._max_independent(M.groundset())) 

[0, 1, 3] 

sage: sorted(M._max_independent(frozenset([0,1,2]))) 

[0, 1] 

sage: sorted(M._max_independent(frozenset([3,4]))) 

[3, 4] 

sage: sorted(M._max_independent(frozenset([3]))) 

[3] 

sage: N = M.graphic_extension(0, element='a') 

sage: sorted(N._max_independent(frozenset(['a']))) 

[] 

""" 

edges = self.groundset_to_edges(X) 

vertices = set([u for (u, v, l) in edges]) 

vertices.update([v for (u, v, l) in edges]) 

 

our_set = set() 

DS_vertices = DisjointSet(vertices) 

for (u, v, l) in edges: 

if DS_vertices.find(u) != DS_vertices.find(v): 

DS_vertices.union(u,v) 

our_set.add(l) 

return frozenset(our_set) 

 

def _max_coindependent(self, X): 

""" 

Compute a maximal coindependent subset. 

 

INPUT: 

 

- ``X`` -- an iterable container of ground set elements 

 

OUTPUT: 

 

``frozenset`` instance containing a subset of the ground set. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: sorted(M._max_coindependent(M.groundset())) 

[2, 4] 

sage: sorted(M._max_coindependent([2,3,4])) 

[2, 4] 

sage: N = M.graphic_extension(0, element=5) 

sage: sorted(N.max_coindependent([0,1,2,5])) 

[1, 2, 5] 

""" 

edges = self.groundset_to_edges(X) 

all_vertices = self._G.vertices() 

not_our_edges = self.groundset_to_edges(self._groundset.difference(X)) 

 

our_set = set() 

DS_vertices = DisjointSet(all_vertices) 

for (u, v, l) in not_our_edges: 

DS_vertices.union(u, v) 

 

for (u, v, l) in edges: 

if DS_vertices.find(u) == DS_vertices.find(v): 

our_set.add(l) 

else: 

DS_vertices.union(u, v) 

return frozenset(our_set) 

 

def _circuit(self, X): 

""" 

Return a minimal dependent subset. 

 

INPUT: 

 

- ``X`` -- an iterable container of ground set elements 

 

OUTPUT: 

 

``frozenset`` instance containing a subset of ``X``. 

A ``ValueError`` is raised if the set contains no circuit. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: sorted(M._circuit(M.groundset())) 

[0, 1, 2] 

sage: N = Matroid(range(9), graphs.CompleteBipartiteGraph(3,3)) 

sage: sorted(N._circuit([0, 1, 2, 6, 7, 8])) 

[0, 1, 6, 7] 

sage: N._circuit([0, 1, 2]) 

Traceback (most recent call last): 

... 

ValueError: no circuit in independent set 

 

TESTS: 

 

With two disjoint cycles in the graph:: 

 

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

sage: M = Matroid(range(7), Graph(edgelist)) 

sage: M 

Graphic matroid of rank 5 on 7 elements 

sage: sorted(M._circuit(M.groundset())) 

[0, 1, 2] 

 

Giving it a long path before it finds a cycle:: 

 

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

sage: M = Matroid(Graph(edgelist, multiedges=True)) 

sage: M.graph().edges() 

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

sage: sorted(M._circuit(M.groundset())) 

[4, 5] 

 

""" 

edges = self.groundset_to_edges(X) 

vertices = set([u for (u, v, l) in edges]).union( 

set([v for (u, v, l) in edges])) 

edge_set = set() 

DS_vertices = DisjointSet(vertices) 

for (u, v, l) in edges: 

edge_set.add((u, v, l)) 

if DS_vertices.find(u) != DS_vertices.find(v): 

DS_vertices.union(u, v) 

else: 

last_edge = (u, v, l) 

break 

else: 

raise ValueError("no circuit in independent set") 

 

vertex_list = ([u for (u, v, l) in edge_set] 

+ [v for (u, v, l) in edge_set]) 

leaves = [(u, v, l) for (u, v, l) in edge_set if (vertex_list.count(u) == 1 

or vertex_list.count(v) == 1)] 

while leaves: 

for leaf in leaves: 

edge_set.remove(leaf) 

vertex_list.remove(leaf[0]) 

vertex_list.remove(leaf[1]) 

leaves = [(u, v, l) for (u, v, l) in edge_set if (vertex_list.count(u) == 1 

or vertex_list.count(v) == 1)] 

 

return frozenset([l for (u, v, l) in edge_set]) 

 

def _coclosure(self, X): 

""" 

Return the coclosure of a set. 

 

INPUT: 

 

- ``X`` -- an iterable container of ground set elements 

 

OUTPUT: 

 

``frozenset`` instance containing a subset of the groundset. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: sorted(M._coclosure([0])) 

[0, 1] 

sage: sorted(M._coclosure([0,1])) 

[0, 1] 

sage: N = M.graphic_extension(0, element=5) 

sage: sorted(N._coclosure([3])) 

[3, 4] 

sage: N = M.graphic_coextension(0, element=5) 

sage: sorted(N._coclosure([3])) 

[3, 4, 5] 

""" 

g = self.graph() 

g.delete_edges(self._groundset_to_edges(X)) 

components = g.connected_components_number() 

X = set(X) 

Y = self.groundset().difference(X) 

for e in self._groundset_to_edges(Y): 

g.delete_edge(e) 

if g.connected_components_number() > components: 

X.add(e[2]) 

g.add_edge(e) 

return frozenset(X) 

 

def _is_closed(self, X): 

""" 

Test if input is a closed set. 

 

INPUT: 

 

- ``X`` -- an object with Python's ``frozenset`` interface containing 

a subset of ``self.groundset()`` 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

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

sage: M = Matroid(range(len(edgelist)), Graph(edgelist, loops=True, 

....: multiedges=True)) 

sage: M._is_closed(frozenset([0,4,5])) 

True 

sage: M._is_closed(frozenset([0,4])) 

False 

sage: M._is_closed(frozenset([1, 2, 3, 4 ,5])) 

False 

""" 

# Take the set of vertices of the edges corresponding to the elements, 

# and check if there are other edges incident with two of those vertices. 

# Also, the must not be loops outside of X. 

X = set(X) 

loop_labels = set([l for (u, v, l) in self._G.loops()]) 

if not loop_labels.issubset(X): 

return False 

 

# Remove loops from input since we don't want to count them as components 

X.difference_update(loop_labels) 

edge_list = self._groundset_to_edges(X) 

 

vertex_set = set() 

Y = self.groundset().difference(X) 

edge_list2 = self._groundset_to_edges(Y) 

for e in edge_list: 

vertex_set.add(e[0]) 

vertex_set.add(e[1]) 

for e in edge_list2: 

if e[0] in vertex_set and e[1] in vertex_set: 

return False 

return True 

 

def _is_isomorphic(self, other, certificate=False): 

""" 

Test if ``self`` is isomorphic to ``other``. 

 

INPUT: 

 

- ``other`` -- a matroid 

- ``certificate`` -- boolean 

 

OUTPUT: 

 

- If ``certificate`` is ``False``, Boolean. 

- If ``certificate`` is ``True``, a tuple containing a boolean and a dictionary 

giving the isomorphism or None. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: N = Matroid(graph=graphs.DiamondGraph(), regular=True) 

sage: M._is_isomorphic(N, certificate=True) 

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

sage: O = Matroid(graphs.WheelGraph(5)) 

sage: M._is_isomorphic(O, certificate=True) 

(False, None) 

 

:: 

 

sage: M1 = Matroid(range(4), graphs.CycleGraph(4)) 

sage: M2 = Matroid(range(4), graphs.CompleteBipartiteGraph(2,2)) 

sage: M3 = matroids.Uniform(3,4) 

sage: M1._is_isomorphic(M2) 

True 

sage: M1._is_isomorphic(M3) 

True 

 

:: 

 

sage: edgelist = [(0,1,'a'),(0,2,'b'),(0,3,'c'),(1,2,'d'),(1,3,'e'),(2,3,'f')] 

sage: M = Matroid(Graph(edgelist)) 

sage: N = Matroid(range(6), graphs.WheelGraph(4)) 

sage: M._is_isomorphic(N, certificate=True) 

(True, {'a': 2, 'b': 4, 'c': 5, 'd': 0, 'e': 1, 'f': 3}) 

sage: N._is_isomorphic(M, certificate=True) 

(True, {0: 'd', 1: 'e', 2: 'a', 3: 'f', 4: 'b', 5: 'c'}) 

sage: O = Matroid(range(6), graphs.CycleGraph(6)) 

sage: M._is_isomorphic(O) 

False 

""" 

# Check for 3-connectivity so we don't have to worry about Whitney twists 

if isinstance(other, GraphicMatroid) and other.is_3connected(): 

G = self.graph() 

H = other.graph() 

G.allow_loops(False) 

G.allow_multiple_edges(False) 

H.allow_loops(False) 

H.allow_multiple_edges(False) 

 

result = G.is_isomorphic(H, certificate=certificate) 

if not certificate or result[0] is False: 

return result 

# If they are isomorphic and the user wants a certificate, 

# result[1] is a dictionary of vertices. 

# We need to translate this to edge labels. 

vertex_certif = result[1] 

elt_certif = {} 

for (u, v, l) in G.edge_iterator(): 

l_maps_to = H.edge_label(vertex_certif[u], vertex_certif[v]) 

elt_certif[l] = l_maps_to 

return (True, elt_certif) 

 

else: 

M = self.regular_matroid() 

if isinstance(other, GraphicMatroid): 

other = other.regular_matroid() 

return M._is_isomorphic(other, certificate=certificate) 

 

def _isomorphism(self, other): 

""" 

Return isomorphism from ``self`` to ``other``, if such an isomorphism exists. 

 

Internal version that performs no checks on input. 

 

INPUT: 

 

- ``other`` -- a matroid 

 

OUTPUT: 

 

A dictionary, or ``None``. 

 

EXAMPLES:: 

 

sage: M1 = Matroid(range(4), graphs.CycleGraph(4)) 

sage: M2 = Matroid(range(4), graphs.CompleteBipartiteGraph(2,2)) 

sage: M1._isomorphism(matroids.named_matroids.BetsyRoss()) 

sage: M1._isomorphism(M2) 

{0: 0, 1: 1, 2: 2, 3: 3} 

sage: M3 = matroids.Uniform(3,4) 

sage: M1._isomorphism(M3) 

{0: 0, 1: 1, 2: 2, 3: 3} 

 

:: 

 

sage: edgelist = [(0,1,'a'),(0,2,'b'),(0,3,'c'),(1,2,'d'),(1,3,'e'),(2,3,'f')] 

sage: M = Matroid(Graph(edgelist)) 

sage: N = Matroid(range(6), graphs.WheelGraph(4)) 

sage: M._isomorphism(N) 

{'a': 2, 'b': 4, 'c': 5, 'd': 0, 'e': 1, 'f': 3} 

sage: O = Matroid(Graph(edgelist), regular=True) 

sage: M._isomorphism(O) 

{'a': 'a', 'b': 'c', 'c': 'b', 'd': 'e', 'e': 'd', 'f': 'f'} 

""" 

return self.is_isomorphic(other, certificate=True)[1] 

 

def is_valid(self): 

""" 

Test if the data obey the matroid axioms. 

 

Since a graph is used for the data, this is always the case. 

 

OUTPUT: 

 

``True``. 

 

EXAMPLES:: 

 

sage: M = matroids.CompleteGraphic(4); M 

M(K4): Graphic matroid of rank 3 on 6 elements 

sage: M.is_valid() 

True 

""" 

return True 

 

# Graphic methods: 

 

def graph(self): 

""" 

Return the graph that represents the matroid. 

 

The graph will always have loops and multiedges enabled. 

 

OUTPUT: 

 

A Graph. 

 

EXAMPLES:: 

 

sage: M = Matroid(Graph([(0, 1, 'a'), (0, 2, 'b'), (0, 3, 'c')])) 

sage: M.graph().edges() 

[(0, 1, 'a'), (0, 2, 'b'), (0, 3, 'c')] 

sage: M = Matroid(graphs.CompleteGraph(5)) 

sage: M.graph() 

Looped multi-graph on 5 vertices 

""" 

# Return a mutable graph 

return self._G.copy(data_structure='sparse') 

 

def vertex_map(self): 

""" 

Return a dictionary mapping the input vertices to the current vertices. 

 

The graph for the matroid is alway connected. If the constructor is 

given a graph with multiple components, it will connect them. The 

Python dictionary given by this method has the vertices from the 

input graph as keys, and the corresponding vertex label after any 

merging as values. 

 

OUTPUT: 

 

A dictionary. 

 

EXAMPLES:: 

 

sage: G = Graph([(0, 1), (0, 2), (1, 2), (3, 4), (3, 5), (4, 5), 

....: (6, 7), (6, 8), (7, 8), (8, 8), (7, 8)], multiedges=True, loops=True) 

sage: M = Matroid(range(G.num_edges()), G) 

sage: M.graph().edges() 

[(0, 1, 0), 

(0, 2, 1), 

(1, 2, 2), 

(2, 4, 3), 

(2, 5, 4), 

(4, 5, 5), 

(5, 7, 6), 

(5, 8, 7), 

(7, 8, 8), 

(7, 8, 9), 

(8, 8, 10)] 

sage: M.vertex_map() 

{0: 0, 1: 1, 2: 2, 3: 2, 4: 4, 5: 5, 6: 5, 7: 7, 8: 8} 

""" 

return copy(self._vertex_map) 

 

def groundset_to_edges(self, X): 

""" 

Return a list of edges corresponding to a set of ground set elements. 

 

INPUT: 

 

- ``X`` -- a subset of the ground set 

 

OUTPUT: 

 

A list of graph edges. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: M.groundset_to_edges([2,3,4]) 

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

sage: M.groundset_to_edges([2,3,4,5]) 

Traceback (most recent call last): 

... 

ValueError: input must be a subset of the ground set 

""" 

for x in X: 

if x not in self._groundset: 

raise ValueError("input must be a subset of the ground set") 

return self._groundset_to_edges(X) 

 

def _groundset_to_edges(self, X): 

""" 

Return a list of edges corresponding to a set of ground set elements. 

 

INPUT: 

 

- ``X`` -- a subset of the ground set 

 

OUTPUT: 

 

A list of graph edges. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: M._groundset_to_edges([2,3,4]) 

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

""" 

return [(self._groundset_edge_map[x][0], self._groundset_edge_map[x][1], x) for x in X] 

 

def subgraph_from_set(self, X): 

""" 

Return the subgraph corresponding to the matroid restricted to `X`. 

 

INPUT: 

 

- ``X`` -- a subset of the ground set 

 

OUTPUT: 

 

A Graph. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: M.subgraph_from_set([0,1,2]) 

Looped multi-graph on 3 vertices 

sage: M.subgraph_from_set([3,4,5]) 

Traceback (most recent call last): 

... 

ValueError: input must be a subset of the ground set 

""" 

for x in X: 

if x not in self._groundset: 

raise ValueError("input must be a subset of the ground set") 

return self._subgraph_from_set(X) 

 

def _subgraph_from_set(self, X): 

""" 

Return the subgraph corresponding to `M` restricted to `X`. 

 

INPUT: 

 

- ``X`` -- a subset of the ground set 

 

OUTPUT: 

 

A Graph. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: M._subgraph_from_set([0,1,2]) 

Looped multi-graph on 3 vertices 

""" 

edge_list = self._groundset_to_edges(X) 

return Graph(edge_list, loops=True, multiedges=True) 

 

def graphic_extension(self, u, v=None, element=None): 

""" 

Return a graphic matroid extended by a new element. 

 

A new edge will be added between ``u`` and ``v``. If ``v`` is not 

specified, then a loop is added on ``u``. 

 

INPUT: 

 

- ``u`` -- a vertex in the matroid's graph 

- ``v`` -- (optional) another vertex 

- ``element`` -- (optional) the label of the new element 

 

OUTPUT: 

 

A GraphicMatroid with the specified element added. Note that if ``v`` is not 

specifies or if ``v`` is ``u``, then the new element will be a loop. If the 

new element's label is not specified, it will be generated automatically. 

 

EXAMPLES:: 

 

sage: M = matroids.CompleteGraphic(4) 

sage: M1 = M.graphic_extension(0,1,'a'); M1 

Graphic matroid of rank 3 on 7 elements 

sage: M1.graph().edges() 

[(0, 1, 0), (0, 1, 'a'), (0, 2, 1), (0, 3, 2), (1, 2, 3), (1, 3, 4), (2, 3, 5)] 

sage: M2 = M1.graphic_extension(3); M2 

Graphic matroid of rank 3 on 8 elements 

 

:: 

 

sage: M = Matroid(range(10), graphs.PetersenGraph()) 

sage: M.graphic_extension(0, 'b', 'c').graph().vertices() 

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

sage: M.graphic_extension('a', 'b', 'c').graph().vertices() 

Traceback (most recent call last): 

... 

ValueError: u must be an existing vertex 

 

TESTS:: 

 

sage: M = Matroid(graphs.EmptyGraph()) 

sage: M.graphic_extension(0) 

Graphic matroid of rank 0 on 1 elements 

sage: M.graphic_extension(0, 1, 'a') 

Graphic matroid of rank 1 on 1 elements 

 

""" 

# This will possibly make a coloop if v is a new vertex 

if element is None: 

element = newlabel(self.groundset()) 

elif element in self.groundset(): 

raise ValueError("cannot extend by element already in ground set") 

if v is None: 

v = u 

G = self.graph() 

if u not in G: 

raise ValueError("u must be an existing vertex") 

G.add_edge(u, v, element) 

return GraphicMatroid(G) 

 

def graphic_extensions(self, element=None, vertices=None, simple=False): 

""" 

Return an iterable containing the graphic extensions. 

 

This method iterates over the vertices in the input. If ``simple == False``, 

it first extends by a loop. It will then add an edge between every pair 

of vertices in the input, skipping pairs of vertices with an edge already 

between them if ``simple == True``. 

 

This method only considers the current graph presentation, and 

does not take 2-isomorphism into account. Use 

:meth:`twist <sage.matroids.graphic_matroid.GraphicMatroid.twist>` or 

:meth:`one_sum <sage.matroids.graphic_matroid.GraphicMatroid.one_sum>` 

if you wish to change the graph presentation. 

 

INPUT: 

 

- ``element`` -- (optional) the name of the newly added element in 

each extension 

- ``vertices`` -- (optional) a set of vertices over which the extension 

may be taken 

- ``simple`` -- (default: ``False``) if true, extensions by loops and parallel 

elements are not taken 

 

OUTPUT: 

 

An iterable containing instances of GraphicMatroid. If ``vertices`` is not 

specified, every vertex is used. 

 

.. NOTE:: 

 

The extension by a loop will always occur unless ``simple == True``. 

The extension by a coloop will never occur. 

 

EXAMPLES:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: I = M.graphic_extensions('a') 

sage: for N in I: 

....: N.graph().edges() 

[(0, 0, 'a'), (0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4)] 

[(0, 1, 0), (0, 1, 'a'), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4)] 

[(0, 1, 0), (0, 2, 1), (0, 2, 'a'), (1, 2, 2), (1, 3, 3), (2, 3, 4)] 

[(0, 1, 0), (0, 2, 1), (0, 3, 'a'), (1, 2, 2), (1, 3, 3), (2, 3, 4)] 

[(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 2, 'a'), (1, 3, 3), (2, 3, 4)] 

[(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (1, 3, 'a'), (2, 3, 4)] 

[(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (2, 3, 'a')] 

 

:: 

 

sage: M = Matroid(graphs.CompleteBipartiteGraph(3,3)) 

sage: I = M.graphic_extensions(simple=True) 

sage: sum (1 for i in I) 

6 

sage: I = M.graphic_extensions(vertices=[0,1,2]) 

sage: sum (1 for i in I) 

4 

""" 

G = self.graph() 

if element is None: 

element = newlabel(self.groundset()) 

elif element in self.groundset(): 

raise ValueError("cannot extend by element already in ground set") 

if vertices is None: 

vertices = self._G.vertices() 

elif not set(vertices).issubset(self._G.vertices()): 

raise ValueError("vertices are not all in the graph") 

 

# First extend by a loop, then consider every pair of vertices. 

# Put the loop on the first vertex. 

if not simple: 

G.add_edge(vertices[0], vertices[0], element) 

yield GraphicMatroid(G) 

G.delete_edge(vertices[0], vertices[0], element) 

 

pairs = combinations(vertices, 2) 

for p in pairs: 

if not simple or not G.has_edge(p[0], p[1]): 

G.add_edge(p[0], p[1], element) 

yield GraphicMatroid(G) 

G.delete_edge(p[0], p[1], element) 

 

def graphic_coextension(self, u, v=None, X=None, element=None): 

""" 

Return a matroid coextended by a new element. 

 

A coextension in a graphic matroid is the opposite of contracting an edge; 

that is, a vertex is split, and a new edge is added between the resulting 

vertices. This method will create a new vertex `v` adjacent to `u`, 

and move the edges indicated by `X` from `u` to `v`. 

 

INPUT: 

 

- ``u`` -- the vertex to be split 

- ``v`` -- (optional) the name of the new vertex after splitting 

- ``X`` -- (optional) a list of the matroid elements corresponding to 

edges incident to ``u`` that move to the new vertex after splitting 

- ``element`` -- (optional) The name of the newly added element 

 

OUTPUT: 

 

An instance of GraphicMatroid coextended by the new element. If ``X`` 

is not specified, the new element will be a coloop. 

 

.. NOTE:: 

 

A loop on ``u`` will stay a loop unless it is in ``X``. 

 

EXAMPLES:: 

 

sage: G = Graph([(0, 1, 0), (0, 2, 1), (0, 3, 2), (0, 4, 3), (1, 2, 4), (1, 4, 5), (2, 3, 6), (3, 4, 7)]) 

sage: M = Matroid(G) 

sage: M1 = M.graphic_coextension(0, X=[1,2], element='a') 

sage: M1.graph().edges() 

[(0, 1, 0), 

(0, 4, 3), 

(0, 5, 'a'), 

(1, 2, 4), 

(1, 4, 5), 

(2, 3, 6), 

(2, 5, 1), 

(3, 4, 7), 

(3, 5, 2)] 

 

TESTS:: 

 

sage: M = Matroid(range(3), graphs.CycleGraph(3)) 

sage: M = M.graphic_extension(0, element='a') 

sage: M.graph().edges() 

[(0, 0, 'a'), (0, 1, 0), (0, 2, 1), (1, 2, 2)] 

sage: M1 = M.graphic_coextension(0, X=[1], element='b') 

sage: M1.graph().edges() 

[(0, 0, 'a'), (0, 1, 0), (0, 3, 'b'), (1, 2, 2), (2, 3, 1)] 

sage: M2 = M.graphic_coextension(0, X=[1, 'a'], element='b') 

sage: M2.graph().edges() 

[(0, 1, 0), (0, 3, 'a'), (0, 3, 'b'), (1, 2, 2), (2, 3, 1)] 

 

:: 

 

sage: M = Matroid(graphs.CycleGraph(3)) 

sage: M = M.graphic_coextension(u=2, element='a') 

sage: M.graph() 

Looped multi-graph on 4 vertices 

sage: M.graph().loops() 

[] 

sage: M = M.graphic_coextension(u=2, element='a') 

Traceback (most recent call last): 

... 

ValueError: cannot extend by element already in ground set 

sage: M = M.graphic_coextension(u=4) 

Traceback (most recent call last): 

... 

ValueError: u must be an existing vertex 

 

TESTS:: 

 

sage: M = Matroid(graphs.EmptyGraph()) 

sage: M.graphic_coextension(u=0) 

Graphic matroid of rank 1 on 1 elements 

 

sage: M = Matroid(graphs.DiamondGraph()) 

sage: N = M.graphic_coextension(0,'q') 

sage: N.graph().vertices() 

[0, 1, 2, 3, 'q'] 

 

:: 

 

sage: M = Matroid(range(5), graphs.DiamondGraph()) 

sage: N = M.graphic_coextension(u=3, v=5, element='a') 

sage: N.graph().edges() 

[(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 5, 'a')] 

sage: N = M.graphic_coextension(u=3, element='a') 

sage: N.graph().edges() 

[(0, 1, 0), (0, 2, 1), (1, 2, 2), (1, 3, 3), (2, 3, 4), (3, 4, 'a')] 

sage: N = M.graphic_coextension(u=3, v=3, element='a') 

Traceback (most recent call last): 

... 

ValueError: u and v must be distinct 

""" 

if element is None: 

element = newlabel(self.groundset()) 

else: 

if element in self.groundset(): 

raise ValueError("cannot extend by element already in ground set") 

 

if u not in self._G.vertices(): 

raise ValueError("u must be an existing vertex") 

if v == u: 

raise ValueError("u and v must be distinct") 

# To prevent an error for iterating over None: 

if X is None: 

X = [] 

 

G = self.graph() 

vertices = G.vertices() 

if v is None: 

v = G.add_vertex() 

 

elif v in G: 

raise ValueError("vertex is already in the graph") 

if u not in vertices: 

G.add_edge(u, v, element) 

return GraphicMatroid(G) 

 

edgelist = self.groundset_to_edges(X) 

 

split_vertex(G, u, v, edgelist) 

G.add_edge(u, v, element) 

 

return GraphicMatroid(G) 

 

def graphic_coextensions(self, vertices=None, v=None, element=None, cosimple=False): 

""" 

Return an iterator of graphic coextensions. 

 

This method iterates over the vertices in the input. If ``cosimple == False``, 

it first coextends by a coloop and series edge for every edge incident 

with the vertices. For vertices of degree four or higher, it will 

consider the ways to partition the vertex into two sets of cardinality 

at least two, and these will be the edges incident with the vertices 

after splitting. 

 

At most one series coextension will be taken for each series class. 

 

INPUT: 

 

- ``vertices`` -- (optional) the vertices to be split 

- ``v`` -- (optional) the name of the new vertex 

- ``element`` -- (optional) the name of the new element 

- ``cosimple`` -- (default: ``False``) if true, coextensions 

by a coloop or series elements will not be taken 

 

OUTPUT: 

 

An iterable containing instances of GraphicMatroid. If ``vertices`` is not 

specified, the method iterates over all vertices. 

 

EXAMPLES:: 

 

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

sage: M = Matroid(range(8), G) 

sage: I = M.graphic_coextensions(vertices=[0], element='a') 

sage: for N in I: 

....: N.graph().edges_incident(0) 

[(0, 1, 0), (0, 2, 1), (0, 3, 2), (0, 4, 3), (0, 5, 'a')] 

[(0, 2, 1), (0, 3, 2), (0, 4, 3), (0, 5, 'a')] 

[(0, 1, 0), (0, 2, 1), (0, 3, 2), (0, 5, 'a')] 

[(0, 1, 0), (0, 3, 2), (0, 4, 3), (0, 5, 'a')] 

[(0, 1, 0), (0, 2, 1), (0, 4, 3), (0, 5, 'a')] 

[(0, 2, 1), (0, 3, 2), (0, 5, 'a')] 

[(0, 1, 0), (0, 3, 2), (0, 5, 'a')] 

[(0, 1, 0), (0, 2, 1), (0, 5, 'a')] 

 

:: 

 

sage: N = Matroid(range(4), graphs.CycleGraph(4)) 

sage: I = N.graphic_coextensions(element='a') 

sage: for N1 in I: 

....: N1.graph().edges() 

[(0, 1, 0), (0, 3, 1), (0, 4, 'a'), (1, 2, 2), (2, 3, 3)] 

[(0, 1, 0), (0, 3, 1), (1, 4, 2), (2, 3, 3), (2, 4, 'a')] 

sage: sum(1 for n in N.graphic_coextensions(cosimple=True)) 

0 

 

TESTS:: 

 

sage: M = Matroid(graphs.EmptyGraph()) 

sage: M.graphic_coextension(0) 

Graphic matroid of rank 1 on 1 elements 

sage: I = M.graphic_coextensions(element='a') 

sage: for m in I: 

....: m.graph().edges() 

[(0, 1, 'a')] 

sage: N = Matroid(graphs.CycleGraph(4)) 

sage: I = N.graphic_coextensions(vertices=[3, 4], element='a') 

sage: next(I) 

Traceback (most recent call last): 

... 

ValueError: vertices are not all in the graph 

 

We expect 136 graphic coextensions of an 8-spoked wheel: 128 extensions 

from the center vertex because there are (256/2) ways to put the 8 

center edges into 2 sets, and then 8 more for series extensions of the 

rims:: 

 

sage: M = Matroid(graphs.WheelGraph(9)) 

sage: I = M.graphic_coextensions() 

sage: sum(1 for N in I) 

136 

sage: I = M.graphic_coextensions(cosimple=True) 

sage: sum(1 for N in I) 

119 

sage: sum(1 for N in Matroid(graphs.WheelGraph(8)).graphic_coextensions()) 

71 

 

This graph has max degree 3, so the only series extensions should be 

non-cosimple, ie. a coloop and one for every coseries class. 

12 total:: 

 

sage: edgedict = {0:[1,2,3], 1:[2,4], 2:[3], 3:[6], 4:[5,7], 5:[6,7], 6:[7]} 

sage: M = Matroid(range(12), Graph(edgedict)) 

sage: sorted(M.coclosure([4])) 

[4, 6] 

sage: sum(1 for N in M.graphic_coextensions()) 

12 

sage: sum(1 for N in M.graphic_coextensions(cosimple=True)) 

0 

""" 

G = self.graph() 

if element is None: 

element = newlabel(self.groundset()) 

elif element in self.groundset(): 

raise ValueError("cannot extend by element already in groundset") 

if vertices is None: 

vertices = self._G.vertices() 

elif not set(vertices).issubset(self._G.vertices()): 

raise ValueError("vertices are not all in the graph") 

 

if v is None: 

# we just need to know what the vertex's name will be 

v = G.add_vertex() 

G.delete_vertex(v) 

elif v in G: 

raise ValueError("vertex is already in the graph") 

 

if not cosimple: 

# First extend by a coloop on the first vertex. 

G.add_edge(vertices[0], v, element) 

yield GraphicMatroid(G) 

G.delete_vertex(v) 

 

# Next add an edge in series, for every series class in the input 

edges = set(G.edges_incident(vertices)) 

while edges: 

u0, v0, l = edges.pop() 

G.delete_edge(u0, v0, l) 

# place the new element on v0 if v0 is a vertex from input 

if v0 in vertices: 

G.add_edge(u0, v, l) 

G.add_edge(v, v0, element) 

else: 

G.add_edge(u0, v, element) 

G.add_edge(v, v0, l) 

yield GraphicMatroid(G) 

G.delete_vertex(v) 

G.add_edge(u0, v0, l) 

 

edges.difference_update(self.groundset_to_edges(self.coclosure([l]))) 

 

# If a vertex has degree 1, or 2, or 3, we already handled it. 

for u in vertices: 

if G.degree(u) > 3: 

elts_incident = [l for (u0, v0, l) in G.edges_incident(u)] 

x = elts_incident.pop() 

for i in range(1, (len(elts_incident) - Integer(1))): 

groups = combinations(elts_incident, i) 

for g in groups: 

g = list(g) 

g.append(x) 

yield self.graphic_coextension( 

X=g, u=u, v=v, element=element) 

 

def twist(self, X): 

""" 

Perform a Whitney twist on the graph. 

 

`X` must be part of a 2-separation. 

The connectivity of `X` must be 1, and the subgraph induced by `X` must 

intersect the subgraph induced by the rest of the elements on exactly 

two vertices. 

 

INPUT: 

 

- ``X`` -- the set of elements to be twisted with respect 

to the rest of the matroid 

 

OUTPUT: 

 

An instance of ``GraphicMatroid`` isomorphic to this matroid but with 

a graph that is not necessarily isomorphic. 

 

EXAMPLES:: 

 

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

sage: M = Matroid(Graph(edgelist, multiedges=True)) 

sage: M1 = M.twist([0,1,2]); M1.graph().edges() 

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

sage: M2 = M.twist([0,1,3]) 

Traceback (most recent call last): 

... 

ValueError: the input must display a 2-separation that is not a 1-separation 

 

TESTS:: 

 

sage: edgedict = {0: [1, 2], 1: [2, 3], 2: [3], 3: [4, 5], 4: [5]} 

sage: M = Matroid(range(8), Graph(edgedict)) 

sage: M.graph().edges() 

[(0, 1, 0), 

(0, 2, 1), 

(1, 2, 2), 

(1, 3, 3), 

(2, 3, 4), 

(3, 4, 5), 

(3, 5, 6), 

(4, 5, 7)] 

sage: M1 = M.twist([0, 1]); M1.graph().edges() 

[(0, 1, 1), 

(0, 2, 0), 

(1, 2, 2), 

(1, 3, 3), 

(2, 3, 4), 

(3, 4, 5), 

(3, 5, 6), 

(4, 5, 7)] 

sage: M2 = M1.twist([0, 1, 2]); M2.graph().edges() 

[(0, 1, 0), 

(0, 2, 1), 

(1, 2, 2), 

(1, 3, 3), 

(2, 3, 4), 

(3, 4, 5), 

(3, 5, 6), 

(4, 5, 7)] 

sage: M1 == M 

False 

sage: M2 == M 

True 

sage: M2.twist([3, 4]) 

Traceback (most recent call last): 

... 

ValueError: too many vertices in the intersection 

""" 

# We require two things: 

# (1) The connectivity of X is 1, 

# (2) X intersects the rest of the graph on 2 vertices 

if not set(X).issubset(self.groundset()): 

raise ValueError("X must be a subset of the ground set") 

connectivity = self.connectivity(X) 

if connectivity != 1: 

raise ValueError("the input must display a 2-separation " 

+ "that is not a 1-separation") 

 

# Determine the vertices 

X_edges = self.groundset_to_edges(X) 

X_vertices = set([e[0] for e in X_edges]).union( 

[e[1] for e in X_edges]) 

Y_edges = self.groundset_to_edges(self.groundset().difference(set(X))) 

Y_vertices = set([e[0] for e in Y_edges]).union( 

[e[1] for e in Y_edges]) 

vertices = X_vertices.intersection(Y_vertices) 

if len(vertices) != 2: 

raise ValueError("too many vertices in the intersection") 

a = list(vertices)[0] 

b = list(vertices)[1] 

 

edges = [(u, v, l) for (u, v, l) in X_edges if ( 

u in vertices or v in vertices)] 

G = self.graph() 

for (u, v, l) in edges: 

G.delete_edge(u, v, l) 

if u == a: 

u = b 

elif u == b: 

u = a 

if v == a: 

v = b 

elif v == b: 

v = a 

G.add_edge(u, v, l) 

return GraphicMatroid(G) 

 

 

def one_sum(self, X, u, v): 

""" 

Arrange matroid components in the graph. 

 

The matroid's graph must be connected even if the matroid is not 

connected, but if there are multiple matroid components, the user may 

choose how they are arranged in the graph. This method will take the 

block of the graph that represents `X` and attach it by vertex `u` to 

another vertex `v` in the graph. 

 

INPUT: 

 

- ``X`` -- a subset of the ground set 

- ``u`` -- a vertex spanned by the edges of the elements in ``X`` 

- ``v`` -- a vertex spanned by the edges of the elements not in ``X`` 

 

OUTPUT: 

 

An instance of ``GraphicMatroid`` isomorphic to this matroid but with 

a graph that is not necessarily isomorphic. 

 

EXAMPLES:: 

 

sage: edgedict = {0:[1, 2], 1:[2, 3], 2:[3], 3:[4, 5], 6:[4, 5]} 

sage: M = Matroid(range(9), Graph(edgedict)) 

sage: M.graph().edges() 

[(0, 1, 0), 

(0, 2, 1), 

(1, 2, 2), 

(1, 3, 3), 

(2, 3, 4), 

(3, 4, 5), 

(3, 5, 6), 

(4, 6, 7), 

(5, 6, 8)] 

sage: M1 = M.one_sum(u=3, v=1, X=[5, 6, 7, 8]) 

sage: M1.graph().edges() 

[(0, 1, 0), 

(0, 2, 1), 

(1, 2, 2), 

(1, 3, 3), 

(1, 4, 5), 

(1, 5, 6), 

(2, 3, 4), 

(4, 6, 7), 

(5, 6, 8)] 

sage: M2 = M.one_sum(u=4, v=3, X=[5, 6, 7, 8]) 

sage: M2.graph().edges() 

[(0, 1, 0), 

(0, 2, 1), 

(1, 2, 2), 

(1, 3, 3), 

(2, 3, 4), 

(3, 6, 7), 

(3, 7, 5), 

(5, 6, 8), 

(5, 7, 6)] 

 

TESTS:: 

 

sage: M = matroids.CompleteGraphic(4) 

sage: M.one_sum(u=1, v=2, X=[0,1]) 

Traceback (most recent call last): 

... 

ValueError: the input must display a 1-separation 

 

:: 

 

sage: M = Matroid(range(5), graphs.BullGraph()) 

sage: M.graph().edges() 

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

sage: M1 = M.one_sum(u=3, v=0, X=[3,4]) 

Traceback (most recent call last): 

... 

ValueError: too many vertices in the intersection 

 

sage: M1 = M.one_sum(u=3, v=2, X=[3]) 

sage: M1.graph().edges() 

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

 

sage: M2 = M1.one_sum(u=5, v=0, X=[3,4]) 

sage: M2.graph().edges() 

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

 

sage: M = Matroid(range(5), graphs.BullGraph()) 

sage: M.one_sum(u=0, v=1, X=[3]) 

Traceback (most recent call last): 

... 

ValueError: first vertex must be spanned by the input 

 

sage: M.one_sum(u=1, v=3, X=[3]) 

Traceback (most recent call last): 

... 

ValueError: second vertex must be spanned by the rest of the graph 

""" 

# We require two things: 

# (1) The connectivity of X is 0, 

# (2) X intersects the rest of the graph on 1 vertex 

if not set(X).issubset(self.groundset()): 

raise ValueError("X must be a subset of the ground set") 

connectivity = self.connectivity(X) 

if connectivity != 0: 

raise ValueError("the input must display a 1-separation") 

G = self.graph() 

if u not in G or v not in G: 

raise ValueError("the vertices must already be in the graph") 

 

# Determine the vertex 

X_edges = self.groundset_to_edges(X) 

X_vertices = set([e[0] for e in X_edges]).union( 

[e[1] for e in X_edges]) 

if u not in X_vertices: 

raise ValueError("first vertex must be spanned by the input") 

Y_edges = self.groundset_to_edges(self.groundset().difference(set(X))) 

Y_vertices = set([e[0] for e in Y_edges]).union( 

[e[1] for e in Y_edges]) 

if v not in Y_vertices: 

raise ValueError("second vertex must be spanned by " + 

"the rest of the graph") 

vertices = X_vertices.intersection(Y_vertices) 

if len(vertices) != 1: 

raise ValueError("too many vertices in the intersection") 

a = vertices.pop() 

b = G.add_vertex() 

 

edgeset = set(X_edges).intersection(set(G.edges_incident(a))) 

split_vertex(G, a, b, edgeset) 

# If u was the cut vertex, u is now detached from our component 

# so we merge the new vertex. Otherwise we can merge u 

if u == a: 

G.merge_vertices([v, b]) 

else: 

G.merge_vertices([v, u]) 

 

return GraphicMatroid(G) 

 

def regular_matroid(self): 

""" 

Return an instance of RegularMatroid isomorphic to this GraphicMatroid. 

 

EXAMPLES:: 

 

sage: M = matroids.CompleteGraphic(5); M 

M(K5): Graphic matroid of rank 4 on 10 elements 

sage: N = M.regular_matroid(); N 

Regular matroid of rank 4 on 10 elements with 125 bases 

sage: M.equals(N) 

True 

sage: M == N 

False 

""" 

from sage.matroids.constructor import Matroid as ConstructorMatroid 

return ConstructorMatroid(groundset=self._groundset, graph=self._G, 

regular=True)