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

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

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

r""" 

Modular Decomposition 

 

This module implements the function for computing the modular decomposition 

of undirected graphs. 

 

 

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

# Copyright (C) 2017 Lokesh Jain <lokeshj1703@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

""" 

 

from sage.graphs.graph import Graph 

from collections import deque 

from enum import Enum 

 

class NodeType(Enum): 

""" 

NodeType is an enumeration class used to define the various types of  

nodes in modular decomposition tree. 

 

The various node types defined are 

 

- ``PARALLEL`` -- indicates the node is a parallel module 

 

- ``SERIES`` -- indicates the node is a series module 

 

- ``PRIME`` -- indicates the node is a prime module 

 

- ``FOREST`` -- indicates a forest containing trees 

 

- ``NORMAL`` -- indicates the node is normal containing a vertex 

 

""" 

PRIME = 0 

SERIES = 1 

PARALLEL = 2 

NORMAL = 3 

FOREST = -1 

 

def __str__(self): 

return self.name 

 

def __repr__(self): 

return self.name 

 

class NodeSplit(Enum): 

""" 

NodeSplit is an enumeration class which is used to specify the split that  

has occurred at the node or at any of its descendants. 

 

NodeSplit is defined for every node in modular decomposition tree and is  

required during the refinement and promotion phase of modular  

decomposition tree computation. Various node splits defined are 

 

- ``LEFT_SPLIT`` -- indicates a left split has occurred 

 

- ``RIGHT_SPLIT`` -- indicates a right split has occurred 

 

- ``BOTH_SPLIT`` -- indicates both left and right split have occurred 

 

- ``NO_SPLIT`` -- indicates no split has occurred 

 

""" 

LEFT_SPLIT = 1 

RIGHT_SPLIT = 2 

BOTH_SPLIT = 3 

NO_SPLIT = 0 

 

class VertexPosition(Enum): 

""" 

VertexPosition is an enumeration class used to define position of a vertex  

w.r.t source in modular decomposition. 

 

For computing modular decomposition of connected graphs a source vertex is  

chosen. The position of vertex is w.r.t this source vertex. The various  

positions defined are 

 

- ``LEFT_OF_SOURCE`` -- indicates vertex is to left of source and is a  

neighbour of source vertex 

 

- ``RIGHT_OF_SOURCE`` -- indicates vertex is to right of source and is  

connected to but not a neighbour of source vertex 

 

- ``SOURCE`` -- indicates vertex is source vertex 

 

""" 

LEFT_OF_SOURCE = -1 

RIGHT_OF_SOURCE = 1 

SOURCE = 0 

 

class Node: 

""" 

Node class stores information about the node type, node split and index  

of the node in the parent tree.  

 

Node type can be PRIME, SERIES, PARALLEL, NORMAL or FOREST. Node split can  

be NO_SPLIT, LEFT_SPLIT, RIGHT_SPLIT or BOTH_SPLIT. A node is split in the  

refinement phase and the split used is propagated to the ancestors. 

 

- ``node_type`` -- is of type NodeType and specifies the type of node 

 

- ``node_split`` -- is of type NodeSplit and specifies the type of splits  

which have occurred in the node and its descendants 

 

- ``index_in_root`` -- specifies the index of the node in the forest  

obtained after promotion phase 

 

- ``comp_num`` -- specifies the number given to nodes in a (co)component  

before refinement 

 

- ``is_separated`` -- specifies whether a split has occurred with the node  

as the root 

 

""" 

 

def __init__(self, node_type): 

self.node_type = node_type 

self.node_split = NodeSplit.NO_SPLIT 

self.index_in_root = -1 

self.comp_num = -1 

self.is_separated = False 

self.children = [] 

 

def set_node_split(self, node_split): 

""" 

Add node_split to the node split of self.  

 

LEFT_SPLIT and RIGHT_SPLIT can exist together in self as BOTH_SPLIT. 

 

INPUT: 

 

- ``node_split`` -- node_split to be added to self 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

NodeSplit 

sage: node = Node(NodeType.PRIME) 

sage: node.set_node_split(NodeSplit.LEFT_SPLIT) 

sage: node.node_split == NodeSplit.LEFT_SPLIT 

True 

sage: node.set_node_split(NodeSplit.RIGHT_SPLIT) 

sage: node.node_split == NodeSplit.BOTH_SPLIT 

True 

sage: node = Node(NodeType.PRIME) 

sage: node.set_node_split(NodeSplit.BOTH_SPLIT) 

sage: node.node_split == NodeSplit.BOTH_SPLIT 

True 

 

""" 

if self.node_split == NodeSplit.NO_SPLIT: 

self.node_split = node_split 

elif ((self.node_split == NodeSplit.LEFT_SPLIT and 

node_split == NodeSplit.RIGHT_SPLIT) or 

(self.node_split == NodeSplit.RIGHT_SPLIT and 

node_split == NodeSplit.LEFT_SPLIT)): 

self.node_split = NodeSplit.BOTH_SPLIT 

 

def has_left_split(self): 

""" 

Return true if self has LEFT_SPLIT 

 

OUTPUT: 

 

``True`` if node has a left split else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

NodeSplit 

sage: node = Node(NodeType.PRIME) 

sage: node.set_node_split(NodeSplit.LEFT_SPLIT) 

sage: node.has_left_split() 

True 

sage: node = Node(NodeType.PRIME) 

sage: node.set_node_split(NodeSplit.BOTH_SPLIT) 

sage: node.has_left_split() 

True 

 

""" 

return self.node_split == NodeSplit.LEFT_SPLIT or \ 

self.node_split == NodeSplit.BOTH_SPLIT 

 

def has_right_split(self): 

""" 

Return true if self has RIGHT_SPLIT 

 

OUTPUT: 

 

``True`` if node has a right split else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

NodeSplit 

sage: node = Node(NodeType.PRIME) 

sage: node.set_node_split(NodeSplit.RIGHT_SPLIT) 

sage: node.has_right_split() 

True 

sage: node = Node(NodeType.PRIME) 

sage: node.set_node_split(NodeSplit.BOTH_SPLIT) 

sage: node.has_right_split() 

True 

 

""" 

return self.node_split == NodeSplit.RIGHT_SPLIT or \ 

self.node_split == NodeSplit.BOTH_SPLIT 

 

def __str__(self): 

if self.node_type == NodeType.SERIES: 

s = "SERIES " 

elif self.node_type == NodeType.PARALLEL: 

s = "PARALLEL " 

elif self.node_type == NodeType.PRIME: 

s = "PRIME " 

elif self.node_type == NodeType.FOREST: 

s = "FOREST " 

else: 

s = "NORMAL " 

 

s += str(self.children) 

return s 

 

def __repr__(self): 

return self.__str__() 

 

def __eq__(self, other): 

return self.node_type == other.node_type and \ 

self.node_split == other.node_split and \ 

self.index_in_root == other.index_in_root and \ 

self.comp_num == other.comp_num and \ 

self.is_separated == other.is_separated and \ 

self.children == other.children 

 

def modular_decomposition(graph): 

