/usr/include/lemon/planarity.h is in liblemon-dev 1.3.1+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2013
* 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_PLANARITY_H
#define LEMON_PLANARITY_H
/// \ingroup planar
/// \file
/// \brief Planarity checking, embedding, drawing and coloring
#include <vector>
#include <list>
#include <lemon/dfs.h>
#include <lemon/bfs.h>
#include <lemon/radix_sort.h>
#include <lemon/maps.h>
#include <lemon/path.h>
#include <lemon/bucket_heap.h>
#include <lemon/adaptors.h>
#include <lemon/edge_set.h>
#include <lemon/color.h>
#include <lemon/dim2.h>
namespace lemon {
namespace _planarity_bits {
template <typename Graph>
struct PlanarityVisitor : DfsVisitor<Graph> {
TEMPLATE_GRAPH_TYPEDEFS(Graph);
typedef typename Graph::template NodeMap<Arc> PredMap;
typedef typename Graph::template EdgeMap<bool> TreeMap;
typedef typename Graph::template NodeMap<int> OrderMap;
typedef std::vector<Node> OrderList;
typedef typename Graph::template NodeMap<int> LowMap;
typedef typename Graph::template NodeMap<int> AncestorMap;
PlanarityVisitor(const Graph& graph,
PredMap& pred_map, TreeMap& tree_map,
OrderMap& order_map, OrderList& order_list,
AncestorMap& ancestor_map, LowMap& low_map)
: _graph(graph), _pred_map(pred_map), _tree_map(tree_map),
_order_map(order_map), _order_list(order_list),
_ancestor_map(ancestor_map), _low_map(low_map) {}
void reach(const Node& node) {
_order_map[node] = _order_list.size();
_low_map[node] = _order_list.size();
_ancestor_map[node] = _order_list.size();
_order_list.push_back(node);
}
void discover(const Arc& arc) {
Node target = _graph.target(arc);
_tree_map[arc] = true;
_pred_map[target] = arc;
}
void examine(const Arc& arc) {
Node source = _graph.source(arc);
Node target = _graph.target(arc);
if (_order_map[target] < _order_map[source] && !_tree_map[arc]) {
if (_low_map[source] > _order_map[target]) {
_low_map[source] = _order_map[target];
}
if (_ancestor_map[source] > _order_map[target]) {
_ancestor_map[source] = _order_map[target];
}
}
}
void backtrack(const Arc& arc) {
Node source = _graph.source(arc);
Node target = _graph.target(arc);
if (_low_map[source] > _low_map[target]) {
_low_map[source] = _low_map[target];
}
}
const Graph& _graph;
PredMap& _pred_map;
TreeMap& _tree_map;
OrderMap& _order_map;
OrderList& _order_list;
AncestorMap& _ancestor_map;
LowMap& _low_map;
};
template <typename Graph, bool embedding = true>
struct NodeDataNode {
int prev, next;
int visited;
typename Graph::Arc first;
bool inverted;
};
template <typename Graph>
struct NodeDataNode<Graph, false> {
int prev, next;
int visited;
};
template <typename Graph>
struct ChildListNode {
typedef typename Graph::Node Node;
Node first;
Node prev, next;
};
template <typename Graph>
struct ArcListNode {
typename Graph::Arc prev, next;
};
template <typename Graph>
class PlanarityChecking {
private:
TEMPLATE_GRAPH_TYPEDEFS(Graph);
const Graph& _graph;
private:
typedef typename Graph::template NodeMap<Arc> PredMap;
typedef typename Graph::template EdgeMap<bool> TreeMap;
typedef typename Graph::template NodeMap<int> OrderMap;
typedef std::vector<Node> OrderList;
typedef typename Graph::template NodeMap<int> LowMap;
typedef typename Graph::template NodeMap<int> AncestorMap;
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
typedef std::vector<NodeDataNode> NodeData;
typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
typedef typename Graph::template NodeMap<bool> EmbedArc;
public:
PlanarityChecking(const Graph& graph) : _graph(graph) {}
bool run() {
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
PredMap pred_map(_graph, INVALID);
TreeMap tree_map(_graph, false);
OrderMap order_map(_graph, -1);
OrderList order_list;
AncestorMap ancestor_map(_graph, -1);
LowMap low_map(_graph, -1);
Visitor visitor(_graph, pred_map, tree_map,
order_map, order_list, ancestor_map, low_map);
DfsVisit<Graph, Visitor> visit(_graph, visitor);
visit.run();
ChildLists child_lists(_graph);
createChildLists(tree_map, order_map, low_map, child_lists);
NodeData node_data(2 * order_list.size());
EmbedArc embed_arc(_graph, false);
MergeRoots merge_roots(_graph);
for (int i = order_list.size() - 1; i >= 0; --i) {
Node node = order_list[i];
Node source = node;
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && tree_map[e]) {
initFace(target, node_data, order_map, order_list);
}
}
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && !tree_map[e]) {
embed_arc[target] = true;
walkUp(target, source, i, pred_map, low_map,
order_map, order_list, node_data, merge_roots);
}
}
for (typename MergeRoots::Value::iterator it =
merge_roots[node].begin();
it != merge_roots[node].end(); ++it) {
int rn = *it;
walkDown(rn, i, node_data, order_list, child_lists,
ancestor_map, low_map, embed_arc, merge_roots);
}
merge_roots[node].clear();
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && !tree_map[e]) {
if (embed_arc[target]) {
return false;
}
}
}
}
return true;
}
private:
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
const LowMap& low_map, ChildLists& child_lists) {
for (NodeIt n(_graph); n != INVALID; ++n) {
Node source = n;
std::vector<Node> targets;
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && tree_map[e]) {
targets.push_back(target);
}
}
if (targets.size() == 0) {
child_lists[source].first = INVALID;
} else if (targets.size() == 1) {
child_lists[source].first = targets[0];
child_lists[targets[0]].prev = INVALID;
child_lists[targets[0]].next = INVALID;
} else {
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
for (int i = 1; i < int(targets.size()); ++i) {
child_lists[targets[i]].prev = targets[i - 1];
child_lists[targets[i - 1]].next = targets[i];
}
child_lists[targets.back()].next = INVALID;
child_lists[targets.front()].prev = INVALID;
child_lists[source].first = targets.front();
}
}
}
void walkUp(const Node& node, Node root, int rorder,
const PredMap& pred_map, const LowMap& low_map,
const OrderMap& order_map, const OrderList& order_list,
NodeData& node_data, MergeRoots& merge_roots) {
int na, nb;
bool da, db;
na = nb = order_map[node];
da = true; db = false;
while (true) {
if (node_data[na].visited == rorder) break;
if (node_data[nb].visited == rorder) break;
node_data[na].visited = rorder;
node_data[nb].visited = rorder;
int rn = -1;
if (na >= int(order_list.size())) {
rn = na;
} else if (nb >= int(order_list.size())) {
rn = nb;
}
if (rn == -1) {
int nn;
nn = da ? node_data[na].prev : node_data[na].next;
da = node_data[nn].prev != na;
na = nn;
nn = db ? node_data[nb].prev : node_data[nb].next;
db = node_data[nn].prev != nb;
nb = nn;
} else {
Node rep = order_list[rn - order_list.size()];
Node parent = _graph.source(pred_map[rep]);
if (low_map[rep] < rorder) {
merge_roots[parent].push_back(rn);
} else {
merge_roots[parent].push_front(rn);
}
if (parent != root) {
na = nb = order_map[parent];
da = true; db = false;
} else {
break;
}
}
}
}
void walkDown(int rn, int rorder, NodeData& node_data,
OrderList& order_list, ChildLists& child_lists,
AncestorMap& ancestor_map, LowMap& low_map,
EmbedArc& embed_arc, MergeRoots& merge_roots) {
std::vector<std::pair<int, bool> > merge_stack;
for (int di = 0; di < 2; ++di) {
bool rd = di == 0;
int pn = rn;
int n = rd ? node_data[rn].next : node_data[rn].prev;
while (n != rn) {
Node node = order_list[n];
if (embed_arc[node]) {
// Merging components on the critical path
while (!merge_stack.empty()) {
// Component root
int cn = merge_stack.back().first;
bool cd = merge_stack.back().second;
merge_stack.pop_back();
// Parent of component
int dn = merge_stack.back().first;
bool dd = merge_stack.back().second;
merge_stack.pop_back();
Node parent = order_list[dn];
// Erasing from merge_roots
merge_roots[parent].pop_front();
Node child = order_list[cn - order_list.size()];
// Erasing from child_lists
if (child_lists[child].prev != INVALID) {
child_lists[child_lists[child].prev].next =
child_lists[child].next;
} else {
child_lists[parent].first = child_lists[child].next;
}
if (child_lists[child].next != INVALID) {
child_lists[child_lists[child].next].prev =
child_lists[child].prev;
}
// Merging external faces
{
int en = cn;
cn = cd ? node_data[cn].prev : node_data[cn].next;
cd = node_data[cn].next == en;
}
if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
}
bool d = pn == node_data[n].prev;
if (node_data[n].prev == node_data[n].next &&
node_data[n].inverted) {
d = !d;
}
// Embedding arc into external face
if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
if (d) node_data[n].prev = rn; else node_data[n].next = rn;
pn = rn;
embed_arc[order_list[n]] = false;
}
if (!merge_roots[node].empty()) {
bool d = pn == node_data[n].prev;
merge_stack.push_back(std::make_pair(n, d));
int rn = merge_roots[node].front();
int xn = node_data[rn].next;
Node xnode = order_list[xn];
int yn = node_data[rn].prev;
Node ynode = order_list[yn];
bool rd;
if (!external(xnode, rorder, child_lists,
ancestor_map, low_map)) {
rd = true;
} else if (!external(ynode, rorder, child_lists,
ancestor_map, low_map)) {
rd = false;
} else if (pertinent(xnode, embed_arc, merge_roots)) {
rd = true;
} else {
rd = false;
}
merge_stack.push_back(std::make_pair(rn, rd));
pn = rn;
n = rd ? xn : yn;
} else if (!external(node, rorder, child_lists,
ancestor_map, low_map)) {
int nn = (node_data[n].next != pn ?
node_data[n].next : node_data[n].prev);
bool nd = n == node_data[nn].prev;
if (nd) node_data[nn].prev = pn;
else node_data[nn].next = pn;
if (n == node_data[pn].prev) node_data[pn].prev = nn;
else node_data[pn].next = nn;
node_data[nn].inverted =
(node_data[nn].prev == node_data[nn].next && nd != rd);
n = nn;
}
else break;
}
if (!merge_stack.empty() || n == rn) {
break;
}
}
}
void initFace(const Node& node, NodeData& node_data,
const OrderMap& order_map, const OrderList& order_list) {
int n = order_map[node];
int rn = n + order_list.size();
node_data[n].next = node_data[n].prev = rn;
node_data[rn].next = node_data[rn].prev = n;
node_data[n].visited = order_list.size();
node_data[rn].visited = order_list.size();
}
bool external(const Node& node, int rorder,
ChildLists& child_lists, AncestorMap& ancestor_map,
LowMap& low_map) {
Node child = child_lists[node].first;
if (child != INVALID) {
if (low_map[child] < rorder) return true;
}
if (ancestor_map[node] < rorder) return true;
return false;
}
bool pertinent(const Node& node, const EmbedArc& embed_arc,
const MergeRoots& merge_roots) {
return !merge_roots[node].empty() || embed_arc[node];
}
};
}
/// \ingroup planar
///
/// \brief Planarity checking of an undirected simple graph
///
/// This function implements the Boyer-Myrvold algorithm for
/// planarity checking of an undirected simple graph. It is a simplified
/// version of the PlanarEmbedding algorithm class because neither
/// the embedding nor the Kuratowski subdivisons are computed.
template <typename GR>
bool checkPlanarity(const GR& graph) {
_planarity_bits::PlanarityChecking<GR> pc(graph);
return pc.run();
}
/// \ingroup planar
///
/// \brief Planar embedding of an undirected simple graph
///
/// This class implements the Boyer-Myrvold algorithm for planar
/// embedding of an undirected simple graph. The planar embedding is an
/// ordering of the outgoing edges of the nodes, which is a possible
/// configuration to draw the graph in the plane. If there is not
/// such ordering then the graph contains a K<sub>5</sub> (full graph
/// with 5 nodes) or a K<sub>3,3</sub> (complete bipartite graph on
/// 3 Red and 3 Blue nodes) subdivision.
///
/// The current implementation calculates either an embedding or a
/// Kuratowski subdivision. The running time of the algorithm is O(n).
///
/// \see PlanarDrawing, checkPlanarity()
template <typename Graph>
class PlanarEmbedding {
private:
TEMPLATE_GRAPH_TYPEDEFS(Graph);
const Graph& _graph;
typename Graph::template ArcMap<Arc> _embedding;
typename Graph::template EdgeMap<bool> _kuratowski;
private:
typedef typename Graph::template NodeMap<Arc> PredMap;
typedef typename Graph::template EdgeMap<bool> TreeMap;
typedef typename Graph::template NodeMap<int> OrderMap;
typedef std::vector<Node> OrderList;
typedef typename Graph::template NodeMap<int> LowMap;
typedef typename Graph::template NodeMap<int> AncestorMap;
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
typedef std::vector<NodeDataNode> NodeData;
typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
typedef typename Graph::template NodeMap<Arc> EmbedArc;
typedef _planarity_bits::ArcListNode<Graph> ArcListNode;
typedef typename Graph::template ArcMap<ArcListNode> ArcLists;
typedef typename Graph::template NodeMap<bool> FlipMap;
typedef typename Graph::template NodeMap<int> TypeMap;
enum IsolatorNodeType {
HIGHX = 6, LOWX = 7,
HIGHY = 8, LOWY = 9,
ROOT = 10, PERTINENT = 11,
INTERNAL = 12
};
public:
/// \brief The map type for storing the embedding
///
/// The map type for storing the embedding.
/// \see embeddingMap()
typedef typename Graph::template ArcMap<Arc> EmbeddingMap;
/// \brief Constructor
///
/// Constructor.
/// \pre The graph must be simple, i.e. it should not
/// contain parallel or loop arcs.
PlanarEmbedding(const Graph& graph)
: _graph(graph), _embedding(_graph), _kuratowski(graph, false) {}
/// \brief Run the algorithm.
///
/// This function runs the algorithm.
/// \param kuratowski If this parameter is set to \c false, then the
/// algorithm does not compute a Kuratowski subdivision.
/// \return \c true if the graph is planar.
bool run(bool kuratowski = true) {
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
PredMap pred_map(_graph, INVALID);
TreeMap tree_map(_graph, false);
OrderMap order_map(_graph, -1);
OrderList order_list;
AncestorMap ancestor_map(_graph, -1);
LowMap low_map(_graph, -1);
Visitor visitor(_graph, pred_map, tree_map,
order_map, order_list, ancestor_map, low_map);
DfsVisit<Graph, Visitor> visit(_graph, visitor);
visit.run();
ChildLists child_lists(_graph);
createChildLists(tree_map, order_map, low_map, child_lists);
NodeData node_data(2 * order_list.size());
EmbedArc embed_arc(_graph, INVALID);
MergeRoots merge_roots(_graph);
ArcLists arc_lists(_graph);
FlipMap flip_map(_graph, false);
for (int i = order_list.size() - 1; i >= 0; --i) {
Node node = order_list[i];
node_data[i].first = INVALID;
Node source = node;
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && tree_map[e]) {
initFace(target, arc_lists, node_data,
pred_map, order_map, order_list);
}
}
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && !tree_map[e]) {
embed_arc[target] = e;
walkUp(target, source, i, pred_map, low_map,
order_map, order_list, node_data, merge_roots);
}
}
for (typename MergeRoots::Value::iterator it =
merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
int rn = *it;
walkDown(rn, i, node_data, arc_lists, flip_map, order_list,
child_lists, ancestor_map, low_map, embed_arc, merge_roots);
}
merge_roots[node].clear();
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && !tree_map[e]) {
if (embed_arc[target] != INVALID) {
if (kuratowski) {
isolateKuratowski(e, node_data, arc_lists, flip_map,
order_map, order_list, pred_map, child_lists,
ancestor_map, low_map,
embed_arc, merge_roots);
}
return false;
}
}
}
}
for (int i = 0; i < int(order_list.size()); ++i) {
mergeRemainingFaces(order_list[i], node_data, order_list, order_map,
child_lists, arc_lists);
storeEmbedding(order_list[i], node_data, order_map, pred_map,
arc_lists, flip_map);
}
return true;
}
/// \brief Give back the successor of an arc
///
/// This function gives back the successor of an arc. It makes
/// possible to query the cyclic order of the outgoing arcs from
/// a node.
Arc next(const Arc& arc) const {
return _embedding[arc];
}
/// \brief Give back the calculated embedding map
///
/// This function gives back the calculated embedding map, which
/// contains the successor of each arc in the cyclic order of the
/// outgoing arcs of its source node.
const EmbeddingMap& embeddingMap() const {
return _embedding;
}
/// \brief Give back \c true if the given edge is in the Kuratowski
/// subdivision
///
/// This function gives back \c true if the given edge is in the found
/// Kuratowski subdivision.
/// \pre The \c run() function must be called with \c true parameter
/// before using this function.
bool kuratowski(const Edge& edge) const {
return _kuratowski[edge];
}
private:
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
const LowMap& low_map, ChildLists& child_lists) {
for (NodeIt n(_graph); n != INVALID; ++n) {
Node source = n;
std::vector<Node> targets;
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Node target = _graph.target(e);
if (order_map[source] < order_map[target] && tree_map[e]) {
targets.push_back(target);
}
}
if (targets.size() == 0) {
child_lists[source].first = INVALID;
} else if (targets.size() == 1) {
child_lists[source].first = targets[0];
child_lists[targets[0]].prev = INVALID;
child_lists[targets[0]].next = INVALID;
} else {
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
for (int i = 1; i < int(targets.size()); ++i) {
child_lists[targets[i]].prev = targets[i - 1];
child_lists[targets[i - 1]].next = targets[i];
}
child_lists[targets.back()].next = INVALID;
child_lists[targets.front()].prev = INVALID;
child_lists[source].first = targets.front();
}
}
}
void walkUp(const Node& node, Node root, int rorder,
const PredMap& pred_map, const LowMap& low_map,
const OrderMap& order_map, const OrderList& order_list,
NodeData& node_data, MergeRoots& merge_roots) {
int na, nb;
bool da, db;
na = nb = order_map[node];
da = true; db = false;
while (true) {
if (node_data[na].visited == rorder) break;
if (node_data[nb].visited == rorder) break;
node_data[na].visited = rorder;
node_data[nb].visited = rorder;
int rn = -1;
if (na >= int(order_list.size())) {
rn = na;
} else if (nb >= int(order_list.size())) {
rn = nb;
}
if (rn == -1) {
int nn;
nn = da ? node_data[na].prev : node_data[na].next;
da = node_data[nn].prev != na;
na = nn;
nn = db ? node_data[nb].prev : node_data[nb].next;
db = node_data[nn].prev != nb;
nb = nn;
} else {
Node rep = order_list[rn - order_list.size()];
Node parent = _graph.source(pred_map[rep]);
if (low_map[rep] < rorder) {
merge_roots[parent].push_back(rn);
} else {
merge_roots[parent].push_front(rn);
}
if (parent != root) {
na = nb = order_map[parent];
da = true; db = false;
} else {
break;
}
}
}
}
void walkDown(int rn, int rorder, NodeData& node_data,
ArcLists& arc_lists, FlipMap& flip_map,
OrderList& order_list, ChildLists& child_lists,
AncestorMap& ancestor_map, LowMap& low_map,
EmbedArc& embed_arc, MergeRoots& merge_roots) {
std::vector<std::pair<int, bool> > merge_stack;
for (int di = 0; di < 2; ++di) {
bool rd = di == 0;
int pn = rn;
int n = rd ? node_data[rn].next : node_data[rn].prev;
while (n != rn) {
Node node = order_list[n];
if (embed_arc[node] != INVALID) {
// Merging components on the critical path
while (!merge_stack.empty()) {
// Component root
int cn = merge_stack.back().first;
bool cd = merge_stack.back().second;
merge_stack.pop_back();
// Parent of component
int dn = merge_stack.back().first;
bool dd = merge_stack.back().second;
merge_stack.pop_back();
Node parent = order_list[dn];
// Erasing from merge_roots
merge_roots[parent].pop_front();
Node child = order_list[cn - order_list.size()];
// Erasing from child_lists
if (child_lists[child].prev != INVALID) {
child_lists[child_lists[child].prev].next =
child_lists[child].next;
} else {
child_lists[parent].first = child_lists[child].next;
}
if (child_lists[child].next != INVALID) {
child_lists[child_lists[child].next].prev =
child_lists[child].prev;
}
// Merging arcs + flipping
Arc de = node_data[dn].first;
Arc ce = node_data[cn].first;
flip_map[order_list[cn - order_list.size()]] = cd != dd;
if (cd != dd) {
std::swap(arc_lists[ce].prev, arc_lists[ce].next);
ce = arc_lists[ce].prev;
std::swap(arc_lists[ce].prev, arc_lists[ce].next);
}
{
Arc dne = arc_lists[de].next;
Arc cne = arc_lists[ce].next;
arc_lists[de].next = cne;
arc_lists[ce].next = dne;
arc_lists[dne].prev = ce;
arc_lists[cne].prev = de;
}
if (dd) {
node_data[dn].first = ce;
}
// Merging external faces
{
int en = cn;
cn = cd ? node_data[cn].prev : node_data[cn].next;
cd = node_data[cn].next == en;
if (node_data[cn].prev == node_data[cn].next &&
node_data[cn].inverted) {
cd = !cd;
}
}
if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
}
bool d = pn == node_data[n].prev;
if (node_data[n].prev == node_data[n].next &&
node_data[n].inverted) {
d = !d;
}
// Add new arc
{
Arc arc = embed_arc[node];
Arc re = node_data[rn].first;
arc_lists[arc_lists[re].next].prev = arc;
arc_lists[arc].next = arc_lists[re].next;
arc_lists[arc].prev = re;
arc_lists[re].next = arc;
if (!rd) {
node_data[rn].first = arc;
}
Arc rev = _graph.oppositeArc(arc);
Arc e = node_data[n].first;
arc_lists[arc_lists[e].next].prev = rev;
arc_lists[rev].next = arc_lists[e].next;
arc_lists[rev].prev = e;
arc_lists[e].next = rev;
if (d) {
node_data[n].first = rev;
}
}
// Embedding arc into external face
if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
if (d) node_data[n].prev = rn; else node_data[n].next = rn;
pn = rn;
embed_arc[order_list[n]] = INVALID;
}
if (!merge_roots[node].empty()) {
bool d = pn == node_data[n].prev;
if (node_data[n].prev == node_data[n].next &&
node_data[n].inverted) {
d = !d;
}
merge_stack.push_back(std::make_pair(n, d));
int rn = merge_roots[node].front();
int xn = node_data[rn].next;
Node xnode = order_list[xn];
int yn = node_data[rn].prev;
Node ynode = order_list[yn];
bool rd;
if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
rd = true;
} else if (!external(ynode, rorder, child_lists,
ancestor_map, low_map)) {
rd = false;
} else if (pertinent(xnode, embed_arc, merge_roots)) {
rd = true;
} else {
rd = false;
}
merge_stack.push_back(std::make_pair(rn, rd));
pn = rn;
n = rd ? xn : yn;
} else if (!external(node, rorder, child_lists,
ancestor_map, low_map)) {
int nn = (node_data[n].next != pn ?
node_data[n].next : node_data[n].prev);
bool nd = n == node_data[nn].prev;
if (nd) node_data[nn].prev = pn;
else node_data[nn].next = pn;
if (n == node_data[pn].prev) node_data[pn].prev = nn;
else node_data[pn].next = nn;
node_data[nn].inverted =
(node_data[nn].prev == node_data[nn].next && nd != rd);
n = nn;
}
else break;
}
if (!merge_stack.empty() || n == rn) {
break;
}
}
}
void initFace(const Node& node, ArcLists& arc_lists,
NodeData& node_data, const PredMap& pred_map,
const OrderMap& order_map, const OrderList& order_list) {
int n = order_map[node];
int rn = n + order_list.size();
node_data[n].next = node_data[n].prev = rn;
node_data[rn].next = node_data[rn].prev = n;
node_data[n].visited = order_list.size();
node_data[rn].visited = order_list.size();
node_data[n].inverted = false;
node_data[rn].inverted = false;
Arc arc = pred_map[node];
Arc rev = _graph.oppositeArc(arc);
node_data[rn].first = arc;
node_data[n].first = rev;
arc_lists[arc].prev = arc;
arc_lists[arc].next = arc;
arc_lists[rev].prev = rev;
arc_lists[rev].next = rev;
}
void mergeRemainingFaces(const Node& node, NodeData& node_data,
OrderList& order_list, OrderMap& order_map,
ChildLists& child_lists, ArcLists& arc_lists) {
while (child_lists[node].first != INVALID) {
int dd = order_map[node];
Node child = child_lists[node].first;
int cd = order_map[child] + order_list.size();
child_lists[node].first = child_lists[child].next;
Arc de = node_data[dd].first;
Arc ce = node_data[cd].first;
if (de != INVALID) {
Arc dne = arc_lists[de].next;
Arc cne = arc_lists[ce].next;
arc_lists[de].next = cne;
arc_lists[ce].next = dne;
arc_lists[dne].prev = ce;
arc_lists[cne].prev = de;
}
node_data[dd].first = ce;
}
}
void storeEmbedding(const Node& node, NodeData& node_data,
OrderMap& order_map, PredMap& pred_map,
ArcLists& arc_lists, FlipMap& flip_map) {
if (node_data[order_map[node]].first == INVALID) return;
if (pred_map[node] != INVALID) {
Node source = _graph.source(pred_map[node]);
flip_map[node] = flip_map[node] != flip_map[source];
}
Arc first = node_data[order_map[node]].first;
Arc prev = first;
Arc arc = flip_map[node] ?
arc_lists[prev].prev : arc_lists[prev].next;
_embedding[prev] = arc;
while (arc != first) {
Arc next = arc_lists[arc].prev == prev ?
arc_lists[arc].next : arc_lists[arc].prev;
prev = arc; arc = next;
_embedding[prev] = arc;
}
}
bool external(const Node& node, int rorder,
ChildLists& child_lists, AncestorMap& ancestor_map,
LowMap& low_map) {
Node child = child_lists[node].first;
if (child != INVALID) {
if (low_map[child] < rorder) return true;
}
if (ancestor_map[node] < rorder) return true;
return false;
}
bool pertinent(const Node& node, const EmbedArc& embed_arc,
const MergeRoots& merge_roots) {
return !merge_roots[node].empty() || embed_arc[node] != INVALID;
}
int lowPoint(const Node& node, OrderMap& order_map, ChildLists& child_lists,
AncestorMap& ancestor_map, LowMap& low_map) {
int low_point;
Node child = child_lists[node].first;
if (child != INVALID) {
low_point = low_map[child];
} else {
low_point = order_map[node];
}
if (low_point > ancestor_map[node]) {
low_point = ancestor_map[node];
}
return low_point;
}
int findComponentRoot(Node root, Node node, ChildLists& child_lists,
OrderMap& order_map, OrderList& order_list) {
int order = order_map[root];
int norder = order_map[node];
Node child = child_lists[root].first;
while (child != INVALID) {
int corder = order_map[child];
if (corder > order && corder < norder) {
order = corder;
}
child = child_lists[child].next;
}
return order + order_list.size();
}
Node findPertinent(Node node, OrderMap& order_map, NodeData& node_data,
EmbedArc& embed_arc, MergeRoots& merge_roots) {
Node wnode =_graph.target(node_data[order_map[node]].first);
while (!pertinent(wnode, embed_arc, merge_roots)) {
wnode = _graph.target(node_data[order_map[wnode]].first);
}
return wnode;
}
Node findExternal(Node node, int rorder, OrderMap& order_map,
ChildLists& child_lists, AncestorMap& ancestor_map,
LowMap& low_map, NodeData& node_data) {
Node wnode =_graph.target(node_data[order_map[node]].first);
while (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
wnode = _graph.target(node_data[order_map[wnode]].first);
}
return wnode;
}
void markCommonPath(Node node, int rorder, Node& wnode, Node& znode,
OrderList& order_list, OrderMap& order_map,
NodeData& node_data, ArcLists& arc_lists,
EmbedArc& embed_arc, MergeRoots& merge_roots,
ChildLists& child_lists, AncestorMap& ancestor_map,
LowMap& low_map) {
Node cnode = node;
Node pred = INVALID;
while (true) {
bool pert = pertinent(cnode, embed_arc, merge_roots);
bool ext = external(cnode, rorder, child_lists, ancestor_map, low_map);
if (pert && ext) {
if (!merge_roots[cnode].empty()) {
int cn = merge_roots[cnode].back();
if (low_map[order_list[cn - order_list.size()]] < rorder) {
Arc arc = node_data[cn].first;
_kuratowski.set(arc, true);
pred = cnode;
cnode = _graph.target(arc);
continue;
}
}
wnode = znode = cnode;
return;
} else if (pert) {
wnode = cnode;
while (!external(cnode, rorder, child_lists, ancestor_map, low_map)) {
Arc arc = node_data[order_map[cnode]].first;
if (_graph.target(arc) == pred) {
arc = arc_lists[arc].next;
}
_kuratowski.set(arc, true);
Node next = _graph.target(arc);
pred = cnode; cnode = next;
}
znode = cnode;
return;
} else if (ext) {
znode = cnode;
while (!pertinent(cnode, embed_arc, merge_roots)) {
Arc arc = node_data[order_map[cnode]].first;
if (_graph.target(arc) == pred) {
arc = arc_lists[arc].next;
}
_kuratowski.set(arc, true);
Node next = _graph.target(arc);
pred = cnode; cnode = next;
}
wnode = cnode;
return;
} else {
Arc arc = node_data[order_map[cnode]].first;
if (_graph.target(arc) == pred) {
arc = arc_lists[arc].next;
}
_kuratowski.set(arc, true);
Node next = _graph.target(arc);
pred = cnode; cnode = next;
}
}
}
void orientComponent(Node root, int rn, OrderMap& order_map,
PredMap& pred_map, NodeData& node_data,
ArcLists& arc_lists, FlipMap& flip_map,
TypeMap& type_map) {
node_data[order_map[root]].first = node_data[rn].first;
type_map[root] = 1;
std::vector<Node> st, qu;
st.push_back(root);
while (!st.empty()) {
Node node = st.back();
st.pop_back();
qu.push_back(node);
Arc arc = node_data[order_map[node]].first;
if (type_map[_graph.target(arc)] == 0) {
st.push_back(_graph.target(arc));
type_map[_graph.target(arc)] = 1;
}
Arc last = arc, pred = arc;
arc = arc_lists[arc].next;
while (arc != last) {
if (type_map[_graph.target(arc)] == 0) {
st.push_back(_graph.target(arc));
type_map[_graph.target(arc)] = 1;
}
Arc next = arc_lists[arc].next != pred ?
arc_lists[arc].next : arc_lists[arc].prev;
pred = arc; arc = next;
}
}
type_map[root] = 2;
flip_map[root] = false;
for (int i = 1; i < int(qu.size()); ++i) {
Node node = qu[i];
while (type_map[node] != 2) {
st.push_back(node);
type_map[node] = 2;
node = _graph.source(pred_map[node]);
}
bool flip = flip_map[node];
while (!st.empty()) {
node = st.back();
st.pop_back();
flip_map[node] = flip != flip_map[node];
flip = flip_map[node];
if (flip) {
Arc arc = node_data[order_map[node]].first;
std::swap(arc_lists[arc].prev, arc_lists[arc].next);
arc = arc_lists[arc].prev;
std::swap(arc_lists[arc].prev, arc_lists[arc].next);
node_data[order_map[node]].first = arc;
}
}
}
for (int i = 0; i < int(qu.size()); ++i) {
Arc arc = node_data[order_map[qu[i]]].first;
Arc last = arc, pred = arc;
arc = arc_lists[arc].next;
while (arc != last) {
if (arc_lists[arc].next == pred) {
std::swap(arc_lists[arc].next, arc_lists[arc].prev);
}
pred = arc; arc = arc_lists[arc].next;
}
}
}
void setFaceFlags(Node root, Node wnode, Node ynode, Node xnode,
OrderMap& order_map, NodeData& node_data,
TypeMap& type_map) {
Node node = _graph.target(node_data[order_map[root]].first);
while (node != ynode) {
type_map[node] = HIGHY;
node = _graph.target(node_data[order_map[node]].first);
}
while (node != wnode) {
type_map[node] = LOWY;
node = _graph.target(node_data[order_map[node]].first);
}
node = _graph.target(node_data[order_map[wnode]].first);
while (node != xnode) {
type_map[node] = LOWX;
node = _graph.target(node_data[order_map[node]].first);
}
type_map[node] = LOWX;
node = _graph.target(node_data[order_map[xnode]].first);
while (node != root) {
type_map[node] = HIGHX;
node = _graph.target(node_data[order_map[node]].first);
}
type_map[wnode] = PERTINENT;
type_map[root] = ROOT;
}
void findInternalPath(std::vector<Arc>& ipath,
Node wnode, Node root, TypeMap& type_map,
OrderMap& order_map, NodeData& node_data,
ArcLists& arc_lists) {
std::vector<Arc> st;
Node node = wnode;
while (node != root) {
Arc arc = arc_lists[node_data[order_map[node]].first].next;
st.push_back(arc);
node = _graph.target(arc);
}
while (true) {
Arc arc = st.back();
if (type_map[_graph.target(arc)] == LOWX ||
type_map[_graph.target(arc)] == HIGHX) {
break;
}
if (type_map[_graph.target(arc)] == 2) {
type_map[_graph.target(arc)] = 3;
arc = arc_lists[_graph.oppositeArc(arc)].next;
st.push_back(arc);
} else {
st.pop_back();
arc = arc_lists[arc].next;
while (_graph.oppositeArc(arc) == st.back()) {
arc = st.back();
st.pop_back();
arc = arc_lists[arc].next;
}
st.push_back(arc);
}
}
for (int i = 0; i < int(st.size()); ++i) {
if (type_map[_graph.target(st[i])] != LOWY &&
type_map[_graph.target(st[i])] != HIGHY) {
for (; i < int(st.size()); ++i) {
ipath.push_back(st[i]);
}
}
}
}
void setInternalFlags(std::vector<Arc>& ipath, TypeMap& type_map) {
for (int i = 1; i < int(ipath.size()); ++i) {
type_map[_graph.source(ipath[i])] = INTERNAL;
}
}
void findPilePath(std::vector<Arc>& ppath,
Node root, TypeMap& type_map, OrderMap& order_map,
NodeData& node_data, ArcLists& arc_lists) {
std::vector<Arc> st;
st.push_back(_graph.oppositeArc(node_data[order_map[root]].first));
st.push_back(node_data[order_map[root]].first);
while (st.size() > 1) {
Arc arc = st.back();
if (type_map[_graph.target(arc)] == INTERNAL) {
break;
}
if (type_map[_graph.target(arc)] == 3) {
type_map[_graph.target(arc)] = 4;
arc = arc_lists[_graph.oppositeArc(arc)].next;
st.push_back(arc);
} else {
st.pop_back();
arc = arc_lists[arc].next;
while (!st.empty() && _graph.oppositeArc(arc) == st.back()) {
arc = st.back();
st.pop_back();
arc = arc_lists[arc].next;
}
st.push_back(arc);
}
}
for (int i = 1; i < int(st.size()); ++i) {
ppath.push_back(st[i]);
}
}
int markExternalPath(Node node, OrderMap& order_map,
ChildLists& child_lists, PredMap& pred_map,
AncestorMap& ancestor_map, LowMap& low_map) {
int lp = lowPoint(node, order_map, child_lists,
ancestor_map, low_map);
if (ancestor_map[node] != lp) {
node = child_lists[node].first;
_kuratowski[pred_map[node]] = true;
while (ancestor_map[node] != lp) {
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
Node tnode = _graph.target(e);
if (order_map[tnode] > order_map[node] && low_map[tnode] == lp) {
node = tnode;
_kuratowski[e] = true;
break;
}
}
}
}
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
if (order_map[_graph.target(e)] == lp) {
_kuratowski[e] = true;
break;
}
}
return lp;
}
void markPertinentPath(Node node, OrderMap& order_map,
NodeData& node_data, ArcLists& arc_lists,
EmbedArc& embed_arc, MergeRoots& merge_roots) {
while (embed_arc[node] == INVALID) {
int n = merge_roots[node].front();
Arc arc = node_data[n].first;
_kuratowski.set(arc, true);
Node pred = node;
node = _graph.target(arc);
while (!pertinent(node, embed_arc, merge_roots)) {
arc = node_data[order_map[node]].first;
if (_graph.target(arc) == pred) {
arc = arc_lists[arc].next;
}
_kuratowski.set(arc, true);
pred = node;
node = _graph.target(arc);
}
}
_kuratowski.set(embed_arc[node], true);
}
void markPredPath(Node node, Node snode, PredMap& pred_map) {
while (node != snode) {
_kuratowski.set(pred_map[node], true);
node = _graph.source(pred_map[node]);
}
}
void markFacePath(Node ynode, Node xnode,
OrderMap& order_map, NodeData& node_data) {
Arc arc = node_data[order_map[ynode]].first;
Node node = _graph.target(arc);
_kuratowski.set(arc, true);
while (node != xnode) {
arc = node_data[order_map[node]].first;
_kuratowski.set(arc, true);
node = _graph.target(arc);
}
}
void markInternalPath(std::vector<Arc>& path) {
for (int i = 0; i < int(path.size()); ++i) {
_kuratowski.set(path[i], true);
}
}
void markPilePath(std::vector<Arc>& path) {
for (int i = 0; i < int(path.size()); ++i) {
_kuratowski.set(path[i], true);
}
}
void isolateKuratowski(Arc arc, NodeData& node_data,
ArcLists& arc_lists, FlipMap& flip_map,
OrderMap& order_map, OrderList& order_list,
PredMap& pred_map, ChildLists& child_lists,
AncestorMap& ancestor_map, LowMap& low_map,
EmbedArc& embed_arc, MergeRoots& merge_roots) {
Node root = _graph.source(arc);
Node enode = _graph.target(arc);
int rorder = order_map[root];
TypeMap type_map(_graph, 0);
int rn = findComponentRoot(root, enode, child_lists,
order_map, order_list);
Node xnode = order_list[node_data[rn].next];
Node ynode = order_list[node_data[rn].prev];
// Minor-A
{
while (!merge_roots[xnode].empty() || !merge_roots[ynode].empty()) {
if (!merge_roots[xnode].empty()) {
root = xnode;
rn = merge_roots[xnode].front();
} else {
root = ynode;
rn = merge_roots[ynode].front();
}
xnode = order_list[node_data[rn].next];
ynode = order_list[node_data[rn].prev];
}
if (root != _graph.source(arc)) {
orientComponent(root, rn, order_map, pred_map,
node_data, arc_lists, flip_map, type_map);
markFacePath(root, root, order_map, node_data);
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
Node lwnode = findPertinent(ynode, order_map, node_data,
embed_arc, merge_roots);
markPertinentPath(lwnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
return;
}
}
orientComponent(root, rn, order_map, pred_map,
node_data, arc_lists, flip_map, type_map);
Node wnode = findPertinent(ynode, order_map, node_data,
embed_arc, merge_roots);
setFaceFlags(root, wnode, ynode, xnode, order_map, node_data, type_map);
//Minor-B
if (!merge_roots[wnode].empty()) {
int cn = merge_roots[wnode].back();
Node rep = order_list[cn - order_list.size()];
if (low_map[rep] < rorder) {
markFacePath(root, root, order_map, node_data);
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
Node lwnode, lznode;
markCommonPath(wnode, rorder, lwnode, lznode, order_list,
order_map, node_data, arc_lists, embed_arc,
merge_roots, child_lists, ancestor_map, low_map);
markPertinentPath(lwnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
int zlp = markExternalPath(lznode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int minlp = xlp < ylp ? xlp : ylp;
if (zlp < minlp) minlp = zlp;
int maxlp = xlp > ylp ? xlp : ylp;
if (zlp > maxlp) maxlp = zlp;
markPredPath(order_list[maxlp], order_list[minlp], pred_map);
return;
}
}
Node pxnode, pynode;
std::vector<Arc> ipath;
findInternalPath(ipath, wnode, root, type_map, order_map,
node_data, arc_lists);
setInternalFlags(ipath, type_map);
pynode = _graph.source(ipath.front());
pxnode = _graph.target(ipath.back());
wnode = findPertinent(pynode, order_map, node_data,
embed_arc, merge_roots);
// Minor-C
{
if (type_map[_graph.source(ipath.front())] == HIGHY) {
if (type_map[_graph.target(ipath.back())] == HIGHX) {
markFacePath(xnode, pxnode, order_map, node_data);
}
markFacePath(root, xnode, order_map, node_data);
markPertinentPath(wnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
markInternalPath(ipath);
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
return;
}
if (type_map[_graph.target(ipath.back())] == HIGHX) {
markFacePath(ynode, root, order_map, node_data);
markPertinentPath(wnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
markInternalPath(ipath);
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
return;
}
}
std::vector<Arc> ppath;
findPilePath(ppath, root, type_map, order_map, node_data, arc_lists);
// Minor-D
if (!ppath.empty()) {
markFacePath(ynode, xnode, order_map, node_data);
markPertinentPath(wnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
markPilePath(ppath);
markInternalPath(ipath);
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
return;
}
// Minor-E*
{
if (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
Node znode = findExternal(pynode, rorder, order_map,
child_lists, ancestor_map,
low_map, node_data);
if (type_map[znode] == LOWY) {
markFacePath(root, xnode, order_map, node_data);
markPertinentPath(wnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
markInternalPath(ipath);
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int zlp = markExternalPath(znode, order_map, child_lists,
pred_map, ancestor_map, low_map);
markPredPath(root, order_list[xlp < zlp ? xlp : zlp], pred_map);
} else {
markFacePath(ynode, root, order_map, node_data);
markPertinentPath(wnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
markInternalPath(ipath);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int zlp = markExternalPath(znode, order_map, child_lists,
pred_map, ancestor_map, low_map);
markPredPath(root, order_list[ylp < zlp ? ylp : zlp], pred_map);
}
return;
}
int xlp = markExternalPath(xnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int ylp = markExternalPath(ynode, order_map, child_lists,
pred_map, ancestor_map, low_map);
int wlp = markExternalPath(wnode, order_map, child_lists,
pred_map, ancestor_map, low_map);
if (wlp > xlp && wlp > ylp) {
markFacePath(root, root, order_map, node_data);
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
return;
}
markInternalPath(ipath);
markPertinentPath(wnode, order_map, node_data, arc_lists,
embed_arc, merge_roots);
if (xlp > ylp && xlp > wlp) {
markFacePath(root, pynode, order_map, node_data);
markFacePath(wnode, xnode, order_map, node_data);
markPredPath(root, order_list[ylp < wlp ? ylp : wlp], pred_map);
return;
}
if (ylp > xlp && ylp > wlp) {
markFacePath(pxnode, root, order_map, node_data);
markFacePath(ynode, wnode, order_map, node_data);
markPredPath(root, order_list[xlp < wlp ? xlp : wlp], pred_map);
return;
}
if (pynode != ynode) {
markFacePath(pxnode, wnode, order_map, node_data);
int minlp = xlp < ylp ? xlp : ylp;
if (wlp < minlp) minlp = wlp;
int maxlp = xlp > ylp ? xlp : ylp;
if (wlp > maxlp) maxlp = wlp;
markPredPath(order_list[maxlp], order_list[minlp], pred_map);
return;
}
if (pxnode != xnode) {
markFacePath(wnode, pynode, order_map, node_data);
int minlp = xlp < ylp ? xlp : ylp;
if (wlp < minlp) minlp = wlp;
int maxlp = xlp > ylp ? xlp : ylp;
if (wlp > maxlp) maxlp = wlp;
markPredPath(order_list[maxlp], order_list[minlp], pred_map);
return;
}
markFacePath(root, root, order_map, node_data);
int minlp = xlp < ylp ? xlp : ylp;
if (wlp < minlp) minlp = wlp;
markPredPath(root, order_list[minlp], pred_map);
return;
}
}
};
namespace _planarity_bits {
template <typename Graph, typename EmbeddingMap>
void makeConnected(Graph& graph, EmbeddingMap& embedding) {
DfsVisitor<Graph> null_visitor;
DfsVisit<Graph, DfsVisitor<Graph> > dfs(graph, null_visitor);
dfs.init();
typename Graph::Node u = INVALID;
for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
if (!dfs.reached(n)) {
dfs.addSource(n);
dfs.start();
if (u == INVALID) {
u = n;
} else {
typename Graph::Node v = n;
typename Graph::Arc ue = typename Graph::OutArcIt(graph, u);
typename Graph::Arc ve = typename Graph::OutArcIt(graph, v);
typename Graph::Arc e = graph.direct(graph.addEdge(u, v), true);
if (ue != INVALID) {
embedding[e] = embedding[ue];
embedding[ue] = e;
} else {
embedding[e] = e;
}
if (ve != INVALID) {
embedding[graph.oppositeArc(e)] = embedding[ve];
embedding[ve] = graph.oppositeArc(e);
} else {
embedding[graph.oppositeArc(e)] = graph.oppositeArc(e);
}
}
}
}
}
template <typename Graph, typename EmbeddingMap>
void makeBiNodeConnected(Graph& graph, EmbeddingMap& embedding) {
typename Graph::template ArcMap<bool> processed(graph);
std::vector<typename Graph::Arc> arcs;
for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
arcs.push_back(e);
}
IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
for (int i = 0; i < int(arcs.size()); ++i) {
typename Graph::Arc pp = arcs[i];
if (processed[pp]) continue;
typename Graph::Arc e = embedding[graph.oppositeArc(pp)];
processed[e] = true;
visited.set(graph.source(e), true);
typename Graph::Arc p = e, l = e;
e = embedding[graph.oppositeArc(e)];
while (e != l) {
processed[e] = true;
if (visited[graph.source(e)]) {
typename Graph::Arc n =
graph.direct(graph.addEdge(graph.source(p),
graph.target(e)), true);
embedding[n] = p;
embedding[graph.oppositeArc(pp)] = n;
embedding[graph.oppositeArc(n)] =
embedding[graph.oppositeArc(e)];
embedding[graph.oppositeArc(e)] =
graph.oppositeArc(n);
p = n;
e = embedding[graph.oppositeArc(n)];
} else {
visited.set(graph.source(e), true);
pp = p;
p = e;
e = embedding[graph.oppositeArc(e)];
}
}
visited.setAll(false);
}
}
template <typename Graph, typename EmbeddingMap>
void makeMaxPlanar(Graph& graph, EmbeddingMap& embedding) {
typename Graph::template NodeMap<int> degree(graph);
for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
degree[n] = countIncEdges(graph, n);
}
typename Graph::template ArcMap<bool> processed(graph);
IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
std::vector<typename Graph::Arc> arcs;
for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
arcs.push_back(e);
}
for (int i = 0; i < int(arcs.size()); ++i) {
typename Graph::Arc e = arcs[i];
if (processed[e]) continue;
processed[e] = true;
typename Graph::Arc mine = e;
int mind = degree[graph.source(e)];
int face_size = 1;
typename Graph::Arc l = e;
e = embedding[graph.oppositeArc(e)];
while (l != e) {
processed[e] = true;
++face_size;
if (degree[graph.source(e)] < mind) {
mine = e;
mind = degree[graph.source(e)];
}
e = embedding[graph.oppositeArc(e)];
}
if (face_size < 4) {
continue;
}
typename Graph::Node s = graph.source(mine);
for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) {
visited.set(graph.target(e), true);
}
typename Graph::Arc oppe = INVALID;
e = embedding[graph.oppositeArc(mine)];
e = embedding[graph.oppositeArc(e)];
while (graph.target(e) != s) {
if (visited[graph.source(e)]) {
oppe = e;
break;
}
e = embedding[graph.oppositeArc(e)];
}
visited.setAll(false);
if (oppe == INVALID) {
e = embedding[graph.oppositeArc(mine)];
typename Graph::Arc pn = mine, p = e;
e = embedding[graph.oppositeArc(e)];
while (graph.target(e) != s) {
typename Graph::Arc n =
graph.direct(graph.addEdge(s, graph.source(e)), true);
embedding[n] = pn;
embedding[graph.oppositeArc(n)] = e;
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
pn = n;
p = e;
e = embedding[graph.oppositeArc(e)];
}
embedding[graph.oppositeArc(e)] = pn;
} else {
mine = embedding[graph.oppositeArc(mine)];
s = graph.source(mine);
oppe = embedding[graph.oppositeArc(oppe)];
typename Graph::Node t = graph.source(oppe);
typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true);
embedding[ce] = mine;
embedding[graph.oppositeArc(ce)] = oppe;
typename Graph::Arc pn = ce, p = oppe;
e = embedding[graph.oppositeArc(oppe)];
while (graph.target(e) != s) {
typename Graph::Arc n =
graph.direct(graph.addEdge(s, graph.source(e)), true);
embedding[n] = pn;
embedding[graph.oppositeArc(n)] = e;
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
pn = n;
p = e;
e = embedding[graph.oppositeArc(e)];
}
embedding[graph.oppositeArc(e)] = pn;
pn = graph.oppositeArc(ce), p = mine;
e = embedding[graph.oppositeArc(mine)];
while (graph.target(e) != t) {
typename Graph::Arc n =
graph.direct(graph.addEdge(t, graph.source(e)), true);
embedding[n] = pn;
embedding[graph.oppositeArc(n)] = e;
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
pn = n;
p = e;
e = embedding[graph.oppositeArc(e)];
}
embedding[graph.oppositeArc(e)] = pn;
}
}
}
}
/// \ingroup planar
///
/// \brief Schnyder's planar drawing algorithm
///
/// The planar drawing algorithm calculates positions for the nodes
/// in the plane. These coordinates satisfy that if the edges are
/// represented with straight lines, then they will not intersect
/// each other.
///
/// Scnyder's algorithm embeds the graph on an \c (n-2)x(n-2) size grid,
/// i.e. each node will be located in the \c [0..n-2]x[0..n-2] square.
/// The time complexity of the algorithm is O(n).
///
/// \see PlanarEmbedding
template <typename Graph>
class PlanarDrawing {
public:
TEMPLATE_GRAPH_TYPEDEFS(Graph);
/// \brief The point type for storing coordinates
typedef dim2::Point<int> Point;
/// \brief The map type for storing the coordinates of the nodes
typedef typename Graph::template NodeMap<Point> PointMap;
/// \brief Constructor
///
/// Constructor
/// \pre The graph must be simple, i.e. it should not
/// contain parallel or loop arcs.
PlanarDrawing(const Graph& graph)
: _graph(graph), _point_map(graph) {}
private:
template <typename AuxGraph, typename AuxEmbeddingMap>
void drawing(const AuxGraph& graph,
const AuxEmbeddingMap& next,
PointMap& point_map) {
TEMPLATE_GRAPH_TYPEDEFS(AuxGraph);
typename AuxGraph::template ArcMap<Arc> prev(graph);
for (NodeIt n(graph); n != INVALID; ++n) {
Arc e = OutArcIt(graph, n);
Arc p = e, l = e;
e = next[e];
while (e != l) {
prev[e] = p;
p = e;
e = next[e];
}
prev[e] = p;
}
Node anode, bnode, cnode;
{
Arc e = ArcIt(graph);
anode = graph.source(e);
bnode = graph.target(e);
cnode = graph.target(next[graph.oppositeArc(e)]);
}
IterableBoolMap<AuxGraph, Node> proper(graph, false);
typename AuxGraph::template NodeMap<int> conn(graph, -1);
conn[anode] = conn[bnode] = -2;
{
for (OutArcIt e(graph, anode); e != INVALID; ++e) {
Node m = graph.target(e);
if (conn[m] == -1) {
conn[m] = 1;
}
}
conn[cnode] = 2;
for (OutArcIt e(graph, bnode); e != INVALID; ++e) {
Node m = graph.target(e);
if (conn[m] == -1) {
conn[m] = 1;
} else if (conn[m] != -2) {
conn[m] += 1;
Arc pe = graph.oppositeArc(e);
if (conn[graph.target(next[pe])] == -2) {
conn[m] -= 1;
}
if (conn[graph.target(prev[pe])] == -2) {
conn[m] -= 1;
}
proper.set(m, conn[m] == 1);
}
}
}
typename AuxGraph::template ArcMap<int> angle(graph, -1);
while (proper.trueNum() != 0) {
Node n = typename IterableBoolMap<AuxGraph, Node>::TrueIt(proper);
proper.set(n, false);
conn[n] = -2;
for (OutArcIt e(graph, n); e != INVALID; ++e) {
Node m = graph.target(e);
if (conn[m] == -1) {
conn[m] = 1;
} else if (conn[m] != -2) {
conn[m] += 1;
Arc pe = graph.oppositeArc(e);
if (conn[graph.target(next[pe])] == -2) {
conn[m] -= 1;
}
if (conn[graph.target(prev[pe])] == -2) {
conn[m] -= 1;
}
proper.set(m, conn[m] == 1);
}
}
{
Arc e = OutArcIt(graph, n);
Arc p = e, l = e;
e = next[e];
while (e != l) {
if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
Arc f = e;
angle[f] = 0;
f = next[graph.oppositeArc(f)];
angle[f] = 1;
f = next[graph.oppositeArc(f)];
angle[f] = 2;
}
p = e;
e = next[e];
}
if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
Arc f = e;
angle[f] = 0;
f = next[graph.oppositeArc(f)];
angle[f] = 1;
f = next[graph.oppositeArc(f)];
angle[f] = 2;
}
}
}
typename AuxGraph::template NodeMap<Node> apred(graph, INVALID);
typename AuxGraph::template NodeMap<Node> bpred(graph, INVALID);
typename AuxGraph::template NodeMap<Node> cpred(graph, INVALID);
typename AuxGraph::template NodeMap<int> apredid(graph, -1);
typename AuxGraph::template NodeMap<int> bpredid(graph, -1);
typename AuxGraph::template NodeMap<int> cpredid(graph, -1);
for (ArcIt e(graph); e != INVALID; ++e) {
if (angle[e] == angle[next[e]]) {
switch (angle[e]) {
case 2:
apred[graph.target(e)] = graph.source(e);
apredid[graph.target(e)] = graph.id(graph.source(e));
break;
case 1:
bpred[graph.target(e)] = graph.source(e);
bpredid[graph.target(e)] = graph.id(graph.source(e));
break;
case 0:
cpred[graph.target(e)] = graph.source(e);
cpredid[graph.target(e)] = graph.id(graph.source(e));
break;
}
}
}
cpred[anode] = INVALID;
cpred[bnode] = INVALID;
std::vector<Node> aorder, border, corder;
{
typename AuxGraph::template NodeMap<bool> processed(graph, false);
std::vector<Node> st;
for (NodeIt n(graph); n != INVALID; ++n) {
if (!processed[n] && n != bnode && n != cnode) {
st.push_back(n);
processed[n] = true;
Node m = apred[n];
while (m != INVALID && !processed[m]) {
st.push_back(m);
processed[m] = true;
m = apred[m];
}
while (!st.empty()) {
aorder.push_back(st.back());
st.pop_back();
}
}
}
}
{
typename AuxGraph::template NodeMap<bool> processed(graph, false);
std::vector<Node> st;
for (NodeIt n(graph); n != INVALID; ++n) {
if (!processed[n] && n != cnode && n != anode) {
st.push_back(n);
processed[n] = true;
Node m = bpred[n];
while (m != INVALID && !processed[m]) {
st.push_back(m);
processed[m] = true;
m = bpred[m];
}
while (!st.empty()) {
border.push_back(st.back());
st.pop_back();
}
}
}
}
{
typename AuxGraph::template NodeMap<bool> processed(graph, false);
std::vector<Node> st;
for (NodeIt n(graph); n != INVALID; ++n) {
if (!processed[n] && n != anode && n != bnode) {
st.push_back(n);
processed[n] = true;
Node m = cpred[n];
while (m != INVALID && !processed[m]) {
st.push_back(m);
processed[m] = true;
m = cpred[m];
}
while (!st.empty()) {
corder.push_back(st.back());
st.pop_back();
}
}
}
}
typename AuxGraph::template NodeMap<int> atree(graph, 0);
for (int i = aorder.size() - 1; i >= 0; --i) {
Node n = aorder[i];
atree[n] = 1;
for (OutArcIt e(graph, n); e != INVALID; ++e) {
if (apred[graph.target(e)] == n) {
atree[n] += atree[graph.target(e)];
}
}
}
typename AuxGraph::template NodeMap<int> btree(graph, 0);
for (int i = border.size() - 1; i >= 0; --i) {
Node n = border[i];
btree[n] = 1;
for (OutArcIt e(graph, n); e != INVALID; ++e) {
if (bpred[graph.target(e)] == n) {
btree[n] += btree[graph.target(e)];
}
}
}
typename AuxGraph::template NodeMap<int> apath(graph, 0);
apath[bnode] = apath[cnode] = 1;
typename AuxGraph::template NodeMap<int> apath_btree(graph, 0);
apath_btree[bnode] = btree[bnode];
for (int i = 1; i < int(aorder.size()); ++i) {
Node n = aorder[i];
apath[n] = apath[apred[n]] + 1;
apath_btree[n] = btree[n] + apath_btree[apred[n]];
}
typename AuxGraph::template NodeMap<int> bpath_atree(graph, 0);
bpath_atree[anode] = atree[anode];
for (int i = 1; i < int(border.size()); ++i) {
Node n = border[i];
bpath_atree[n] = atree[n] + bpath_atree[bpred[n]];
}
typename AuxGraph::template NodeMap<int> cpath(graph, 0);
cpath[anode] = cpath[bnode] = 1;
typename AuxGraph::template NodeMap<int> cpath_atree(graph, 0);
cpath_atree[anode] = atree[anode];
typename AuxGraph::template NodeMap<int> cpath_btree(graph, 0);
cpath_btree[bnode] = btree[bnode];
for (int i = 1; i < int(corder.size()); ++i) {
Node n = corder[i];
cpath[n] = cpath[cpred[n]] + 1;
cpath_atree[n] = atree[n] + cpath_atree[cpred[n]];
cpath_btree[n] = btree[n] + cpath_btree[cpred[n]];
}
typename AuxGraph::template NodeMap<int> third(graph);
for (NodeIt n(graph); n != INVALID; ++n) {
point_map[n].x =
bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1;
point_map[n].y =
cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1;
}
}
public:
/// \brief Calculate the node positions
///
/// This function calculates the node positions on the plane.
/// \return \c true if the graph is planar.
bool run() {
PlanarEmbedding<Graph> pe(_graph);
if (!pe.run()) return false;
run(pe);
return true;
}
/// \brief Calculate the node positions according to a
/// combinatorical embedding
///
/// This function calculates the node positions on the plane.
/// The given \c embedding map should contain a valid combinatorical
/// embedding, i.e. a valid cyclic order of the arcs.
/// It can be computed using PlanarEmbedding.
template <typename EmbeddingMap>
void run(const EmbeddingMap& embedding) {
typedef SmartEdgeSet<Graph> AuxGraph;
if (3 * countNodes(_graph) - 6 == countEdges(_graph)) {
drawing(_graph, embedding, _point_map);
return;
}
AuxGraph aux_graph(_graph);
typename AuxGraph::template ArcMap<typename AuxGraph::Arc>
aux_embedding(aux_graph);
{
typename Graph::template EdgeMap<typename AuxGraph::Edge>
ref(_graph);
for (EdgeIt e(_graph); e != INVALID; ++e) {
ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e));
}
for (EdgeIt e(_graph); e != INVALID; ++e) {
Arc ee = embedding[_graph.direct(e, true)];
aux_embedding[aux_graph.direct(ref[e], true)] =
aux_graph.direct(ref[ee], _graph.direction(ee));
ee = embedding[_graph.direct(e, false)];
aux_embedding[aux_graph.direct(ref[e], false)] =
aux_graph.direct(ref[ee], _graph.direction(ee));
}
}
_planarity_bits::makeConnected(aux_graph, aux_embedding);
_planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding);
_planarity_bits::makeMaxPlanar(aux_graph, aux_embedding);
drawing(aux_graph, aux_embedding, _point_map);
}
/// \brief The coordinate of the given node
///
/// This function returns the coordinate of the given node.
Point operator[](const Node& node) const {
return _point_map[node];
}
/// \brief Return the grid embedding in a node map
///
/// This function returns the grid embedding in a node map of
/// \c dim2::Point<int> coordinates.
const PointMap& coords() const {
return _point_map;
}
private:
const Graph& _graph;
PointMap _point_map;
};
namespace _planarity_bits {
template <typename ColorMap>
class KempeFilter {
public:
typedef typename ColorMap::Key Key;
typedef bool Value;
KempeFilter(const ColorMap& color_map,
const typename ColorMap::Value& first,
const typename ColorMap::Value& second)
: _color_map(color_map), _first(first), _second(second) {}
Value operator[](const Key& key) const {
return _color_map[key] == _first || _color_map[key] == _second;
}
private:
const ColorMap& _color_map;
typename ColorMap::Value _first, _second;
};
}
/// \ingroup planar
///
/// \brief Coloring planar graphs
///
/// The graph coloring problem is the coloring of the graph nodes
/// so that there are no adjacent nodes with the same color. The
/// planar graphs can always be colored with four colors, which is
/// proved by Appel and Haken. Their proofs provide a quadratic
/// time algorithm for four coloring, but it could not be used to
/// implement an efficient algorithm. The five and six coloring can be
/// made in linear time, but in this class, the five coloring has
/// quadratic worst case time complexity. The two coloring (if
/// possible) is solvable with a graph search algorithm and it is
/// implemented in \ref bipartitePartitions() function in LEMON. To
/// decide whether a planar graph is three colorable is NP-complete.
///
/// This class contains member functions for calculate colorings
/// with five and six colors. The six coloring algorithm is a simple
/// greedy coloring on the backward minimum outgoing order of nodes.
/// This order can be computed by selecting the node with least
/// outgoing arcs to unprocessed nodes in each phase. This order
/// guarantees that when a node is chosen for coloring it has at
/// most five already colored adjacents. The five coloring algorithm
/// use the same method, but if the greedy approach fails to color
/// with five colors, i.e. the node has five already different
/// colored neighbours, it swaps the colors in one of the connected
/// two colored sets with the Kempe recoloring method.
template <typename Graph>
class PlanarColoring {
public:
TEMPLATE_GRAPH_TYPEDEFS(Graph);
/// \brief The map type for storing color indices
typedef typename Graph::template NodeMap<int> IndexMap;
/// \brief The map type for storing colors
///
/// The map type for storing colors.
/// \see Palette, Color
typedef ComposeMap<Palette, IndexMap> ColorMap;
/// \brief Constructor
///
/// Constructor.
/// \pre The graph must be simple, i.e. it should not
/// contain parallel or loop arcs.
PlanarColoring(const Graph& graph)
: _graph(graph), _color_map(graph), _palette(0) {
_palette.add(Color(1,0,0));
_palette.add(Color(0,1,0));
_palette.add(Color(0,0,1));
_palette.add(Color(1,1,0));
_palette.add(Color(1,0,1));
_palette.add(Color(0,1,1));
}
/// \brief Return the node map of color indices
///
/// This function returns the node map of color indices. The values are
/// in the range \c [0..4] or \c [0..5] according to the coloring method.
IndexMap colorIndexMap() const {
return _color_map;
}
/// \brief Return the node map of colors
///
/// This function returns the node map of colors. The values are among
/// five or six distinct \ref lemon::Color "colors".
ColorMap colorMap() const {
return composeMap(_palette, _color_map);
}
/// \brief Return the color index of the node
///
/// This function returns the color index of the given node. The value is
/// in the range \c [0..4] or \c [0..5] according to the coloring method.
int colorIndex(const Node& node) const {
return _color_map[node];
}
/// \brief Return the color of the node
///
/// This function returns the color of the given node. The value is among
/// five or six distinct \ref lemon::Color "colors".
Color color(const Node& node) const {
return _palette[_color_map[node]];
}
/// \brief Calculate a coloring with at most six colors
///
/// This function calculates a coloring with at most six colors. The time
/// complexity of this variant is linear in the size of the graph.
/// \return \c true if the algorithm could color the graph with six colors.
/// If the algorithm fails, then the graph is not planar.
/// \note This function can return \c true if the graph is not
/// planar, but it can be colored with at most six colors.
bool runSixColoring() {
typename Graph::template NodeMap<int> heap_index(_graph, -1);
BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
for (NodeIt n(_graph); n != INVALID; ++n) {
_color_map[n] = -2;
heap.push(n, countOutArcs(_graph, n));
}
std::vector<Node> order;
while (!heap.empty()) {
Node n = heap.top();
heap.pop();
_color_map[n] = -1;
order.push_back(n);
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Node t = _graph.runningNode(e);
if (_color_map[t] == -2) {
heap.decrease(t, heap[t] - 1);
}
}
}
for (int i = order.size() - 1; i >= 0; --i) {
std::vector<bool> forbidden(6, false);
for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
Node t = _graph.runningNode(e);
if (_color_map[t] != -1) {
forbidden[_color_map[t]] = true;
}
}
for (int k = 0; k < 6; ++k) {
if (!forbidden[k]) {
_color_map[order[i]] = k;
break;
}
}
if (_color_map[order[i]] == -1) {
return false;
}
}
return true;
}
private:
bool recolor(const Node& u, const Node& v) {
int ucolor = _color_map[u];
int vcolor = _color_map[v];
typedef _planarity_bits::KempeFilter<IndexMap> KempeFilter;
KempeFilter filter(_color_map, ucolor, vcolor);
typedef FilterNodes<const Graph, const KempeFilter> KempeGraph;
KempeGraph kempe_graph(_graph, filter);
std::vector<Node> comp;
Bfs<KempeGraph> bfs(kempe_graph);
bfs.init();
bfs.addSource(u);
while (!bfs.emptyQueue()) {
Node n = bfs.nextNode();
if (n == v) return false;
comp.push_back(n);
bfs.processNextNode();
}
int scolor = ucolor + vcolor;
for (int i = 0; i < static_cast<int>(comp.size()); ++i) {
_color_map[comp[i]] = scolor - _color_map[comp[i]];
}
return true;
}
template <typename EmbeddingMap>
void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) {
std::vector<Node> nodes;
nodes.reserve(4);
for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) {
Node t = _graph.target(e);
if (_color_map[t] != -1) {
nodes.push_back(t);
if (nodes.size() == 4) break;
}
}
int color = _color_map[nodes[0]];
if (recolor(nodes[0], nodes[2])) {
_color_map[node] = color;
} else {
color = _color_map[nodes[1]];
recolor(nodes[1], nodes[3]);
_color_map[node] = color;
}
}
public:
/// \brief Calculate a coloring with at most five colors
///
/// This function calculates a coloring with at most five
/// colors. The worst case time complexity of this variant is
/// quadratic in the size of the graph.
/// \param embedding This map should contain a valid combinatorical
/// embedding, i.e. a valid cyclic order of the arcs.
/// It can be computed using PlanarEmbedding.
template <typename EmbeddingMap>
void runFiveColoring(const EmbeddingMap& embedding) {
typename Graph::template NodeMap<int> heap_index(_graph, -1);
BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
for (NodeIt n(_graph); n != INVALID; ++n) {
_color_map[n] = -2;
heap.push(n, countOutArcs(_graph, n));
}
std::vector<Node> order;
while (!heap.empty()) {
Node n = heap.top();
heap.pop();
_color_map[n] = -1;
order.push_back(n);
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Node t = _graph.runningNode(e);
if (_color_map[t] == -2) {
heap.decrease(t, heap[t] - 1);
}
}
}
for (int i = order.size() - 1; i >= 0; --i) {
std::vector<bool> forbidden(5, false);
for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
Node t = _graph.runningNode(e);
if (_color_map[t] != -1) {
forbidden[_color_map[t]] = true;
}
}
for (int k = 0; k < 5; ++k) {
if (!forbidden[k]) {
_color_map[order[i]] = k;
break;
}
}
if (_color_map[order[i]] == -1) {
kempeRecoloring(order[i], embedding);
}
}
}
/// \brief Calculate a coloring with at most five colors
///
/// This function calculates a coloring with at most five
/// colors. The worst case time complexity of this variant is
/// quadratic in the size of the graph.
/// \return \c true if the graph is planar.
bool runFiveColoring() {
PlanarEmbedding<Graph> pe(_graph);
if (!pe.run()) return false;
runFiveColoring(pe.embeddingMap());
return true;
}
private:
const Graph& _graph;
IndexMap _color_map;
Palette _palette;
};
}
#endif
|