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

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

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

r""" 

Hasse diagrams of posets 

 

{INDEX_OF_FUNCTIONS} 

 

""" 

 

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

# Copyright (C) 2008 Peter Jipsen <jipsen@chapman.edu> 

# Copyright (C) 2008 Franco Saliola <saliola@gmail.com> 

# 

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

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

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

# (at your option) any later version. 

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

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

from __future__ import print_function 

 

from six.moves import range 

 

from sage.graphs.digraph import DiGraph 

from sage.matrix.constructor import matrix 

from sage.rings.integer_ring import ZZ 

from sage.misc.lazy_attribute import lazy_attribute 

from sage.misc.cachefunc import cached_method 

from sage.misc.superseded import deprecated_function_alias 

 

class LatticeError(ValueError): 

""" 

Helper exception class to forward elements without meet or 

join to upper level, so that the user will see "No meet for 

a and b" instead of "No meet for 1 and 2". 

""" 

 

def __init__(self, fail, x, y): 

""" 

Initialize the exception. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import LatticeError 

sage: error = LatticeError('join', 3, 8) 

sage: error.x 

3 

""" 

ValueError.__init__(self, None) 

self.fail = fail 

self.x = x 

self.y = y 

 

def __str__(self): 

""" 

Return string representation of the exception. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import LatticeError 

sage: error = LatticeError('meet', 15, 18) 

sage: error.__str__() 

'no meet for 15 and 18' 

""" 

return "no {} for {} and {}".format(self.fail, self.x, self.y) 

 

class HasseDiagram(DiGraph): 

""" 

The Hasse diagram of a poset. This is just a transitively-reduced, 

directed, acyclic graph without loops or multiple edges. 

 

.. note:: 

 

We assume that ``range(n)`` is a linear extension of the poset. 

That is, ``range(n)`` is the vertex set and a topological sort of 

the digraph. 

 

This should not be called directly, use Poset instead; all type 

checking happens there. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2],1:[3],2:[3],3:[]}); H 

Hasse diagram of a poset containing 4 elements 

sage: TestSuite(H).run() 

""" 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2],1:[3],2:[3],3:[]}) 

sage: H._repr_() 

'Hasse diagram of a poset containing 4 elements' 

""" 

return "Hasse diagram of a poset containing %s elements"%self.order() 

 

def linear_extension(self): 

r""" 

Return a linear extension 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2],1:[3],2:[3],3:[]}) 

sage: H.linear_extension() 

[0, 1, 2, 3] 

""" 

# Recall: we assume range(n) is a linear extension. 

return list(range(len(self))) 

 

def linear_extensions(self): 

r""" 

Return all linear extensions 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2],1:[3],2:[3],3:[]}) 

sage: H.linear_extensions() 

[[0, 1, 2, 3], [0, 2, 1, 3]] 

""" 

return self.topological_sort_generator() 

 

def greedy_linear_extensions_iterator(self): 

""" 

Return an iterator over greedy linear extensions of the Hasse diagram. 

 

A linear extension `[e_1, e_2, \ldots, e_n]` is *greedy* if for 

every `i` either `e_{i+1}` covers `e_i` or all upper covers 

of `e_i` have at least one lower cover that is not in 

`[e_1, e_2, \ldots, e_i]`. 

 

Informally said a linear extension is greedy if it "always 

goes up when possible" and so has no unnecessary jumps. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: N5 = HasseDiagram({0: [1, 2], 2: [3], 1: [4], 3: [4]}) 

sage: for l in N5.greedy_linear_extensions_iterator(): 

....: print(l) 

[0, 1, 2, 3, 4] 

[0, 2, 3, 1, 4] 

 

TESTS: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: list(HasseDiagram({}).greedy_linear_extensions_iterator()) 

[[]] 

sage: H = HasseDiagram({0: []}) 

sage: list(H.greedy_linear_extensions_iterator()) 

[[0]] 

""" 

N = self.order() 

 

def greedy_rec(H, linext): 

if len(linext) == N: 

yield linext 

 

S = [] 

if linext: 

S = [x for x in H.neighbors_out(linext[-1]) if all(low in linext for low in H.neighbors_in(x))] 

if not S: 

S_ = set(self).difference(set(linext)) 

S = [x for x in S_ if 

not any(low in S_ for low in self.neighbors_in(x))] 

 

for e in S: 

# Python3-todo: use yield from 

for tmp in greedy_rec(H, linext+[e]): 

yield tmp 

 

return greedy_rec(self, []) 

 

def supergreedy_linear_extensions_iterator(self): 

""" 

Return an iterator over supergreedy linear extensions of the Hasse diagram. 

 

A linear extension `[e_1, e_2, \ldots, e_n]` is *supergreedy* if, 

for every `i` and `j` where `i > j`, `e_i` covers `e_j` if for 

every `i > k > j` at least one lower cover of `e_k` is not in 

`[e_1, e_2, \ldots, e_k]`. 

 

Informally said a linear extension is supergreedy if it "always 

goes as high possible, and withdraw so less as possible". 

These are also called depth-first linear extensions. 

 

EXAMPLES: 

 

We show the difference between "only greedy" and supergreedy 

extensions:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 2: [3, 4]}) 

sage: G_ext = list(H.greedy_linear_extensions_iterator()) 

sage: SG_ext = list(H.supergreedy_linear_extensions_iterator()) 

sage: [0, 2, 3, 1, 4] in G_ext 

True 

sage: [0, 2, 3, 1, 4] in SG_ext 

False 

 

sage: len(SG_ext) 

4 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: list(HasseDiagram({}).supergreedy_linear_extensions_iterator()) 

[[]] 

sage: list(HasseDiagram({0: [], 1: []}).supergreedy_linear_extensions_iterator()) 

[[0, 1], [1, 0]] 

""" 

N = self.order() 

self_as_set = set(self) 

 

def supergreedy_rec(H, linext): 

k = len(linext) 

 

if k == N: 

yield linext 

 

else: 

S = [] 

while not S: 

if not k: # Start from new minimal element 

S = [x for x in self.sources() if x not in linext] 

else: 

S = [x for x in self.neighbors_out(linext[k-1]) if x not in linext and all(low in linext for low in self.neighbors_in(x))] 

k -= 1 

 

for e in S: 

# Python3-todo: use yield from 

for tmp in supergreedy_rec(H, linext+[e]): 

yield tmp 

 

return supergreedy_rec(self, []) 

 

def is_linear_extension(self, lin_ext=None): 

r""" 

Test if an ordering is a linear extension. 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2],1:[3],2:[3],3:[]}) 

sage: H.is_linear_extension(list(range(4))) 

True 

sage: H.is_linear_extension([3,2,1,0]) 

False 

""" 

if lin_ext is None or lin_ext == list(range(len(self))): 

for x, y in self.cover_relations_iterator(): 

if not x < y: 

return False 

return True 

else: 

for x, y in self.cover_relations_iterator(): 

if not lin_ext.index(x) < lin_ext.index(y): 

return False 

return True 

 

def cover_relations_iterator(self): 

r""" 

Iterate over cover relations. 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2,3], 1:[3,4], 2:[5], 3:[5], 4:[5]}) 

sage: list(H.cover_relations_iterator()) 

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

""" 

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

yield (u,v) 

 

def cover_relations(self): 

r""" 

Return the list of cover relations. 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2,3], 1:[3,4], 2:[5], 3:[5], 4:[5]}) 

sage: H.cover_relations() 

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

""" 

return list(self.cover_relations_iterator()) 

 

def is_lequal(self, i, j): 

""" 

Returns True if i is less than or equal to j in the poset, and 

False otherwise. 

 

.. note:: 

 

If the :meth:`lequal_matrix` has been computed, then this method is 

redefined to use the cached matrix (see :meth:`_alternate_is_lequal`). 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2], 1:[2], 2:[3], 3:[4], 4:[]}) 

sage: x,y,z = 0, 1, 4 

sage: H.is_lequal(x,y) 

False 

sage: H.is_lequal(y,x) 

False 

sage: H.is_lequal(x,z) 

True 

sage: H.is_lequal(y,z) 

True 

sage: H.is_lequal(z,z) 

True 

""" 

return i == j or \ 

(i < j and j in self.breadth_first_search(i)) 

 

def is_less_than(self, x, y): 

r""" 

Returns True if ``x`` is less than or equal to ``y`` in the 

poset, and False otherwise. 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2], 1:[2], 2:[3], 3:[4], 4:[]}) 

sage: x,y,z = 0, 1, 4 

sage: H.is_less_than(x,y) 

False 

sage: H.is_less_than(y,x) 

False 

sage: H.is_less_than(x,z) 

True 

sage: H.is_less_than(y,z) 

True 

sage: H.is_less_than(z,z) 

False 

""" 

if x == y: 

return False 

else: 

return self.is_lequal(x,y) 

 

def is_gequal(self, x, y): 

r""" 

Returns ``True`` if ``x`` is greater than or equal to ``y``, and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: Q = HasseDiagram({0:[2], 1:[2], 2:[3], 3:[4], 4:[]}) 

sage: x,y,z = 0,1,4 

sage: Q.is_gequal(x,y) 

False 

sage: Q.is_gequal(y,x) 

False 

sage: Q.is_gequal(x,z) 

False 

sage: Q.is_gequal(z,x) 

True 

sage: Q.is_gequal(z,y) 

True 

sage: Q.is_gequal(z,z) 

True 

""" 

return self.is_lequal(y,x) 

 

def is_greater_than(self, x, y): 

""" 

Returns ``True`` if ``x`` is greater than but not equal to 

``y``, and ``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: Q = HasseDiagram({0:[2], 1:[2], 2:[3], 3:[4], 4:[]}) 

sage: x,y,z = 0,1,4 

sage: Q.is_greater_than(x,y) 

False 

sage: Q.is_greater_than(y,x) 

False 

sage: Q.is_greater_than(x,z) 

False 

sage: Q.is_greater_than(z,x) 

True 

sage: Q.is_greater_than(z,y) 

True 

sage: Q.is_greater_than(z,z) 

False 

""" 

return self.is_less_than(y,x) 

 

def minimal_elements(self): 

""" 

Returns a list of the minimal elements of the poset. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4],4:[]}) 

sage: P(0) in P.minimal_elements() 

True 

sage: P(1) in P.minimal_elements() 

True 

sage: P(2) in P.minimal_elements() 

True 

""" 

return self.sources() 

 

def maximal_elements(self): 

""" 

Returns a list of the maximal elements of the poset. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4],4:[]}) 

sage: P.maximal_elements() 

[4] 

""" 

return self.sinks() 

 

def bottom(self): 

""" 

Returns the bottom element of the poset, if it exists. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4],4:[]}) 

sage: P.bottom() is None 

True 

sage: Q = Poset({0:[1],1:[]}) 

sage: Q.bottom() 

0 

""" 

min_elms = self.minimal_elements() 

if len(min_elms) == 1: return min_elms[0] 

return None 

 

def has_bottom(self): 

""" 

Returns True if the poset has a unique minimal element. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4],4:[]}) 

sage: P.has_bottom() 

False 