""" 

Compute the modular decomposition tree for the input graph. 

 

The tree structure is represented in form of nested lists. A tree node is  

an object of type Node. The Node object further contains a list of its  

children 

 

INPUT: 

 

- ``graph`` -- The graph for which modular decomposition 

tree needs to be computed 

 

OUTPUT: 

 

A nested list representing the modular decomposition tree computed 

for the graph 

 

EXAMPLES: 

 

The Icosahedral graph is Prime:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, test_modular_decomposition, print_md_tree 

sage: print_md_tree(modular_decomposition(graphs.IcosahedralGraph())) 

PRIME 

8 

5 

1 

11 

7 

0 

6 

9 

2 

4 

10 

3 

 

The Octahedral graph is not Prime:: 

 

sage: print_md_tree(modular_decomposition(graphs.OctahedralGraph())) 

SERIES 

PARALLEL 

2 

3 

PARALLEL 

1 

4 

PARALLEL 

0 

5 

 

Tetrahedral Graph is Series:: 

 

sage: print_md_tree(modular_decomposition(graphs.TetrahedralGraph())) 

SERIES 

3 

2 

1 

0 

 

Modular Decomposition tree containing both parallel and series modules:: 

 

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

sage: g = Graph(d) 

sage: print_md_tree(modular_decomposition(g)) 

SERIES 

5 

PARALLEL 

3 

4 

PARALLEL 

1 

2 

 

TESTS: 

 

Bad Input:: 

 

sage: g = DiGraph() 

sage: modular_decomposition(g) 

Traceback (most recent call last): 

... 

ValueError: Graph must be undirected 

 

Empty Graph is Prime:: 

 

sage: g = Graph() 

sage: modular_decomposition(g) 

PRIME [] 

 

Graph from Marc Tedder implementation of modular decomposition:: 

 

sage: d = {1:[5,4,3,24,6,7,8,9,2,10,11,12,13,14,16,17], 2:[1], \ 

3:[24,9,1], 4:[5,24,9,1], 5:[4,24,9,1], 6:[7,8,9,1], \ 

7:[6,8,9,1], 8:[6,7,9,1], 9:[6,7,8,5,4,3,1], 10:[1], \ 

11:[12,1], 12:[11,1], 13:[14,16,17,1], 14:[13,17,1], \ 

16:[13,17,1], 17:[13,14,16,18,1], 18:[17], 24:[5,4,3,1]} 

sage: g = Graph(d) 

sage: test_modular_decomposition(modular_decomposition(g), g) 

True 

 

Graph from wikipedia link :wikipedia:`Modular_decomposition`:: 

 

sage: d2 = {1:[2,3,4], 2:[1,4,5,6,7], 3:[1,4,5,6,7], 4:[1,2,3,5,6,7], \ 

5:[2,3,4,6,7], 6:[2,3,4,5,8,9,10,11], \ 

7:[2,3,4,5,8,9,10,11], 8:[6,7,9,10,11], 9:[6,7,8,10,11], \ 

10:[6,7,8,9], 11:[6,7,8,9]} 

sage: g = Graph(d2) 

sage: test_modular_decomposition(modular_decomposition(g), g) 

True 

 

Tetrahedral Graph is Series:: 

 

sage: print_md_tree(modular_decomposition(graphs.TetrahedralGraph())) 

SERIES 

3 

2 

1 

0 

 

Modular Decomposition tree containing both parallel and series modules:: 

 

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

sage: g = Graph(d) 

sage: print_md_tree(modular_decomposition(g)) 

SERIES 

5 

PARALLEL 

3 

4 

PARALLEL 

1 

2 

 

""" 

if graph.is_directed(): 

raise ValueError("Graph must be undirected") 

 

if graph.order() == 0: #Empty Graph 

return create_prime_node() 

 

if graph.order() == 1: # Single vertex graph 

root = create_normal_node(next(graph.vertex_iterator())) 

return root 

 

if not graph.is_connected(): 

 

# Parallel case:- The tree contains the MD trees of its connected 

# components as subtrees 

components = graph.connected_components() 

root = create_parallel_node() 

for component in components: 

root.children.append( 

modular_decomposition(graph.subgraph(component))) 

return root 

elif graph.complement().is_connected(): #Prime Graph 

root = create_prime_node() 

else: 

root = create_series_node() #Series Graph 

 

bfs_generator = graph.breadth_first_search(next(graph.vertex_iterator()), 

report_distance=True) 

 

prev_level_distance = -1 # used as a demarker for different levels in bfs 

prev_level_list = [] # stores the vertices in previous level 

 

# dictionary stores the distance of vertices from the SOURCE 

vertex_dist = {} 

 

# dictionary stores the position of vertices w.r.t SOURCE 

vertex_status = {} 

vertex_status[next(graph.vertex_iterator())] = VertexPosition.SOURCE 

 

# Different distances from the source vertex are considered 

# as different levels in the algorithm 

for (vertex, distance) in bfs_generator: 

vertex_dist[vertex] = distance 

 

# Mark the neighbours of source as LEFT_OF_SOURCE as they appear to 

# left of source in the forest, other vertices are marked as 

# RIGHT_OF_SOURCE 

if distance == 1: 

vertex_status[vertex] = VertexPosition.LEFT_OF_SOURCE 

elif distance != 0: 

vertex_status[vertex] = VertexPosition.RIGHT_OF_SOURCE 

 

if distance != prev_level_distance: # On start of new level in BFS 

if prev_level_list: 

# MD Tree is computed for each level and added to the forest 

root.children.append(modular_decomposition( 

graph.subgraph(prev_level_list)) 

) 

prev_level_list = [] 

prev_level_distance = distance 

prev_level_list.append(vertex) 

 

# The last level is left out in the above loop 

root.children.append( 

modular_decomposition(graph.subgraph(prev_level_list))) 

 

# The MD tree for the neighbours of source marked as LEFT_OF_SOURCE 

# are placed left of Source in the forest. root.children[1] is required to  

# be source and root.children[0] is required to be the MD tree for the  

# neighbours therefore, the first two elements in the list are replaced 

root.children[0], root.children[1] = root.children[1], root.children[0] 

 

root.node_type = NodeType.FOREST 

clear_node_split_info(root) 

number_cocomponents(root, vertex_status) 

number_components(root, vertex_status) 

refine(graph, root, vertex_dist, vertex_status) 

promote_left(root) 

promote_right(root) 

promote_child(root) 

assembly(graph, root, vertex_status, vertex_dist) 

 

if root.node_type == NodeType.FOREST: 

return root.children[0] 

else: 

return root 

 

def number_components(root, vertex_status): 

""" 

Function to number the components to the right of SOURCE vertex in the 

forest input to the assembly phase 

 

INPUT: 

 

- ``root`` -- the forest which contains the components and cocomponents 

- ``vertex_status`` -- dictionary which stores the position of vertex 

w.r.t SOURCE 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, number_components 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.append(series_node) 

sage: forest.children.append(parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.RIGHT_OF_SOURCE, \ 

5: VertexPosition.RIGHT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: number_components(forest, vertex_status) 

sage: forest.children[-1].children[0].comp_num 

2 

sage: forest.children[-1].children[1].comp_num 

3 

 

TESTS:: 

 

sage: forest.children[-1].children[0].comp_num == 2 and \ 

forest.children[-1].children[1].comp_num == 3 

True 

sage: forest.children[-2].children[0].comp_num == 1 and \ 

forest.children[-2].children[1].comp_num == 1 

True 

 

""" 

comp_num = 0 

flag = False 

 

if not root: #root is empty 

return ValueError("Input forest {} is empty".format(root)) 

 

for node in root.children: 

 

#flag set to True after source vertex is encountered 

if node.node_type == NodeType.NORMAL and \ 

vertex_status[node.children[0]] == VertexPosition.SOURCE: 

flag = True 

continue 

 

if not flag: # Cocomponents are skipped 

continue 

 

comp_num += recursively_number_parts(node, comp_num, NodeType.PARALLEL) 

 

def number_cocomponents(root, vertex_status): 

""" 

Function to number the cocomponents to the left of SOURCE vertex in the 

forest input to the assembly phase 

 

INPUT: 

 

- ``root`` -- the forest which contains the cocomponents and components 

- ``vertex_status`` -- dictionary which stores the position of vertex 

w.r.t SOURCE 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, number_cocomponents 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(2, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.LEFT_OF_SOURCE, \ 

7: VertexPosition.LEFT_OF_SOURCE} 

sage: number_cocomponents(forest, vertex_status) 

sage: forest.children[1].children[0].comp_num 

1 

sage: forest.children[1].children[1].comp_num 

2 

 

TESTS:: 

 

sage: forest.children[1].children[0].comp_num and \ 

forest.children[1].children[1].comp_num == 2 

True 

sage: forest.children[2].children[0].comp_num == 3 and \ 

forest.children[2].children[1].comp_num == 3 

True 

 

""" 

cocomp_num = 0 

for node in root.children: 

# Only cocomponents are numbered 

if node.node_type == NodeType.NORMAL and \ 

vertex_status[node.children[0]] == VertexPosition.SOURCE: 

break 

cocomp_num += recursively_number_parts(node, cocomp_num, 

NodeType.SERIES) 

 

 

def recursively_number_parts(part_root, part_num, by_type): 

""" 

Recursively number the nodes in the (co)components(parts).  

 

If the node_type of part_root is same as by_type then part_num is  

incremented for subtree at each child of part_root else part is numbered  

by part_num 

 

INPUT: 

 

- ``part_root`` -- root of the part to be numbered 

- ``part_num`` -- input number to be used as reference for numbering 

the (co)components 

- ``by_type`` -- type which determines how numbering is done 

 

OUTPUT: 

 

The value incremented to part_num 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, recursively_number_parts 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: recursively_number_parts(series_node, 1, NodeType.SERIES) 

2 

sage: series_node.comp_num  

1 

sage: series_node.children[0].comp_num 

1 

sage: series_node.children[1].comp_num 

2 

 

TESTS:: 

 

sage: series_node.comp_num == 1 and \ 

series_node.children[0].comp_num == 1 and \ 

series_node.children[1].comp_num == 2 

True 

 

""" 

 

