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
|
/*
* $Revision: 2573 $
*
* last checkin:
* $Author: gutwenger $
* $Date: 2012-07-10 18:48:33 +0200 (Di, 10. Jul 2012) $
***************************************************************/
/** \file
* \brief Implements the class ClusterGraph, providing
* extra functionality for clustered graphs.
* A clustered graph C=(G,T) consists of an undirected graph G
* and a rooted tree T in which the leaves of T correspond
* to the vertices of G=(V,E).
*
* \author Sebastian Leipert, Karsten Klein
*
* \par License:
* This file is part of the Open Graph Drawing Framework (OGDF).
*
* \par
* Copyright (C)<br>
* See README.txt in the root directory of the OGDF installation for details.
*
* \par
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* Version 2 or 3 as published by the Free Software Foundation;
* see the file LICENSE.txt included in the packaging of this file
* for details.
*
* \par
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* \par
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* \see http://www.gnu.org/copyleft/gpl.html
***************************************************************/
#include "ClusterGraph.h"
#include "ClusterArray.h"
#include "ClusterGraphObserver.h"
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include "../basic/String.h"
#include "../basic/AdjEntryArray.h"
#include "../fileformats/GmlParser.h"
namespace ogdf {
#define MIN_CLUSTER_TABLE_SIZE (1 << 4)
//---------------------------------------------------------
//node search in cluster hierarchy
//---------------------------------------------------------
void ClusterElement::getClusterInducedNodes(List<node> &clusterNodes) {
ListConstIterator<node> nit;
for (nit=m_entries.begin(); nit.valid(); ++nit) {
clusterNodes.pushBack(*nit);
}
ListConstIterator<cluster> cit;
for (cit=m_children.begin(); cit.valid(); ++cit) {
(*cit)->getClusterInducedNodes(clusterNodes);
}
}
void ClusterElement::getClusterNodes(List<node> &clusterNodes) {
clusterNodes.clear();
getClusterInducedNodes(clusterNodes);
}
void ClusterElement::getClusterInducedNodes(NodeArray<bool> &clusterNode, int& num) {
ListConstIterator<node> nit;
for (nit=m_entries.begin(); nit.valid(); ++nit) {
clusterNode[*nit] = true;
num++;
}
ListConstIterator<cluster> cit;
for (cit=m_children.begin(); cit.valid(); ++cit) {
(*cit)->getClusterInducedNodes(clusterNode, num);
}
}
int ClusterElement::getClusterNodes(NodeArray<bool> &clusterNode)
{
int num = 0;
getClusterInducedNodes(clusterNode, num);
return num;
}
//---------------------------------------------------------
// Construction
//---------------------------------------------------------
ClusterGraph::ClusterGraph()
{
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_rootCluster = 0;
m_allowEmptyClusters = true;
m_updateDepth = false;
m_depthUpToDate = false;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = MIN_CLUSTER_TABLE_SIZE;
m_adjAvailable = false;
m_lcaNumber = 0;
m_lcaSearch = 0;
m_vAncestor = 0;
m_wAncestor = 0;
//m_clusterDepth.init(*this, 0);
}
// Construction of a new cluster graph. All nodes
// are children of the root cluster
ClusterGraph::ClusterGraph(const Graph &G) : GraphObserver(&G), m_pGraph(&G)
{
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_rootCluster = 0;
m_allowEmptyClusters = true;
m_updateDepth = false;
m_depthUpToDate = false;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = G.nextPower2(MIN_CLUSTER_TABLE_SIZE, G.nodeArrayTableSize());
//m_clusterDepth.init(*this, 0);
initGraph(G);
}
ClusterGraph::ClusterGraph(const ClusterGraph &C) :
GraphObserver(&(C.getGraph())),
m_lcaSearch(0),
m_vAncestor(0),
m_wAncestor(0)
{
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_rootCluster = 0;
m_allowEmptyClusters = true;
m_updateDepth = false;
m_depthUpToDate = false;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = C.m_clusterArrayTableSize;
shallowCopy(C);
}
ClusterGraph::ClusterGraph(
const ClusterGraph &C,
Graph &G,
ClusterArray<cluster> &originalClusterTable,
NodeArray<node> &originalNodeTable)
:
GraphObserver(&G),
m_lcaSearch(0),
m_vAncestor(0),
m_wAncestor(0)
{
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_rootCluster = 0;
m_allowEmptyClusters = true;
m_updateDepth = false;
m_depthUpToDate = false;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = C.m_clusterArrayTableSize;
deepCopy(C,G,originalClusterTable,originalNodeTable);
}
ClusterGraph::ClusterGraph(
const ClusterGraph &C,
Graph &G,
ClusterArray<cluster> &originalClusterTable,
NodeArray<node> &originalNodeTable,
EdgeArray<edge> &edgeCopy)
:
GraphObserver(&G),
m_lcaSearch(0),
m_vAncestor(0),
m_wAncestor(0)
{
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_rootCluster = 0;
m_allowEmptyClusters = true;
m_updateDepth = false;
m_depthUpToDate = false;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = C.m_clusterArrayTableSize;
deepCopy(C, G, originalClusterTable, originalNodeTable, edgeCopy);
}
ClusterGraph::ClusterGraph(const ClusterGraph &C,Graph &G) :
GraphObserver(&G),
m_lcaSearch(0),
m_vAncestor(0),
m_wAncestor(0)
{
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_rootCluster = 0;
m_allowEmptyClusters = true;
m_updateDepth = false;
m_depthUpToDate = false;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = C.m_clusterArrayTableSize;
deepCopy(C,G);
}
ClusterGraph::~ClusterGraph()
{
for(ListIterator<ClusterArrayBase*> it = m_regClusterArrays.begin();
it.valid(); ++it)
{
(*it)->disconnect();
}
clear();
}
// Construction of a new cluster graph. All nodes
// are children of the root cluster
void ClusterGraph::init(const Graph &G)
{
clear();
m_clusterIdCount = 0;
m_postOrderStart = 0;
m_pGraph = &G;
m_nClusters = 0;
m_lcaNumber = 0;
m_clusterArrayTableSize = G.nextPower2(MIN_CLUSTER_TABLE_SIZE, G.nodeArrayTableSize());
//m_clusterDepth.init(*this, 0);
initGraph(G);
}
//---------------------------------------------------------
// =
//---------------------------------------------------------
ClusterGraph &ClusterGraph::operator=(const ClusterGraph &C)
{
clear(); shallowCopy(C);
m_clusterArrayTableSize = C.m_clusterArrayTableSize;
reinitArrays();
OGDF_ASSERT_IF(dlConsistencyChecks, consistencyCheck());
return *this;
}
//---------------------------------------------------------
// copy,initGraph
//---------------------------------------------------------
// Copy Function
void ClusterGraph::shallowCopy(const ClusterGraph &C)
{
const Graph &G = C;
m_pGraph = &G;
m_nClusters = 0;
//m_clusterDepth.init(*this, 0);
initGraph(G);
m_updateDepth = C.m_updateDepth;
m_depthUpToDate = C.m_depthUpToDate;
// Construct cluster tree
ClusterArray<cluster> originalClusterTable(C);
cluster c = 0;
forall_clusters(c,C)
{
if (c == C.m_rootCluster)
{
originalClusterTable[c] = m_rootCluster;
//does not really need to be assigned HERE in for
m_rootCluster->depth() = 1;
OGDF_ASSERT(C.rootCluster()->depth() == 1)
continue;
}
originalClusterTable[c] = newCluster();
originalClusterTable[c]->depth() = c->depth();
}
forall_clusters(c,C)
{
if (c == C.m_rootCluster)
continue;
originalClusterTable[c]->m_parent = originalClusterTable[c->m_parent];
originalClusterTable[c->m_parent]->m_children.pushBack(originalClusterTable[c]);
originalClusterTable[c]->m_it = originalClusterTable[c->m_parent]->m_children.rbegin();
}
node v;
forall_nodes(v,G)
reassignNode(v,originalClusterTable[C.clusterOf(v)]);
copyLCA(C);
}
// Initialize the graph
void ClusterGraph::initGraph(const Graph &G)
{
reregister(&G); //will in some constructors cause double registration
m_lcaNumber = 0;
m_lcaSearch = 0;
m_vAncestor = 0;
m_wAncestor = 0;
//if (G.empty())
// return;
m_adjAvailable = false;
//assign already existing nodes, new nodes are assigned
//over nodeadded
List<node> allNodes;
G.allNodes(allNodes);
//clusterIdcount may be zero in case of constructor call,
//but can be != zero if readgraphwin is used
//root cluster should always get id 0
#ifdef OGDF_DEBUG
m_rootCluster = OGDF_NEW ClusterElement(this, 0);//m_clusterIdCount++);
#else
m_rootCluster = OGDF_NEW ClusterElement(0);//m_clusterIdCount++);
#endif
OGDF_ASSERT(m_nClusters == 0)
m_clusterIdCount++;
m_rootCluster->depth() = 1;
m_rootCluster->init(allNodes);
m_nodeMap.init(G,m_rootCluster);
m_itMap.init(G,0);
ListIterator<node> it;
for (it = m_rootCluster->m_entries.begin(); it.valid(); it++)
m_itMap[*it] = it;
m_nClusters++;
m_clusters.pushBack(m_rootCluster);
}
void ClusterGraph::reinitGraph(const Graph &G)
{
m_pGraph = &G;
OGDF_ASSERT_IF(dlConsistencyChecks, G.consistencyCheck());
m_clusterArrayTableSize = G.nextPower2(MIN_CLUSTER_TABLE_SIZE, G.nodeArrayTableSize());
if (numberOfClusters() != 0)
{
clear();
}//if
initGraph(G); //already constructs root cluster, reassign
}
void ClusterGraph::reinitArrays()
{
ListIterator<ClusterArrayBase*> itCluster = m_regClusterArrays.begin();
for(; itCluster.valid(); ++itCluster)
(*itCluster)->reinit(m_clusterArrayTableSize);
}
// Copy Function
void ClusterGraph::deepCopy(const ClusterGraph &C,Graph &G)
{
const Graph &cG = C; // original graph
ClusterArray<cluster> originalClusterTable(C);
NodeArray<node> originalNodeTable(cG);
EdgeArray<edge> edgeCopy(cG);
deepCopy(C,G,originalClusterTable, originalNodeTable, edgeCopy);
}
void ClusterGraph::deepCopy(const ClusterGraph &C,Graph &G,
ClusterArray<cluster> &originalClusterTable,
NodeArray<node> &originalNodeTable)
{
const Graph &cG = C; // original graph
EdgeArray<edge> edgeCopy(cG);
deepCopy(C,G,originalClusterTable, originalNodeTable, edgeCopy);
}
void ClusterGraph::deepCopy(const ClusterGraph &C,Graph &G,
ClusterArray<cluster> &originalClusterTable,
NodeArray<node> &originalNodeTable,
EdgeArray<edge> &edgeCopy)
{
G.clear();
const Graph &cG = C; // original graph
m_pGraph = &G;
m_nClusters = 0;
initGraph(G); //arrays have already to be initialized for newnode
m_updateDepth = C.m_updateDepth;
m_depthUpToDate = C.m_depthUpToDate;
NodeArray<node> orig(G);
node v;
edge e;
forall_nodes(v,cG)
{
node w = G.newNode();
orig[w] = v;
originalNodeTable[v] = w;
}
forall_edges(e,cG)
{
edge eNew = G.newEdge(originalNodeTable[e->adjSource()->theNode()],
originalNodeTable[e->adjTarget()->theNode()]);
edgeCopy[e] = eNew;
}//foralledges
//m_clusterDepth.init(*this, 0);
// Construct cluster tree
cluster c = 0;
forall_clusters(c,C)
{
if (c == C.m_rootCluster)
{
originalClusterTable[c] = m_rootCluster;
//does not really need to be assigned HERE in for
m_rootCluster->depth() = 1;
OGDF_ASSERT(c->depth() == 1)
continue;
}
originalClusterTable[c] = newCluster();
originalClusterTable[c]->depth() = c->depth();
}
forall_clusters(c,C)
{
if (c == C.m_rootCluster)
continue;
originalClusterTable[c]->m_parent = originalClusterTable[c->m_parent];
originalClusterTable[c->m_parent]->m_children.pushBack(originalClusterTable[c]);
originalClusterTable[c]->m_it = originalClusterTable[c->m_parent]->m_children.rbegin();
}
forall_nodes(v,G)
reassignNode(v,originalClusterTable[C.clusterOf(orig[v])]);
//ClusterArray<cluster>* ca = ;
copyLCA(C, &originalClusterTable);
}
//*********************************************************
//cluster search
//We search for the lowest common cluster of a set of nodes.
//We first compute the common path of two nodes, then update path if root
//path from other nodes hits it .
//We always stop if we encounter root cluster.
cluster ClusterGraph::commonCluster(SList<node>& nodes)
{
//worst case running time #nodes x clustertreeheight-1
//always <= complete tree run
//we could even use pathcompression...
//at any time, we stop if root is encountered as lowest
//common cluster of a node subset
if (nodes.empty()) return 0;
//For simplicity, we use cluster arrays
ClusterArray<int> commonPathHit(*this, 0); //count for clusters path hits
int runs = 0; //number of nodes already considered
cluster pathCluster;
SListIterator<node> sIt = nodes.begin();
node v1 = (*sIt);
if (nodes.size() == 1) return clusterOf(v1);
sIt++;
node v2 = (*sIt);
cluster lowestCommon = commonCluster(v1, v2);
commonPathHit[lowestCommon] = 2;
pathCluster = lowestCommon;
while (pathCluster->parent())
{
pathCluster = pathCluster->parent();
commonPathHit[pathCluster] = 2;
}
runs = 2;
//we save direct lca access, it also lies on a runs hit path from root
while ((runs < nodes.size()) && (lowestCommon != m_rootCluster))
{
sIt++;
node v = (*sIt);
pathCluster = clusterOf(v);
while (commonPathHit[pathCluster] == 0)
{
if (pathCluster->parent()) pathCluster = pathCluster->parent();
else return m_rootCluster; //can never happen
}//while
//assign new (maybe same) lowest common
if (commonPathHit[pathCluster] == runs) lowestCommon = pathCluster;
commonPathHit[pathCluster] = commonPathHit[pathCluster]+1;
if (pathCluster == m_rootCluster) return m_rootCluster;
//update hits in path to root
while (pathCluster->parent())
{
pathCluster = pathCluster->parent();
commonPathHit[pathCluster] = commonPathHit[pathCluster]+1;
}
runs++;
}
return lowestCommon;
}//commoncluster
//lowest common cluster of v,w
cluster ClusterGraph::commonCluster(node v, node w) const
{
cluster c1, c2;
return commonClusterLastAncestors(v, w, c1, c2);
}//commonCluster
//lowest common cluster of v,w and its ancestors
cluster ClusterGraph::commonClusterLastAncestors(node v,
node w,
cluster& c1,
cluster& c2) const
{
List<cluster> e;
return commonClusterAncestorsPath(v, w, c1, c2, e);
}//commonClusterLastAncestors
//lowest common cluster and path between v and w containing it
//note that eL is directed from v to w
cluster ClusterGraph::commonClusterPath(node v,
node w,
List<cluster>& eL) const
{
cluster c1, c2;
return commonClusterAncestorsPath(v, w, c1, c2, eL);
}//commonClusterLastAncestors
//note that eL is directed from v to w
cluster ClusterGraph::commonClusterAncestorsPath(node v,
node w,
cluster& c1,
cluster& c2,
List<cluster>& eL) const
{
OGDF_ASSERT(v->graphOf() == m_pGraph)
OGDF_ASSERT(w->graphOf() == m_pGraph)
cluster cv = clusterOf(v);
cluster cw = clusterOf(w);
//clusters from v and w to common
List<cluster> vList;
List<cluster> wList;
//CASE1 no search necessary
//if both nodes are in the same cluster, we return this cluster
//and have to check if c1 == c2 to have a (v,w) representation edge
if (cv == cw)
{
c1 = c2 = cv;
eL.pushBack(c1);
return cv;
}
if (m_lcaNumber == INT_MAX - 1) m_lcaNumber = 0;
else m_lcaNumber++;
if (!m_lcaSearch)
{
m_lcaSearch = OGDF_NEW ClusterArray<int>(*this, -1);
m_vAncestor = OGDF_NEW ClusterArray<cluster>(*this, 0);
m_wAncestor = OGDF_NEW ClusterArray<cluster>(*this, 0);
}
//CASE2: one of the nodes hangs at root: save root as ancestor
//any other case: save cluster of node as ancestor, too, to check this
//case:: common = xCluster != yCluster
//(*m_vAncestor)[rootCluster()] = rootCluster();
//(*m_wAncestor)[rootCluster()] = rootCluster();
(*m_vAncestor)[cv] = 0;
(*m_wAncestor)[cw] = 0;
//we rely on the fact all nodes are in the rootcluster or
//that parent is initialized to zero to terminate
//we start with different clusters due to CASE1
//save the ancestor information
(*m_lcaSearch)[cw] = m_lcaNumber; //not really necessary, we won't return
(*m_lcaSearch)[cv] = m_lcaNumber;
vList.pushBack(cv);
wList.pushBack(cw);
//we break and return if we find a common node
//before we reach the rootcluster
do
{
if (cv->parent()) //root reached?
{
(*m_vAncestor)[cv->parent()] = cv;
cv = cv->parent();
//was cv visited on path from w
if ((*m_lcaSearch)[cv] == m_lcaNumber)
{
c1 = (*m_vAncestor)[cv];
c2 = (*m_wAncestor)[cv];
//setup list
ListIterator<cluster> itC = vList.begin();
while (itC.valid())
{
eL.pushBack(*itC);
itC++;
}
itC = wList.rbegin();
while (itC.valid() && ((*itC) != cv))
itC--;
while (itC.valid())
{
eL.pushBack(*itC);
itC--;
}
return cv;
}
vList.pushBack(cv);
(*m_lcaSearch)[cv] = m_lcaNumber;
}//if not root reached on cvpath
if (cw->parent())
{
(*m_wAncestor)[cw->parent()] = cw;
cw = cw->parent();
//was cw visited on path from v
if ((*m_lcaSearch)[cw] == m_lcaNumber)
{
c1 = (*m_vAncestor)[cw];
c2 = (*m_wAncestor)[cw];
//setup list
ListIterator<cluster> itC = vList.begin();
while (itC.valid() && ((*itC) != cw))
{
eL.pushBack(*itC);
itC++;
}
eL.pushBack(cw);
itC = wList.rbegin();
while (itC.valid())
{
eL.pushBack(*itC);
itC--;
}
return cw;
}
wList.pushBack(cw);
(*m_lcaSearch)[cw] = m_lcaNumber;
}//if not root reached on cwpath
} while ( cv->parent() || cw->parent() );
//v,w should be at least together in the rootcluster
c1 = (*m_vAncestor)[rootCluster()];
c2 = (*m_wAncestor)[rootCluster()];
return rootCluster();
}//commonclusterlastAncestors
void ClusterGraph::copyLCA(
const ClusterGraph &C,
ClusterArray<cluster>* /*clusterCopy*/)
{
if (m_lcaSearch)
{
delete m_lcaSearch;
delete m_vAncestor;
delete m_wAncestor;
}//if
if (C.m_lcaSearch)
{
//otherwise, initialization won't work
m_clusterArrayTableSize = C.m_clusterArrayTableSize;
m_lcaSearch = OGDF_NEW ClusterArray<int>(*this, -1);//(*C.m_lcaSearch);
m_vAncestor = OGDF_NEW ClusterArray<cluster>(*this, 0); //*C.m_vAncestor);
//m_vAncestor->init(*this, 0),
m_wAncestor = OGDF_NEW ClusterArray<cluster>(*this, 0);//*C.m_wAncestor);
//setting of clusters is not necessary!
//(*m_v/wAncestor)[(*clusterCopy)[c]]= (*(C.m_v/wAncestor))[c];
}//if
}//copylca
//---------------------------------------------------------
// check the graph for empty clusters
//---------------------------------------------------------
//we never set rootcluster to be one of the empty clusters!!
void ClusterGraph::emptyClusters(SList<cluster>& emptyCluster,
SList<cluster>* checkCluster)
{
emptyCluster.clear();
cluster cc;
//for all nodes = #nodes
if (checkCluster)
{
SListIterator<cluster> it = checkCluster->begin();
while (it.valid())
{
if ((*it)->cCount() + (*it)->nCount() == 0)
if ((*it) != rootCluster()) //we dont add rootcluster
emptyCluster.pushBack((*it));
it++;
}//while
}//if checkcluster given
else
{
forall_clusters(cc, *this)
{
if (cc->cCount() + cc->nCount() == 0)
if (cc != rootCluster()) //we dont add rootcluster
emptyCluster.pushBack(cc);
}//forallclusters
}//else checkcluster
//other clusters can get empty, too, if we delete these
ClusterArray<int> delCount(*this, 0);
SList<cluster> emptyParent;
SListIterator<cluster> itC = emptyCluster.begin();
while (itC.valid())
{
//count deleted children
cluster runc = (*itC)->parent();
if (runc) //is always the case as long as root was not inserted to list
{
delCount[runc]++;
while ((runc->nCount() == 0) && (runc->cCount() == delCount[runc]))
{
if (runc == rootCluster()) break;
emptyParent.pushBack(runc);
runc = runc->parent();
delCount[runc]++;
}//while parent emptied
}//if not runc = root->parent
itC++;
}//while empty leaves
emptyCluster.conc(emptyParent);
//for reinsertion, start at emptycluster's back
}//emptyClusters
//---------------------------------------------------------
// newCluster, delCluster, createCluster
//---------------------------------------------------------
// Inserts a new cluster prescribing its parent
cluster ClusterGraph::newCluster(cluster parent, int id)
{
OGDF_ASSERT(parent);
cluster c;
if (id > 0)
c = newCluster(id);
else
c = newCluster();
parent->m_children.pushBack(c);
c->m_it = parent->m_children.rbegin();
c->m_parent = parent;
c->depth() = parent->depth() + 1;
return c;
}
//Insert a new cluster with given ID, precondition: id not used
//has to be updated in the same way as newcluster()
cluster ClusterGraph::newCluster(int id)
{
m_nClusters++;
m_adjAvailable = false;
m_postOrderStart = 0;
if (id >= m_clusterIdCount) m_clusterIdCount = id+1;
if (m_clusterIdCount >= m_clusterArrayTableSize)
{
m_clusterArrayTableSize =
m_pGraph->nextPower2(m_clusterArrayTableSize, id);
for(ListIterator<ClusterArrayBase*> it = m_regClusterArrays.begin();
it.valid(); ++it)
{
(*it)->enlargeTable(m_clusterArrayTableSize);
}
}
#ifdef OGDF_DEBUG
cluster c = OGDF_NEW ClusterElement(this,id);
#else
cluster c = OGDF_NEW ClusterElement(id);
#endif
m_clusters.pushBack(c);
// notify observers
for(ListIterator<ClusterGraphObserver*> it = m_regObservers.begin();
it.valid(); ++it) (*it)->clusterAdded(c);
return c;
}
// Inserts a new cluster
//has to be updated in the same way as newcluster(id)
cluster ClusterGraph::newCluster()
{
m_nClusters++;
m_adjAvailable = false;
m_postOrderStart = 0;
if (m_clusterIdCount == m_clusterArrayTableSize)
{
m_clusterArrayTableSize <<= 1;
for(ListIterator<ClusterArrayBase*> it = m_regClusterArrays.begin();
it.valid(); ++it)
{
(*it)->enlargeTable(m_clusterArrayTableSize);
}
}
#ifdef OGDF_DEBUG
cluster c = OGDF_NEW ClusterElement(this,m_clusterIdCount++);
#else
cluster c = OGDF_NEW ClusterElement(m_clusterIdCount++);
#endif
m_clusters.pushBack(c);
// notify observers
for(ListIterator<ClusterGraphObserver*> it = m_regObservers.begin();
it.valid(); ++it) (*it)->clusterAdded(c);
return c;
}
cluster ClusterGraph::createEmptyCluster(const cluster parent, int clusterId)
{
//if no id given, use next free id
if (clusterId < 0) clusterId = m_clusterIdCount;
//create the new cluster
cluster cnew;
if (parent)
cnew = newCluster(parent, clusterId);
else
cnew = newCluster(m_rootCluster, clusterId);
return cnew;
}//createemptycluster
cluster ClusterGraph::createCluster(SList<node>& nodes, const cluster parent)
{
cluster c;
if (m_allowEmptyClusters)
{
c = doCreateCluster(nodes, parent);
return c;
}
else
{
SList<cluster> emptyCluster;
c = doCreateCluster(nodes, emptyCluster, parent);
SListIterator<cluster> sIt = emptyCluster.begin();
while (sIt.valid())
{
delCluster((*sIt));
//root cluster can never be empty, as we deleted a node
sIt++;
}//While
}
return c;
}
cluster ClusterGraph::doCreateCluster(SList<node>& nodes,
const cluster parent,
int clusterId)
{
if (nodes.empty()) return 0;
//if no id given, use next free id
if (clusterId < 0) clusterId = m_clusterIdCount;
//create the new cluster
cluster cnew;
if (parent)
cnew = newCluster(parent, clusterId);
else
cnew = newCluster(m_rootCluster, clusterId);
//insert nodes in new cluster
SListIterator<node> it = nodes.begin();
while (it.valid())
{
reassignNode((*it), cnew);
it++;
}//while
return cnew;
}//createcluster
cluster ClusterGraph::doCreateCluster(SList<node>& nodes,
SList<cluster>& emptyCluster,
const cluster parent,
int clusterId)
{
// Even if m_allowEmptyClusters is set we check if a cluster
// looses all of its nodes and has
// no more entries and childs. This can be used for special cluster
// object handling or for deletion if m_allowEmptyClusters is not set
// if it is not the new parent, it can be deleted
// running time max(#cluster, length(nodelist))
// TODO: Parameter, der dies auslaesst, da hohe Laufzeit
// hier macht das nur Sinn, wenn es schneller ist als forallclusters,
// sonst koennte man es ja auch aussen testen, aber bisher ist es nicht
// schneller implementiert
// Vorgehen: hash auf cluster index, falls nicht gesetzt, in liste einfuegen
// und als checkcluster an emptycluster uebergeben
if (nodes.empty()) return 0;
//if no id given, use next free id
if (clusterId < 0) clusterId = m_clusterIdCount;
//create the new cluster
cluster cnew;
if (parent)
cnew = newCluster(parent, clusterId);
else
cnew = newCluster(m_rootCluster, clusterId);
//insert nodes in new cluster
SListIterator<node> it = nodes.begin();
while (it.valid())
{
reassignNode((*it), cnew);
it++;
}//while
//should be: only for changed clusters (see comment above)
//it is important to save the cluster in an order
//that allows deletion as well as reinsertion
emptyClusters(emptyCluster);
//for reinsertion, start at emptycluster's back
return cnew;
}//createcluster
// Deletes cluster c
// All subclusters become children of parent cluster
// Precondition: c is not the root cluster
// updating of cluster depth information pumps running time
// up to worst case O(#C)
void ClusterGraph::delCluster(cluster c)
{
OGDF_ASSERT(c != 0 && c->graphOf() == this && c != m_rootCluster)
// notify observers
for(ListIterator<ClusterGraphObserver*> it = m_regObservers.begin();
it.valid(); ++it) (*it)->clusterDeleted(c);
--m_nClusters;
m_adjAvailable = false;
c->m_parent->m_children.del(c->m_it);
c->m_it = 0;
while (!c->m_children.empty())
{
cluster trace = c->m_children.popFrontRet();
trace->m_parent = c->m_parent;
trace->m_parent->m_children.pushBack(trace);
trace->m_it = trace->m_parent->m_children.rbegin();
//only recompute depth if option set and it makes sense
if (m_updateDepth && m_depthUpToDate)
{
//update depth for all children in subtree
OGDF_ASSERT(trace->depth() == trace->parent()->depth()+2)
pullUpSubTree(trace);
//could just set depth-1 here
//trace->depth() = trace->parent()->depth()+1;
}///if depth update
else m_depthUpToDate = false;
}
while (!c->m_entries.empty())
{
node v = c->m_entries.popFrontRet();
m_nodeMap[v] = 0;
reassignNode(v,c->m_parent);
}
m_clusters.del(c);
}
//pulls up depth of subtree located at c by one
//precondition: depth is consistent
//we dont ask for depthuptodate since the caller needs
//to know for himself if he wants the tree to be pulled
//for any special purpose
void ClusterGraph::pullUpSubTree(cluster c)
{
c->depth() = c->depth() - 1;
ListConstIterator<cluster> it = c->getChildren().begin();
while (it.valid())
{
pullUpSubTree(*it);
it++;
}
}
//---------------------------------------------------------
// clear, clearClusterTree
//---------------------------------------------------------
void ClusterGraph::clear()
{
//split condition
if (m_lcaSearch)
{
delete m_lcaSearch;
delete m_vAncestor;
delete m_wAncestor;
}
if (m_nClusters != 0)
{
clearClusterTree(m_rootCluster);
m_clusters.del(m_rootCluster);
}
//no clusters, so we can restart at 0
m_clusterIdCount = 0;
m_nClusters = 0;
}
// Removes the Clustering of a Tree and frees the allocated memory
void ClusterGraph::clearClusterTree(cluster c)
{
cluster trace = 0;
cluster parent = c->parent();
m_postOrderStart = 0;
m_adjAvailable = false;
List<cluster> children = c->getChildren();
List<node> attached;
while (!children.empty())
{
trace = children.popFrontRet();
clearClusterTree(trace,attached);
}
if (parent != 0)
{
ListIterator<node> it;
for (it = attached.begin();it.valid();it++)
{
m_nodeMap[(*it)] = parent;
parent->m_entries.pushBack((*it));
m_itMap[(*it)] = parent->m_entries.rbegin();
}
m_clusters.del(c);
}
else if (c == m_rootCluster)
{
ListIterator<node> it;
for (it = attached.begin();it.valid();it++)
{
m_nodeMap[(*it)] = m_rootCluster;
m_rootCluster->m_entries.pushBack((*it));
m_itMap[(*it)] = m_rootCluster->m_entries.rbegin();
}
m_rootCluster->m_children.clear();
}
}
void ClusterGraph::clearClusterTree(cluster c,List<node> &attached)
{
cluster trace;
List<cluster> children = c->getChildren();
attached.conc(c->m_entries);
m_adjAvailable = false;
while (!children.empty())
{
trace = children.popFrontRet();
clearClusterTree(trace,attached);
}
m_clusters.del(c);
}
//don't delete root cluster
void ClusterGraph::semiClear()
{
//split condition
if (m_lcaSearch)
{
delete m_lcaSearch;
delete m_vAncestor;
delete m_wAncestor;
}
if (m_nClusters != 0)
{
//clear the cluster structure under root cluster
clearClusterTree(m_rootCluster);
//now delete all rootcluster entries
while (!m_rootCluster->m_entries.empty())
{
node v = m_rootCluster->m_entries.popFrontRet();
m_nodeMap[v] = 0;
}
}
//no child clusters, so we can restart at 1
m_clusterIdCount = 1;
m_nClusters = 1;
}
//reassign cluster depth for clusters in subtree rooted at c
void ClusterGraph::computeSubTreeDepth(cluster c) const
{
if (c == rootCluster()) m_depthUpToDate = true;
if (!(c->parent())) c->depth() = 1;
else c->depth() = c->parent()->depth() + 1;
ListConstIterator<cluster> it = c->getChildren().begin();
while (it.valid())
{
computeSubTreeDepth(*it);
it++;
}
}
//move cluster from old parent to an other
void ClusterGraph::moveCluster(cluster c, cluster newParent)
{
if (c == rootCluster()) return;
if ((c == 0) || (newParent == 0)) return; //no cheap tricks
if (c->parent() == newParent) return; //no work to do
cluster oldParent = c->parent();
//we dont move root
OGDF_ASSERT(oldParent)
//check if we move to a descendant
cluster crun = newParent->parent();
bool descendant = false;
while (crun)
{
if (crun == c)
{
descendant = true;
break;
}
crun = crun->parent();
}//while running upwards
//do not allow to move empty clusters to descendants
if (descendant && (c->nCount() == 0))
return;
// save postorder for old parent
bool newOrder = false;
if (!m_postOrderStart)
{
newOrder = true;
}
//temporarily only recompute postorder for all clusters
oldParent->m_children.del(c->m_it);
newParent->m_children.pushBack(c);
c->m_it = newParent->m_children.rbegin();
c->m_parent = newParent;
//update the cluster depth information in the subtree
//If moved to descendant, recompute
//depth for parent (including all brother trees)
if (descendant)
{
//how do we move:
//only entries with c? => may be empty
//we currently dont allow this, because it makes
//no sense, you could just delete the cluster or move
//the children
//move all children to oldparent
while (!c->m_children.empty())
{
cluster child = c->m_children.popFrontRet();
child->m_parent = oldParent;
child->m_parent->m_children.pushBack(child);
child->m_it = child->m_parent->m_children.rbegin();
//child++;
}
//recompute depth only if option set AND it makes sense at that point
if (m_updateDepth && m_depthUpToDate)
computeSubTreeDepth(oldParent);
else m_depthUpToDate = false;
}//moved to descendant
else
{
if (m_updateDepth && m_depthUpToDate)
computeSubTreeDepth(c);
else m_depthUpToDate = false;
}
// update postorder for new parent
// we only recompute postorder for all clusters
// because of special cases like move to descendant...
if (newOrder) postOrder();
else postOrder();
m_adjAvailable = false;
//checkPostOrder();
}//move cluster
//*****************
//postorder updates
//leftmostcluster in subtree rooted at c, has postorderpred for subtree
cluster ClusterGraph::leftMostCluster(cluster c) const
{
cluster result = c;
if (!c) return 0;
while (!result->m_children.empty())
{
result = result->m_children.front();
}
return result;
}//leftMostCluster
//searches for predecessor of SUBTREE at c
cluster ClusterGraph::postOrderPredecessor(cluster c) const
{
//all clusters on a path from root to leftmost cluster in tree
//have no predecessor for their subtree
cluster run = c;
ListConstIterator<cluster> it;
do
{
//predecessor of clustertree is 0
if (run == m_rootCluster) return 0;
it = run->m_it;
//a child to the left is the immediate predecessor,
//otherwise we go one level up
if (it == (run->m_parent)->m_children.begin())
run = run->parent();
else return (*(it.pred()));
} while (run);
return 0;
}//postorderpredecessor
//***************
//node assignment
//Assigns a node to a new cluster
void ClusterGraph::assignNode(node v, cluster c)
{
m_adjAvailable = false;
m_postOrderStart = 0;
m_nodeMap[v] = c;
c->m_entries.pushBack(v);
m_itMap[v] = c->m_entries.rbegin();
}
//Reassigns a node to a new cluster
void ClusterGraph::reassignNode(node v, cluster c)
{
OGDF_ASSERT(v->graphOf() == m_pGraph);
OGDF_ASSERT(c->graphOf() == this);
unassignNode(v);
m_nodeMap[v] = c;
c->m_entries.pushBack(v);
m_itMap[v] = c->m_entries.rbegin();
}
//Unassigns a node of cluster
//Note: Nodes can already be unassigned by the nodeDeleted function.
void ClusterGraph::unassignNode(node v)
{
m_adjAvailable = false;
m_postOrderStart = 0;
removeNodeAssignment(v);
}
//---------------------------------------------------------
// Sort clusters in post order
//---------------------------------------------------------
// Start function for post order
void ClusterGraph::postOrder() const
{
SListPure<cluster> L;
postOrder(m_rootCluster,L);
cluster c = 0;
cluster prev = L.popFrontRet();
prev->m_pPrev = 0;
m_postOrderStart = prev;
while (!L.empty())
{
c = L.popFrontRet();
prev->m_pNext = c;
c->m_pPrev = prev;
prev = c;
}
if (c != 0)
c->m_pNext = 0;
else
m_postOrderStart->m_pNext = 0;
#ifdef OGDF_DEBUG
forall_clusters(c, *this)
{
cluster cp = leftMostCluster(c);
OGDF_ASSERT(cp->pPred() == postOrderPredecessor(c))
}
#endif
}
//void ClusterGraph::checkPostOrder() const
//{
// SListPure<cluster> L;
// postOrder(m_rootCluster,L);
// cluster c = 0;
// cluster prev = L.popFrontRet();
// OGDF_ASSERT(prev->m_pPrev == 0);
// while (!L.empty())
// {
// c = L.popFrontRet();
// OGDF_ASSERT(prev->m_pNext == c)
// OGDF_ASSERT(c->m_pPrev == prev)
// prev = c;
// }
// if (c != 0)
// {
// OGDF_ASSERT(c->m_pNext == 0)
// }
// else
// {
// OGDF_ASSERT(m_postOrderStart->m_pNext == 0);
// }
//}
// Recursive function for post order
void ClusterGraph::postOrder(cluster c,SListPure<cluster> &L) const
{
ListIterator<cluster> it;
for (it = c->m_children.begin(); it.valid(); it++)
postOrder((*it),L);
L.pushBack(c);
}
//---------------------------------------------------------
// Methods for debugging
//---------------------------------------------------------
// checks the consistency of the data structure
// (for debugging only)
bool ClusterGraph::consistencyCheck()
{
ClusterArray<bool> visitedClusters((*this),false);
NodeArray<bool> visitedNodes((*m_pGraph),false);
cluster c = 0;
forall_postOrderClusters(c,(*this))
{
visitedClusters[c] = true;
ListIterator<node> itn;
for (itn = c->m_entries.begin(); itn.valid(); itn++)
{
node v = *itn;
if (m_nodeMap[v] != c)
return false;
visitedNodes[v] = true;
}
}
forall_clusters(c,(*this))
if (!visitedClusters[c])
return false;
node v;
forall_nodes(v,(*m_pGraph))
if (!visitedNodes[v])
return false;
return true;
}
bool ClusterGraph::representsCombEmbedding()
{
if (!m_adjAvailable)
return false;
if (!consistencyCheck())
return false;
cluster c = 0;
forall_postOrderClusters(c,(*this))
{
#ifdef OGDF_DEBUG
if (int(ogdf::debugLevel) >= int(dlHeavyChecks)){
cout << "__________________________________________________________________"
<< endl << endl
<< "Testing cluster " << c << endl
<< "Check on AdjList of c" << endl;
adjEntry adjDD;
forall_cluster_adj(adjDD,c)
cout << adjDD << "; ";
cout << endl;
}
#endif
if (c != m_rootCluster)
{
ListIterator<adjEntry> it;
it = c->firstAdj();
adjEntry start = *it;
#ifdef OGDF_DEBUG
if (int(ogdf::debugLevel) >= int(dlHeavyChecks)){
cout << "firstAdj " << start << endl; }
#endif
while (it.valid())
{
AdjEntryArray<bool> visitedAdjEntries((*m_pGraph),false);
ListIterator<adjEntry> succ = it.succ();
adjEntry adj = *it;
adjEntry succAdj;
if (succ.valid())
succAdj = *succ;
else
succAdj = start; // reached the last outgoing edge
#ifdef OGDF_DEBUG
if (int(ogdf::debugLevel) >= int(dlHeavyChecks)){
cout << "Check next " << endl;
cout << "current in adj list of" << adj << endl;
cout << "succ in adj list of c " << succAdj << endl;
cout << "cyclic succ in outer face " << adj->cyclicSucc() << endl;
}
#endif
if (adj->cyclicSucc() != succAdj)
// run along the outer face of the cluster
// until you find the next outgoing edge
{
adjEntry next = adj->cyclicSucc();
adjEntry twin = next->twin();
#ifdef OGDF_DEBUG
if (int(ogdf::debugLevel) >= int(dlHeavyChecks)){
cout << "Running along the outer face ... " << endl;
cout << "next adj " << next << endl;
cout << "twin adj " << twin << endl;
}
#endif
if (visitedAdjEntries[twin])
return false;
visitedAdjEntries[twin] = true;
while ( next != succAdj)
{
next = twin->cyclicSucc();
twin = next->twin();
#ifdef OGDF_DEBUG
if (int(ogdf::debugLevel) >= int(dlHeavyChecks)){
cout << "Running along the outer face ... " << endl;
cout << "next adj " << next << endl;
cout << "twin adj " << twin << endl;
}
#endif
if (visitedAdjEntries[twin])
return false;
visitedAdjEntries[twin] = true;
}
}
// else
// next edge is also outgoing
it = succ;
}
}
}
return true;
}
// registers a cluster array
ListIterator<ClusterArrayBase*> ClusterGraph::registerArray(
ClusterArrayBase *pClusterArray) const
{
return m_regClusterArrays.pushBack(pClusterArray);
}
// unregisters a cluster array
void ClusterGraph::unregisterArray(ListIterator<ClusterArrayBase*> it) const
{
m_regClusterArrays.del(it);
}
//! Registers a ClusterGraphObserver.
ListIterator<ClusterGraphObserver*> ClusterGraph::registerObserver(ClusterGraphObserver *pObserver) const
{
return m_regObservers.pushBack(pObserver);
}
//! Unregisters a ClusterGraphObserver.
void ClusterGraph::unregisterObserver(ListIterator<ClusterGraphObserver*> it) const
{
m_regObservers.del(it);
}
//---------------------------------------------------------
// Methods for printing
//---------------------------------------------------------
// writes graph in GML format to file fileName
void ClusterGraph::writeGML(const char *fileName)
{
ofstream os(fileName);
writeGML(os);
}
// writes graph in GML format to output stream os
void ClusterGraph::writeGML(ostream &os)
{
NodeArray<int> nId(*m_pGraph);
ClusterArray<int> cId(*this);
int nextId = 0;
os << "Creator \"ogdf::ClusterGraph::writeGML\"\n";
os << "graph [\n";
os << " directed 1\n";
node v;
forall_nodes(v,*m_pGraph) {
os << " node [\n";
os << " id " << (nId[v] = nextId++) << "\n";
os << " ]\n"; // node
}
edge e;
forall_edges(e,*m_pGraph) {
os << " edge [\n";
os << " source " << nId[e->source()] << "\n";
os << " target " << nId[e->target()] << "\n";
os << " ]\n"; // edge
}
String scip = " ";
nextId = 0;
writeCluster(os,nId,cId,nextId,m_rootCluster,scip);
os << "]\n"; // graph
}
// recursively write the cluster structure in GML
void ClusterGraph::writeCluster(ostream &os,
NodeArray<int> &nId,
ClusterArray<int> & cId,
int &nextId,
cluster c,
String scip)
{
String newScip = scip;
newScip+=" ";
os << scip << "cluster [\n";
os << scip << " id " << (cId[c] = nextId++) << "\n";
ListIterator<cluster> it;
for (it = c->m_children.begin(); it.valid(); it++)
writeCluster(os,nId,cId,nextId,*it,newScip);
ListIterator<node> itn;
for (itn = c->m_entries.begin(); itn.valid(); itn++)
os << scip << " node " << nId[*itn] << "\n";
os << scip << "]\n"; // cluster
}
// recursively write the cluster structure in GraphWin GML
void ClusterGraph::writeGraphWinCluster(ostream &os,
NodeArray<int> &nId,
NodeArray<String> &nStr,
ClusterArray<int> & cId,
ClusterArray<String> & cStr,
int &nextId,
cluster c,
String scip)
{
String newScip = scip;
newScip+=" ";
if (c == m_rootCluster)
os << scip << "rootcluster [\n";
else
{
os << scip << "cluster [\n";
// os << scip << " id " << (cId[c] = nextId++) << "\n";
os << scip << " id " << c->index() << "\n";
char newLabel[124];
// sprintf(newLabel,"C%d",cId[c]);
ogdf::sprintf(newLabel,124,"C%d",c->index());
cStr[c] = newLabel;
os << scip << " label \"" << cStr[c] << "\"\n";
}
ListIterator<cluster> it;
for (it = c->m_children.begin(); it.valid(); it++)
writeGraphWinCluster(os,nId,nStr,cId,cStr,nextId,*it,newScip);
ListIterator<node> itn;
for (itn = c->m_entries.begin(); itn.valid(); itn++)
os << scip << " vertex \"v" << nId[*itn] << "\"\n";
os << scip << "]\n"; // cluster
}
//++++++++++++++++++++++++++++++++++++++++++++
//reading graph, cluster structure
bool ClusterGraph::readClusterGML(const char* fileName,
Graph& G)
{
ifstream is(fileName);
if (!is)
return false; // couldn't open file
return readClusterGML(is, G);
}
bool ClusterGraph::readClusterGML(istream& is,
Graph& G)
{
bool result;
GmlParser gml(is);
if (gml.error())
return false;
result = gml.read(G);
if (!result) return false;
return gml.readCluster(G, *this);
}
// read Cluster Graph from OGML file
//bool ClusterGraph::readClusterGraphOGML(const char* fileName,
// ClusterGraph& CG,
// Graph& G)
//{
// ifstream is(fileName);
// // not able to open file
// if (!is) return false;
//
// OgmlParser *op = new OgmlParser();
// // build graph
// // method read contains the validation
// if (!op->read(fileName, G, CG, *this)){
// delete(op);
// cerr << "ERROR occured while reading. Aborting." << endl << flush;
// return false;
// }
//
// delete(op);
// return true;
//};
} // end namespace ogdf
//****************************************************************
ostream &operator<<(ostream &os, ogdf::cluster c)
{
if (c) os << c->index(); else os << "nil";
return os;
}
|