sage: Q = Poset({0:[1],1:[]}) 

sage: Q.has_bottom() 

True 

""" 

if self.bottom() is not None: return True 

return False 

 

def top(self): 

""" 

Returns the top element of the poset, if it exists. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4,5],4:[],5:[]}) 

sage: P.top() is None 

True 

sage: Q = Poset({0:[1],1:[]}) 

sage: Q.top() 

1 

""" 

max_elms = self.maximal_elements() 

if len(max_elms) == 1: return max_elms[0] 

return None 

 

def has_top(self): 

""" 

Returns ``True`` if the poset contains a unique maximal element, and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4,5],4:[],5:[]}) 

sage: P.has_top() 

False 

sage: Q = Poset({0:[1],1:[]}) 

sage: Q.has_top() 

True 

""" 

if not self.top() is None: return True 

return False 

 

def is_bounded(self): 

""" 

Returns True if the poset contains a unique maximal element and a 

unique minimal element, and False otherwise. 

 

EXAMPLES:: 

 

sage: P = Poset({0:[3],1:[3],2:[3],3:[4,5],4:[],5:[]}) 

sage: P.is_bounded() 

False 

sage: Q = Poset({0:[1],1:[]}) 

sage: Q.is_bounded() 

True 

""" 

return self.has_top() and self.has_bottom() 

 

def is_chain(self): 

""" 

Returns True if the poset is totally ordered, and False otherwise. 

 

EXAMPLES:: 

 

sage: L = Poset({0:[1],1:[2],2:[3],3:[4]}) 

sage: L.is_chain() 

True 

sage: V = Poset({0:[1,2]}) 

sage: V.is_chain() 

False 

 

TESTS: 

 

Check :trac:`15330`:: 

 

sage: p = Poset(DiGraph({0:[1],2:[1]})) 

sage: p.is_chain() 

False 

""" 

if self.cardinality() == 0: 

return True 

return (self.num_edges()+1 == self.num_verts() and # Hasse Diagram is a tree 

all(d<=1 for d in self.out_degree()) and # max outdegree is <= 1 

all(d<=1 for d in self.in_degree())) # max indegree is <= 1 

 

def is_antichain_of_poset(self, elms): 

""" 

Return ``True`` if ``elms`` is an antichain of the Hasse 

diagram and ``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2, 3], 1: [4], 2: [4], 3: [4]}) 

sage: H.is_antichain_of_poset([1, 2, 3]) 

True 

sage: H.is_antichain_of_poset([0, 2, 3]) 

False 

""" 

from itertools import combinations 

from sage.misc.misc import uniq 

 

elms_sorted = uniq(elms) 

return not any(self.is_lequal(a, b) for a, b in 

combinations(elms_sorted, 2)) 

 

def dual(self): 

""" 

Returns a poset that is dual to the given poset. 

 

EXAMPLES:: 

 

sage: P = posets.IntegerPartitions(4) 

sage: H = P._hasse_diagram; H 

Hasse diagram of a poset containing 5 elements 

sage: H.dual() 

Hasse diagram of a poset containing 5 elements 

 

TESTS:: 

 

sage: H = posets.IntegerPartitions(4)._hasse_diagram 

sage: H.is_isomorphic( H.dual().dual() ) 

True 

sage: H.is_isomorphic( H.dual() ) 

False 

""" 

H = self.reverse() 

H.relabel(perm=list(range(H.num_verts()-1, -1, -1)), inplace=True) 

return HasseDiagram(H) 

 

def _precompute_intervals(self): 

""" 

Precompute all intervals of the poset. 

 

This will significantly speed up computing congruences. On the 

other hand it will cost much more memory. Currently this is 

"hidden" feature. See example below of using. 

 

EXAMPLES:: 

 

sage: B4 = posets.BooleanLattice(4) 

sage: B4.is_isoform() # Slow 

True 

sage: B4._hasse_diagram._precompute_intervals() 

sage: B4 = posets.BooleanLattice(4) 

sage: B4.is_isoform() # Faster now 

True 

""" 

n = self.order() 

 

v_up = [frozenset(self.depth_first_search(v)) for v in range(n)] 

v_down = [frozenset(self.depth_first_search(v, neighbors=self.neighbors_in)) 

for v in range(n)] 

self._intervals = [[sorted(up.intersection(down)) for down in v_down] 

for up in v_up] 

 

self.interval = self._alternate_interval 

 

def _alternate_interval(self, x, y): 

""" 

Return the list of the elements greater than or equal to ``x`` 

and less than or equal to ``y``. 

 

The list is sorted by numerical value, which is one linear 

extension for the elements of the interval, but not necessary 

the same as returned by ``interval()``. 

 

This will be taken to use when ``_precompute_intervals()`` 

is called. 

 

EXAMPLES:: 

 

sage: P = posets.BooleanLattice(3) 

sage: P.interval(1, 7) 

[1, 3, 5, 7] 

sage: P._hasse_diagram._precompute_intervals() 

sage: P.interval(1, 7) # Uses this function 

[1, 3, 5, 7] 

 

""" 

return self._intervals[x][y] 

 

def interval(self, x, y): 

""" 

Return a list of the elements `z` of ``self`` such that 

`x \leq z \leq y`. The order is that induced by the 

ordering in ``self.linear_extension``. 

 

INPUT: 

 

- ``x`` -- any element of the poset 

 

- ``y`` -- any element of the poset 

 

EXAMPLES:: 

 

sage: uc = [[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]] 

sage: dag = DiGraph(dict(zip(range(len(uc)),uc))) 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram(dag) 

sage: I = set([2,5,6,4,7]) 

sage: I == set(H.interval(2,7)) 

True 

""" 

return [z for z in range(x, y+1) if 

self.is_lequal(x, z) and self.is_lequal(z, y)] 

 

closed_interval = interval 

 

def open_interval(self, x, y): 

""" 

Return a list of the elements `z` of ``self`` such that 

`x < z < y`. The order is that induced by the ordering in 

``self.linear_extension``. 

 

EXAMPLES:: 

 

sage: uc = [[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]] 

sage: dag = DiGraph(dict(zip(range(len(uc)),uc))) 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram(dag) 

sage: set([5,6,4]) == set(H.open_interval(2,7)) 

True 

sage: H.open_interval(7,2) 

[] 

""" 

ci = self.interval(x,y) 

if len(ci) == 0: 

return [] 

else: 

return ci[1:-1] 

 

def rank_function(self): 

r""" 

Return the (normalized) rank function of the poset, 

if it exists. 

 

A *rank function* of a poset `P` is a function `r` 

that maps elements of `P` to integers and satisfies: 

`r(x) = r(y) + 1` if `x` covers `y`. The function `r` 

is normalized such that its minimum value on every 

connected component of the Hasse diagram of `P` is 

`0`. This determines the function `r` uniquely (when 

it exists). 

 

OUTPUT: 

 

- a lambda function, if the poset admits a rank function 

- ``None``, if the poset does not admit a rank function 

 

EXAMPLES:: 

 

sage: P = Poset([[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]]) 

sage: P.rank_function() is not None 

True 

sage: P = Poset(([1,2,3,4],[[1,4],[2,3],[3,4]]), facade = True) 

sage: P.rank_function() is not None 

True 

sage: P = Poset(([1,2,3,4,5],[[1,2],[2,3],[3,4],[1,5],[5,4]]), facade = True) 

sage: P.rank_function() is not None 

False 

sage: P = Poset(([1,2,3,4,5,6,7,8],[[1,4],[2,3],[3,4],[5,7],[6,7]]), facade = True) 

sage: f = P.rank_function(); f is not None 

True 

sage: f(5) 

0 

sage: f(2) 

0 

 

TESTS:: 

 

sage: P = Poset([[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]]) 

sage: r = P.rank_function() 

sage: for u,v in P.cover_relations_iterator(): 

....: if r(v) != r(u) + 1: 

....: print("Bug in rank_function!") 

 

:: 

 

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

sage: Q.rank_function() is None 

True 

 

test for ticket :trac:`14006`:: 

 

sage: H = Poset()._hasse_diagram 

sage: s = dumps(H) 

sage: f = H.rank_function() 

sage: s = dumps(H) 

""" 

if(self._rank is None): 

return None 

return self._rank.__getitem__ # the rank function is just the getitem of the list 

 

@lazy_attribute 

def _rank(self): 

r""" 

Builds the rank function of the poset, if it exists, i.e. 

an array ``d`` where ``d[object] = self.rank_function()(object)`` 

 

A *rank function* of a poset `P` is a function `r` 

that maps elements of `P` to integers and satisfies: 

`r(x) = r(y) + 1` if `x` covers `y`. The function `r` 

is normalized such that its minimum value on every 

connected component of the Hasse diagram of `P` is 

`0`. This determines the function `r` uniquely (when 

it exists). 

 

EXAMPLES:: 

 

sage: H = Poset()._hasse_diagram 

sage: H._rank 

[] 

sage: H = Poset([[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]])._hasse_diagram 

sage: H._rank 

[0, 1, 1, 2, 2, 1, 2, 3] 

sage: H = Poset(([1,2,3,4,5],[[1,2],[2,3],[3,4],[1,5],[5,4]]))._hasse_diagram 

sage: H._rank is None 

True 

""" 

# rank[i] is the rank of point i. It is equal to None until the rank of 

# i is computed 

rank = [None]*self.order() 

not_found = set(self.vertices()) 

while not_found: 

y = not_found.pop() 

rank[y] = 0 # We set some vertex to have rank 0 

component = set([y]) 

queue = set([y]) 

while queue: # look at the neighbors of y and set the ranks; 

# then look at the neighbors of the neighbors ... 

y = queue.pop() 

for x in self.neighbors_out(y): 

if rank[x] is None: 

rank[x] = rank[y] + 1 

queue.add(x) 

component.add(x) 

for x in self.neighbors_in(y): 

if rank[x] is None: 

rank[x] = rank[y] - 1 

queue.add(x) 

component.add(x) 

elif rank[x] != rank[y] - 1: 

return None 

# Normalize the ranks of vertices in the connected component 

# so that smallest is 0: 

m = min(rank[j] for j in component) 

for j in component: 

rank[j] -= m 

not_found.difference_update(component) 

#now, all ranks are set. 

return rank 

 

def rank(self,element=None): 

r""" 

Returns the rank of ``element``, or the rank of the poset if 

``element`` is ``None``. (The rank of a poset is the length of 

the longest chain of elements of the poset.) 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.rank(5) 

2 

sage: H.rank() 

3 

sage: Q = HasseDiagram({0:[1,2],1:[3],2:[],3:[]}) 

sage: Q.rank() 

2 

sage: Q.rank(1) 

1 

""" 

if element is None: 

return len(self.level_sets())-1 

else: 

return self.rank_function()(element) 

 

def is_ranked(self): 

r""" 

Returns True if the poset is ranked, and False otherwise. 

 

A poset is *ranked* if it admits a rank function. For more information 

about the rank function, see :meth:`~rank_function` 

and :meth:`~is_graded`. 

 

EXAMPLES:: 

 

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

sage: P.is_ranked() 

True 