# inner function 

def number_subtree(subtree_root, number): 

""" 

set the ``comp_num`` for all the nodes in the subtree to ``number`` 

 

INPUT: 

 

- ``subtree_root`` -- root of the subtree to be numbered 

- ``number`` -- number assigned to the subtree 

 

""" 

subtree_root.comp_num = number 

if subtree_root.node_type != NodeType.NORMAL: 

for child in subtree_root.children: 

number_subtree(child, number) 

 

orig_part_num = part_num 

 

if part_root.node_type == by_type: 

# if node_type is same as tree's node_type then cocomp_num is 

# incremented before assigning to each subtree 

part_root.comp_num = part_num 

for child in part_root.children: 

number_subtree(child, part_num) 

part_num += 1 

else: 

# entire tree is numbered by cocomp_num 

number_subtree(part_root, part_num) 

part_num += 1 

return part_num - orig_part_num 

 

def assembly(graph, root, vertex_status, vertex_dist): 

""" 

Assemble the forest obtained after the promotion phase into a modular  

decomposition tree. 

 

INPUT: 

 

- ``graph`` -- graph whose MD tree is to be computed 

- ``root`` -- Forest which would be assembled into a MD tree 

- ``vertex_status`` -- Dictionary which stores the position of vertex with  

respect to the source 

- ``vertex_dist`` -- Dictionary which stores the distance of vertex from  

source vertex 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \  

VertexPosition, create_normal_node, number_cocomponents, \ 

number_components, assembly 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(6, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: forest.children[0].comp_num = 1 

sage: forest.children[1].comp_num = 1 

sage: forest.children[1].children[0].comp_num = 1 

sage: forest.children[1].children[1].comp_num = 1 

sage: number_components(forest, vertex_status) 

sage: assembly(g, forest, vertex_status, vertex_dist) 

sage: forest.children 

[PRIME [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], PARALLEL [NORMAL [6], NORMAL [7]], NORMAL [1]]] 

 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: number_cocomponents(forest, vertex_status) 

sage: assembly(g, forest, vertex_status, vertex_dist) 

sage: forest.children 

[PRIME [NORMAL [2], SERIES [NORMAL [4], NORMAL [5], NORMAL [3]], PARALLEL [NORMAL [6], NORMAL [7]], NORMAL [1]]] 

 

""" 

 

# Maps index to the mu computed for the (co)component at the index 

mu = {} 

 

# Stores index in the forest containing the source vertex  

source_index = -1 

 

# Maps index to list of vertices in the (co)component at the index  

vertices_in_component = {} 

 

# comp_num of parent should be equal to comp_num of its first child 

update_comp_num(root) 

 

for index, component in enumerate(root.children): 

 

if component.node_type == NodeType.NORMAL and \ 

vertex_status[component.children[0]] == VertexPosition.SOURCE: 

source_index = root.children.index(component) 

 

vertices_in_component[index] = get_vertices(component) 

component.index_in_root = index 

 

# compute mu values for (co)components 

for index, component in enumerate(root.children): 

if index < source_index: 

mu[index] = compute_mu_for_co_component(graph, index, 

source_index, root, 

vertices_in_component) 

elif index > source_index: 

mu[index] = compute_mu_for_component(graph, index, 

source_index, root, 

vertices_in_component) 

 

mu[source_index] = root.children[source_index] 

 

# stores the leftmost cocomponent included in the module containing 

# source_index 

left = root.children[source_index] 

 

# stores the rightmost component included in the module containing 

# source_index 

right = root.children[source_index] 

 

while len(root.children) != 1: 

# source_index is changed everytime a new module is formed therefore 

# updated. left or right are also updated every time module is formed. 

 

# First series module is attempted 

result, source_index = check_series(root, left, right, 

source_index, mu) 

if result: 

left = root.children[source_index].children[0] 

continue 

 

# If series module cant be formed, parallel is tried 

result, source_index = check_parallel(graph, root, left, right, 

source_index, mu, vertex_dist, 

vertices_in_component) 

if result: 

right = root.children[source_index].children[-1] 

continue 

 

# Finally a prime module is formed if both 

# series and parallel can not be created 

result, source_index = check_prime(graph, root, left, right, 

source_index, mu, vertex_dist, 

vertices_in_component) 

if result: 

if root.children[source_index].children[0].index_in_root != -1: 

left = root.children[source_index].children[0] 

if root.children[source_index].children[-1].index_in_root != -1: 

right = root.children[source_index].children[-1] 

 

 

def update_comp_num(node): 

""" 

Set the comp_num of the node to the comp_num of its first child 

 

INPUT: 

 

- ``node`` -- node whose comp_num needs to be updated 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, update_comp_num 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.comp_num = 2 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: series_node.children[0].comp_num = 3 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(0, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: update_comp_num(forest) 

sage: series_node.comp_num 

3 

sage: forest.comp_num 

2 

 

""" 

if node.node_type != NodeType.NORMAL: 

node.comp_num = node.children[0].comp_num 

for child in node.children: 

update_comp_num(child) 

 

 

def check_prime(graph, root, left, right, 

source_index, mu, vertex_dist, 

vertices_in_component): 

""" 

Assemble the forest to create a prime module. 

 

INPUT: 

 

- ``root`` - forest which needs to be assembled 

- ``left`` - The leftmost fragment of the last module 

- ``right`` - The rightmost fragment of the last module 

- ``source_index`` - index of the tree containing the source vertex 

- ``mu`` - dictionary which maps the (co)components with their mu values. 

- ``vertex_dist`` -- Dictionary which stores the distance of vertex from  

source vertex 

- ``vertices_in_component`` -- Dictionary which stores a list of various  

vertices in a (co)component 

 

OUTPUT: 

 

``[module_formed, source_index]`` where ``module_formed`` is ``True`` if 

module is formed else ``False`` and ``source_index`` is the index of the 

new module which contains the source vertex 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, number_cocomponents, \ 

number_components, check_prime, get_vertices, \ 

compute_mu_for_co_component, compute_mu_for_component 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(6, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: source_index = 2 

sage: vertices_in_component = {} 

sage: mu = {} 

sage: left = right = forest.children[2] 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

....: component.index_in_root = index 

sage: for index, component in enumerate(forest.children): 

....: if index < source_index: 

....: mu[index] = compute_mu_for_co_component(g, index, 

....: source_index, forest, 

....: vertices_in_component) 

....: elif index > source_index: 

....: mu[index] = compute_mu_for_component(g, index, 

....: source_index, forest, 

....: vertices_in_component) 

sage: forest.children[0].comp_num = 1 

sage: forest.children[1].comp_num = 1 

sage: forest.children[1].children[0].comp_num = 1 

sage: forest.children[1].children[1].comp_num = 1 

sage: number_components(forest, vertex_status) 

sage: check_prime(g, forest, left, right, 

....: source_index, mu, vertex_dist, 

....: vertices_in_component) 

[True, 0] 

sage: forest.children 

[PRIME [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], PARALLEL [NORMAL [6], NORMAL [7]], NORMAL [1]]] 

 

""" 

# stores the index of rightmost component included in the prime module 

new_right_index = source_index + 1 if source_index + 1 < \ 

len(root.children) \ 

else source_index 

 

# stores the index of leftmost component included in the prime module 

new_left_index = source_index - 1 if source_index - 1 >= 0 \ 

else source_index 

 

# stores the indices of the cocomponents included in the prime module 

# the cocomponents are extracted one by one from left_queue for adding  

# more components 

left_queue = deque() 

 

# stores the indices of the components included in the prime module 

# the components are extracted one by one from right_queue for adding  

# more cocomponents 

right_queue = deque() 

 

if new_left_index != source_index: 

left_queue.append(new_left_index) 

if new_right_index != source_index: 

right_queue.append(new_right_index) 

 

while left_queue or right_queue: 

 

if left_queue: 

 

# cocomponent indices extracted from the queue 

left_index = left_queue.popleft() 

 

# more components added based on the below condition 

while new_right_index < len(root.children) - 1 and \ 

root.children[new_right_index].index_in_root < \ 

mu[left_index].index_in_root: 

new_right_index += 1 

right_queue.append(new_right_index) 

 

# cocomponent added while cocomponent at left_index 

# has cocomponent to its left with same comp_num 

while has_left_cocomponent_fragment(root, left_index): 

if left_index >= 1: 

left_index -= 1 

