Location: LEMON/LEMON-main/lemon/graph_utils.h

Load file history
gravatar
deba@inf.elte.hu
SmartGraph addEdge bug fix (ticket #88)
   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
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
/* -*- C++ -*-
*
* This file is a part of LEMON, a generic C++ optimization library
*
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef LEMON_GRAPH_UTILS_H
#define LEMON_GRAPH_UTILS_H
#include <iterator>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <lemon/bits/invalid.h>
#include <lemon/bits/utility.h>
#include <lemon/maps.h>
#include <lemon/bits/traits.h>
#include <lemon/bits/alteration_notifier.h>
#include <lemon/bits/default_map.h>
///\ingroup gutils
///\file
///\brief Digraph utilities.
namespace lemon {
/// \addtogroup gutils
/// @{
///Creates convenience typedefs for the digraph types and iterators
///This \c \#define creates convenience typedefs for the following types
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
///\c OutArcIt
///\note If \c G it a template parameter, it should be used in this way.
///\code
/// GRAPH_TYPEDEFS(typename G);
///\endcode
///
///\warning There are no typedefs for the digraph maps because of the lack of
///template typedefs in C++.
#define GRAPH_TYPEDEFS(Digraph) \
typedef Digraph:: Node Node; \
typedef Digraph:: NodeIt NodeIt; \
typedef Digraph:: Arc Arc; \
typedef Digraph:: ArcIt ArcIt; \
typedef Digraph:: InArcIt InArcIt; \
typedef Digraph::OutArcIt OutArcIt
///Creates convenience typedefs for the graph types and iterators
///This \c \#define creates the same convenience typedefs as defined by
///\ref GRAPH_TYPEDEFS(Digraph) and three more, namely it creates
///\c Edge, \c EdgeIt, \c IncArcIt,
///
///\note If \c G it a template parameter, it should be used in this way.
///\code
/// UGRAPH_TYPEDEFS(typename G);
///\endcode
///
///\warning There are no typedefs for the digraph maps because of the lack of
///template typedefs in C++.
#define UGRAPH_TYPEDEFS(Digraph) \
GRAPH_TYPEDEFS(Digraph); \
typedef Digraph:: Edge Edge; \
typedef Digraph:: EdgeIt EdgeIt; \
typedef Digraph:: IncArcIt IncArcIt
///\brief Creates convenience typedefs for the bipartite digraph
///types and iterators
///This \c \#define creates the same convenience typedefs as defined by
///\ref UGRAPH_TYPEDEFS(Digraph) and two more, namely it creates
///\c RedIt, \c BlueIt,
///
///\note If \c G it a template parameter, it should be used in this way.
///\code
/// BPUGRAPH_TYPEDEFS(typename G);
///\endcode
///
///\warning There are no typedefs for the digraph maps because of the lack of
///template typedefs in C++.
#define BPUGRAPH_TYPEDEFS(Digraph) \
UGRAPH_TYPEDEFS(Digraph); \
typedef Digraph::Red Red; \
typedef Digraph::Blue Blue; \
typedef Digraph::RedIt RedIt; \
typedef Digraph::BlueIt BlueIt
/// \brief Function to count the items in the digraph.
///
/// This function counts the items (nodes, arcs etc) in the digraph.
/// The complexity of the function is O(n) because
/// it iterates on all of the items.
template <typename Digraph, typename Item>
inline int countItems(const Digraph& g) {
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
int num = 0;
for (ItemIt it(g); it != INVALID; ++it) {
++num;
}
return num;
}
// Node counting:
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct CountNodesSelector {
static int count(const Digraph &g) {
return countItems<Digraph, typename Digraph::Node>(g);
}
};
template <typename Digraph>
struct CountNodesSelector<
Digraph, typename
enable_if<typename Digraph::NodeNumTag, void>::type>
{
static int count(const Digraph &g) {
return g.nodeNum();
}
};
}
/// \brief Function to count the nodes in the digraph.
///
/// This function counts the nodes in the digraph.
/// The complexity of the function is O(n) but for some
/// digraph structures it is specialized to run in O(1).
///
/// If the digraph contains a \e nodeNum() member function and a
/// \e NodeNumTag tag then this function calls directly the member
/// function to query the cardinality of the node set.
template <typename Digraph>
inline int countNodes(const Digraph& g) {
return _digraph_utils_bits::CountNodesSelector<Digraph>::count(g);
}
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct CountRedsSelector {
static int count(const Digraph &g) {
return countItems<Digraph, typename Digraph::Red>(g);
}
};
template <typename Digraph>
struct CountRedsSelector<
Digraph, typename
enable_if<typename Digraph::NodeNumTag, void>::type>
{
static int count(const Digraph &g) {
return g.redNum();
}
};
}
/// \brief Function to count the reds in the digraph.
///
/// This function counts the reds in the digraph.
/// The complexity of the function is O(an) but for some
/// digraph structures it is specialized to run in O(1).
///
/// If the digraph contains an \e redNum() member function and a
/// \e NodeNumTag tag then this function calls directly the member
/// function to query the cardinality of the A-node set.
template <typename Digraph>
inline int countReds(const Digraph& g) {
return _digraph_utils_bits::CountRedsSelector<Digraph>::count(g);
}
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct CountBluesSelector {
static int count(const Digraph &g) {
return countItems<Digraph, typename Digraph::Blue>(g);
}
};
template <typename Digraph>
struct CountBluesSelector<
Digraph, typename
enable_if<typename Digraph::NodeNumTag, void>::type>
{
static int count(const Digraph &g) {
return g.blueNum();
}
};
}
/// \brief Function to count the blues in the digraph.
///
/// This function counts the blues in the digraph.
/// The complexity of the function is O(bn) but for some
/// digraph structures it is specialized to run in O(1).
///
/// If the digraph contains a \e blueNum() member function and a
/// \e NodeNumTag tag then this function calls directly the member
/// function to query the cardinality of the B-node set.
template <typename Digraph>
inline int countBlues(const Digraph& g) {
return _digraph_utils_bits::CountBluesSelector<Digraph>::count(g);
}
// Arc counting:
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct CountArcsSelector {
static int count(const Digraph &g) {
return countItems<Digraph, typename Digraph::Arc>(g);
}
};
template <typename Digraph>
struct CountArcsSelector<
Digraph,
typename enable_if<typename Digraph::ArcNumTag, void>::type>
{
static int count(const Digraph &g) {
return g.arcNum();
}
};
}
/// \brief Function to count the arcs in the digraph.
///
/// This function counts the arcs in the digraph.
/// The complexity of the function is O(e) but for some
/// digraph structures it is specialized to run in O(1).
///
/// If the digraph contains a \e arcNum() member function and a
/// \e ArcNumTag tag then this function calls directly the member
/// function to query the cardinality of the arc set.
template <typename Digraph>
inline int countArcs(const Digraph& g) {
return _digraph_utils_bits::CountArcsSelector<Digraph>::count(g);
}
// Undirected arc counting:
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct CountEdgesSelector {
static int count(const Digraph &g) {
return countItems<Digraph, typename Digraph::Edge>(g);
}
};
template <typename Digraph>
struct CountEdgesSelector<
Digraph,
typename enable_if<typename Digraph::ArcNumTag, void>::type>
{
static int count(const Digraph &g) {
return g.edgeNum();
}
};
}
/// \brief Function to count the edges in the digraph.
///
/// This function counts the edges in the digraph.
/// The complexity of the function is O(e) but for some
/// digraph structures it is specialized to run in O(1).
///
/// If the digraph contains a \e edgeNum() member function and a
/// \e ArcNumTag tag then this function calls directly the member
/// function to query the cardinality of the edge set.
template <typename Digraph>
inline int countEdges(const Digraph& g) {
return _digraph_utils_bits::CountEdgesSelector<Digraph>::count(g);
}
template <typename Digraph, typename DegIt>
inline int countNodeDegree(const Digraph& _g, const typename Digraph::Node& _n) {
int num = 0;
for (DegIt it(_g, _n); it != INVALID; ++it) {
++num;
}
return num;
}
/// \brief Function to count the number of the out-arcs from node \c n.
///
/// This function counts the number of the out-arcs from node \c n
/// in the digraph.
template <typename Digraph>
inline int countOutArcs(const Digraph& _g, const typename Digraph::Node& _n) {
return countNodeDegree<Digraph, typename Digraph::OutArcIt>(_g, _n);
}
/// \brief Function to count the number of the in-arcs to node \c n.
///
/// This function counts the number of the in-arcs to node \c n
/// in the digraph.
template <typename Digraph>
inline int countInArcs(const Digraph& _g, const typename Digraph::Node& _n) {
return countNodeDegree<Digraph, typename Digraph::InArcIt>(_g, _n);
}
/// \brief Function to count the number of the inc-arcs to node \c n.
///
/// This function counts the number of the inc-arcs to node \c n
/// in the digraph.
template <typename Digraph>
inline int countIncArcs(const Digraph& _g, const typename Digraph::Node& _n) {
return countNodeDegree<Digraph, typename Digraph::IncArcIt>(_g, _n);
}
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct FindArcSelector {
typedef typename Digraph::Node Node;
typedef typename Digraph::Arc Arc;
static Arc find(const Digraph &g, Node u, Node v, Arc e) {
if (e == INVALID) {
g.firstOut(e, u);
} else {
g.nextOut(e);
}
while (e != INVALID && g.target(e) != v) {
g.nextOut(e);
}
return e;
}
};
template <typename Digraph>
struct FindArcSelector<
Digraph,
typename enable_if<typename Digraph::FindArcTag, void>::type>
{
typedef typename Digraph::Node Node;
typedef typename Digraph::Arc Arc;
static Arc find(const Digraph &g, Node u, Node v, Arc prev) {
return g.findArc(u, v, prev);
}
};
}
/// \brief Finds an arc between two nodes of a digraph.
///
/// Finds an arc from node \c u to node \c v in digraph \c g.
///
/// If \c prev is \ref INVALID (this is the default value), then
/// it finds the first arc from \c u to \c v. Otherwise it looks for
/// the next arc from \c u to \c v after \c prev.
/// \return The found arc or \ref INVALID if there is no such an arc.
///
/// Thus you can iterate through each arc from \c u to \c v as it follows.
///\code
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
/// ...
/// }
///\endcode
///
///\sa ArcLookUp
///\sa AllArcLookUp
///\sa DynArcLookUp
///\sa ConArcIt
template <typename Digraph>
inline typename Digraph::Arc
findArc(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
typename Digraph::Arc prev = INVALID) {
return _digraph_utils_bits::FindArcSelector<Digraph>::find(g, u, v, prev);
}
/// \brief Iterator for iterating on arcs connected the same nodes.
///
/// Iterator for iterating on arcs connected the same nodes. It is
/// higher level interface for the findArc() function. You can
/// use it the following way:
///\code
/// for (ConArcIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
/// ...
/// }
///\endcode
///
///\sa findArc()
///\sa ArcLookUp
///\sa AllArcLookUp
///\sa DynArcLookUp
///
/// \author Balazs Dezso
template <typename _Digraph>
class ConArcIt : public _Digraph::Arc {
public:
typedef _Digraph Digraph;
typedef typename Digraph::Arc Parent;
typedef typename Digraph::Arc Arc;
typedef typename Digraph::Node Node;
/// \brief Constructor.
///
/// Construct a new ConArcIt iterating on the arcs which
/// connects the \c u and \c v node.
ConArcIt(const Digraph& g, Node u, Node v) : digraph(g) {
Parent::operator=(findArc(digraph, u, v));
}
/// \brief Constructor.
///
/// Construct a new ConArcIt which continues the iterating from
/// the \c e arc.
ConArcIt(const Digraph& g, Arc e) : Parent(e), digraph(g) {}
/// \brief Increment operator.
///
/// It increments the iterator and gives back the next arc.
ConArcIt& operator++() {
Parent::operator=(findArc(digraph, digraph.source(*this),
digraph.target(*this), *this));
return *this;
}
private:
const Digraph& digraph;
};
namespace _digraph_utils_bits {
template <typename Digraph, typename Enable = void>
struct FindEdgeSelector {
typedef typename Digraph::Node Node;
typedef typename Digraph::Edge Edge;
static Edge find(const Digraph &g, Node u, Node v, Edge e) {
bool b;
if (u != v) {
if (e == INVALID) {
g.firstInc(e, b, u);
} else {
b = g.source(e) == u;
g.nextInc(e, b);
}
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
g.nextInc(e, b);
}
} else {
if (e == INVALID) {
g.firstInc(e, b, u);
} else {
b = true;
g.nextInc(e, b);
}
while (e != INVALID && (!b || g.target(e) != v)) {
g.nextInc(e, b);
}
}
return e;
}
};
template <typename Digraph>
struct FindEdgeSelector<
Digraph,
typename enable_if<typename Digraph::FindArcTag, void>::type>
{
typedef typename Digraph::Node Node;
typedef typename Digraph::Edge Edge;
static Edge find(const Digraph &g, Node u, Node v, Edge prev) {
return g.findEdge(u, v, prev);
}
};
}
/// \brief Finds an edge between two nodes of a digraph.
///
/// Finds an edge from node \c u to node \c v in digraph \c g.
/// If the node \c u and node \c v is equal then each loop arc
/// will be enumerated.
///
/// If \c prev is \ref INVALID (this is the default value), then
/// it finds the first arc from \c u to \c v. Otherwise it looks for
/// the next arc from \c u to \c v after \c prev.
/// \return The found arc or \ref INVALID if there is no such an arc.
///
/// Thus you can iterate through each arc from \c u to \c v as it follows.
///\code
/// for(Edge e = findEdge(g,u,v); e != INVALID;
/// e = findEdge(g,u,v,e)) {
/// ...
/// }
///\endcode
///
///\sa ConArcIt
template <typename Digraph>
inline typename Digraph::Edge
findEdge(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
typename Digraph::Edge p = INVALID) {
return _digraph_utils_bits::FindEdgeSelector<Digraph>::find(g, u, v, p);
}
/// \brief Iterator for iterating on edges connected the same nodes.
///
/// Iterator for iterating on edges connected the same nodes. It is
/// higher level interface for the findEdge() function. You can
/// use it the following way:
///\code
/// for (ConEdgeIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
/// ...
/// }
///\endcode
///
///\sa findEdge()
///
/// \author Balazs Dezso
template <typename _Digraph>
class ConEdgeIt : public _Digraph::Edge {
public:
typedef _Digraph Digraph;
typedef typename Digraph::Edge Parent;
typedef typename Digraph::Edge Edge;
typedef typename Digraph::Node Node;
/// \brief Constructor.
///
/// Construct a new ConEdgeIt iterating on the arcs which
/// connects the \c u and \c v node.
ConEdgeIt(const Digraph& g, Node u, Node v) : digraph(g) {
Parent::operator=(findEdge(digraph, u, v));
}
/// \brief Constructor.
///
/// Construct a new ConEdgeIt which continues the iterating from
/// the \c e arc.
ConEdgeIt(const Digraph& g, Edge e) : Parent(e), digraph(g) {}
/// \brief Increment operator.
///
/// It increments the iterator and gives back the next arc.
ConEdgeIt& operator++() {
Parent::operator=(findEdge(digraph, digraph.source(*this),
digraph.target(*this), *this));
return *this;
}
private:
const Digraph& digraph;
};
/// \brief Copy a map.
///
/// This function copies the \c from map to the \c to map. It uses the
/// given iterator to iterate on the data structure and it uses the \c ref
/// mapping to convert the from's keys to the to's keys.
template <typename To, typename From,
typename ItemIt, typename Ref>
void copyMap(To& to, const From& from,
ItemIt it, const Ref& ref) {
for (; it != INVALID; ++it) {
to[ref[it]] = from[it];
}
}
/// \brief Copy the from map to the to map.
///
/// Copy the \c from map to the \c to map. It uses the given iterator
/// to iterate on the data structure.
template <typename To, typename From, typename ItemIt>
void copyMap(To& to, const From& from, ItemIt it) {
for (; it != INVALID; ++it) {
to[it] = from[it];
}
}
namespace _digraph_utils_bits {
template <typename Digraph, typename Item, typename RefMap>
class MapCopyBase {
public:
virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
virtual ~MapCopyBase() {}
};
template <typename Digraph, typename Item, typename RefMap,
typename ToMap, typename FromMap>
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
public:
MapCopy(ToMap& tmap, const FromMap& map)
: _tmap(tmap), _map(map) {}
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
for (ItemIt it(digraph); it != INVALID; ++it) {
_tmap.set(refMap[it], _map[it]);
}
}
private:
ToMap& _tmap;
const FromMap& _map;
};
template <typename Digraph, typename Item, typename RefMap, typename It>
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
public:
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
virtual void copy(const Digraph&, const RefMap& refMap) {
_it = refMap[_item];
}
private:
It& _it;
Item _item;
};
template <typename Digraph, typename Item, typename RefMap, typename Ref>
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
public:
RefCopy(Ref& map) : _map(map) {}
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
for (ItemIt it(digraph); it != INVALID; ++it) {
_map.set(it, refMap[it]);
}
}
private:
Ref& _map;
};
template <typename Digraph, typename Item, typename RefMap,
typename CrossRef>
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
public:
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
for (ItemIt it(digraph); it != INVALID; ++it) {
_cmap.set(refMap[it], it);
}
}
private:
CrossRef& _cmap;
};
template <typename Digraph, typename Enable = void>
struct DigraphCopySelector {
template <typename From, typename NodeRefMap, typename ArcRefMap>
static void copy(Digraph &to, const From& from,
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
for (typename From::NodeIt it(from); it != INVALID; ++it) {
nodeRefMap[it] = to.addNode();
}
for (typename From::ArcIt it(from); it != INVALID; ++it) {
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
nodeRefMap[from.target(it)]);
}
}
};
template <typename Digraph>
struct DigraphCopySelector<
Digraph,
typename enable_if<typename Digraph::BuildTag, void>::type>
{
template <typename From, typename NodeRefMap, typename ArcRefMap>
static void copy(Digraph &to, const From& from,
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
to.build(from, nodeRefMap, arcRefMap);
}
};
template <typename Graph, typename Enable = void>
struct GraphCopySelector {
template <typename From, typename NodeRefMap, typename EdgeRefMap>
static void copy(Graph &to, const From& from,
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
for (typename From::NodeIt it(from); it != INVALID; ++it) {
nodeRefMap[it] = to.addNode();
}
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
nodeRefMap[from.target(it)]);
}
}
};
template <typename Graph>
struct GraphCopySelector<
Graph,
typename enable_if<typename Graph::BuildTag, void>::type>
{
template <typename From, typename NodeRefMap, typename EdgeRefMap>
static void copy(Graph &to, const From& from,
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
to.build(from, nodeRefMap, edgeRefMap);
}
};
template <typename BpGraph, typename Enable = void>
struct BpGraphCopySelector {
template <typename From, typename RedRefMap,
typename BlueRefMap, typename EdgeRefMap>
static void copy(BpGraph &to, const From& from,
RedRefMap& redRefMap, BlueRefMap& blueRefMap,
EdgeRefMap& edgeRefMap) {
for (typename From::RedIt it(from); it != INVALID; ++it) {
redRefMap[it] = to.addRed();
}
for (typename From::BlueIt it(from); it != INVALID; ++it) {
blueRefMap[it] = to.addBlue();
}
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
edgeRefMap[it] = to.addArc(redRefMap[from.red(it)],
blueRefMap[from.blue(it)]);
}
}
};
template <typename BpGraph>
struct BpGraphCopySelector<
BpGraph,
typename enable_if<typename BpGraph::BuildTag, void>::type>
{
template <typename From, typename RedRefMap,
typename BlueRefMap, typename EdgeRefMap>
static void copy(BpGraph &to, const From& from,
RedRefMap& redRefMap, BlueRefMap& blueRefMap,
EdgeRefMap& edgeRefMap) {
to.build(from, redRefMap, blueRefMap, edgeRefMap);
}
};
}
/// \brief Class to copy a digraph.
///
/// Class to copy a digraph to another digraph (duplicate a digraph). The
/// simplest way of using it is through the \c copyDigraph() function.
template <typename To, typename From>
class DigraphCopy {
private:
typedef typename From::Node Node;
typedef typename From::NodeIt NodeIt;
typedef typename From::Arc Arc;
typedef typename From::ArcIt ArcIt;
typedef typename To::Node TNode;
typedef typename To::Arc TArc;
typedef typename From::template NodeMap<TNode> NodeRefMap;
typedef typename From::template ArcMap<TArc> ArcRefMap;
public:
/// \brief Constructor for the DigraphCopy.
///
/// It copies the content of the \c _from digraph into the
/// \c _to digraph.
DigraphCopy(To& _to, const From& _from)
: from(_from), to(_to) {}
/// \brief Destructor of the DigraphCopy
///
/// Destructor of the DigraphCopy
~DigraphCopy() {
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
delete nodeMapCopies[i];
}
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
delete arcMapCopies[i];
}
}
/// \brief Copies the node references into the given map.
///
/// Copies the node references into the given map.
template <typename NodeRef>
DigraphCopy& nodeRef(NodeRef& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
NodeRefMap, NodeRef>(map));
return *this;
}
/// \brief Copies the node cross references into the given map.
///
/// Copies the node cross references (reverse references) into
/// the given map.
template <typename NodeCrossRef>
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
NodeRefMap, NodeCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's node type,
/// and the copied map's key type is the from digraph's node
/// type.
template <typename ToMap, typename FromMap>
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
NodeRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given node.
///
/// Make a copy of the given node.
DigraphCopy& node(TNode& tnode, const Node& snode) {
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
NodeRefMap, TNode>(tnode, snode));
return *this;
}
/// \brief Copies the arc references into the given map.
///
/// Copies the arc references into the given map.
template <typename ArcRef>
DigraphCopy& arcRef(ArcRef& map) {
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
ArcRefMap, ArcRef>(map));
return *this;
}
/// \brief Copies the arc cross references into the given map.
///
/// Copies the arc cross references (reverse references) into
/// the given map.
template <typename ArcCrossRef>
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
ArcRefMap, ArcCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's arc type,
/// and the copied map's key type is the from digraph's arc
/// type.
template <typename ToMap, typename FromMap>
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
ArcRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given arc.
///
/// Make a copy of the given arc.
DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
ArcRefMap, TArc>(tarc, sarc));
return *this;
}
/// \brief Executes the copies.
///
/// Executes the copies.
void run() {
NodeRefMap nodeRefMap(from);
ArcRefMap arcRefMap(from);
_digraph_utils_bits::DigraphCopySelector<To>::
copy(to, from, nodeRefMap, arcRefMap);
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
nodeMapCopies[i]->copy(from, nodeRefMap);
}
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
arcMapCopies[i]->copy(from, arcRefMap);
}
}
protected:
const From& from;
To& to;
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
nodeMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
arcMapCopies;
};
/// \brief Copy a digraph to another digraph.
///
/// Copy a digraph to another digraph.
/// The usage of the function:
///
///\code
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
///\endcode
///
/// After the copy the \c nr map will contain the mapping from the
/// nodes of the \c from digraph to the nodes of the \c to digraph and
/// \c ecr will contain the mapping from the arcs of the \c to digraph
/// to the arcs of the \c from digraph.
///
/// \see DigraphCopy
template <typename To, typename From>
DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
return DigraphCopy<To, From>(to, from);
}
/// \brief Class to copy an graph.
///
/// Class to copy an graph to another digraph (duplicate a digraph).
/// The simplest way of using it is through the \c copyGraph() function.
template <typename To, typename From>
class GraphCopy {
private:
typedef typename From::Node Node;
typedef typename From::NodeIt NodeIt;
typedef typename From::Arc Arc;
typedef typename From::ArcIt ArcIt;
typedef typename From::Edge Edge;
typedef typename From::EdgeIt EdgeIt;
typedef typename To::Node TNode;
typedef typename To::Arc TArc;
typedef typename To::Edge TEdge;
typedef typename From::template NodeMap<TNode> NodeRefMap;
typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
struct ArcRefMap {
ArcRefMap(const To& _to, const From& _from,
const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref)
: to(_to), from(_from),
edge_ref(_edge_ref), node_ref(_node_ref) {}
typedef typename From::Arc Key;
typedef typename To::Arc Value;
Value operator[](const Key& key) const {
bool forward =
(from.direction(key) ==
(node_ref[from.source(static_cast<const Edge&>(key))] ==
to.source(edge_ref[static_cast<const Edge&>(key)])));
return to.direct(edge_ref[key], forward);
}
const To& to;
const From& from;
const EdgeRefMap& edge_ref;
const NodeRefMap& node_ref;
};
public:
/// \brief Constructor for the DigraphCopy.
///
/// It copies the content of the \c _from digraph into the
/// \c _to digraph.
GraphCopy(To& _to, const From& _from)
: from(_from), to(_to) {}
/// \brief Destructor of the DigraphCopy
///
/// Destructor of the DigraphCopy
~GraphCopy() {
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
delete nodeMapCopies[i];
}
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
delete arcMapCopies[i];
}
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
delete edgeMapCopies[i];
}
}
/// \brief Copies the node references into the given map.
///
/// Copies the node references into the given map.
template <typename NodeRef>
GraphCopy& nodeRef(NodeRef& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
NodeRefMap, NodeRef>(map));
return *this;
}
/// \brief Copies the node cross references into the given map.
///
/// Copies the node cross references (reverse references) into
/// the given map.
template <typename NodeCrossRef>
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
NodeRefMap, NodeCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's node type,
/// and the copied map's key type is the from digraph's node
/// type.
template <typename ToMap, typename FromMap>
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
NodeRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given node.
///
/// Make a copy of the given node.
GraphCopy& node(TNode& tnode, const Node& snode) {
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
NodeRefMap, TNode>(tnode, snode));
return *this;
}
/// \brief Copies the arc references into the given map.
///
/// Copies the arc references into the given map.
template <typename ArcRef>
GraphCopy& arcRef(ArcRef& map) {
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
ArcRefMap, ArcRef>(map));
return *this;
}
/// \brief Copies the arc cross references into the given map.
///
/// Copies the arc cross references (reverse references) into
/// the given map.
template <typename ArcCrossRef>
GraphCopy& arcCrossRef(ArcCrossRef& map) {
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
ArcRefMap, ArcCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's arc type,
/// and the copied map's key type is the from digraph's arc
/// type.
template <typename ToMap, typename FromMap>
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
ArcRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given arc.
///
/// Make a copy of the given arc.
GraphCopy& arc(TArc& tarc, const Arc& sarc) {
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
ArcRefMap, TArc>(tarc, sarc));
return *this;
}
/// \brief Copies the edge references into the given map.
///
/// Copies the edge references into the given map.
template <typename EdgeRef>
GraphCopy& edgeRef(EdgeRef& map) {
edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge,
EdgeRefMap, EdgeRef>(map));
return *this;
}
/// \brief Copies the edge cross references into the given map.
///
/// Copies the edge cross references (reverse
/// references) into the given map.
template <typename EdgeCrossRef>
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
Edge, EdgeRefMap, EdgeCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's edge type,
/// and the copied map's key type is the from digraph's edge
/// type.
template <typename ToMap, typename FromMap>
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge,
EdgeRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given edge.
///
/// Make a copy of the given edge.
GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge,
EdgeRefMap, TEdge>(tedge, sedge));
return *this;
}
/// \brief Executes the copies.
///
/// Executes the copies.
void run() {
NodeRefMap nodeRefMap(from);
EdgeRefMap edgeRefMap(from);
ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
_digraph_utils_bits::GraphCopySelector<To>::
copy(to, from, nodeRefMap, edgeRefMap);
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
nodeMapCopies[i]->copy(from, nodeRefMap);
}
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
edgeMapCopies[i]->copy(from, edgeRefMap);
}
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
arcMapCopies[i]->copy(from, arcRefMap);
}
}
private:
const From& from;
To& to;
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
nodeMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
arcMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
edgeMapCopies;
};
/// \brief Copy an graph to another digraph.
///
/// Copy an graph to another digraph.
/// The usage of the function:
///
///\code
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
///\endcode
///
/// After the copy the \c nr map will contain the mapping from the
/// nodes of the \c from digraph to the nodes of the \c to digraph and
/// \c ecr will contain the mapping from the arcs of the \c to digraph
/// to the arcs of the \c from digraph.
///
/// \see GraphCopy
template <typename To, typename From>
GraphCopy<To, From>
copyGraph(To& to, const From& from) {
return GraphCopy<To, From>(to, from);
}
/// \brief Class to copy a bipartite digraph.
///
/// Class to copy a bipartite digraph to another digraph
/// (duplicate a digraph). The simplest way of using it is through
/// the \c copyBpGraph() function.
template <typename To, typename From>
class BpGraphCopy {
private:
typedef typename From::Node Node;
typedef typename From::Red Red;
typedef typename From::Blue Blue;
typedef typename From::NodeIt NodeIt;
typedef typename From::Arc Arc;
typedef typename From::ArcIt ArcIt;
typedef typename From::Edge Edge;
typedef typename From::EdgeIt EdgeIt;
typedef typename To::Node TNode;
typedef typename To::Arc TArc;
typedef typename To::Edge TEdge;
typedef typename From::template RedMap<TNode> RedRefMap;
typedef typename From::template BlueMap<TNode> BlueRefMap;
typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
struct NodeRefMap {
NodeRefMap(const From& _from, const RedRefMap& _red_ref,
const BlueRefMap& _blue_ref)
: from(_from), red_ref(_red_ref), blue_ref(_blue_ref) {}
typedef typename From::Node Key;
typedef typename To::Node Value;
Value operator[](const Key& key) const {
return from.red(key) ? red_ref[key] : blue_ref[key];
}
const From& from;
const RedRefMap& red_ref;
const BlueRefMap& blue_ref;
};
struct ArcRefMap {
ArcRefMap(const To& _to, const From& _from,
const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref)
: to(_to), from(_from),
edge_ref(_edge_ref), node_ref(_node_ref) {}
typedef typename From::Arc Key;
typedef typename To::Arc Value;
Value operator[](const Key& key) const {
bool forward =
(from.direction(key) ==
(node_ref[from.source(static_cast<const Edge&>(key))] ==
to.source(edge_ref[static_cast<const Edge&>(key)])));
return to.direct(edge_ref[key], forward);
}
const To& to;
const From& from;
const EdgeRefMap& edge_ref;
const NodeRefMap& node_ref;
};
public:
/// \brief Constructor for the DigraphCopy.
///
/// It copies the content of the \c _from digraph into the
/// \c _to digraph.
BpGraphCopy(To& _to, const From& _from)
: from(_from), to(_to) {}
/// \brief Destructor of the DigraphCopy
///
/// Destructor of the DigraphCopy
~BpGraphCopy() {
for (int i = 0; i < int(redMapCopies.size()); ++i) {
delete redMapCopies[i];
}
for (int i = 0; i < int(blueMapCopies.size()); ++i) {
delete blueMapCopies[i];
}
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
delete nodeMapCopies[i];
}
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
delete arcMapCopies[i];
}
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
delete edgeMapCopies[i];
}
}
/// \brief Copies the A-node references into the given map.
///
/// Copies the A-node references into the given map.
template <typename RedRef>
BpGraphCopy& redRef(RedRef& map) {
redMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Red,
RedRefMap, RedRef>(map));
return *this;
}
/// \brief Copies the A-node cross references into the given map.
///
/// Copies the A-node cross references (reverse references) into
/// the given map.
template <typename RedCrossRef>
BpGraphCopy& redCrossRef(RedCrossRef& map) {
redMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
Red, RedRefMap, RedCrossRef>(map));
return *this;
}
/// \brief Make copy of the given A-node map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's node type,
/// and the copied map's key type is the from digraph's node
/// type.
template <typename ToMap, typename FromMap>
BpGraphCopy& redMap(ToMap& tmap, const FromMap& map) {
redMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Red,
RedRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Copies the B-node references into the given map.
///
/// Copies the B-node references into the given map.
template <typename BlueRef>
BpGraphCopy& blueRef(BlueRef& map) {
blueMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Blue,
BlueRefMap, BlueRef>(map));
return *this;
}
/// \brief Copies the B-node cross references into the given map.
///
/// Copies the B-node cross references (reverse references) into
/// the given map.
template <typename BlueCrossRef>
BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
blueMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
Blue, BlueRefMap, BlueCrossRef>(map));
return *this;
}
/// \brief Make copy of the given B-node map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's node type,
/// and the copied map's key type is the from digraph's node
/// type.
template <typename ToMap, typename FromMap>
BpGraphCopy& blueMap(ToMap& tmap, const FromMap& map) {
blueMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Blue,
BlueRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Copies the node references into the given map.
///
/// Copies the node references into the given map.
template <typename NodeRef>
BpGraphCopy& nodeRef(NodeRef& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
NodeRefMap, NodeRef>(map));
return *this;
}
/// \brief Copies the node cross references into the given map.
///
/// Copies the node cross references (reverse references) into
/// the given map.
template <typename NodeCrossRef>
BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
NodeRefMap, NodeCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's node type,
/// and the copied map's key type is the from digraph's node
/// type.
template <typename ToMap, typename FromMap>
BpGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
NodeRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given node.
///
/// Make a copy of the given node.
BpGraphCopy& node(TNode& tnode, const Node& snode) {
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
NodeRefMap, TNode>(tnode, snode));
return *this;
}
/// \brief Copies the arc references into the given map.
///
/// Copies the arc references into the given map.
template <typename ArcRef>
BpGraphCopy& arcRef(ArcRef& map) {
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
ArcRefMap, ArcRef>(map));
return *this;
}
/// \brief Copies the arc cross references into the given map.
///
/// Copies the arc cross references (reverse references) into
/// the given map.
template <typename ArcCrossRef>
BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
ArcRefMap, ArcCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's arc type,
/// and the copied map's key type is the from digraph's arc
/// type.
template <typename ToMap, typename FromMap>
BpGraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
ArcRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given arc.
///
/// Make a copy of the given arc.
BpGraphCopy& arc(TArc& tarc, const Arc& sarc) {
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
ArcRefMap, TArc>(tarc, sarc));
return *this;
}
/// \brief Copies the edge references into the given map.
///
/// Copies the edge references into the given map.
template <typename EdgeRef>
BpGraphCopy& edgeRef(EdgeRef& map) {
edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge,
EdgeRefMap, EdgeRef>(map));
return *this;
}
/// \brief Copies the edge cross references into the given map.
///
/// Copies the edge cross references (reverse
/// references) into the given map.
template <typename EdgeCrossRef>
BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
Edge, EdgeRefMap, EdgeCrossRef>(map));
return *this;
}
/// \brief Make copy of the given map.
///
/// Makes copy of the given map for the newly created digraph.
/// The new map's key type is the to digraph's edge type,
/// and the copied map's key type is the from digraph's edge
/// type.
template <typename ToMap, typename FromMap>
BpGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge,
EdgeRefMap, ToMap, FromMap>(tmap, map));
return *this;
}
/// \brief Make a copy of the given edge.
///
/// Make a copy of the given edge.
BpGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge,
EdgeRefMap, TEdge>(tedge, sedge));
return *this;
}
/// \brief Executes the copies.
///
/// Executes the copies.
void run() {
RedRefMap redRefMap(from);
BlueRefMap blueRefMap(from);
NodeRefMap nodeRefMap(from, redRefMap, blueRefMap);
EdgeRefMap edgeRefMap(from);
ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
_digraph_utils_bits::BpGraphCopySelector<To>::
copy(to, from, redRefMap, blueRefMap, edgeRefMap);
for (int i = 0; i < int(redMapCopies.size()); ++i) {
redMapCopies[i]->copy(from, redRefMap);
}
for (int i = 0; i < int(blueMapCopies.size()); ++i) {
blueMapCopies[i]->copy(from, blueRefMap);
}
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
nodeMapCopies[i]->copy(from, nodeRefMap);
}
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
edgeMapCopies[i]->copy(from, edgeRefMap);
}
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
arcMapCopies[i]->copy(from, arcRefMap);
}
}
private:
const From& from;
To& to;
std::vector<_digraph_utils_bits::MapCopyBase<From, Red, RedRefMap>* >
redMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Blue, BlueRefMap>* >
blueMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
nodeMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
arcMapCopies;
std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
edgeMapCopies;
};
/// \brief Copy a bipartite digraph to another digraph.
///
/// Copy a bipartite digraph to another digraph.
/// The usage of the function:
///
///\code
/// copyBpGraph(trg, src).redRef(anr).arcCrossRef(ecr).run();
///\endcode
///
/// After the copy the \c nr map will contain the mapping from the
/// nodes of the \c from digraph to the nodes of the \c to digraph and
/// \c ecr will contain the mapping from the arcs of the \c to digraph
/// to the arcs of the \c from digraph.
///
/// \see BpGraphCopy
template <typename To, typename From>
BpGraphCopy<To, From>
copyBpGraph(To& to, const From& from) {
return BpGraphCopy<To, From>(to, from);
}
/// @}
/// \addtogroup digraph_maps
/// @{
/// Provides an immutable and unique id for each item in the digraph.
/// The IdMap class provides a unique and immutable id for each item of the
/// same type (e.g. node) in the digraph. This id is <ul><li>\b unique:
/// different items (nodes) get different ids <li>\b immutable: the id of an
/// item (node) does not change (even if you delete other nodes). </ul>
/// Through this map you get access (i.e. can read) the inner id values of
/// the items stored in the digraph. This map can be inverted with its member
/// class \c InverseMap.
///
template <typename _Digraph, typename _Item>
class IdMap {
public:
typedef _Digraph Digraph;
typedef int Value;
typedef _Item Item;
typedef _Item Key;
/// \brief Constructor.
///
/// Constructor of the map.
explicit IdMap(const Digraph& _digraph) : digraph(&_digraph) {}
/// \brief Gives back the \e id of the item.
///
/// Gives back the immutable and unique \e id of the item.
int operator[](const Item& item) const { return digraph->id(item);}
/// \brief Gives back the item by its id.
///
/// Gives back the item by its id.
Item operator()(int id) { return digraph->fromId(id, Item()); }
private:
const Digraph* digraph;
public:
/// \brief The class represents the inverse of its owner (IdMap).
///
/// The class represents the inverse of its owner (IdMap).
/// \see inverse()
class InverseMap {
public:
/// \brief Constructor.
///
/// Constructor for creating an id-to-item map.
explicit InverseMap(const Digraph& _digraph) : digraph(&_digraph) {}
/// \brief Constructor.
///
/// Constructor for creating an id-to-item map.
explicit InverseMap(const IdMap& idMap) : digraph(idMap.digraph) {}
/// \brief Gives back the given item from its id.
///
/// Gives back the given item from its id.
///
Item operator[](int id) const { return digraph->fromId(id, Item());}
private:
const Digraph* digraph;
};
/// \brief Gives back the inverse of the map.
///
/// Gives back the inverse of the IdMap.
InverseMap inverse() const { return InverseMap(*digraph);}
};
/// \brief General invertable digraph-map type.
/// This type provides simple invertable digraph-maps.
/// The InvertableMap wraps an arbitrary ReadWriteMap
/// and if a key is set to a new value then store it
/// in the inverse map.
///
/// The values of the map can be accessed
/// with stl compatible forward iterator.
///
/// \param _Digraph The digraph type.
/// \param _Item The item type of the digraph.
/// \param _Value The value type of the map.
///
/// \see IterableValueMap
template <typename _Digraph, typename _Item, typename _Value>
class InvertableMap : protected DefaultMap<_Digraph, _Item, _Value> {
private:
typedef DefaultMap<_Digraph, _Item, _Value> Map;
typedef _Digraph Digraph;
typedef std::map<_Value, _Item> Container;
Container invMap;
public:
/// The key type of InvertableMap (Node, Arc, Edge).
typedef typename Map::Key Key;
/// The value type of the InvertableMap.
typedef typename Map::Value Value;
/// \brief Constructor.
///
/// Construct a new InvertableMap for the digraph.
///
explicit InvertableMap(const Digraph& digraph) : Map(digraph) {}
/// \brief Forward iterator for values.
///
/// This iterator is an stl compatible forward
/// iterator on the values of the map. The values can
/// be accessed in the [beginValue, endValue) range.
///
class ValueIterator
: public std::iterator<std::forward_iterator_tag, Value> {
friend class InvertableMap;
private:
ValueIterator(typename Container::const_iterator _it)
: it(_it) {}
public:
ValueIterator() {}
ValueIterator& operator++() { ++it; return *this; }
ValueIterator operator++(int) {
ValueIterator tmp(*this);
operator++();
return tmp;
}
const Value& operator*() const { return it->first; }
const Value* operator->() const { return &(it->first); }
bool operator==(ValueIterator jt) const { return it == jt.it; }
bool operator!=(ValueIterator jt) const { return it != jt.it; }
private:
typename Container::const_iterator it;
};
/// \brief Returns an iterator to the first value.
///
/// Returns an stl compatible iterator to the
/// first value of the map. The values of the
/// map can be accessed in the [beginValue, endValue)
/// range.
ValueIterator beginValue() const {
return ValueIterator(invMap.begin());
}
/// \brief Returns an iterator after the last value.
///
/// Returns an stl compatible iterator after the
/// last value of the map. The values of the
/// map can be accessed in the [beginValue, endValue)
/// range.
ValueIterator endValue() const {
return ValueIterator(invMap.end());
}
/// \brief The setter function of the map.
///
/// Sets the mapped value.
void set(const Key& key, const Value& val) {
Value oldval = Map::operator[](key);
typename Container::iterator it = invMap.find(oldval);
if (it != invMap.end() && it->second == key) {
invMap.erase(it);
}
invMap.insert(make_pair(val, key));
Map::set(key, val);
}
/// \brief The getter function of the map.
///
/// It gives back the value associated with the key.
typename MapTraits<Map>::ConstReturnValue
operator[](const Key& key) const {
return Map::operator[](key);
}
/// \brief Gives back the item by its value.
///
/// Gives back the item by its value.
Key operator()(const Value& key) const {
typename Container::const_iterator it = invMap.find(key);
return it != invMap.end() ? it->second : INVALID;
}
protected:
/// \brief Erase the key from the map.
///
/// Erase the key to the map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const Key& key) {
Value val = Map::operator[](key);
typename Container::iterator it = invMap.find(val);
if (it != invMap.end() && it->second == key) {
invMap.erase(it);
}
Map::erase(key);
}
/// \brief Erase more keys from the map.
///
/// Erase more keys from the map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const std::vector<Key>& keys) {
for (int i = 0; i < int(keys.size()); ++i) {
Value val = Map::operator[](keys[i]);
typename Container::iterator it = invMap.find(val);
if (it != invMap.end() && it->second == keys[i]) {
invMap.erase(it);
}
}
Map::erase(keys);
}
/// \brief Clear the keys from the map and inverse map.
///
/// Clear the keys from the map and inverse map. It is called by the
/// \c AlterationNotifier.
virtual void clear() {
invMap.clear();
Map::clear();
}
public:
/// \brief The inverse map type.
///
/// The inverse of this map. The subscript operator of the map
/// gives back always the item what was last assigned to the value.
class InverseMap {
public:
/// \brief Constructor of the InverseMap.
///
/// Constructor of the InverseMap.
explicit InverseMap(const InvertableMap& _inverted)
: inverted(_inverted) {}
/// The value type of the InverseMap.
typedef typename InvertableMap::Key Value;
/// The key type of the InverseMap.
typedef typename InvertableMap::Value Key;
/// \brief Subscript operator.
///
/// Subscript operator. It gives back always the item
/// what was last assigned to the value.
Value operator[](const Key& key) const {
return inverted(key);
}
private:
const InvertableMap& inverted;
};
/// \brief It gives back the just readable inverse map.
///
/// It gives back the just readable inverse map.
InverseMap inverse() const {
return InverseMap(*this);
}
};
/// \brief Provides a mutable, continuous and unique descriptor for each
/// item in the digraph.
///
/// The DescriptorMap class provides a unique and continuous (but mutable)
/// descriptor (id) for each item of the same type (e.g. node) in the
/// digraph. This id is <ul><li>\b unique: different items (nodes) get
/// different ids <li>\b continuous: the range of the ids is the set of
/// integers between 0 and \c n-1, where \c n is the number of the items of
/// this type (e.g. nodes) (so the id of a node can change if you delete an
/// other node, i.e. this id is mutable). </ul> This map can be inverted
/// with its member class \c InverseMap.
///
/// \param _Digraph The digraph class the \c DescriptorMap belongs to.
/// \param _Item The Item is the Key of the Map. It may be Node, Arc or
/// Edge.
template <typename _Digraph, typename _Item>
class DescriptorMap : protected DefaultMap<_Digraph, _Item, int> {
typedef _Item Item;
typedef DefaultMap<_Digraph, _Item, int> Map;
public:
/// The digraph class of DescriptorMap.
typedef _Digraph Digraph;
/// The key type of DescriptorMap (Node, Arc, Edge).
typedef typename Map::Key Key;
/// The value type of DescriptorMap.
typedef typename Map::Value Value;
/// \brief Constructor.
///
/// Constructor for descriptor map.
explicit DescriptorMap(const Digraph& _digraph) : Map(_digraph) {
Item it;
const typename Map::Notifier* nf = Map::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Map::set(it, invMap.size());
invMap.push_back(it);
}
}
protected:
/// \brief Add a new key to the map.
///
/// Add a new key to the map. It is called by the
/// \c AlterationNotifier.
virtual void add(const Item& item) {
Map::add(item);
Map::set(item, invMap.size());
invMap.push_back(item);
}
/// \brief Add more new keys to the map.
///
/// Add more new keys to the map. It is called by the
/// \c AlterationNotifier.
virtual void add(const std::vector<Item>& items) {
Map::add(items);
for (int i = 0; i < int(items.size()); ++i) {
Map::set(items[i], invMap.size());
invMap.push_back(items[i]);
}
}
/// \brief Erase the key from the map.
///
/// Erase the key from the map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const Item& item) {
Map::set(invMap.back(), Map::operator[](item));
invMap[Map::operator[](item)] = invMap.back();
invMap.pop_back();
Map::erase(item);
}
/// \brief Erase more keys from the map.
///
/// Erase more keys from the map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const std::vector<Item>& items) {
for (int i = 0; i < int(items.size()); ++i) {
Map::set(invMap.back(), Map::operator[](items[i]));
invMap[Map::operator[](items[i])] = invMap.back();
invMap.pop_back();
}
Map::erase(items);
}
/// \brief Build the unique map.
///
/// Build the unique map. It is called by the
/// \c AlterationNotifier.
virtual void build() {
Map::build();
Item it;
const typename Map::Notifier* nf = Map::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Map::set(it, invMap.size());
invMap.push_back(it);
}
}
/// \brief Clear the keys from the map.
///
/// Clear the keys from the map. It is called by the
/// \c AlterationNotifier.
virtual void clear() {
invMap.clear();
Map::clear();
}
public:
/// \brief Returns the maximal value plus one.
///
/// Returns the maximal value plus one in the map.
unsigned int size() const {
return invMap.size();
}
/// \brief Swaps the position of the two items in the map.
///
/// Swaps the position of the two items in the map.
void swap(const Item& p, const Item& q) {
int pi = Map::operator[](p);
int qi = Map::operator[](q);
Map::set(p, qi);
invMap[qi] = p;
Map::set(q, pi);
invMap[pi] = q;
}
/// \brief Gives back the \e descriptor of the item.
///
/// Gives back the mutable and unique \e descriptor of the map.
int operator[](const Item& item) const {
return Map::operator[](item);
}
/// \brief Gives back the item by its descriptor.
///
/// Gives back th item by its descriptor.
Item operator()(int id) const {
return invMap[id];
}
private:
typedef std::vector<Item> Container;
Container invMap;
public:
/// \brief The inverse map type of DescriptorMap.
///
/// The inverse map type of DescriptorMap.
class InverseMap {
public:
/// \brief Constructor of the InverseMap.
///
/// Constructor of the InverseMap.
explicit InverseMap(const DescriptorMap& _inverted)
: inverted(_inverted) {}
/// The value type of the InverseMap.
typedef typename DescriptorMap::Key Value;
/// The key type of the InverseMap.
typedef typename DescriptorMap::Value Key;
/// \brief Subscript operator.
///
/// Subscript operator. It gives back the item
/// that the descriptor belongs to currently.
Value operator[](const Key& key) const {
return inverted(key);
}
/// \brief Size of the map.
///
/// Returns the size of the map.
unsigned int size() const {
return inverted.size();
}
private:
const DescriptorMap& inverted;
};
/// \brief Gives back the inverse of the map.
///
/// Gives back the inverse of the map.
const InverseMap inverse() const {
return InverseMap(*this);
}
};
/// \brief Returns the source of the given arc.
///
/// The SourceMap gives back the source Node of the given arc.
/// \see TargetMap
/// \author Balazs Dezso
template <typename Digraph>
class SourceMap {
public:
typedef typename Digraph::Node Value;
typedef typename Digraph::Arc Key;
/// \brief Constructor
///
/// Constructor
/// \param _digraph The digraph that the map belongs to.
explicit SourceMap(const Digraph& _digraph) : digraph(_digraph) {}
/// \brief The subscript operator.
///
/// The subscript operator.
/// \param arc The arc
/// \return The source of the arc
Value operator[](const Key& arc) const {
return digraph.source(arc);
}
private:
const Digraph& digraph;
};
/// \brief Returns a \ref SourceMap class.
///
/// This function just returns an \ref SourceMap class.
/// \relates SourceMap
template <typename Digraph>
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
return SourceMap<Digraph>(digraph);
}
/// \brief Returns the target of the given arc.
///
/// The TargetMap gives back the target Node of the given arc.
/// \see SourceMap
/// \author Balazs Dezso
template <typename Digraph>
class TargetMap {
public:
typedef typename Digraph::Node Value;
typedef typename Digraph::Arc Key;
/// \brief Constructor
///
/// Constructor
/// \param _digraph The digraph that the map belongs to.
explicit TargetMap(const Digraph& _digraph) : digraph(_digraph) {}
/// \brief The subscript operator.
///
/// The subscript operator.
/// \param e The arc
/// \return The target of the arc
Value operator[](const Key& e) const {
return digraph.target(e);
}
private:
const Digraph& digraph;
};
/// \brief Returns a \ref TargetMap class.
///
/// This function just returns a \ref TargetMap class.
/// \relates TargetMap
template <typename Digraph>
inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
return TargetMap<Digraph>(digraph);
}
/// \brief Returns the "forward" directed arc view of an edge.
///
/// Returns the "forward" directed arc view of an edge.
/// \see BackwardMap
/// \author Balazs Dezso
template <typename Digraph>
class ForwardMap {
public:
typedef typename Digraph::Arc Value;
typedef typename Digraph::Edge Key;
/// \brief Constructor
///
/// Constructor
/// \param _digraph The digraph that the map belongs to.
explicit ForwardMap(const Digraph& _digraph) : digraph(_digraph) {}
/// \brief The subscript operator.
///
/// The subscript operator.
/// \param key An edge
/// \return The "forward" directed arc view of edge
Value operator[](const Key& key) const {
return digraph.direct(key, true);
}
private:
const Digraph& digraph;
};
/// \brief Returns a \ref ForwardMap class.
///
/// This function just returns an \ref ForwardMap class.
/// \relates ForwardMap
template <typename Digraph>
inline ForwardMap<Digraph> forwardMap(const Digraph& digraph) {
return ForwardMap<Digraph>(digraph);
}
/// \brief Returns the "backward" directed arc view of an edge.
///
/// Returns the "backward" directed arc view of an edge.
/// \see ForwardMap
/// \author Balazs Dezso
template <typename Digraph>
class BackwardMap {
public:
typedef typename Digraph::Arc Value;
typedef typename Digraph::Edge Key;
/// \brief Constructor
///
/// Constructor
/// \param _digraph The digraph that the map belongs to.
explicit BackwardMap(const Digraph& _digraph) : digraph(_digraph) {}
/// \brief The subscript operator.
///
/// The subscript operator.
/// \param key An edge
/// \return The "backward" directed arc view of edge
Value operator[](const Key& key) const {
return digraph.direct(key, false);
}
private:
const Digraph& digraph;
};
/// \brief Returns a \ref BackwardMap class
/// This function just returns a \ref BackwardMap class.
/// \relates BackwardMap
template <typename Digraph>
inline BackwardMap<Digraph> backwardMap(const Digraph& digraph) {
return BackwardMap<Digraph>(digraph);
}
/// \brief Potential difference map
///
/// If there is an potential map on the nodes then we
/// can get an arc map as we get the substraction of the
/// values of the target and source.
template <typename Digraph, typename NodeMap>
class PotentialDifferenceMap {
public:
typedef typename Digraph::Arc Key;
typedef typename NodeMap::Value Value;
/// \brief Constructor
///
/// Contructor of the map
explicit PotentialDifferenceMap(const Digraph& _digraph,
const NodeMap& _potential)
: digraph(_digraph), potential(_potential) {}
/// \brief Const subscription operator
///
/// Const subscription operator
Value operator[](const Key& arc) const {
return potential[digraph.target(arc)] - potential[digraph.source(arc)];
}
private:
const Digraph& digraph;
const NodeMap& potential;
};
/// \brief Returns a PotentialDifferenceMap.
///
/// This function just returns a PotentialDifferenceMap.
/// \relates PotentialDifferenceMap
template <typename Digraph, typename NodeMap>
PotentialDifferenceMap<Digraph, NodeMap>
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
}
/// \brief Map of the node in-degrees.
///
/// This map returns the in-degree of a node. Once it is constructed,
/// the degrees are stored in a standard NodeMap, so each query is done
/// in constant time. On the other hand, the values are updated automatically
/// whenever the digraph changes.
///
/// \warning Besides addNode() and addArc(), a digraph structure may provide
/// alternative ways to modify the digraph. The correct behavior of InDegMap
/// is not guarantied if these additional features are used. For example
/// the functions \ref ListDigraph::changeSource() "changeSource()",
/// \ref ListDigraph::changeTarget() "changeTarget()" and
/// \ref ListDigraph::reverseArc() "reverseArc()"
/// of \ref ListDigraph will \e not update the degree values correctly.
///
/// \sa OutDegMap
template <typename _Digraph>
class InDegMap
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
::ItemNotifier::ObserverBase {
public:
typedef _Digraph Digraph;
typedef int Value;
typedef typename Digraph::Node Key;
typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
::ItemNotifier::ObserverBase Parent;
private:
class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
public:
typedef DefaultMap<_Digraph, Key, int> Parent;
typedef typename Parent::Digraph Digraph;
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
virtual void add(const Key& key) {
Parent::add(key);
Parent::set(key, 0);
}
virtual void add(const std::vector<Key>& keys) {
Parent::add(keys);
for (int i = 0; i < int(keys.size()); ++i) {
Parent::set(keys[i], 0);
}
}
virtual void build() {
Parent::build();
Key it;
typename Parent::Notifier* nf = Parent::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, 0);
}
}
};
public:
/// \brief Constructor.
///
/// Constructor for creating in-degree map.
explicit InDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
Parent::attach(digraph.notifier(typename _Digraph::Arc()));
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
deg[it] = countInArcs(digraph, it);
}
}
/// Gives back the in-degree of a Node.
int operator[](const Key& key) const {
return deg[key];
}
protected:
typedef typename Digraph::Arc Arc;
virtual void add(const Arc& arc) {
++deg[digraph.target(arc)];
}
virtual void add(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
++deg[digraph.target(arcs[i])];
}
}
virtual void erase(const Arc& arc) {
--deg[digraph.target(arc)];
}
virtual void erase(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
--deg[digraph.target(arcs[i])];
}
}
virtual void build() {
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
deg[it] = countInArcs(digraph, it);
}
}
virtual void clear() {
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
deg[it] = 0;
}
}
private:
const _Digraph& digraph;
AutoNodeMap deg;
};
/// \brief Map of the node out-degrees.
///
/// This map returns the out-degree of a node. Once it is constructed,
/// the degrees are stored in a standard NodeMap, so each query is done
/// in constant time. On the other hand, the values are updated automatically
/// whenever the digraph changes.
///
/// \warning Besides addNode() and addArc(), a digraph structure may provide
/// alternative ways to modify the digraph. The correct behavior of OutDegMap
/// is not guarantied if these additional features are used. For example
/// the functions \ref ListDigraph::changeSource() "changeSource()",
/// \ref ListDigraph::changeTarget() "changeTarget()" and
/// \ref ListDigraph::reverseArc() "reverseArc()"
/// of \ref ListDigraph will \e not update the degree values correctly.
///
/// \sa InDegMap
template <typename _Digraph>
class OutDegMap
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
::ItemNotifier::ObserverBase {
public:
typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
::ItemNotifier::ObserverBase Parent;
typedef _Digraph Digraph;
typedef int Value;
typedef typename Digraph::Node Key;
private:
class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
public:
typedef DefaultMap<_Digraph, Key, int> Parent;
typedef typename Parent::Digraph Digraph;
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
virtual void add(const Key& key) {
Parent::add(key);
Parent::set(key, 0);
}
virtual void add(const std::vector<Key>& keys) {
Parent::add(keys);
for (int i = 0; i < int(keys.size()); ++i) {
Parent::set(keys[i], 0);
}
}
virtual void build() {
Parent::build();
Key it;
typename Parent::Notifier* nf = Parent::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, 0);
}
}
};
public:
/// \brief Constructor.
///
/// Constructor for creating out-degree map.
explicit OutDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
Parent::attach(digraph.notifier(typename _Digraph::Arc()));
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
deg[it] = countOutArcs(digraph, it);
}
}
/// Gives back the out-degree of a Node.
int operator[](const Key& key) const {
return deg[key];
}
protected:
typedef typename Digraph::Arc Arc;
virtual void add(const Arc& arc) {
++deg[digraph.source(arc)];
}
virtual void add(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
++deg[digraph.source(arcs[i])];
}
}
virtual void erase(const Arc& arc) {
--deg[digraph.source(arc)];
}
virtual void erase(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
--deg[digraph.source(arcs[i])];
}
}
virtual void build() {
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
deg[it] = countOutArcs(digraph, it);
}
}
virtual void clear() {
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
deg[it] = 0;
}
}
private:
const _Digraph& digraph;
AutoNodeMap deg;
};
///Dynamic arc look up between given endpoints.
///\ingroup gutils
///Using this class, you can find an arc in a digraph from a given
///source to a given target in amortized time <em>O(log d)</em>,
///where <em>d</em> is the out-degree of the source node.
///
///It is possible to find \e all parallel arcs between two nodes with
///the \c findFirst() and \c findNext() members.
///
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
///digraph do not changed so frequently.
///
///This class uses a self-adjusting binary search tree, Sleator's
///and Tarjan's Splay tree for guarantee the logarithmic amortized
///time bound for arc lookups. This class also guarantees the
///optimal time bound in a constant factor for any distribution of
///queries.
///
///\param G The type of the underlying digraph.
///
///\sa ArcLookUp
///\sa AllArcLookUp
template<class G>
class DynArcLookUp
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
{
public:
typedef typename ItemSetTraits<G, typename G::Arc>
::ItemNotifier::ObserverBase Parent;
GRAPH_TYPEDEFS(typename G);
typedef G Digraph;
protected:
class AutoNodeMap : public DefaultMap<G, Node, Arc> {
public:
typedef DefaultMap<G, Node, Arc> Parent;
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
virtual void add(const Node& node) {
Parent::add(node);
Parent::set(node, INVALID);
}
virtual void add(const std::vector<Node>& nodes) {
Parent::add(nodes);
for (int i = 0; i < int(nodes.size()); ++i) {
Parent::set(nodes[i], INVALID);
}
}
virtual void build() {
Parent::build();
Node it;
typename Parent::Notifier* nf = Parent::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, INVALID);
}
}
};
const Digraph &_g;
AutoNodeMap _head;
typename Digraph::template ArcMap<Arc> _parent;
typename Digraph::template ArcMap<Arc> _left;
typename Digraph::template ArcMap<Arc> _right;
class ArcLess {
const Digraph &g;
public:
ArcLess(const Digraph &_g) : g(_g) {}
bool operator()(Arc a,Arc b) const
{
return g.target(a)<g.target(b);
}
};
public:
///Constructor
///Constructor.
///
///It builds up the search database.
DynArcLookUp(const Digraph &g)
: _g(g),_head(g),_parent(g),_left(g),_right(g)
{
Parent::attach(_g.notifier(typename Digraph::Arc()));
refresh();
}
protected:
virtual void add(const Arc& arc) {
insert(arc);
}
virtual void add(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
insert(arcs[i]);
}
}
virtual void erase(const Arc& arc) {
remove(arc);
}
virtual void erase(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
remove(arcs[i]);
}
}
virtual void build() {
refresh();
}
virtual void clear() {
for(NodeIt n(_g);n!=INVALID;++n) {
_head.set(n, INVALID);
}
}
void insert(Arc arc) {
Node s = _g.source(arc);
Node t = _g.target(arc);
_left.set(arc, INVALID);
_right.set(arc, INVALID);
Arc e = _head[s];
if (e == INVALID) {
_head.set(s, arc);
_parent.set(arc, INVALID);
return;
}
while (true) {
if (t < _g.target(e)) {
if (_left[e] == INVALID) {
_left.set(e, arc);
_parent.set(arc, e);
splay(arc);
return;
} else {
e = _left[e];
}
} else {
if (_right[e] == INVALID) {
_right.set(e, arc);
_parent.set(arc, e);
splay(arc);
return;
} else {
e = _right[e];
}
}
}
}
void remove(Arc arc) {
if (_left[arc] == INVALID) {
if (_right[arc] != INVALID) {
_parent.set(_right[arc], _parent[arc]);
}
if (_parent[arc] != INVALID) {
if (_left[_parent[arc]] == arc) {
_left.set(_parent[arc], _right[arc]);
} else {
_right.set(_parent[arc], _right[arc]);
}
} else {
_head.set(_g.source(arc), _right[arc]);
}
} else if (_right[arc] == INVALID) {
_parent.set(_left[arc], _parent[arc]);
if (_parent[arc] != INVALID) {
if (_left[_parent[arc]] == arc) {
_left.set(_parent[arc], _left[arc]);
} else {
_right.set(_parent[arc], _left[arc]);
}
} else {
_head.set(_g.source(arc), _left[arc]);
}
} else {
Arc e = _left[arc];
if (_right[e] != INVALID) {
e = _right[e];
while (_right[e] != INVALID) {
e = _right[e];
}
Arc s = _parent[e];
_right.set(_parent[e], _left[e]);
if (_left[e] != INVALID) {
_parent.set(_left[e], _parent[e]);
}
_left.set(e, _left[arc]);
_parent.set(_left[arc], e);
_right.set(e, _right[arc]);
_parent.set(_right[arc], e);
_parent.set(e, _parent[arc]);
if (_parent[arc] != INVALID) {
if (_left[_parent[arc]] == arc) {
_left.set(_parent[arc], e);
} else {
_right.set(_parent[arc], e);
}
}
splay(s);
} else {
_right.set(e, _right[arc]);
_parent.set(_right[arc], e);
if (_parent[arc] != INVALID) {
if (_left[_parent[arc]] == arc) {
_left.set(_parent[arc], e);
} else {
_right.set(_parent[arc], e);
}
} else {
_head.set(_g.source(arc), e);
}
}
}
}
Arc refreshRec(std::vector<Arc> &v,int a,int b)
{
int m=(a+b)/2;
Arc me=v[m];
if (a < m) {
Arc left = refreshRec(v,a,m-1);
_left.set(me, left);
_parent.set(left, me);
} else {
_left.set(me, INVALID);
}
if (m < b) {
Arc right = refreshRec(v,m+1,b);
_right.set(me, right);
_parent.set(right, me);
} else {
_right.set(me, INVALID);
}
return me;
}
void refresh() {
for(NodeIt n(_g);n!=INVALID;++n) {
std::vector<Arc> v;
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
if(v.size()) {
std::sort(v.begin(),v.end(),ArcLess(_g));
Arc head = refreshRec(v,0,v.size()-1);
_head.set(n, head);
_parent.set(head, INVALID);
}
else _head.set(n, INVALID);
}
}
void zig(Arc v) {
Arc w = _parent[v];
_parent.set(v, _parent[w]);
_parent.set(w, v);
_left.set(w, _right[v]);
_right.set(v, w);
if (_parent[v] != INVALID) {
if (_right[_parent[v]] == w) {
_right.set(_parent[v], v);
} else {
_left.set(_parent[v], v);
}
}
if (_left[w] != INVALID){
_parent.set(_left[w], w);
}
}
void zag(Arc v) {
Arc w = _parent[v];
_parent.set(v, _parent[w]);
_parent.set(w, v);
_right.set(w, _left[v]);
_left.set(v, w);
if (_parent[v] != INVALID){
if (_left[_parent[v]] == w) {
_left.set(_parent[v], v);
} else {
_right.set(_parent[v], v);
}
}
if (_right[w] != INVALID){
_parent.set(_right[w], w);
}
}
void splay(Arc v) {
while (_parent[v] != INVALID) {
if (v == _left[_parent[v]]) {
if (_parent[_parent[v]] == INVALID) {
zig(v);
} else {
if (_parent[v] == _left[_parent[_parent[v]]]) {
zig(_parent[v]);
zig(v);
} else {
zig(v);
zag(v);
}
}
} else {
if (_parent[_parent[v]] == INVALID) {
zag(v);
} else {
if (_parent[v] == _left[_parent[_parent[v]]]) {
zag(v);
zig(v);
} else {
zag(_parent[v]);
zag(v);
}
}
}
}
_head[_g.source(v)] = v;
}
public:
///Find an arc between two nodes.
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
/// <em>d</em> is the number of outgoing arcs of \c s.
///\param s The source node
///\param t The target node
///\return An arc from \c s to \c t if there exists,
///\ref INVALID otherwise.
Arc operator()(Node s, Node t) const
{
Arc e = _head[s];
while (true) {
if (_g.target(e) == t) {
const_cast<DynArcLookUp&>(*this).splay(e);
return e;
} else if (t < _g.target(e)) {
if (_left[e] == INVALID) {
const_cast<DynArcLookUp&>(*this).splay(e);
return INVALID;
} else {
e = _left[e];
}
} else {
if (_right[e] == INVALID) {
const_cast<DynArcLookUp&>(*this).splay(e);
return INVALID;
} else {
e = _right[e];
}
}
}
}
///Find the first arc between two nodes.
///Find the first arc between two nodes in time
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
/// outgoing arcs of \c s.
///\param s The source node
///\param t The target node
///\return An arc from \c s to \c t if there exists, \ref INVALID
/// otherwise.
Arc findFirst(Node s, Node t) const
{
Arc e = _head[s];
Arc r = INVALID;
while (true) {
if (_g.target(e) < t) {
if (_right[e] == INVALID) {
const_cast<DynArcLookUp&>(*this).splay(e);
return r;
} else {
e = _right[e];
}
} else {
if (_g.target(e) == t) {
r = e;
}
if (_left[e] == INVALID) {
const_cast<DynArcLookUp&>(*this).splay(e);
return r;
} else {
e = _left[e];
}
}
}
}
///Find the next arc between two nodes.
///Find the next arc between two nodes in time
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
/// outgoing arcs of \c s.
///\param s The source node
///\param t The target node
///\return An arc from \c s to \c t if there exists, \ref INVALID
/// otherwise.
///\note If \c e is not the result of the previous \c findFirst()
///operation then the amorized time bound can not be guaranteed.
#ifdef DOXYGEN
Arc findNext(Node s, Node t, Arc e) const
#else
Arc findNext(Node, Node t, Arc e) const
#endif
{
if (_right[e] != INVALID) {
e = _right[e];
while (_left[e] != INVALID) {
e = _left[e];
}
const_cast<DynArcLookUp&>(*this).splay(e);
} else {
while (_parent[e] != INVALID && _right[_parent[e]] == e) {
e = _parent[e];
}
if (_parent[e] == INVALID) {
return INVALID;
} else {
e = _parent[e];
const_cast<DynArcLookUp&>(*this).splay(e);
}
}
if (_g.target(e) == t) return e;
else return INVALID;
}
};
///Fast arc look up between given endpoints.
///\ingroup gutils
///Using this class, you can find an arc in a digraph from a given
///source to a given target in time <em>O(log d)</em>,
///where <em>d</em> is the out-degree of the source node.
///
///It is not possible to find \e all parallel arcs between two nodes.
///Use \ref AllArcLookUp for this purpose.
///
///\warning This class is static, so you should refresh() (or at least
///refresh(Node)) this data structure
///whenever the digraph changes. This is a time consuming (superlinearly
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
///
///\param G The type of the underlying digraph.
///
///\sa DynArcLookUp
///\sa AllArcLookUp
template<class G>
class ArcLookUp
{
public:
GRAPH_TYPEDEFS(typename G);
typedef G Digraph;
protected:
const Digraph &_g;
typename Digraph::template NodeMap<Arc> _head;
typename Digraph::template ArcMap<Arc> _left;
typename Digraph::template ArcMap<Arc> _right;
class ArcLess {
const Digraph &g;
public:
ArcLess(const Digraph &_g) : g(_g) {}
bool operator()(Arc a,Arc b) const
{
return g.target(a)<g.target(b);
}
};
public:
///Constructor
///Constructor.
///
///It builds up the search database, which remains valid until the digraph
///changes.
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
private:
Arc refreshRec(std::vector<Arc> &v,int a,int b)
{
int m=(a+b)/2;
Arc me=v[m];
_left[me] = a<m?refreshRec(v,a,m-1):INVALID;
_right[me] = m<b?refreshRec(v,m+1,b):INVALID;
return me;
}
public:
///Refresh the data structure at a node.
///Build up the search database of node \c n.
///
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
///the number of the outgoing arcs of \c n.
void refresh(Node n)
{
std::vector<Arc> v;
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
if(v.size()) {
std::sort(v.begin(),v.end(),ArcLess(_g));
_head[n]=refreshRec(v,0,v.size()-1);
}
else _head[n]=INVALID;
}
///Refresh the full data structure.
///Build up the full search database. In fact, it simply calls
///\ref refresh(Node) "refresh(n)" for each node \c n.
///
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
///the number of the arcs of \c n and <em>D</em> is the maximum
///out-degree of the digraph.
void refresh()
{
for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
}
///Find an arc between two nodes.
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
/// <em>d</em> is the number of outgoing arcs of \c s.
///\param s The source node
///\param t The target node
///\return An arc from \c s to \c t if there exists,
///\ref INVALID otherwise.
///
///\warning If you change the digraph, refresh() must be called before using
///this operator. If you change the outgoing arcs of
///a single node \c n, then
///\ref refresh(Node) "refresh(n)" is enough.
///
Arc operator()(Node s, Node t) const
{
Arc e;
for(e=_head[s];
e!=INVALID&&_g.target(e)!=t;
e = t < _g.target(e)?_left[e]:_right[e]) ;
return e;
}
};
///Fast look up of all arcs between given endpoints.
///\ingroup gutils
///This class is the same as \ref ArcLookUp, with the addition
///that it makes it possible to find all arcs between given endpoints.
///
///\warning This class is static, so you should refresh() (or at least
///refresh(Node)) this data structure
///whenever the digraph changes. This is a time consuming (superlinearly
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
///
///\param G The type of the underlying digraph.
///
///\sa DynArcLookUp
///\sa ArcLookUp
template<class G>
class AllArcLookUp : public ArcLookUp<G>
{
using ArcLookUp<G>::_g;
using ArcLookUp<G>::_right;
using ArcLookUp<G>::_left;
using ArcLookUp<G>::_head;
GRAPH_TYPEDEFS(typename G);
typedef G Digraph;
typename Digraph::template ArcMap<Arc> _next;
Arc refreshNext(Arc head,Arc next=INVALID)
{
if(head==INVALID) return next;
else {
next=refreshNext(_right[head],next);
// _next[head]=next;
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
? next : INVALID;
return refreshNext(_left[head],head);
}
}
void refreshNext()
{
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
}
public:
///Constructor
///Constructor.
///
///It builds up the search database, which remains valid until the digraph
///changes.
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
///Refresh the data structure at a node.
///Build up the search database of node \c n.
///
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
///the number of the outgoing arcs of \c n.
void refresh(Node n)
{
ArcLookUp<G>::refresh(n);
refreshNext(_head[n]);
}
///Refresh the full data structure.
///Build up the full search database. In fact, it simply calls
///\ref refresh(Node) "refresh(n)" for each node \c n.
///
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
///the number of the arcs of \c n and <em>D</em> is the maximum
///out-degree of the digraph.
void refresh()
{
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
}
///Find an arc between two nodes.
///Find an arc between two nodes.
///\param s The source node
///\param t The target node
///\param prev The previous arc between \c s and \c t. It it is INVALID or
///not given, the operator finds the first appropriate arc.
///\return An arc from \c s to \c t after \c prev or
///\ref INVALID if there is no more.
///
///For example, you can count the number of arcs from \c u to \c v in the
///following way.
///\code
///AllArcLookUp<ListDigraph> ae(g);
///...
///int n=0;
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
///\endcode
///
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the
///consecutive arcs are found in constant time.
///
///\warning If you change the digraph, refresh() must be called before using
///this operator. If you change the outgoing arcs of
///a single node \c n, then
///\ref refresh(Node) "refresh(n)" is enough.
///
#ifdef DOXYGEN
Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
#else
using ArcLookUp<G>::operator() ;
Arc operator()(Node s, Node t, Arc prev) const
{
return prev==INVALID?(*this)(s,t):_next[prev];
}
#endif
};
/// @}
} //END OF NAMESPACE LEMON
#endif