sage: Q = Poset([[1,5],[2,6],[3],[4],[],[6,3],[4]]) 

sage: Q.is_ranked() 

False 

""" 

return bool(self.rank_function()) 

 

def covers(self,x,y): 

""" 

Returns True if y covers x and False otherwise. 

 

EXAMPLES:: 

 

sage: Q = Poset([[1,5],[2,6],[3],[4],[],[6,3],[4]]) 

sage: Q.covers(Q(1),Q(6)) 

True 

sage: Q.covers(Q(1),Q(4)) 

False 

""" 

return self.has_edge(x,y) 

 

def upper_covers_iterator(self,element): 

r""" 

Returns the list of elements that cover ``element``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: list(H.upper_covers_iterator(0)) 

[1, 2, 3] 

sage: list(H.upper_covers_iterator(7)) 

[] 

""" 

for x in self.neighbor_out_iterator(element): 

yield x 

 

def lower_covers_iterator(self,element): 

r""" 

Returns the list of elements that are covered by ``element``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: list(H.lower_covers_iterator(0)) 

[] 

sage: list(H.lower_covers_iterator(4)) 

[1, 2] 

""" 

for x in self.neighbor_in_iterator(element): 

yield x 

 

def cardinality(self): 

r""" 

Returns the number of elements in the poset. 

 

EXAMPLES:: 

 

sage: Poset([[1,2,3],[4],[4],[4],[]]).cardinality() 

5 

 

TESTS: 

 

For a time, this function was named ``size()``, which 

would override the same-named method of the underlying 

digraph. :trac:`8735` renamed this method to ``cardinality()`` 

with a deprecation warning. :trac:`11214` removed the warning 

since code for graphs was raising the warning inadvertently. 

This tests that ``size()`` for a Hasse diagram returns the 

number of edges in the digraph. :: 

 

sage: L = posets.BooleanLattice(5) 

sage: H = L.hasse_diagram() 

sage: H.size() 

80 

sage: H.size() == H.num_edges() 

True 

""" 

return self.order() 

 

def moebius_function(self,i,j): # dumb algorithm 

r""" 

Returns the value of the Möbius function of the poset 

on the elements ``i`` and ``j``. 

 

EXAMPLES:: 

 

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

sage: H = P._hasse_diagram 

sage: H.moebius_function(0,4) 

2 

sage: for u,v in P.cover_relations_iterator(): 

....: if P.moebius_function(u,v) != -1: 

....: print("Bug in moebius_function!") 

""" 

try: 

return self._moebius_function_values[(i,j)] 

except AttributeError: 

self._moebius_function_values = {} 

return self.moebius_function(i,j) 

except KeyError: 

if i == j: 

self._moebius_function_values[(i,j)] = 1 

elif i > j: 

self._moebius_function_values[(i,j)] = 0 

else: 

ci = self.closed_interval(i,j) 

if len(ci) == 0: 

self._moebius_function_values[(i,j)] = 0 

else: 

self._moebius_function_values[(i,j)] = \ 

-sum([self.moebius_function(i,k) for k in ci[:-1]]) 

return self._moebius_function_values[(i,j)] 

mobius_function = deprecated_function_alias(19855, moebius_function) 

 

def moebius_function_matrix(self): 

r""" 

Returns the matrix of the Möbius function of this poset 

 

This returns the sparse matrix over `\ZZ` whose ``(x, y)`` entry 

is the value of the Möbius function of ``self`` evaluated on 

``x`` and ``y``, and redefines :meth:`moebius_function` to use 

it. 

 

.. NOTE:: 

 

The result is cached in :meth:`_moebius_function_matrix`. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.moebius_function_matrix() 

[ 1 -1 -1 -1 1 0 1 0] 

[ 0 1 0 0 -1 0 0 0] 

[ 0 0 1 0 -1 -1 -1 2] 

[ 0 0 0 1 0 0 -1 0] 

[ 0 0 0 0 1 0 0 -1] 

[ 0 0 0 0 0 1 0 -1] 

[ 0 0 0 0 0 0 1 -1] 

[ 0 0 0 0 0 0 0 1] 

 

TESTS:: 

 

sage: H.moebius_function_matrix().is_immutable() 

True 

sage: hasattr(H,'_moebius_function_matrix') 

True 

 

sage: H.moebius_function == H._moebius_function_from_matrix 

True 

""" 

if not hasattr(self,'_moebius_function_matrix'): 

self._moebius_function_matrix = self.lequal_matrix().inverse().change_ring(ZZ) 

self._moebius_function_matrix.set_immutable() 

self.moebius_function = self._moebius_function_from_matrix 

return self._moebius_function_matrix 

mobius_function_matrix = deprecated_function_alias(19855, moebius_function_matrix) 

 

# Redefine self.moebius_function 

def _moebius_function_from_matrix(self, i,j): 

r""" 

Returns the value of the Möbius function of the poset 

on the elements ``i`` and ``j``. 

 

EXAMPLES:: 

 

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

sage: H = P._hasse_diagram 

sage: H.moebius_function(0,4) # indirect doctest 

2 

sage: for u,v in P.cover_relations_iterator(): 

....: if P.moebius_function(u,v) != -1: 

....: print("Bug in moebius_function!") 

 

This uses ``self._moebius_function_matrix``, as computed by 

:meth:`moebius_function_matrix`. 

""" 

return self._moebius_function_matrix[i,j] 

_mobius_function_from_matrix = deprecated_function_alias(19855, _moebius_function_from_matrix) 

 

@cached_method 

def coxeter_transformation(self): 

r""" 

Returns the matrix of the Auslander-Reiten translation acting on 

the Grothendieck group of the derived category of modules on the 

poset, in the basis of simple modules. 

 

EXAMPLES:: 

 

sage: M = posets.PentagonPoset()._hasse_diagram.coxeter_transformation(); M 

[ 0 0 0 0 -1] 

[ 0 0 0 1 -1] 

[ 0 1 0 0 -1] 

[-1 1 1 0 -1] 

[-1 1 0 1 -1] 

 

TESTS:: 

 

sage: M = posets.PentagonPoset()._hasse_diagram.coxeter_transformation() 

sage: M**8 == 1 

True 

""" 

return - self.lequal_matrix()*self.moebius_function_matrix().transpose() 

 

def order_filter(self, elements): 

""" 

Return the order filter generated by a list of elements. 

 

`I` is an order filter if, for any `x` in `I` and `y` such that 

`y \ge x`, then `y` is in `I`. 

 

EXAMPLES:: 

 

sage: H = posets.BooleanLattice(4)._hasse_diagram 

sage: H.order_filter([3,8]) 

[3, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

""" 

return sorted(list(self.depth_first_search(elements))) 

 

def principal_order_filter(self, i): 

""" 

Returns the order filter generated by ``i``. 

 

EXAMPLES:: 

 

sage: H = posets.BooleanLattice(4)._hasse_diagram 

sage: H.principal_order_filter(2) 

[2, 3, 6, 7, 10, 11, 14, 15] 

""" 

return self.order_filter([i]) 

 

def order_ideal(self, elements): 

""" 

Return the order ideal generated by a list of elements. 

 

`I` is an order ideal if, for any `x` in `I` and `y` such that 

`y \le x`, then `y` is in `I`. 

 

EXAMPLES:: 

 

sage: H = posets.BooleanLattice(4)._hasse_diagram 

sage: H.order_ideal([7,10]) 

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

""" 

return sorted(list( 

self.depth_first_search(elements, neighbors=self.neighbors_in))) 

 

def principal_order_ideal(self, i): 

""" 

Returns the order ideal generated by `i`. 

 

EXAMPLES:: 

 

sage: H = posets.BooleanLattice(4)._hasse_diagram 

sage: H.principal_order_ideal(6) 

[0, 2, 4, 6] 

""" 

return self.order_ideal([i]) 

 

@lazy_attribute 

def _leq_matrix(self): 

r""" 

Computes a matrix whose ``(i,j)`` entry is 1 if ``i`` is less than 

``j`` in the poset, and 0 otherwise; and redefines ``__lt__`` to 

use this matrix. 

 

EXAMPLES:: 

 

sage: P = Poset([[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]]) 

sage: H = P._hasse_diagram 

sage: H._leq_matrix 

[1 1 1 1 1 1 1 1] 

[0 1 0 1 0 0 0 1] 

[0 0 1 1 1 0 1 1] 

[0 0 0 1 0 0 0 1] 

[0 0 0 0 1 0 0 1] 

[0 0 0 0 0 1 1 1] 

[0 0 0 0 0 0 1 1] 

[0 0 0 0 0 0 0 1] 

 

""" 

# Create the matrix 

n = self.order() 

D = {} 

for i in range(n): 

for v in self.breadth_first_search(i): 

D[(i,v)] = 1 

M = matrix(ZZ, n, n, D, sparse=True) 

M.set_immutable() 

# Redefine self.is_lequal 

self.is_lequal = self._alternate_is_lequal 

# Return the matrix 

return M 

 

def lequal_matrix(self): 

""" 

Returns the matrix whose ``(i,j)`` entry is 1 if ``i`` is less 

than ``j`` in the poset, and 0 otherwise; and redefines 

``__lt__`` to use this matrix. 

 

EXAMPLES:: 

 

sage: P = Poset([[1,3,2],[4],[4,5,6],[6],[7],[7],[7],[]]) 

sage: H = P._hasse_diagram 

sage: H.lequal_matrix() 

[1 1 1 1 1 1 1 1] 

[0 1 0 1 0 0 0 1] 

[0 0 1 1 1 0 1 1] 

[0 0 0 1 0 0 0 1] 

[0 0 0 0 1 0 0 1] 

[0 0 0 0 0 1 1 1] 

[0 0 0 0 0 0 1 1] 

[0 0 0 0 0 0 0 1] 

 

TESTS:: 

 

sage: H.lequal_matrix().is_immutable() 

True 

""" 

return self._leq_matrix 

 

def _alternate_is_lequal(self,i,j): 

r""" 

Returns ``True`` if ``i`` is less than or equal to ``j`` in 

``self``, and ``False`` otherwise. 

 

.. NOTE:: 

 

If the :meth:`lequal_matrix` has been computed, then 

:meth:`is_lequal` is redefined to use the cached matrix. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2], 1:[2], 2:[3], 3:[4], 4:[]}) 

sage: H.lequal_matrix() 

[1 0 1 1 1] 

[0 1 1 1 1] 

[0 0 1 1 1] 

[0 0 0 1 1] 

[0 0 0 0 1] 

sage: x,y,z = 0, 1, 4 

sage: H._alternate_is_lequal(x,y) 

False 

sage: H._alternate_is_lequal(y,x) 

False 

sage: H._alternate_is_lequal(x,z) 

True 

sage: H._alternate_is_lequal(y,z) 

True 

sage: H._alternate_is_lequal(z,z) 

True 

""" 

return bool(self._leq_matrix[i,j]) 

 

def prime_elements(self): 

r""" 

Return the join-prime and meet-prime elements of the bounded poset. 

 

An element `x` of a poset `P` is join-prime if the subposet 

induced by `\{y \in P \mid y \not\ge x\}` has a top element. 