if new_left_index > left_index: 

left_queue.append(left_index) 

new_left_index = min(left_index, new_left_index) 

 

if right_queue: 

 

# component indices extracted from the queue 

right_index = right_queue.popleft() 

 

# more cocomponents added based on the below condition 

while new_left_index > 0 and \ 

root.children[new_left_index].index_in_root > \ 

mu[right_index].index_in_root: 

new_left_index -= 1 

left_queue.append(new_left_index) 

 

# component is added while component at right_index 

# has component to its right with same comp_num 

# or has a connected component with vertices at different 

# level from the source vertex 

while has_right_component_fragment(root, right_index) or \ 

has_right_layer_neighbor(graph, root, 

right_index, vertex_dist, 

vertices_in_component): 

 

if has_right_layer_neighbor(graph, root, 

right_index, vertex_dist, 

vertices_in_component): 

new_left_index = 0 

new_right_index = len(root.children) - 1 

break 

 

if right_index + 1 < len(root.children): 

right_index += 1 

if new_right_index < right_index: 

right_queue.append(right_index) 

new_right_index = max(right_index, new_right_index) 

 

node = create_prime_node() 

 

# vertices or modules are added in the prime_module 

for temp in range(new_left_index, new_right_index + 1): 

node.children.append(root.children[temp]) 

 

# list elements included in the prime module 

# are removed from the forest 

root.children[new_left_index:new_right_index + 1] = [] 

 

#insert the newly created prime module in the forest 

root.children.insert(new_left_index, node) 

 

return [True, new_left_index] 

 

 

def check_parallel(graph, root, left, right, 

source_index, mu, vertex_dist, 

vertices_in_component): 

""" 

Assemble the forest to create a parallel module. 

 

INPUT: 

 

- ``root`` -- forest which needs to be assembled 

- ``left`` -- The leftmost fragment of the last module 

- ``right`` -- The rightmost fragment of the last module 

- ``source_index`` -- index of the tree containing the source vertex 

- ``mu`` -- dictionary which maps the (co)components with their mu values. 

- ``vertex_dist`` -- Dictionary which stores the distance of vertex from  

source vertex 

- ``vertices_in_component`` -- Dictionary which stores a list of various  

vertices in a (co)component 

 

OUTPUT: 

 

``[module_formed, source_index]`` where ``module_formed`` is ``True`` if 

module is formed else ``False`` and ``source_index`` is the index of the 

new module which contains the source vertex 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, number_cocomponents, \ 

number_components, check_parallel, get_vertices, \ 

compute_mu_for_co_component, compute_mu_for_component 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(4, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7), create_normal_node(1)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.append(parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 2} 

sage: source_index = 2 

sage: vertices_in_component = {} 

sage: mu = {} 

sage: left = right = forest.children[2] 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

....: component.index_in_root = index 

sage: for index, component in enumerate(forest.children): 

....: if index < source_index: 

....: mu[index] = compute_mu_for_co_component(g, index, 

....: source_index, forest, 

....: vertices_in_component) 

....: elif index > source_index: 

....: mu[index] = compute_mu_for_component(g, index, 

....: source_index, forest, 

....: vertices_in_component) 

sage: number_components(forest, vertex_status) 

sage: check_parallel(g, forest, left, right, 

....: source_index, mu, vertex_dist, 

....: vertices_in_component) 

[True, 2] 

sage: forest.children 

[NORMAL [2], 

SERIES [NORMAL [4], NORMAL [5]], 

PARALLEL [NORMAL [3], NORMAL [6], NORMAL [7], NORMAL [1]]] 

 

""" 

 

# stores the index of rightmost component included in the parallel module 

new_right_index = source_index 

 

while new_right_index + 1 < len(root.children): 

 

# component at new_right_index + 1 is added only if it doesn't have 

# a component to its right with same comp_num 

if has_right_component_fragment(root, new_right_index + 1): 

break 

 

# component at new_right_index + 1 is added only if it doesn't have a 

# connected component to its right with vertices at different level 

# from its vertices 

if has_right_layer_neighbor(graph, root, new_right_index + 1, 

vertex_dist, vertices_in_component): 

break 

 

# stores the index in root of new component to be added in the 

# parallel module 

i = root.children[new_right_index + 1].index_in_root 

 

# condition for adding more components in the parallel module 

if mu[i].index_in_root >= left.index_in_root: 

new_right_index += 1 

else: 

break 

 

# if new_right_index > source_index then only parallel 

# module can be formed 

if source_index != new_right_index: 

node = create_parallel_node() 

temp = source_index 

for temp in range(source_index, new_right_index + 1): 

 

# if module X to be included in the new parallel module Y is also 

# parallel then children of X and not X are included in Y 

if root.children[temp].node_type == NodeType.PARALLEL: 

for child in root.children[temp].children: 

node.children.append(child) 

child.index_in_root = root.children[temp].index_in_root 

else: 

node.children.append(root.children[temp]) 

 

# list elements included in the parallel module are removed from the 

# forest 

root.children[source_index:new_right_index + 1] = [] 

 

# insert the newly created parallel module into the forest 

root.children.insert(source_index, node) 

 

return [True, source_index] 

 

# no parallel module was formed 

return [False, source_index] 

 

 

def check_series(root, left, right, source_index, mu): 

""" 

Assemble the forest to create a series module. 

 

- ``root`` -- forest which needs to be assembled 

- ``left`` -- The leftmost fragment of the last module 

- ``right`` -- The rightmost fragment of the last module 

- ``source_index`` -- index of the tree containing the source vertex 

- ``mu`` -- dictionary which maps the (co)components with their mu values. 

- ``vertex_dist`` -- Dictionary which stores the distance of vertex from  

source vertex 

- ``vertices_in_component`` -- Dictionary which stores a list of various  

vertices in a (co)component 

 

OUTPUT: 

 

``[module_formed, source_index]`` where ``module_formed`` is ``True`` if 

module is formed else ``False`` and ``source_index`` is the index of the 

new module which contains the source vertex 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, number_cocomponents, \ 

number_components, check_series, get_vertices, \ 

compute_mu_for_co_component, compute_mu_for_component 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: source_index = 2 

sage: vertices_in_component = {} 

sage: mu = {} 

sage: left = right = forest.children[2] 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

....: component.index_in_root = index 

sage: for index, component in enumerate(forest.children): 

....: if index < source_index: 

....: mu[index] = compute_mu_for_co_component(g, index, 

....: source_index, forest, 

....: vertices_in_component) 

....: elif index > source_index: 

....: mu[index] = compute_mu_for_component(g, index, 

....: source_index, forest, 

....: vertices_in_component) 

sage: number_cocomponents(forest, vertex_status) 

sage: number_components(forest, vertex_status) 

sage: check_series(forest, left, right, 

....: source_index, mu) 

[True, 1] 

sage: forest.children 

[NORMAL [2], 

SERIES [NORMAL [4], NORMAL [5], NORMAL [3]], 

PARALLEL [NORMAL [6], NORMAL [7]], 

NORMAL [1]] 

 

""" 

 

# stores the index of leftmost component included in the parallel module 

new_left_index = source_index 

 

while new_left_index > 0: 

 

# cocomponent at new_left_index - 1 is added only if it doesn't have 

# a cocomponent to its left with same comp_num 

if has_left_cocomponent_fragment(root, new_left_index - 1): 

break 

 

# stores the index in root of new cocomponent to be added in the 

# series module 

i = root.children[new_left_index - 1].index_in_root 

 

# condition for adding more cocomponents in the series module 

if mu[i].index_in_root <= right.index_in_root: 

new_left_index -= 1 

else: 

break 

 

# if new_left_index < source_index then only series module can be formed 

if source_index != new_left_index: 

node = create_series_node() 

for temp in range(new_left_index, source_index + 1): 

 

if root.children[temp].node_type == NodeType.SERIES: 

# if module X to be included in the new series module Y is 

# also series then children of X and not X are included in Y 

for child in root.children[temp].children: 

child.index_in_root = root.children[temp].index_in_root 

node.children.append(child) 

else: 

node.children.append(root.children[temp]) 

 

# list elements included in the series module 

# are removed from the forest 

root.children[new_left_index:source_index + 1] = [] 

 

# insert the newly created series module into the forest 

root.children.insert(new_left_index, node) 

 

return [True, new_left_index] 

 

# no series module could be formed 

return [False, new_left_index] 

 

 

def has_left_cocomponent_fragment(root, cocomp_index): 