Meet-prime is defined dually. 

 

.. NOTE:: 

 

The poset is expected to be bounded, and this is *not* checked. 

 

OUTPUT: 

 

A pair `(j, m)` where `j` is a list of join-prime elements 

and `m` is a list of meet-prime elements. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1: [3], 2: [4], 3: [4]}) 

sage: H.prime_elements() 

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

""" 

n = self.order() 

join_primes = [] 

meet_primes = [] 

 

def add_elements(e): 

upset = frozenset(self.depth_first_search(e)) 

# The complement of the upper set of a join-prime must have 

# a top element. Maximal elements of the complement are those 

# covered by only elements in the upper set. If there is only 

# one maximal element, it is a meet-prime and 'e' is a 

# join-prime. 

meet_prime = None 

for u in upset: 

for m in self.neighbor_in_iterator(u): 

if (m not in upset and 

all(u_ in upset for u_ in 

self.neighbor_out_iterator(m))): 

if meet_prime is not None: 

return 

meet_prime = m 

join_primes.append(e) 

meet_primes.append(meet_prime) 

 

for e in range(n): 

# Join-primes are join-irreducibles, only check those. 

if self.in_degree(e) == 1: 

add_elements(e) 

 

return join_primes, meet_primes 

 

@lazy_attribute 

def _meet(self): 

r""" 

Return the matrix of meets of ``self``. The ``(x,y)``-entry of 

this matrix is the meet of ``x`` and ``y`` in ``self``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H._meet 

[0 0 0 0 0 0 0 0] 

[0 1 0 0 1 0 0 1] 

[0 0 2 0 2 2 2 2] 

[0 0 0 3 0 0 3 3] 

[0 1 2 0 4 2 2 4] 

[0 0 2 0 2 5 2 5] 

[0 0 2 3 2 2 6 6] 

[0 1 2 3 4 5 6 7] 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2,3],1:[2,3]}) 

sage: H.meet_matrix() 

Traceback (most recent call last): 

... 

ValueError: not a meet-semilattice: no bottom element 

 

sage: H = HasseDiagram({0:[1,2],1:[3,4],2:[3,4]}) 

sage: H.meet_matrix() 

Traceback (most recent call last): 

... 

LatticeError: no meet for ... 

 

sage: L = LatticePoset({0:[1,2,3],1:[4],2:[4],3:[4]}) 

sage: P = L.dual() 

sage: P.meet(2,3) 

4 

""" 

n = self.cardinality() 

if n == 0: 

return matrix(0) 

if not self.has_bottom(): 

raise ValueError("not a meet-semilattice: no bottom element") 

meet = [[0 for x in range(n)] for x in range(n)] 

lc = [self.neighbors_in(x) for x in range(n)] # Lc = lower covers 

 

for x in range(n): 

meet[x][x] = x 

for y in range(x): 

T = [meet[y][z] for z in lc[x]] 

 

q = max(T) 

for z in T: 

if meet[z][q] != z: 

raise LatticeError('meet', x, y) 

meet[x][y] = q 

meet[y][x] = q 

 

return matrix(ZZ, meet) 

 

def meet_matrix(self): 

r""" 

Returns the matrix of meets of ``self``. The ``(x,y)``-entry of 

this matrix is the meet of ``x`` and ``y`` in ``self``. 

 

This algorithm is modelled after the algorithm of Freese-Jezek-Nation 

(p217). It can also be found on page 140 of [Gec81]_. 

 

.. NOTE:: 

 

Once the matrix has been computed, it is stored in 

:meth:`_meet_matrix`. Delete this attribute if you want to 

recompute the matrix. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.meet_matrix() 

[0 0 0 0 0 0 0 0] 

[0 1 0 0 1 0 0 1] 

[0 0 2 0 2 2 2 2] 

[0 0 0 3 0 0 3 3] 

[0 1 2 0 4 2 2 4] 

[0 0 2 0 2 5 2 5] 

[0 0 2 3 2 2 6 6] 

[0 1 2 3 4 5 6 7] 

 

REFERENCE: 

 

.. [Gec81] Fundamentals of Computation Theory 

Gecseg, F. 

Proceedings of the 1981 International Fct-Conference 

Szeged, Hungaria, August 24-28, vol 117 

Springer-Verlag, 1981 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2,3],1:[2,3]}) 

sage: H.meet_matrix() 

Traceback (most recent call last): 

... 

ValueError: not a meet-semilattice: no bottom element 

 

sage: H = HasseDiagram({0:[1,2],1:[3,4],2:[3,4]}) 

sage: H.meet_matrix() 

Traceback (most recent call last): 

... 

LatticeError: no meet for ... 

""" 

return self._meet 

 

def is_meet_semilattice(self): 

r""" 

Returns ``True`` if ``self`` has a meet operation, and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.is_meet_semilattice() 

True 

 

sage: H = HasseDiagram({0:[1,2],1:[3],2:[3],3:[]}) 

sage: H.is_meet_semilattice() 

True 

 

sage: H = HasseDiagram({0:[2,3],1:[2,3]}) 

sage: H.is_meet_semilattice() 

False 

""" 

try: 

self.meet_matrix() 

except ValueError: 

return False 

else: 

return True 

 

@lazy_attribute 

def _join(self): 

r""" 

Computes a matrix whose ``(x,y)``-entry is the join of ``x`` 

and ``y`` in ``self`` 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.join_matrix() # indirect doctest 

[0 1 2 3 4 5 6 7] 

[1 1 4 7 4 7 7 7] 

[2 4 2 6 4 5 6 7] 

[3 7 6 3 7 7 6 7] 

[4 4 4 7 4 7 7 7] 

[5 7 5 7 7 5 7 7] 

[6 7 6 6 7 7 6 7] 

[7 7 7 7 7 7 7 7] 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2,3],1:[2,3]}) 

sage: H.join_matrix() 

Traceback (most recent call last): 

... 

ValueError: not a join-semilattice: no top element 

 

sage: H = HasseDiagram({0:[2,3],1:[2,3],2:[4],3:[4]}) 

sage: H.join_matrix() 

Traceback (most recent call last): 

... 

LatticeError: no join for ... 

 

sage: L = LatticePoset({0:[1,2,3],1:[4],2:[4],3:[4]}) 

sage: P = L.dual() 

sage: P.join(2,3) 

0 

""" 

n = self.cardinality() 

if n == 0: 

return matrix(0) 

if not self.has_top(): 

raise ValueError("not a join-semilattice: no top element") 

join = [[n for x in range(n)] for x in range(n)] 

uc = [self.neighbors_out(x) for x in range(n)] # uc = upper covers 

 

for x in range(n-1, -1, -1): 

join[x][x] = x 

for y in range(n-1, x, -1): 

T = [join[y][z] for z in uc[x]] 

 

q = min(T) 

for z in T: 

if join[z][q] != z: 

raise LatticeError('join', x, y) 

join[x][y] = q 

join[y][x] = q 

 

return matrix(ZZ, join) 

 

def join_matrix(self): 

r""" 

Returns the matrix of joins of ``self``. The ``(x,y)``-entry 

of this matrix is the join of ``x`` and ``y`` in ``self``. 

 

This algorithm is modelled after the algorithm of Freese-Jezek-Nation 

(p217). It can also be found on page 140 of [Gec81]_. 

 

.. note:: 

 

Once the matrix has been computed, it is stored in 

:meth:`_join_matrix`. Delete this attribute if you want 

to recompute the matrix. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.join_matrix() 

[0 1 2 3 4 5 6 7] 

[1 1 4 7 4 7 7 7] 

[2 4 2 6 4 5 6 7] 

[3 7 6 3 7 7 6 7] 

[4 4 4 7 4 7 7 7] 

[5 7 5 7 7 5 7 7] 

[6 7 6 6 7 7 6 7] 

[7 7 7 7 7 7 7 7] 

 

TESTS:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[2,3],1:[2,3]}) 

sage: H.join_matrix() 

Traceback (most recent call last): 

... 

ValueError: not a join-semilattice: no top element 

 

sage: H = HasseDiagram({0:[2,3],1:[2,3],2:[4],3:[4]}) 

sage: H.join_matrix() 

Traceback (most recent call last): 

... 

LatticeError: no join for ... 

""" 

return self._join 

 

def is_join_semilattice(self): 

r""" 

Returns ``True`` if ``self`` has a join operation, and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.is_join_semilattice() 

True 

sage: H = HasseDiagram({0:[2,3],1:[2,3]}) 

sage: H.is_join_semilattice() 

False 

sage: H = HasseDiagram({0:[2,3],1:[2,3],2:[4],3:[4]}) 

sage: H.is_join_semilattice() 

False 

""" 

try: 

self.join_matrix() 

except ValueError: 

return False 

else: 

return True 

 

def find_nonsemidistributive_elements(self, meet_or_join): 

r""" 

Check if the lattice is semidistributive or not. 

 

INPUT: 

 

- ``meet_or_join`` -- string ``'meet'`` or ``'join'`` 

to decide if to check for join-semidistributivity or 

meet-semidistributivity 

 

OUTPUT: 

 

- ``None`` if the lattice is semidistributive OR 

- tuple ``(u, e, x, y)`` such that 

`u = e \vee x = e \vee y` but `u \neq e \vee (x \wedge y)` 

if ``meet_or_join=='join'`` and 

`u = e \wedge x = e \wedge y` but `u \neq e \wedge (x \vee y)` 

if ``meet_or_join=='meet'`` 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1, 2], 1:[3, 4], 2:[4, 5], 3:[6], 

....: 4:[6], 5:[6]}) 

sage: H.find_nonsemidistributive_elements('join') is None 

False 

sage: H.find_nonsemidistributive_elements('meet') is None 

True 

""" 

if meet_or_join == 'join': 

M1 = self._join 

M2 = self._meet 

elif meet_or_join == 'meet': 

M1 = self._meet 

M2 = self._join 

else: 

raise ValueError("meet_or_join must be 'join' or 'meet'") 

 

n = self.order() 

 

for e in range(n): 

for x in range(n): 

u = M1[e, x] 

for y in range(x): 

if u == M1[e, y]: 

if u != M1[e, M2[x, y]]: 

return (u, e, x, y) 

 

return None 

 

def is_distributive_lattice(self): # still a dumb algorithm... 

r""" 

Deprecated. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,3,2],1:[4],2:[4,5,6],3:[6],4:[7],5:[7],6:[7],7:[]}) 

sage: H.is_distributive_lattice() 

doctest:...: DeprecationWarning: This function will be removed in future versions. Convert ... 

False 

""" 

from sage.misc.superseded import deprecation 

deprecation(23111, "This function will be removed in future versions. Convert to lattice and use is_distributive().") 

try: 

jn = self.join_matrix() 

mt = self.meet_matrix() 

except ValueError: 

return False 

n = jn.ncols() 

for x in range(n): 

for y in range(n): 

for z in range(n): 

if mt[x][jn[y][z]]!=jn[mt[x][y]][mt[x][z]]: return False 

return True 

 

def vertical_decomposition(self, return_list=False): 

""" 

Return vertical decomposition of the lattice. 

 

This is the backend function for vertical decomposition 

functions of lattices. 

 

The property of being vertically decomposable is defined for lattices. 

This is *not* checked, and the function works with any bounded poset. 

 

INPUT: 

 

- ``return_list``, a boolean. If ``False`` (the default), return 

an element that is not the top neither the bottom element of the 

lattice, but is comparable to all elements of the lattice, if 

the lattice is vertically decomposable and ``None`` otherwise. 

If ``True``, return list of decomposition elements. 

 

EXAMPLES:: 

 

sage: H = posets.BooleanLattice(4)._hasse_diagram 

sage: H.vertical_decomposition() is None 

True 

sage: P = Poset( ([1,2,3,6,12,18,36], attrcall("divides")) ) 

sage: P._hasse_diagram.vertical_decomposition() 

3 

sage: P._hasse_diagram.vertical_decomposition(return_list=True) 

[3] 

""" 

n = self.cardinality() 

if n < 3: 

if return_list: 

return [] 

else: 

return None 

result = [] # Never take the bottom element to list. 

e = 0 

m = 0 

for i in range(n-1): 

for j in self.outgoing_edge_iterator(i): 

m = max(m, j[1]) 

if m == i+1: 

if not return_list: 

if m < n-1: 

return m 

else: 

return None 

result.append(m) 

result.pop() # Remove the top element. 

return result 

 

def is_complemented(self): 

""" 

Return an element of the lattice that has no complement. 

 

If the lattice is complemented, return ``None``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

 

sage: H = HasseDiagram({0:[1, 2], 1:[3], 2:[3], 3:[4]}) 

sage: H.is_complemented() 

1 

 

sage: H = HasseDiagram({0:[1, 2, 3], 1:[4], 2:[4], 3:[4]}) 

sage: H.is_complemented() is None 

True 

""" 

mt = self.meet_matrix() 

jn = self.join_matrix() 

top = self.cardinality() - 1 

has_complement = [False] * top 

 

for i in range(1, top): 

if has_complement[i]: 

continue 

for j in range(top, 0, -1): 

if jn[i, j] == top and mt[i, j] == 0: 

has_complement[j] = True 

break 

else: 

return i 

 

return None 

 

def pseudocomplement(self, element): 

""" 

Return the pseudocomplement of ``element``, if it exists. 

 

The pseudocomplement is the greatest element whose 

meet with given element is the bottom element. It may 

not exist, and then the function returns ``None``. 

 

INPUT: 

 

- ``element`` -- an element of the lattice. 

 

OUTPUT: 

 

An element of the Hasse diagram, i.e. an integer, or 

``None`` if the pseudocomplement does not exist. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1: [3], 2: [4], 3: [4]}) 

sage: H.pseudocomplement(2) 

3 

 

sage: H = HasseDiagram({0: [1, 2, 3], 1: [4], 2: [4], 3: [4]}) 

sage: H.pseudocomplement(2) is None 

True 

""" 

e = self.order() - 1 

while self._meet[e, element] != 0: 

e -= 1 

e1 = e 

while e1 > 0: 

if self._meet[e1, element] == 0 and not self.is_lequal(e1, e): 

return None 

e1 -= 1 

return e 

 

def orthocomplementations_iterator(self): 

r""" 

Return an iterator over orthocomplementations of the lattice. 

 

OUTPUT: 

 

An iterator that gives plain list of integers. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2], 1:[3,4], 3:[5], 4:[5], 2:[6,7], 

....: 6:[8], 7:[8], 5:[9], 8:[9]}) 

sage: list(H.orthocomplementations_iterator()) 

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

 

ALGORITHM: 

 

As ``DiamondPoset(2*n+2)`` has `(2n)!/(n!2^n)` different 

orthocomplementations, the complexity of listing all of 

them is necessarily `O(n!)`. 

 

An orthocomplemented lattice is self-dual, so that for example 

orthocomplement of an atom is a coatom. This function 

basically just computes list of possible orthocomplementations 

for every element (i.e. they must be complements and "duals"), 

and then tries to fit them all. 

 

TESTS: 

 

Special and corner cases:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram() # Empty 

sage: list(H.orthocomplementations_iterator()) 

[[]] 

sage: H = HasseDiagram({0:[]}) # One element 

sage: list(H.orthocomplementations_iterator()) 

[[0]] 

sage: H = HasseDiagram({0:[1]}) # Two elements 

sage: list(H.orthocomplementations_iterator()) 

[[1, 0]] 

 

Trivial cases: odd number of elements, not self-dual, not complemented:: 

 

sage: H = posets.DiamondPoset(5)._hasse_diagram 

sage: list(H.orthocomplementations_iterator()) 

[] 

sage: H = posets.ChainPoset(4)._hasse_diagram 

sage: list(H.orthocomplementations_iterator()) 

[] 

sage: H = HasseDiagram( ([[0, 1], [0, 2], [0, 3], [1, 4], [1, 8], [4, 6], [4, 7], [6, 9], [7, 9], [2, 5], [3, 5], [5, 8], [8, 9]]) ) 

sage: list(H.orthocomplementations_iterator()) 

[] 

sage: H = HasseDiagram({0:[1, 2, 3], 1: [4], 2:[4], 3: [5], 4:[5]}) 

sage: list(H.orthocomplementations_iterator()) 

[] 

 

Complemented, self-dual and even number of elements, but 

not orthocomplemented:: 

 

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

sage: list(H.orthocomplementations_iterator()) 

[] 

 

Unique orthocomplementations; second is not uniquely complemented, 

but has only one orthocomplementation. 

 

sage: H = posets.BooleanLattice(4)._hasse_diagram # Uniquely complemented 

sage: len(list(H.orthocomplementations_iterator())) 

1 

sage: H = HasseDiagram({0:[1, 2], 1:[3], 2:[4], 3:[5], 4:[5]}) 

sage: len([_ for _ in H.orthocomplementations_iterator()]) 

1 

 

"Lengthening diamond" must keep the number of orthocomplementations:: 

 

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

sage: n = len([_ for _ in H.orthocomplementations_iterator()]); n 

3 

sage: H = HasseDiagram('M]??O?@??C??OA???OA??@?A??C?A??O??') 

sage: len([_ for _ in H.orthocomplementations_iterator()]) == n 

True 

 

This lattice has an unique "possible orthocomplement" for every 

element, but they can not be fit together; orthocomplement pairs 

would be 0-11, 1-7, 2-4, 3-10, 5-9 and 6-8, and then orthocomplements 

for chain 0-1-6-11 would be 11-7-8-0, which is not a chain:: 

 

sage: H = HasseDiagram('KTGG_?AAC?O?o?@?@?E?@?@??') 

sage: list([_ for _ in H.orthocomplementations_iterator()]) 

[] 

""" 

n = self.order() 

 

# Special cases first 

if n == 0: 

yield [] 

return 

if n == 1: 

yield [0] 

return 

if n % 2 == 1: 

return 

 

dual_isomorphism = self.is_isomorphic(self.reverse(), certificate=True)[1] 

if dual_isomorphism is None: # i.e. if the lattice is not self-dual. 

return 

 

# We compute possible orthocomplements, i.e. elements 

# with "dual position" and complement to each other. 

 

orbits = self.automorphism_group(return_group=False, orbits=True) 

 

orbit_number = [None] * n 

for ind, orbit in enumerate(orbits): 

for e in orbit: 

orbit_number[e] = ind 

 

comps = [None] * n 

for e in range(n): 

# Fix following after ticket #20727 

comps[e] = [x for x in range(n) if 

self._meet[e, x] == 0 and self._join[e, x] == n-1 and 

x in orbits[orbit_number[dual_isomorphism[e]]]] 

 

# Fitting is done by this recursive function: 

def recursive_fit(orthocomplements, unbinded): 

if not unbinded: 

yield orthocomplements 

else: 

next_to_fit = unbinded[0] 

possible_values = [x for x in comps[next_to_fit] if not x in orthocomplements] 

for x in self.lower_covers_iterator(next_to_fit): 

if orthocomplements[x] is not None: 

possible_values = [y for y in possible_values if self.has_edge(y, orthocomplements[x])] 

for x in self.upper_covers_iterator(next_to_fit): 

if orthocomplements[x] is not None: 

possible_values = [y for y in possible_values if self.has_edge(orthocomplements[x], y)] 

 

for e in possible_values: 

 

new_binded = orthocomplements[:] 

new_binded[next_to_fit] = e 

new_binded[e] = next_to_fit 

 

new_unbinded = unbinded[1:] # Remove next_to_fit 

new_unbinded.remove(e) 

 

for i_want_python3_yield_from in recursive_fit(new_binded, new_unbinded): 

yield i_want_python3_yield_from 

 

start = [None] * n 

# A little optimization 

for e in range(n): 

if len(comps[e]) == 0: # Not any possible orthocomplement 

return 

if len(comps[e]) == 1: # Do not re-fit this every time 

e_ = comps[e][0] 

# Every element might have one possible orthocomplement, 

# but so that they don't fit together. Must check that. 

for lc in self.lower_covers_iterator(e): 

if start[lc] is not None: 

if not self.has_edge(e_, start[lc]): 

return 

if start[e_] is None: 

start[e] = e_ 

start[e_] = e 

start_unbinded = [e for e in range(n) if start[e] is None] 

 

for i_want_python3_yield_from in recursive_fit(start, start_unbinded): 

yield i_want_python3_yield_from 

 

def find_nonsemimodular_pair(self, upper): 

""" 

Return pair of elements showing the lattice is not modular. 

 

INPUT: 

 

- upper, a Boolean -- if ``True``, test wheter the lattice is 

upper semimodular; otherwise test whether the lattice is 

lower semimodular. 

 

OUTPUT: 

 

``None``, if the lattice is semimodular. Pair `(a, b)` violating 

semimodularity otherwise. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1, 2], 1:[3, 4], 2:[4, 5], 3:[6], 4:[6], 5:[6]}) 

sage: H.find_nonsemimodular_pair(upper=True) is None 

True 

sage: H.find_nonsemimodular_pair(upper=False) 

(5, 3) 

 

sage: H_ = HasseDiagram(H.reverse().relabel(lambda x: 6-x, inplace=False)) 

sage: H_.find_nonsemimodular_pair(upper=True) 

(3, 1) 

sage: H_.find_nonsemimodular_pair(upper=False) is None 

True 

""" 

if upper: 

neighbors = self.neighbors_out 

else: 

neighbors = self.neighbors_in 

 

n = self.order() 

for e in range(n): 

covers = neighbors(e) 

covers_len = len(covers) 

if covers_len < 2: 

continue 

for a_i in range(covers_len): 

a = covers[a_i] 

covers_a = neighbors(a) 

for b_i in range(a_i): 

b = covers[b_i] 

if not any(j in covers_a for j in neighbors(b)): 

return (a, b) 

return None 

 

def antichains_iterator(self): 

r""" 

Return an iterator over the antichains of the poset. 

 

.. note:: 

 

The algorithm is based on Freese-Jezek-Nation p. 226. 

It does a depth first search through the set of all 