""" 

Return True if cocomponent at cocomp_index has a cocomponent to its left  

with same comp_num 

 

INPUT: 

 

- ``root`` -- The forest to which cocomponent belongs 

- ``cocomp_index`` -- Index at which cocomponent is present in root 

 

OUTPUT: 

 

``True`` if cocomponent at cocomp_index has a cocomponent 

to its left with same comp_num else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, has_left_cocomponent_fragment 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: forest.children[0].comp_num = 1 

sage: forest.children[1].comp_num = 1 

sage: forest.children[1].children[0].comp_num = 1 

sage: forest.children[1].children[1].comp_num = 1 

sage: has_left_cocomponent_fragment(forest, 1) 

True 

sage: has_left_cocomponent_fragment(forest, 0) 

False 

 

""" 

for index in range(cocomp_index): 

if root.children[index].comp_num == \ 

root.children[cocomp_index].comp_num: 

return True 

return False 

 

 

def has_right_component_fragment(root, comp_index): 

""" 

Return True if component at comp_index has a component to its right with  

same comp_num 

 

INPUT: 

 

- ``root`` -- The forest to which component belongs 

- ``comp_index`` -- Index at which component is present in root 

 

OUTPUT: 

 

``True`` if component at comp_index has a component 

to its right with same comp_num else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, has_right_component_fragment 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: forest.children[3].comp_num = 1 

sage: forest.children[4].comp_num = 1 

sage: has_right_component_fragment(forest, 3) 

True 

sage: has_right_component_fragment(forest, 4) 

False 

 

""" 

for index in range(comp_index + 1, len(root.children)): 

if root.children[index].comp_num == \ 

root.children[comp_index].comp_num: 

return True 

return False 

 

 

def has_right_layer_neighbor(graph, root, comp_index, 

vertex_dist, vertices_in_component): 

""" 

Return True if component at comp_index has a connected component to its  

right with vertices at different level from the source vertex 

 

INPUT: 

 

- ``root`` -- The forest to which component belongs 

- ``comp_index`` -- Index at which component is present in root 

- ``vertex_dist`` -- Dictionary which stores the distance of vertex from  

source vertex 

- ``vertices_in_component`` -- Dictionary which stores a list of various  

vertices in a (co)component 

 

OUTPUT: 

 

``True`` if component at comp_index has a right layer neighbor 

else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, get_vertices, has_right_layer_neighbor 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: vertices_in_component = {} 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

....: component.index_in_root = index 

sage: has_right_layer_neighbor(g, forest, 3, vertex_dist,  

....: vertices_in_component) 

True 

 

""" 

for index in range(comp_index + 1, len(root.children)): 

 

# check vertex in component at index has different level from vertex 

# in component at comp_index and are connected to each other 

if ((vertex_dist[get_vertex_in(root.children[index])] > 

vertex_dist[get_vertex_in(root.children[comp_index])] 

) and 

(is_component_connected(graph, root.children[index].index_in_root, 

root.children[comp_index].index_in_root, 

vertices_in_component) 

)): 

return True 

 

return False 

 

 

def get_vertex_in(node): 

""" 

Return the first vertex encountered in the depth-first traversal of the  

tree rooted at node 

 

INPUT: 

 

- ``tree`` -- input modular decomposition tree 

 

OUTPUT: 

 

Return the first vertex encountered in recursion 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, get_vertex_in 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: forest.children.insert(1, series_node) 

sage: get_vertex_in(forest) 

2 

 

""" 

while node.node_type != NodeType.NORMAL: 

node = node.children[0] 

return node.children[0] 

 

def compute_mu_for_co_component(graph, component_index, source_index, 

root, vertices_in_component): 

""" 

Compute the mu value for co-component 

 

INPUT: 

 

- ``graph`` -- Graph whose MD tree needs to be computed 

- ``component_index`` -- index of the co-component 

- ``source_index`` -- index of the source in the forest 

- ``root`` -- the forest which needs to be assembled into a MD tree 

- ``vertices_in_component`` -- Dictionary which maps index i to list of 

vertices in the tree at index i in the forest 

 

OUTPUT: 

 

The mu value (component in the forest) for the co-component 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, get_vertices, compute_mu_for_co_component 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertices_in_component = {} 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

sage: compute_mu_for_co_component(g, 0, 2, forest,  

....: vertices_in_component) 

NORMAL [1] 

sage: compute_mu_for_co_component(g, 1, 2, forest,  

....: vertices_in_component) 

NORMAL [3] 

 

""" 

 

for index in range(len(root.children) - 1, source_index, -1): 

if is_component_connected(graph, component_index, 

index, vertices_in_component): 

return root.children[index] 

 

# return the default value 

return root.children[source_index] 

 

 

def compute_mu_for_component(graph, component_index, source_index, 

root, vertices_in_component): 

""" 

Compute the mu value for component 

 

INPUT: 

 

- ``graph`` -- Graph whose MD tree needs to be computed 

- ``component_index`` -- index of the component 

- ``source_index`` -- index of the source in the forest 

- ``root`` -- the forest which needs to be assembled into a MD tree 

- ``vertices_in_component`` -- Dictionary which maps index i to list of 

vertices in the tree at the index i in the 

forest 

 

OUTPUT: 

 

The mu value (co-component in the forest) for the component 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, get_vertices, compute_mu_for_component 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(6, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertices_in_component = {} 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

sage: compute_mu_for_component(g, 3, 2, forest,  

....: vertices_in_component) 

SERIES [NORMAL [4], NORMAL [5]] 

sage: compute_mu_for_component(g, 4, 2, forest,  

....: vertices_in_component) 

NORMAL [2] 

 

""" 

 

# default mu value for a component 

mu_for_component = root.children[0] 

 

for index in range(0, source_index): 

if mu_for_component == root.children[index] and \ 

is_component_connected(graph, component_index, 

index, vertices_in_component): 

mu_for_component = root.children[index + 1] 

 

# return the default value 

return mu_for_component 

 

 

def is_component_connected(graph, index1, index2, vertices_in_component): 

""" 

Return True if two (co)components are connected else False 

 

INPUT: 

 

- ``graph`` -- Graph whose MD tree needs to be computed 

- ``index1`` -- index of the first (co)component 

- ``index2`` -- index of the second (co)component 

- ``vertices_in_component`` -- Dictionary which maps index i to list of 

vertices in the tree at the index i in the 

forest 

 

OUTPUT: 

 

``True`` if the (co)components are connected else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, get_vertices, is_component_connected 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(6, 1) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertices_in_component = {} 

sage: for index, component in enumerate(forest.children): 

....: vertices_in_component[index] = get_vertices(component) 

sage: is_component_connected(g, 0, 1, vertices_in_component) 

False 

sage: is_component_connected(g, 0, 3, vertices_in_component) 

True 

 

""" 

vertices = vertices_in_component[index1] 

index2_vertices_set = set(vertices_in_component[index2]) 

 

for vertex in vertices: 

neighbors = graph.neighbors(vertex) 

if not index2_vertices_set.isdisjoint(neighbors): 

return True 

return False 

 

 

def get_vertices(component_root): 

""" 

Compute the list of vertices in the (co)component 

 

INPUT: 

 

- ``component_root`` -- root of the (co)component whose vertices need to  

be returned as a list 

 

OUTPUT: 

 

list of vertices in the (co)component 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

create_normal_node, get_vertices 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: get_vertices(forest) 

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

 

""" 

vertices = [] 

 

# inner recursive function to recurse over the elements in the  

# ``component`` 

def recurse_component(node, vertices): 

if node.node_type == NodeType.NORMAL: 

vertices.append(node.children[0]) 

return 

for child in node.children: 

recurse_component(child, vertices) 

 

recurse_component(component_root, vertices) 

return vertices 

 

def promote_left(root): 

""" 

Perform the promotion phase on the forest root.  

 

If child and parent both are marked by LEFT_SPLIT then child is removed  

and placed just before the parent 

 

INPUT: 

 

- ``root`` -- The forest which needs to be promoted 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, \ 

maximal_subtrees_with_leaves_in_x, promote_left 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(4, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: x = {u for u in g.neighbor_iterator(2)  

....: if vertex_dist[u] != vertex_dist[2]} 

sage: maximal_subtrees_with_leaves_in_x(forest, 2, x, vertex_status,  

....: False, 0) 

sage: promote_left(forest) 

sage: forest 

FOREST [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], PARALLEL [NORMAL [6]], PARALLEL [NORMAL [7]], PARALLEL [], NORMAL [1]] 

 

""" 

q = deque() 

 

# q has [parent, child] elements as parent needs to be modified 

for child in root.children: 