antichains organized in a prefix tree. 

 

EXAMPLES:: 

 

sage: P = posets.PentagonPoset() 

sage: H = P._hasse_diagram 

sage: H.antichains_iterator() 

<generator object antichains_iterator at ...> 

sage: list(H.antichains_iterator()) 

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

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0:[1,2],1:[4],2:[3],3:[4]}) 

sage: list(H.antichains_iterator()) 

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

 

sage: H = HasseDiagram({0:[],1:[],2:[]}) 

sage: list(H.antichains_iterator()) 

[[], [2], [1], [1, 2], [0], [0, 2], [0, 1], [0, 1, 2]] 

 

sage: H = HasseDiagram({0:[1],1:[2],2:[3],3:[4]}) 

sage: list(H.antichains_iterator()) 

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

 

TESTS:: 

 

sage: H = Poset()._hasse_diagram 

sage: list(H.antichains_iterator()) 

[[]] 

""" 

# NOTE: Ordering of antichains as a prefix tree is crucial for 

# congruences_iterator() to work. Change it, if you change this. 

 

# Complexity note: 

# antichains_queues never grows longer than self.cardinality(). 

# Indeed, if a appears before b in antichains_queues, then 

# the largest element of a is strictly smaller than that of b. 

antichains_queues = [([], list(range(self.cardinality()-1, -1, -1)))] 

leq = self.lequal_matrix() 

while antichains_queues: 

(antichain, queue) = antichains_queues.pop() 

# Invariant: 

# - the elements of antichain are independent 

# - the elements of queue are independent from those of antichain 

yield antichain 

while queue: 

x = queue.pop() 

new_antichain = antichain + [x] 

new_queue = [t for t in queue if not (leq[t,x] or leq[x,t])] 

antichains_queues.append((new_antichain, new_queue)) 

 

def are_incomparable(self, i, j): 

""" 

Returns whether ``i`` and ``j`` are incomparable in the poset 

 

INPUT: 

 

- ``i``, ``j`` -- vertices of this Hasse diagram 

 

EXAMPLES:: 

 

sage: P = posets.PentagonPoset() 

sage: H = P._hasse_diagram 

sage: H.are_incomparable(1,2) 

True 

sage: [ (i,j) for i in H.vertices() for j in H.vertices() if H.are_incomparable(i,j)] 

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

""" 

mat = self._leq_matrix 

return not mat[i,j] and not mat[j,i] 

 

def are_comparable(self, i, j): 

""" 

Returns whether ``i`` and ``j`` are comparable in the poset 

 

INPUT: 

 

- ``i``, ``j`` -- vertices of this Hasse diagram 

 

EXAMPLES:: 

 

sage: P = posets.PentagonPoset() 

sage: H = P._hasse_diagram 

sage: H.are_comparable(1,2) 

False 

sage: [ (i,j) for i in H.vertices() for j in H.vertices() if H.are_comparable(i,j)] 

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

""" 

mat = self._leq_matrix 

return bool(mat[i,j]) or bool(mat[j,i]) 

 

def antichains(self, element_class = list): 

""" 

Returns all antichains of ``self``, organized as a 

prefix tree 

 

INPUT: 

 

- ``element_class`` -- (default:list) an iterable type 

 

EXAMPLES:: 

 

sage: P = posets.PentagonPoset() 

sage: H = P._hasse_diagram 

sage: A = H.antichains() 

sage: list(A) 

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

sage: A.cardinality() 

8 

sage: [1,3] in A 

True 

sage: [1,4] in A 

False 

 

TESTS:: 

 

sage: TestSuite(A).run(skip = "_test_pickling") 

 

.. note:: It's actually the pickling of the cached method 

:meth:`coxeter_transformation` that fails ... 

 

TESTS:: 

 

sage: A = Poset()._hasse_diagram.antichains() 

sage: list(A) 

[[]] 

sage: TestSuite(A).run() 

""" 

from sage.combinat.subsets_pairwise import PairwiseCompatibleSubsets 

return PairwiseCompatibleSubsets(self.vertices(), 

self.are_incomparable, 

element_class = element_class) 

 

def chains(self, element_class=list, exclude=None): 

""" 

Return all chains of ``self``, organized as a prefix tree. 

 

INPUT: 

 

- ``element_class`` -- (default: ``list``) an iterable type 

 

- ``exclude`` -- elements of the poset to be excluded 

(default: ``None``) 

 

OUTPUT: 

 

The enumerated set (with a forest structure given by prefix 

ordering) consisting of all chains of ``self``, each of 

which is given as an ``element_class``. 

 

EXAMPLES:: 

 

sage: P = posets.PentagonPoset() 

sage: H = P._hasse_diagram 

sage: A = H.chains() 

sage: list(A) 

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

sage: A.cardinality() 

20 

sage: [1,3] in A 

False 

sage: [1,4] in A 

True 

 

One can exclude some vertices:: 

 

sage: list(H.chains(exclude=[4, 3])) 

[[], [0], [0, 1], [0, 2], [1], [2]] 

 

The ``element_class`` keyword determines how the chains are 

being returned: 

 

sage: P = Poset({1: [2, 3], 2: [4]}) 

sage: list(P._hasse_diagram.chains(element_class=tuple)) 

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

sage: list(P._hasse_diagram.chains()) 

[[], [0], [0, 1], [0, 1, 2], [0, 2], [0, 3], [1], [1, 2], [2], [3]] 

 

(Note that taking the Hasse diagram has renamed the vertices.) 

 

sage: list(P._hasse_diagram.chains(element_class=tuple, exclude=[0])) 

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

 

.. SEEALSO:: :meth:`antichains` 

""" 

from sage.combinat.subsets_pairwise import PairwiseCompatibleSubsets 

if not(exclude is None): 

vertices = [u for u in self.vertices() if not u in exclude] 

else: 

vertices = self.vertices() 

return PairwiseCompatibleSubsets(vertices, 

self.are_comparable, 

element_class = element_class) 

 

def sublattices_iterator(self, elms, min_e): 

""" 

Return an iterator over sublattices of the Hasse diagram. 

 

INPUT: 

 

- ``elms`` -- elements already in sublattice; use set() at start 

- ``min_e`` -- smallest new element to add for new sublattices 

 

OUTPUT: 

 

List of sublattices as sets of integers. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1:[3], 2:[3]}) 

sage: it = H.sublattices_iterator(set(), 0); it 

<generator object sublattices_iterator at ...> 

sage: next(it) 

set() 

sage: next(it) 

{0} 

""" 

# Python3-note: "yield from" would be simpler. 

yield elms 

for e in range(min_e, self.cardinality()): 

if e in elms: 

continue 

current_set = set(elms) 

gens = set([e]) 

while gens: 

g = gens.pop() 

if g < e and g not in elms: 

break 

if g in current_set: 

continue 

for x in current_set: 

gens.add(self._meet[x, g]) 

gens.add(self._join[x, g]) 

current_set.add(g) 

else: 

for x in self.sublattices_iterator(current_set, e+1): 

yield x 

 

def maximal_sublattices(self): 

""" 

Return maximal sublattices of the lattice. 

 

EXAMPLES:: 

 

sage: L = posets.PentagonPoset() 

sage: ms = L._hasse_diagram.maximal_sublattices() 

sage: sorted(ms, key=sorted) 

[{0, 1, 2, 4}, {0, 1, 3, 4}, {0, 2, 3, 4}] 

""" 

jn = self.join_matrix() 

mt = self.meet_matrix() 

 

def sublattice(elms, e): 

""" 

Helper function to get sublattice generated by list 

of elements. 

""" 

gens_remaining = set([e]) 

current_set = set(elms) 

 

while gens_remaining: 

g = gens_remaining.pop() 

if g in current_set: 

continue 

for x in current_set: 

gens_remaining.add(jn[x, g]) 

gens_remaining.add(mt[x, g]) 

current_set.add(g) 

 

return current_set 

 

N = self.cardinality() 

elms = [0] 

sublats = [set([0])] 

result = [] 

skip = -1 

 

while True: 

# First try to append an element 

found_element_to_append = False 

e = elms[-1] 

while e != skip: 

e += 1 

if e == N: 

maybe_found = sublats[-1] 

if not any(maybe_found.issubset(x) for x in result): 

result.append(sublats[-1]) 

break 

if e in sublats[-1]: 

continue 

# Let's try to add 'e' and see what happens. 

sl = sublattice(sublats[-1], e) 

if len(sl) < N: 

# Skip this, if it generated a back-reference. 

new_elms = sl.difference(sublats[-1]) 

if not any(x < e for x in new_elms): 

found_element_to_append = True 

break 

# Now sl is whole lattice, so we continue and try 

# appending another element. 

 

if found_element_to_append: 

elms.append(e) 

sublats.append(sl) 

continue 

 

# Can not append. Try to increment last element. 

e = elms.pop() 

sublats.pop() 

 

last_element_increment = True 

while True: 

e += 1 

if e == N: 

last_element_increment = False 

break 

if e in sublats[-1]: 

continue 

sl = sublattice(sublats[-1], e) 

if len(sl) == N: 

continue 

 

new_elms = sl.difference(set(sublats[-1])) 

if any(x < e for x in new_elms): 

continue 

 

elms.append(e) 

sublats.append(sl) 

break 

 

if not last_element_increment: 

# Can not append nor increment. "Backtracking". 

skip = elms[-1] 

if skip == 0: 

break 

 

# Special case to handle at last. 

if len(self.neighbors_out(0)) == 1: 

result.append(set(range(1, N))) 

 

return result 

 

def frattini_sublattice(self): 

""" 

Return the list of elements of the Frattini sublattice of the lattice. 

 

EXAMPLES:: 

 

sage: H = posets.PentagonPoset()._hasse_diagram 

sage: H.frattini_sublattice() 

[0, 4] 

""" 

# Just a direct computation, no optimization at all. 

n = self.cardinality() 

if n == 0 or n == 2: return [] 

if n == 1: return [0] 

max_sublats = self.maximal_sublattices() 

return [e for e in range(self.cardinality()) if 

all(e in ms for ms in max_sublats)] 

 

def kappa_dual(self, a): 

r""" 

Return the minimum element smaller than the element covering 

``a`` but not smaller than ``a``. 

 

Define `\kappa^*(a)` as the minimum element of 

`(\downarrow a_*) \setminus (\downarrow a)`, where `a_*` is the element 

covering `a`. It is always a join-irreducible element, if it exists. 

 

.. NOTE:: 

 

Element ``a`` is expected to be meet-irreducible, and 

this is *not* checked. 

 

INPUT: 

 

- ``a`` -- a join-irreducible element of the lattice 

 

OUTPUT: 

 

The element `\kappa^*(a)` or ``None`` if there 

is not a unique smallest element with given constraints. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1: [3, 4], 2: [4, 5], 3: [6], 4: [6], 5: [6]}) 

sage: H.kappa_dual(3) 

2 

sage: H.kappa_dual(4) is None 

True 

 

TESTS:: 

 

sage: H = HasseDiagram({0: [1]}) 

sage: H.kappa_dual(0) 

1 