q.append([root, child]) 

 

while q: 

 

parent, child = q.popleft() 

 

if child.node_type == NodeType.NORMAL: 

continue 

 

# stores the elements to be removed from the child 

to_remove = [] 

 

# stores the index of child in parent list 

index = parent.children.index(child) 

 

for grand_child in child.children: 

 

# if tree and child both have LEFT_SPLIT then tree from 

# child is inserted just before child in the parent 

if grand_child.has_left_split() and child.has_left_split(): 

parent.children.insert(index, grand_child) 

index += 1 

to_remove.append(grand_child) 

q.append([parent, grand_child]) 

else: 

q.append([child, grand_child]) 

 

for grand_child in to_remove: 

child.children.remove(grand_child) 

 

 

def promote_right(root): 

""" 

Perform the promotion phase on the forest root.  

 

If child and parent both are marked by RIGHT_SPLIT then child is removed  

and placed just after the parent 

 

INPUT: 

 

- ``root`` -- The forest which needs to be promoted 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, refine, promote_right 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(4, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: refine(g, forest, vertex_dist, vertex_status) 

sage: promote_right(forest) 

sage: forest 

FOREST [NORMAL [2], SERIES [SERIES [NORMAL [4]], SERIES [NORMAL [5]]], NORMAL [3], PARALLEL [], PARALLEL [NORMAL [7]], PARALLEL [NORMAL [6]], NORMAL [1]] 

 

""" 

q = deque() 

 

# q has [parent, child] elements as parent needs to be modified 

for child in root.children: 

q.append([root, child]) 

 

while q: 

 

parent, child = q.popleft() 

 

if child.node_type == NodeType.NORMAL: 

continue 

 

# stores the elements to be removed from the child 

to_remove = [] 

 

# stores the index of child in parent list 

index = parent.children.index(child) 

 

for grand_child in child.children: 

 

# if tree and child both have RIGHT_SPLIT then tree from 

# child is inserted just after child in the parent 

if grand_child.has_right_split() and child.has_right_split(): 

parent.children.insert(index + 1, grand_child) 

to_remove.append(grand_child) 

q.append([parent, grand_child]) 

else: 

q.append([child, grand_child]) 

 

for grand_child in to_remove: 

child.children.remove(grand_child) 

 

 

def promote_child(root): 

""" 

Perform the promotion phase on the forest `root`.  

 

If marked parent has no children it is removed, if it has one child then  

it is replaced by its child 

 

INPUT: 

 

- ``root`` -- The forest which needs to be promoted 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, refine, promote_right, \ 

promote_child 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(4, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: refine(g, forest, vertex_dist, vertex_status) 

sage: promote_right(forest) 

sage: promote_child(forest) 

sage: forest 

FOREST [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], NORMAL [7], NORMAL [6], NORMAL [1]] 

 

""" 

q = deque() 

 

# q has [parent, child] elements as parent needs to be modified 

for child in root.children: 

q.append([root, child]) 

 

while q: 

 

parent, child = q.popleft() 

 

if child.node_type == NodeType.NORMAL: 

continue 

 

# if child node itself has only one child 

if len(child.children) == 1 and (child.node_split != \ 

NodeSplit.NO_SPLIT or \ 

child.node_type == NodeType.FOREST): 

# replace child node by its own child 

 

grand_child = child.children[0] 

index = parent.children.index(child) 

parent.children.insert(index, grand_child) 

parent.children.remove(child) 

q.append([parent, grand_child]) 

# if child node has no children 

elif ((not child.children) and child.node_split != NodeSplit.NO_SPLIT): 

# remove the child node 

parent.children.remove(child) 

else: 

for grand_child in child.children: 

q.append([child, grand_child]) 

 

 

def clear_node_split_info(root): 

""" 

Set the node_split of nodes to NO_SPLIT 

 

INPUT: 

 

- ``root`` -- The forest which needs to be cleared of split information 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

NodeSplit, create_normal_node, clear_node_split_info 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: series_node.children[0].node_split = NodeSplit.LEFT_SPLIT 

sage: series_node.node_split = NodeSplit.RIGHT_SPLIT 

sage: forest.children.insert(1, series_node) 

sage: clear_node_split_info(forest) 

sage: series_node.node_split == NodeSplit.NO_SPLIT 

True 

sage: series_node.children[0].node_split == NodeSplit.NO_SPLIT 

True 

 

""" 

 

root.node_split = NodeSplit.NO_SPLIT 

 

if root.node_type != NodeType.NORMAL: 

for node in root.children: 

clear_node_split_info(node) 

 

 

def refine(graph, root, vertex_dist, vertex_status): 

""" 

Refine the forest based on the active edges 

 

INPUT: 

 

- ``graph`` -- graph whose MD tree needs to be computed 

- ``root`` -- the forest which needs to be assembled into a MD tree 

- ``vertex_dist`` -- dictionary mapping the vertex with distance from the 

source 

- ``vertex_status`` -- dictionary mapping the vertex to the position 

w.r.t source 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, refine 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: refine(g, forest, vertex_dist, vertex_status) 

sage: forest 

FOREST [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], PARALLEL [PARALLEL [NORMAL [6]], PARALLEL [NORMAL [7]]], NORMAL [1]] 

 

""" 

x_used = [] 

 

# active edges of each vertex in the graph is used to refine the forest 

for v in graph.vertices(): 

if v in vertex_status and vertex_status[v] == VertexPosition.SOURCE: 

continue 

 

# set of vertices connected through active edges to v 

x = {u for u in graph.neighbor_iterator(v) 

if vertex_dist[u] != vertex_dist[v]} 

 

if x not in x_used: 

x_used.append(x) 

maximal_subtrees_with_leaves_in_x(root, v, x, 

vertex_status, False, 0) 

 

get_child_splits(root) 

 

 

def get_child_splits(root): 

""" 

Add the node_split of children to the parent node 

 

INPUT: 

 

- ``root`` -- input modular decomposition tree 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

NodeSplit, create_normal_node, get_child_splits 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: series_node.children[0].node_split = NodeSplit.LEFT_SPLIT 

sage: series_node.node_split = NodeSplit.RIGHT_SPLIT 

sage: forest.children.insert(1, series_node) 

sage: get_child_splits(forest) 

sage: series_node.node_split == NodeSplit.BOTH_SPLIT 

True 

sage: forest.node_split == NodeSplit.BOTH_SPLIT 

True 

 

 

""" 

if root.node_type != NodeType.NORMAL: 

for node in root.children: 

get_child_splits(node) 

root.set_node_split(node.node_split) 

 

 

def maximal_subtrees_with_leaves_in_x(root, v, x, vertex_status, 

tree_left_of_source, level): 

""" 

Refine the forest based on the active edges(x) of vertex v 

 

INPUT: 

 

- ``root`` -- the forest which needs to be assembled into a MD tree 

- ``v`` -- the vertex used to refine 

- ``x`` -- set of vertices connected to v and at different distance 

from source compared to v 

- ``vertex_status`` -- dictionary mapping the vertex to the position 

w.r.t source 

- ``tree_left_of_source`` -- flag indicating whether tree is 

- ``level`` -- indicates the recursion level, 0 for root 

 

OUTPUT: 

 

``[contained_in_x, split]`` where ``contained_in_x`` is ``True`` if all  

vertices in root are subset of x else ``False`` and ``split`` is the  

split which occurred at any node in root 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import Node, NodeType, \ 

VertexPosition, create_normal_node, \ 

maximal_subtrees_with_leaves_in_x 

sage: g = Graph() 

sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7]) 

sage: g.add_edge(2, 3) 

sage: g.add_edge(4, 3) 

sage: g.add_edge(5, 3) 

sage: g.add_edge(2, 6) 

sage: g.add_edge(2, 7) 

sage: g.add_edge(2, 1) 

sage: g.add_edge(6, 1) 

sage: g.add_edge(4, 2) 

sage: g.add_edge(5, 2) 

sage: forest = Node(NodeType.FOREST) 

sage: forest.children = [create_normal_node(2), \ 

create_normal_node(3), create_normal_node(1)] 

sage: series_node = Node(NodeType.SERIES) 

sage: series_node.children = [create_normal_node(4), \ 

create_normal_node(5)] 

sage: parallel_node = Node(NodeType.PARALLEL) 

sage: parallel_node.children = [create_normal_node(6), \ 

create_normal_node(7)] 

sage: forest.children.insert(1, series_node) 

sage: forest.children.insert(3, parallel_node) 

sage: vertex_status = {2: VertexPosition.LEFT_OF_SOURCE, \ 

3: VertexPosition.SOURCE, \ 

1: VertexPosition.RIGHT_OF_SOURCE, \ 

4: VertexPosition.LEFT_OF_SOURCE, \ 

5: VertexPosition.LEFT_OF_SOURCE, \ 

6: VertexPosition.RIGHT_OF_SOURCE, \ 

7: VertexPosition.RIGHT_OF_SOURCE} 

sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3} 

sage: x = {u for u in g.neighbor_iterator(2)  

....: if vertex_dist[u] != vertex_dist[2]} 

sage: maximal_subtrees_with_leaves_in_x(forest, 2, x, vertex_status,  

....: False, 0) 

sage: forest 

FOREST [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], PARALLEL [NORMAL [6], NORMAL [7]], NORMAL [1]] 

sage: x = {u for u in g.neighbor_iterator(1)  

....: if vertex_dist[u] != vertex_dist[1]} 

sage: maximal_subtrees_with_leaves_in_x(forest, 1, x, vertex_status,  

....: False, 0) 

sage: forest 

FOREST [NORMAL [2], SERIES [NORMAL [4], NORMAL [5]], NORMAL [3], PARALLEL [PARALLEL [NORMAL [6]], PARALLEL [NORMAL [7]]], NORMAL [1]] 

 

 

""" 

 

def update_node_info(node, node_type, node_split, comp_num, subtree_list): 

""" 

Set the various fields for a tree node and update its subtrees 

 

- ``node`` -- node whose fields need to be updated 

- ``node_type`` -- node_type to be set 

- ``node_split`` -- node_split to be set 

- ``comp_num`` -- comp_num to be set 

- ``subtree_list`` -- list containing the subtrees 

 

""" 

node.node_type = node_type 

node.node_split = node_split 

node.comp_num = comp_num 

node.children = subtree_list 

 

return_split = NodeSplit.NO_SPLIT #initialize split to NO_SPLIT 

 

# all trees in a forest are refined using x 

if root.node_type == NodeType.FOREST: 

 

# indicates whether tree is left of source, True if left of source 

left_flag = True 

 

for node in root.children: 

if node.node_type == NodeType.NORMAL and \ 

node.children[0] in vertex_status and \ 

vertex_status[node.children[0]] == VertexPosition.SOURCE: 

left_flag = False 

subtree_result = maximal_subtrees_with_leaves_in_x(node, v, x, 

vertex_status, 

left_flag, 

level) 

if subtree_result: 

# Mark the ancestors 

root.set_node_split(subtree_result[1]) 

 

# handles the prime, series and parallel cases 

elif root.node_type != NodeType.NORMAL: 

 

flag = True # indicates the entire root is contained in x 

split_flag = False # indicates a split is required 

Ta = [] # contains subtrees with leaves in x 

Tb = [] # contains subtrees with leaves not in x 

 

for node in root.children: 

 

# refines the children of root 

subtree_result = maximal_subtrees_with_leaves_in_x(node, v, x, 

vertex_status, 

tree_left_of_source, 

level + 1) 

 

if subtree_result: 

flag = flag and subtree_result[0] 

 

# add the node split of children to root 

root.set_node_split(subtree_result[1]) 

 

if subtree_result[0]: 

Ta.append(node) 

split_flag = True 

else: 

Tb.append(node) 

 

if root.node_type == NodeType.PRIME: 

# mark all the children of prime nodes 

for node in root.children: 

node.set_node_split(root.node_split) 

 

if flag: 

# return if all subtrees are in x, no split required 

return [True, root.node_split] 

elif split_flag: # split required` 

 

split = NodeSplit.LEFT_SPLIT 

 

# if v is right of source and tree is also right of source then 

# RIGHT_SPLIT 

if vertex_status[v] == VertexPosition.RIGHT_OF_SOURCE and \ 

not tree_left_of_source: 

split = NodeSplit.RIGHT_SPLIT 

 

# add the split to root node_split 

root.set_node_split(split) 

 

if root.node_type == NodeType.PRIME: 

# mark all the children of prime nodes 

for node in root.children: 

node.set_node_split(split) 

return [False, split] 

 

if root.is_separated: 

# if root has already been split then further split not 

# required 

return [flag, root.node_split] 

 

node_type = root.node_type 

root.is_separated = True 

 

# root[1] would now contain Ta and Tb 

root.children = [] 

 

# add two nodes for Ta and Tb 

a = create_parallel_node() 

update_node_info(a, node_type, root.node_split, 

Ta[0].comp_num, Ta) 

b = create_parallel_node() 

update_node_info(b, node_type, root.node_split, 

Tb[0].comp_num, Tb) 

root.children.append(a) 

root.children.append(b) 

 

return_split = root.node_split 

return [flag, return_split] 

# root is a vertex and is contained in x 

elif root.children[0] in x: 

return [True, root.node_split] 

# root is a vertex and is not contained in x 

else: 

return [False, root.node_split] 

 

 

def create_prime_node(): 

""" 

Return a prime node with no children 

 

OUTPUT: 

 

A node object with node_type set as NodeType.PRIME 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import create_prime_node 

sage: node = create_prime_node() 

sage: node 

PRIME [] 

 

""" 

 

return Node(NodeType.PRIME) 

 

 

def create_parallel_node(): 

""" 

Return a parallel node with no children 

 

OUTPUT: 

 

A node object with node_type set as NodeType.PARALLEL 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import create_parallel_node 

sage: node = create_parallel_node() 

sage: node 

PARALLEL [] 

 

""" 

return Node(NodeType.PARALLEL) 

 

 

def create_series_node(): 

""" 

Return a series node with no children 

 

OUTPUT: 

 

A node object with node_type set as NodeType.SERIES 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import create_series_node 

sage: node = create_series_node() 

sage: node 

SERIES [] 

 

""" 

return Node(NodeType.SERIES) 

 

 

def create_normal_node(vertex): 

""" 

Return a normal node with no children 

 

INPUT: 

 

- ``vertex`` -- vertex number 

 

OUTPUT: 

 

A node object representing the vertex with node_type set as NodeType.NORMAL 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import create_normal_node 

sage: node = create_normal_node(2) 

sage: node 

NORMAL [2] 

 

""" 

node = Node(NodeType.NORMAL) 

node.children.append(vertex) 

return node 

 

def print_md_tree(root): 

""" 

Print the modular decomposition tree 

 

INPUT: 

 

- ``root`` -- root of the modular decomposition tree 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, test_modular_decomposition, print_md_tree 

sage: print_md_tree(modular_decomposition(graphs.IcosahedralGraph())) 

PRIME 

8 

5 

1 

11 

7 

0 

6 

9 

2 

4 

10 

3  

 

""" 

 

def recursive_print_md_tree(root, level): 

""" 

Print the modular decomposition tree at root 

 

INPUT: 

 

- ``root`` -- root of the modular decomposition tree  

- ``level`` -- indicates the depth of root in the original modular  

decomposition tree 

 

""" 

if root.node_type != NodeType.NORMAL: 

print("{}{}".format(level,str(root.node_type))) 

for tree in root.children: 

recursive_print_md_tree(tree, level + " ") 

else: 

print("{}{}".format(level,str(root.children[0]))) 

 

recursive_print_md_tree(root, "") 

 

#============================================================================= 

 

# Below functions are implemented to test the modular decomposition tree 

 

#============================================================================= 

 

#Function implemented for testing 

def test_modular_decomposition(tree_root, graph): 

""" 

This function tests the input modular decomposition tree using recursion. 

 

INPUT: 

 

- ``tree_root`` -- root of the modular decomposition tree to be tested 

- ``graph`` -- Graph whose modular decomposition tree needs to be tested 

 

OUTPUT: 

 

``True`` if input tree is a modular decomposition else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, test_modular_decomposition 

sage: g = graphs.HexahedralGraph() 

sage: test_modular_decomposition(modular_decomposition(g), g) 

True 

 

""" 

if tree_root.node_type != NodeType.NORMAL: 

for module in tree_root.children: 

if not test_module(module, graph): 

# test whether modules pass the defining 

# characteristics of modules 

return False 

if not test_modular_decomposition(module, 

graph.subgraph( 

get_vertices(module))): 

# recursively test the modular decomposition subtrees 

return False 

 

if not test_maximal_modules(tree_root, graph): 

# test whether the mdoules are maximal in nature 

return False 

 

return True 

 

#Function implemented for testing 

def test_maximal_modules(tree_root, graph): 