""" 

uc = next(self.neighbor_out_iterator(a)) 

if self.in_degree(uc) == 1: 

return uc 

lt_a = set(self.depth_first_search(a, neighbors=self.neighbors_in)) 

tmp = list(self.depth_first_search(uc, neighbors=lambda v: [v_ for v_ in self.neighbors_in(v) if v_ not in lt_a])) 

result = None 

for e in tmp: 

if all(x not in tmp for x in self.neighbors_in(e)): 

if result: 

return None 

result = e 

return result 

 

def skeleton(self): 

""" 

Return the skeleton of the lattice. 

 

The lattice is expected to be pseudocomplemented and non-empty. 

 

The skeleton of the lattice is the subposet induced by 

those elements that are the pseudocomplement to at least one 

element. 

 

OUTPUT: 

 

List of elements such that the subposet induced by them is 

the skeleton of the lattice. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1: [3, 4], 2: [4], 

....: 3: [5], 4: [5]}) 

sage: H.skeleton() 

[5, 2, 0, 3] 

""" 

p_atoms = [] 

for atom in self.neighbor_out_iterator(0): 

p_atom = self.pseudocomplement(atom) 

if p_atom is None: 

raise ValueError("lattice is not pseudocomplemented") 

p_atoms.append(p_atom) 

n = len(p_atoms) 

mt = self._meet 

pos = [0] * n 

meets = [self.order()-1] * n 

result = [self.order()-1] 

i = 0 

 

while i >= 0: 

new_meet = mt[meets[i-1], p_atoms[pos[i]]] 

result.append(new_meet) 

if pos[i] == n-1: 

i -= 1 

pos[i] = pos[i]+1 

else: 

meets[i] = new_meet 

pos[i+1] = pos[i]+1 

i += 1 

 

return result 

 

def is_convex_subset(self, S): 

r""" 

Return ``True`` if `S` is a convex subset of the poset, 

and ``False`` otherwise. 

 

A subset `S` is *convex* in the poset if `b \in S` whenever 

`a, c \in S` and `a \le b \le c`. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: B3 = HasseDiagram({0: [1, 2, 4], 1: [3, 5], 2: [3, 6], 

....: 3: [7], 4: [5, 6], 5: [7], 6: [7]}) 

sage: B3.is_convex_subset([1, 3, 5, 4]) # Also connected 

True 

sage: B3.is_convex_subset([1, 3, 4]) # Not connected 

True 

 

sage: B3.is_convex_subset([0, 1, 2, 3, 6]) # No, 0 < 4 < 6 

False 

sage: B3.is_convex_subset([0, 1, 2, 7]) # No, 1 < 3 < 7. 

False 

 

TESTS:: 

 

sage: B3.is_convex_subset([]) 

True 

sage: B3.is_convex_subset([6]) 

True 

""" 

if not S: # S is empty set 

return True 

s_max = max(S) 

ok = set() # Already checked elements not less than any element is S. 

 

for a in S: 

for b in self.neighbor_out_iterator(a): 

if b >= s_max or b in S: 

continue 

# Now b not in S, b > a and a in S. 

neighbors = lambda v_: [v for v in self.neighbor_out_iterator(v_) 

if v <= s_max and v not in ok] 

for c in self.depth_first_search(b, neighbors=neighbors): 

if c in S: # Now c in S, b not in S, a in S, a < b < c. 

return False 

ok.add(c) # Do not re-check this for being our b. 

 

return True 

 

def neutral_elements(self): 

""" 

Return the list of neutral elements of the lattice. 

 

An element `a` in a lattice is neutral if the sublattice 

generated by `a`, `x` and `y` is distributive for every 

`x`, `y` in the lattice. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H= HasseDiagram({0: [1, 2], 1: [4], 2: [3], 3: [4, 5], 

....: 4: [6], 5:[6]}) 

sage: sorted(H.neutral_elements()) 

[0, 4, 6] 

 

ALGORITHM: 

 

Basically we just check the distributivity against all element 

pairs `x, y` to see if element `a` is neutral or not. 

 

If we found that `a, x, y` is not a distributive triple, we add 

all three to list of non-neutral elements. If we found `a` to 

be neutral, we add it to list of neutral elements. When testing 

we skip already found neutral elements, as they can't be our `x` 

or `y`. 

 

We skip `a, x, y` as trivial if it is a chain. We do that by 

letting `x` to be a non-comparable to `a`; `y` can be any element. 

 

We first try to found `x` and `y` from elements not yet tested, 

so that we could get three birds with one stone. 

 

And last, the top and bottom elements are always neutral and 

need not be tested. 

""" 

n = self.order() 

if n < 5: 

return set(range(n)) 

 

todo = set(range(1, n-1)) 

neutrals = set([0, n-1]) 

notneutrals = set() 

all_elements = set(range(n)) 

 

mt = self._meet 

jn = self._join 

 

def is_neutral(a): 

noncomp = all_elements.difference(self.depth_first_search(a)) 

noncomp.difference_update(self.depth_first_search(a, neighbors=self.neighbors_in)) 

 

for x in noncomp.intersection(todo): 

meet_ax = mt[a, x] 

join_ax = jn[a, x] 

for y in todo: 

if (mt[mt[join_ax, jn[a, y]], jn[x, y]] != 

jn[jn[meet_ax, mt[a, y]], mt[x, y]]): 

notneutrals.add(x) 

notneutrals.add(y) 

return False 

for y in notneutrals: 

if (mt[mt[join_ax, jn[a, y]], jn[x, y]] != 

jn[jn[meet_ax, mt[a, y]], mt[x, y]]): 

notneutrals.add(x) 

return False 

for x in noncomp.difference(todo): 

meet_ax = mt[a, x] 

join_ax = jn[a, x] 

for y in todo: 

if (mt[mt[join_ax, jn[a, y]], jn[x, y]] != 

jn[jn[meet_ax, mt[a, y]], mt[x, y]]): 

notneutrals.add(y) 

return False 

for y in notneutrals: 

if (mt[mt[join_ax, jn[a, y]], jn[x, y]] != 

jn[jn[meet_ax, mt[a, y]], mt[x, y]]): 

return False 

return True 

 

while todo: 

e = todo.pop() 

if is_neutral(e): 

neutrals.add(e) 

else: 

notneutrals.add(e) 

 

return neutrals 

 

def kappa(self, a): 

r""" 

Return the maximum element greater than the element covered 

by ``a`` but not greater than ``a``. 

 

Define `\kappa(a)` as the maximum element of 

`(\uparrow a_*) \setminus (\uparrow a)`, where `a_*` is the element 

covered by `a`. It is always a meet-irreducible element, if it exists. 

 

.. NOTE:: 

 

Element ``a`` is expected to be join-irreducible, and 

this is *not* checked. 

 

INPUT: 

 

- ``a`` -- a join-irreducible element of the lattice 

 

OUTPUT: 

 

The element `\kappa(a)` or ``None`` if there 

is not a unique greatest element with given constraints. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2, 3], 1: [4], 2: [4, 5], 3: [5], 4: [6], 5: [6]}) 

sage: H.kappa(1) 

5 

sage: H.kappa(2) is None 

True 

 

TESTS:: 

 

sage: H = HasseDiagram({0: [1]}) 

sage: H.kappa(1) 

0 

""" 

lc = next(self.neighbor_in_iterator(a)) 

if self.out_degree(lc) == 1: 

return lc 

gt_a = set(self.depth_first_search(a)) 

tmp = list(self.depth_first_search(lc, neighbors=lambda v: [v_ for v_ in self.neighbors_out(v) if v_ not in gt_a])) 

result = None 

for e in tmp: 

if all(x not in tmp for x in self.neighbors_out(e)): 

if result: 

return None 

result = e 

return result 

 

def atoms_of_congruence_lattice(self): 

r""" 

Return atoms of the congruence lattice. 

 

In other words, return "minimal non-trivial" congruences: 

A congruence is minimal if the only finer (as a partition 

of set of elements) congruence is the trivial congruence 

where every block contains only one element. 

 

.. SEEALSO:: :meth:`congruence` 

 

OUTPUT: 

 

List of congruences, every congruence as 

:class:`sage.combinat.set_partition.SetPartition` 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: N5 = HasseDiagram({0: [1, 2], 1: [4], 2: [3], 3:[4]}) 

sage: N5.atoms_of_congruence_lattice() 

[{{0}, {1}, {2, 3}, {4}}] 

sage: Hex = HasseDiagram({0: [1, 2], 1: [3], 2: [4], 3: [5], 4: [5]}) 

sage: Hex.atoms_of_congruence_lattice() 

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

 

ALGORITHM: 

 

Every atom is a join-irreducible. Every join-irreducible of 

`\mathrm{Con}(L)` is a principal congruence generated by a 

meet-irreducible element and the only element covering it (and also 

by a join-irreducible element and the only element covered by it). 

Hence we check those principal congruences to find the minimal ones. 

""" 

# Note: A lattice L if subdirectly reducible (i.e. is a sublattice 

# of a Cartesian product of two smaller lattices) iff Con(L) has 

# at least two atoms. That's were this is used for. 

 

from sage.combinat.set_partition import SetPartitions 

 

# Get smaller set, meet- or join-irreducibles 

join_irreducibles = [v for v in self if self.in_degree(v) == 1] 

meet_irreducibles = [v for v in self if self.out_degree(v) == 1] 

if len(join_irreducibles) < len(meet_irreducibles): 

irr = [(v, next(self.neighbor_in_iterator(v))) for v in join_irreducibles] 

else: 

irr = [(next(self.neighbor_out_iterator(v)), v) for v in meet_irreducibles] 

 

S = SetPartitions(range(self.order())) 

min_congruences = [] 

already_tried = [] 

 

while irr: 

next_pair = irr.pop() 

cong = self.congruence([next_pair], stop_pairs=already_tried) 

already_tried.append(next_pair) 

if cong is not None: 

cong = S(cong) 

min_congruences = [c for c in min_congruences if c != cong and not S.is_less_than(cong, c)] 

if not any(S.is_less_than(c, cong) for c in min_congruences): 

min_congruences.append(cong) 

 

return min_congruences 

 

def congruence(self, parts, start=None, stop_pairs=[]): 

""" 

Return the congruence ``start`` "extended" by ``parts``. 

 

``start`` is assumed to be a valid congruence of the lattice, 

and this is *not* checked. 

 

INPUT: 

 

- ``parts`` -- a list of lists; congruences to add 

- ``start`` -- a disjoint set; already computed congruence (or ``None``) 

- ``stop_pairs`` -- a list of pairs; list of pairs for stopping computation 

 

OUTPUT: 

 

``None``, if the congruence generated by ``start`` and ``parts`` 

together contains a block that has elements `a, b` so that ``(a, b)`` 

is in the list ``stop_pairs``. Otherwise the least congruence that 

contains a block whose subset is `p` for every `p` in ``parts`` or 

``start``, given as :class:`sage.sets.disjoint_set.DisjointSet_class`. 

 

ALGORITHM: 

 

Use the quadrilateral argument from page 120 of [Dav1997]_. 

 

Basically we take one block from todo-list, search quadrilateral 

blocks up and down against the block, and then complete them to 

closed intervals and add to todo-list. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1: [3], 2: [4], 3: [4]}) 

sage: cong = H.congruence([[0, 1]]); cong 

{{0, 1, 3}, {2, 4}} 

sage: H.congruence([[0, 2]], start=cong) 

{{0, 1, 2, 3, 4}} 

 

sage: H.congruence([[0, 1]], stop_pairs=[(1, 3)]) is None 

True 

 

TESTS:: 

 

sage: H = HasseDiagram('HT@O?GO?OE?G@??') 

sage: H.congruence([[0, 1]]).number_of_subsets() 

1 

sage: H = HasseDiagram('HW_oC?@@O@?O@??') 

sage: H.congruence([[0, 1]]).number_of_subsets() 

1 

 

Check :trac:`21861`:: 

 

sage: H = HasseDiagram({0: [1, 2], 1: [3], 2: [4], 3: [4]}) 

sage: tmp = H.congruence([[1, 3]]) 

sage: tmp.number_of_subsets() 

4 

sage: H.congruence([[0, 1]], start=tmp).number_of_subsets() 

2 

sage: tmp.number_of_subsets() 

4 

""" 

from sage.sets.disjoint_set import DisjointSet 

from copy import copy 

 

n = self.order() 

mt = self._meet 

jn = self._join 

 

def fill_to_interval(S): 

""" 

Return the smallest interval containing elements in the set S. 

""" 

m = n-1 

for e in S: 

m = mt[m, e] 

j = 0 

for e in S: 

j = jn[j, e] 

return self.interval(m, j) 

 

cong = copy(start) if start else DisjointSet(n) 

t = -1 

 

while t != cong.number_of_subsets(): 

for part in parts: 

if part: # Skip empty parts 

c = part[0] 

for e in fill_to_interval(part): 

cong.union(e, c) 

t = cong.number_of_subsets() 

 

# Following is needed for cases like 

# posets.BooleanLattice(3).congruence([(0,1), (0,2), (0,4)]) 

for c in list(cong): 

r = c[0] 

for v in fill_to_interval(c): 

cong.union(r, v) 

 

todo = set(cong.find(e) for part in parts for e in part) 

 

while todo: 

 

# First check if we should stop now. 

for a, b in stop_pairs: 

if cong.find(a) == cong.find(b): 

return None 

 

# We take one block and try to find as big interval 

# as possible to unify as a new block by the quadrilateral 

# argument. 

block = sorted(cong.root_to_elements_dict()[cong.find(todo.pop())]) 

 

b = block[-1] 

for a in block: # Quadrilateral up 

for c in self.neighbor_out_iterator(a): 

if c not in block: 

d = self._join[c, b] 

if cong.find(d) != cong.find(c): 

break 

else: 

continue 

break 

 

else: # Not found, so... 

a = block[0] 

for b in reversed(block): # ...quadrilateral down 

for d in self.neighbor_in_iterator(b): 

if d not in block: 

c = self._meet[d, a] 

if cong.find(c) != cong.find(d): 

break 

else: 

continue 

break 

else: # Nothing found 

continue 

 

# Something was found, so we put this block back to todo 

# together with just found new block. 

todo.add(a) 

todo.add(c) 

 

# Now the interval [c, d] will be of the same block. 

# It may "crab" other blocks within, and that can be 

# recursive process. In particular it may also combine to 

# [a, b] block we just used. 

while c is not None: 

newblock = cong.find(c) 

I = self.interval(c, d) 

for i in I: 

cong.union(newblock, i) 

C = cong.root_to_elements_dict()[cong.find(newblock)] 

mins = [i for i in C if all(i_ not in C for i_ in self.neighbor_in_iterator(i))] 

maxs = [i for i in C if all(i_ not in C for i_ in self.neighbor_out_iterator(i))] 

c = None # To stop loop, if this is not changed below. 

if len(mins) > 1 or len(maxs) > 1: 

c = n-1 

for m in mins: 

c = self._meet[c, m] 

d = 0 

for m in maxs: 

d = self._join[d, m] 

 

# This removes duplicates from todo. 

todo = set(cong.find(x) for x in todo) 

 

return cong 

 

def find_nontrivial_congruence(self): 

""" 

Return a pair that generates non-trivial congruence or 

``None`` if there is not any. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram({0: [1, 2], 1: [5], 2: [3, 4], 3: [5], 4: [5]}) 

sage: H.find_nontrivial_congruence() 

{{0, 1}, {2, 3, 4, 5}} 

 

sage: H = HasseDiagram({0: [1, 2, 3], 1: [4], 2: [4], 3: [4]}) 

sage: H.find_nontrivial_congruence() is None 

True 

 

ALGORITHM: 

 

See http://www.math.hawaii.edu/~ralph/Preprints/conlat.pdf: 

 

If `\Theta` is a join irreducible element of a `\mathrm{Con}(L)`, 

then there is at least one join-irreducible `j` and one 

meet-irreducible `m` such that `\Theta` is both the principal 

congruence generated by `(j^*, j)`, where `j^*` is the unique 

lower cover of `j`, and the principal congruence generated by 

`(m, m^*)`, where `m^*` is the unique upper cover of `m`. 

 

So, we only check join irreducibles or meet irreducibles, 

whichever is a smaller set. To optimize more we stop computation 

whenever it founds a pair that we know to generate one-element 

congruence. 

""" 

join_irreducibles = [v for v in self if self.in_degree(v) == 1] 

meet_irreducibles = [v for v in self if self.out_degree(v) == 1] 

if len(join_irreducibles) < len(meet_irreducibles): 

irr = [(v, self.neighbors_in(v)[0]) for v in join_irreducibles] 

else: 

irr = [(self.neighbors_out(v)[0], v) for v in meet_irreducibles] 

tried = [] 

for pair in irr: 

cong = self.congruence([pair], stop_pairs=tried) 

if cong is not None and cong.number_of_subsets() > 1: 

return cong 

tried.append(pair) 

return None 

 

def principal_congruences_poset(self): 

r""" 

Return the poset of join-irreducibles of the congruence lattice. 

 

OUTPUT: 

 

A pair `(P, D)` where `P` is a poset and `D` is a dictionary. 

 

Elements of `P` are pairs `(x, y)` such that `x` is an element 

of the lattice and `y` is an element covering it. In the poset 

`(a, b)` is less than `(c, d)` iff the principal congruence 

generated by `(a, b)` is refinement of the principal congruence 

generated by `(c, d)`. 

 

`D` is a dictionary from pairs `(x, y)` to the congruence 

(given as DisjointSet) generated by the pair. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: N5 = HasseDiagram({0: [1, 2], 1: [4], 2: [3], 3: [4]}) 

sage: P, D = N5.principal_congruences_poset() 

sage: P 

Finite poset containing 3 elements 

sage: P.bottom() 

(2, 3) 

sage: D[(2, 3)] 

{{0}, {1}, {2, 3}, {4}} 

""" 

from sage.combinat.set_partition import SetPartition, SetPartitions 

from sage.combinat.posets.posets import Poset 

 

n = self.order() 

 

# Select smaller set, meet- or join-irreducibles 

if self.in_degree_sequence().count(1) > self.out_degree_sequence().count(1): 

irr = [(e, next(self.neighbor_out_iterator(e))) for e in range(n) if self.out_degree(e) == 1] 

else: 

irr = [(next(self.neighbor_in_iterator(e)), e) for e in range(n) if self.in_degree(e) == 1] 

 

D = {} 

P = {} 

uniq_congs = set() 

for ab in irr: 

cong = self.congruence([ab]) 

cong_ = SetPartition(cong) 

if cong_ not in uniq_congs: 

uniq_congs.add(cong_) 

D[ab] = cong 

P[ab] = cong_ 

 

# Todo: Make a function that creates the poset from a set 

# by comparison function with minimal number of comparisons. 

 

T = SetPartitions(n) 

P = DiGraph([D, lambda a, b: T.is_less_than(P[a], P[b])]) 

return (Poset(P), D) 

 

def congruences_iterator(self): 

""" 

Return an iterator over all congruences of the lattice. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram('GY@OQ?OW@?O?') 

sage: it = H.congruences_iterator(); it 

<generator object ...> 

sage: sorted([cong.number_of_subsets() for cong in it]) 

[1, 2, 2, 2, 4, 4, 4, 8] 

""" 

from sage.sets.disjoint_set import DisjointSet 

 

P, congs = self.principal_congruences_poset() 

for a in P.antichains_iterator(): 

achain = tuple(a) 

n = len(achain) 

if n == 0: 

yield DisjointSet(self.order()) 

if n == 1: 

# We have congs[(x,y)], but we want congs[((x,y))]. 

congs[achain] = congs[a[0]] 

yield congs[achain[0]] 

if n > 1: 

c = congs[achain[:-1]] 

c = self.congruence([achain[-1]], start=c) 

yield c 

congs[achain] = c 

 

def is_congruence_normal(self): 

""" 

Return ``True`` if the lattice can be constructed from the one-element 

lattice with Day doubling constructions of convex subsets. 

 

Subsets to double does not need to be lower nor upper pseudo-intervals. 

On the other hand they must be convex, i.e. doubling a non-convex but 

municipal subset will give a lattice that returns ``False`` from 

this function. 

 

EXAMPLES:: 

 

sage: from sage.combinat.posets.hasse_diagram import HasseDiagram 

sage: H = HasseDiagram('IX?Q@?AG?OG?W?O@??') 

sage: H.is_congruence_normal() 

True 

 

The 5-element diamond is the smallest non-example:: 

 

sage: H = HasseDiagram({0: [1, 2, 3], 1: [4], 2: [4], 3: [4]}) 

sage: H.is_congruence_normal() 

False 

 

This is done by doubling a non-convex subset:: 

 

sage: H = HasseDiagram('OQC?a?@CO?G_C@?GA?O??_??@?BO?A_?G??C??_?@???') 

sage: H.is_congruence_normal() 

False 

 

TESTS:: 

 

sage: HasseDiagram().is_congruence_normal() 

True 

sage: HasseDiagram({0: []}).is_congruence_normal() 

True 

 

ALGORITHM: 

 

See http://www.math.hawaii.edu/~jb/inflation.pdf 

""" 

from sage.combinat.set_partition import SetPartition 

 

n = self.order() 

congs_ji = {} 

 

for ji in range(n): 

if self.in_degree(ji) == 1: 

cong = SetPartition(self.congruence([[ji, next(self.neighbor_in_iterator(ji))]])) 

if cong not in congs_ji: 

congs_ji[cong] = [] 

congs_ji[cong].append(ji) 

 

for mi in range(n): 

if self.out_degree(mi) == 1: 

cong = SetPartition(self.congruence([[mi, next(self.neighbor_out_iterator(mi))]])) 

if any(self.is_lequal(ji, mi) for ji in congs_ji[cong]): 

return False 

 

return True 

 

from sage.misc.rest_index_of_methods import gen_rest_table_index 

__doc__ = __doc__.format(INDEX_OF_FUNCTIONS=gen_rest_table_index(HasseDiagram))