""" 

This function tests maximal nature of modules in a modular decomposition 

tree.  

 

Suppose the module M = [M1, M2, ..., Mn] is the input modular 

decomposition tree. Algorithm forms pairs like (M1, M2), (M1, M3), 

...(M1, Mn); (M2, M3), (M2, M4), ...(M2, Mn); ... and so on and tries to 

form a module using the pair. If the module formed has same type as M and 

is of type SERIES or PARALLEL then the formed module is not considered 

maximal. Otherwise it is considered maximal and M is not a modular 

decomposition tree. 

 

INPUT: 

 

- ``tree_root`` -- Modular decomposition tree whose modules are tested for 

maximal nature 

- ``graph`` -- Graph whose modular decomposition tree is tested 

 

OUTPUT: 

 

``True`` if all modules at first level in the modular ddecomposition tree 

are maximal in nature 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, test_maximal_modules 

sage: g = graphs.HexahedralGraph() 

sage: test_maximal_modules(modular_decomposition(g), g) 

True 

""" 

if tree_root.node_type != NodeType.NORMAL: 

for index, module in enumerate(tree_root.children): 

for other_index in range(index + 1, len(tree_root.children)): 

 

# compute the module formed using modules at index and 

# other_index 

module_formed = form_module(index, other_index, 

tree_root, graph) 

 

if module_formed[0]: 

# Module formed and the parent of the formed module 

# should not both be of type SERIES or PARALLEL 

if ((get_module_type(graph.subgraph(module_formed[1])) == 

tree_root.node_type 

) and 

(tree_root.node_type == NodeType.PARALLEL or 

tree_root.node_type == NodeType.SERIES 

)): 

continue 

return False 

return True 

 

 

#Function implemented for testing 

def get_module_type(graph): 

""" 

Return the module type of the root of modular decomposition tree for the 

input graph 

 

INPUT: 

 

- ``graph`` -- Input sage graph 

 

OUTPUT: 

 

``PRIME`` if graph is PRIME, ``PARALLEL`` if graph is PARALLEL and 

``SERIES`` if graph is of type SERIES 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import get_module_type 

sage: g = graphs.HexahedralGraph() 

sage: get_module_type(g) 

PRIME 

 

""" 

if not graph.is_connected(): 

return NodeType.PARALLEL 

elif graph.complement().is_connected(): 

return NodeType.PRIME 

return NodeType.SERIES 

 

 

#Function implemented for testing 

def form_module(index, other_index, tree_root, graph): 

""" 

This function forms a module out of the modules in the module pair.  

 

Let modules input be M1 and M2. Let V be the set of vertices in these 

modules. Suppose x is a neighbor of subset of the vertices in V but not 

all the vertices and x does not belong to V. Then the set of modules also 

include the module which contains x. This process is repeated until a 

module is formed and the formed module if subset of V is returned. 

 

INPUT: 

 

- ``index`` -- First module in the module pair 

- ``other_index`` -- Second module in the module pair 

- ``tree_root`` -- Modular decomposition tree which contains the modules  

in the module pair 

- ``graph`` -- Graph whose modular decomposition tree is created 

 

OUTPUT: 

 

``[module_formed, vertices]`` where ``module_formed`` is ``True`` if 

module is formed else ``False`` and ``vertices`` is a list of vertices 

included in the formed module 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, form_module 

sage: g = graphs.HexahedralGraph() 

sage: tree_root = modular_decomposition(g) 

sage: form_module(0, 2, tree_root, g) 

[False, {0, 1, 2, 3, 4, 5, 6, 7}] 

 

""" 

vertices = set(get_vertices(tree_root.children[index]) + 

get_vertices(tree_root.children[other_index])) 

 

# stores all neighbors which are common for all vertices in V 

common_neighbors = set() 

 

# stores all neighbors of vertices in V which are outside V 

all_neighbors = set() 

 

while True: 

# remove vertices from all_neighbors and common_neighbors 

all_neighbors = all_neighbors - set(vertices) 

common_neighbors = common_neighbors - set(vertices) 

 

for v in vertices: 

# stores the neighbors of v which are outside the set of vertices 

neighbor_list = set(graph.neighbors(v)) 

neighbor_list = neighbor_list - vertices 

 

# update all_neighbors and common_neighbors using the 

# neighbor_list 

all_neighbors = all_neighbors | neighbor_list 

common_neighbors = common_neighbors & neighbor_list 

 

if all_neighbors == common_neighbors: # indicates a module is formed 

 

# module formed covers the entire graph 

if len(vertices) == graph.order(): 

return [False, vertices] 

 

return [True, vertices] 

 

# add modules containing uncommon neighbors into the formed module 

for v in (all_neighbors - common_neighbors): 

for index in range(len(tree_root.children)): 

if v in get_vertices(tree_root.children[index]): 

vertices = vertices | \ 

set(get_vertices(tree_root.children[index])) 

break 

 

#Function implemented for testing 

def test_module(module, graph): 

""" 

Test whether input module is actually a module 

 

INPUT: 

 

- ``module`` -- Module which needs to be tested 

- ``graph`` -- Input sage graph which contains the module 

 

OUTPUT: 

 

``True`` if input module is a module by definition else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, test_module 

sage: g = graphs.HexahedralGraph() 

sage: tree_root = modular_decomposition(g) 

sage: test_module(tree_root, g) 

True 

sage: test_module(tree_root.children[0], g) 

True 

 

""" 

 

# A single vertex is a module 

if module.node_type == NodeType.NORMAL: 

return True 

 

#vertices contained in module 

vertices_in_module = get_vertices(module) 

 

#vertices outside module 

vertices_outside = list(set(graph.vertices()) - set(vertices_in_module)) 

 

# Nested module with only one child 

if module.node_type != NodeType.NORMAL and len(module.children) == 1: 

return False 

 

# If children of SERIES module are all SERIES modules 

if module.node_type == NodeType.SERIES: 

if children_node_type(module, NodeType.SERIES): 

return False 

 

# If children of PARALLEL module are all PARALLEL modules 

if module.node_type == NodeType.PARALLEL: 

if children_node_type(module, NodeType.PARALLEL): 

return False 

 

# check the module by definition. Vertices in a module should all either 

# be connected or disconnected to any vertex outside module 

for v in vertices_outside: 

if not either_connected_or_not_connected(v, vertices_in_module, 

graph): 

return False 

return True 

 

 

#Function implemented for testing 

def children_node_type(module, node_type): 

""" 

Test whether node_type of children of a node is same as input node_type 

 

INPUT: 

 

- ``module`` -- module which is tested 

- ``node_type`` -- input node_type 

 

OUTPUT: 

 

``True`` if node_type of children of module is same as input node_type 

else ``False`` 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

modular_decomposition, print_md_tree, children_node_type, \ 

NodeType 

sage: g = graphs.OctahedralGraph() 

sage: tree_root = modular_decomposition(g) 

sage: print_md_tree(modular_decomposition(g)) 

SERIES 

PARALLEL 

2 

3 

PARALLEL 

1 

4 

PARALLEL 

0 

5 

sage: children_node_type(tree_root, NodeType.SERIES) 

False 

sage: children_node_type(tree_root, NodeType.PARALLEL) 

True 

 

""" 

for node in module.children: 

if node.node_type != node_type: 

return False 

return True 

 

 

#Function implemented for testing 

def either_connected_or_not_connected(v, vertices_in_module, graph): 

""" 

Test whether v is connected or disconnected to all vertices in the module 

 

INPUT: 

 

- ``v`` -- vertex tested 

- ``vertices_in_module`` -- list containing vertices in the module 

- ``graph`` -- graph to which the vertices belong 

 

OUTPUT: 

 

``True`` if v is either connected or disconnected to all the vertices in 

the module else ``False`` 

 

 

EXAMPLES:: 

 

sage: from sage.graphs.modular_decomposition import \ 

print_md_tree, modular_decomposition, \ 

either_connected_or_not_connected 

sage: g = graphs.OctahedralGraph() 

sage: print_md_tree(modular_decomposition(g)) 

SERIES 

PARALLEL 

2 

3 

PARALLEL 

1 

4 

PARALLEL 

0 

5 

sage: either_connected_or_not_connected(2, [1, 4], g) 

True 

sage: either_connected_or_not_connected(2, [3, 4], g) 

False 

 

""" 

 

# marks whether vertex v is connected to first vertex in the module 

connected = graph.has_edge(vertices_in_module[0], v) 

 

# if connected is True then all vertices in module should be connected to 

# v else disconnected 

for u in vertices_in_module: 

if (graph.has_edge(u,v) != connected): 

return False 

return True