1 /** \file 2 * \brief Declaration of class BCTree 3 * 4 * \author Jan Papenfuß 5 * 6 * \par License: 7 * This file is part of the Open Graph Drawing Framework (OGDF). 8 * 9 * \par 10 * Copyright (C)<br> 11 * See README.md in the OGDF root directory for details. 12 * 13 * \par 14 * This program is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU General Public License 16 * Version 2 or 3 as published by the Free Software Foundation; 17 * see the file LICENSE.txt included in the packaging of this file 18 * for details. 19 * 20 * \par 21 * This program is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * GNU General Public License for more details. 25 * 26 * \par 27 * You should have received a copy of the GNU General Public 28 * License along with this program; if not, see 29 * http://www.gnu.org/copyleft/gpl.html 30 */ 31 32 #pragma once 33 34 #include <ogdf/basic/EdgeArray.h> 35 #include <ogdf/basic/NodeArray.h> 36 #include <ogdf/basic/SList.h> 37 38 namespace ogdf { 39 40 /** 41 * Static BC-trees. 42 * 43 * @ingroup graph-decomp 44 * 45 * This class provides static BC-trees.\n 46 * The data structure consists of three parts: 47 * - The original graph itself (\a G) is represented by an ordinary ogdf::Graph 48 * structure. 49 * - The BC-tree (\a B) is represented by an ogdf::Graph structure, each 50 * vertex representing a B-component or a C-component. 51 * - The biconnected components graph (\a H), which contains a set of copies of 52 * the biconnected components and the cut-vertices of the original graph, 53 * combined but not interconnected within a single ogdf::Graph structure. 54 */ 55 class OGDF_EXPORT BCTree { 56 57 public: 58 //! Enumeration type for characterizing the vertices of the original graph. 59 enum class GNodeType { 60 //! an ordinary vertex, i.e. not a cut-vertex 61 Normal, 62 //! a cut-vertex 63 CutVertex 64 }; 65 66 //! Enumeration type for characterizing the BC-tree-vertices. 67 enum class BNodeType { 68 //! a vertex representing a B-component 69 BComp, 70 //! a vertex representing a C-component 71 CComp 72 }; 73 74 protected: 75 //! The original graph. 76 Graph& m_G; 77 78 /** 79 * The BC-tree. 80 * 81 * Each vertex is representing a biconnected component (B-component) or a 82 * cut-vertex (C-component) of the original graph. 83 */ 84 Graph m_B; 85 86 /** 87 * The biconnected components graph. 88 * 89 * This graph contains copies of the biconnected components (B-components) 90 * and the cut-vertices (C-components) of the original graph. The copies of the 91 * B- and C-components of the original graph are not interconnected, i.e. the 92 * biconnected components graph is representing B-components as isolated 93 * biconnected subgraphs and C-components as isolated single vertices. Thus the 94 * copies of the edges and non-cut-vertices of the original graph are 95 * unambiguous, but each cut-vertex of the original graph being common to a 96 * C-component and several B-components appears multiple times. 97 */ 98 mutable Graph m_H; 99 100 //! @{ 101 //! The number of B-components. 102 int m_numB; 103 104 //! The number of C-components. 105 int m_numC; 106 //! @} 107 108 /** @{ 109 * Array of marks for the vertices of the original graph. 110 * 111 * They are needed during the generation of the BC-tree by DFS method. 112 */ 113 NodeArray<bool> m_gNode_isMarked; 114 115 /** 116 * An injective mapping vertices(\a G) -> vertices(\a H). 117 * 118 * For each vertex \a vG of the original graph: 119 * - If \a vG is not a cut-vertex, then m_gNode_hNode[\a vG] is the very vertex 120 * of the biconnected components graph corresponding to \a vG. 121 * - If \a vG is a cut-vertex, then m_gNode_hNode[\a vG] is the very vertex of 122 * the biconnected components graph representing the C-component, which \a vG 123 * is belonging to, as a single isolated vertex. 124 */ 125 NodeArray<node> m_gNode_hNode; 126 127 /** 128 * A bijective mapping edges(\a G) -> edges(\a H). 129 * 130 * For each edge \a eG of the original graph, m_gEdge_hEdge[\a eG] is the very 131 * edge of the biconnected components graph corresponding to \a eG. 132 */ 133 EdgeArray<edge> m_gEdge_hEdge; 134 //! @} 135 136 //! @{ 137 //! Array that contains the type of each BC-tree-vertex. 138 NodeArray<BNodeType> m_bNode_type; 139 140 /** 141 * Array of marks for the BC-tree-vertices. 142 * 143 * They are needed for searching for the nearest common ancestor of two 144 * vertices of the BC-tree. 145 */ 146 mutable NodeArray<bool> m_bNode_isMarked; 147 148 /** 149 * Array that contains for each BC-tree-vertex the representantive of its 150 * parent within the subgraph in the biconnected components graph belonging to 151 * the biconnected component represented by the respective BC-tree-vertex. 152 * 153 * For each vertex \a vB of the BC-tree: 154 * - If \a vB is representing a B-component and \a vB is the root of the 155 * BC-tree, then m_bNode_hRefNode[\a vB] is \a nullptr. 156 * - If \a vB is representing a B-component and \a vB is not the root of the 157 * BC-tree, then m_bNode_hRefNode[\a vB] is the very vertex of the 158 * biconnected components graph which is the duplicate of the cut-vertex 159 * represented by the parent of \a vB <em>in the copy of the B-component 160 * represented by</em> \a vB. 161 * - If \a vB is representing a C-component, then m_bNode_hRefNode[\a vB] 162 * is the single isolated vertex of the biconnected components graph 163 * corresponding to the cut-vertex which the C-component consists of, 164 * irrespective of whether \a vB is the root of the BC-tree or not. 165 */ 166 NodeArray<node> m_bNode_hRefNode; 167 168 /** 169 * Array that contains for each BC-tree-vertex the representant of 170 * itself within the subgraph in the biconnected components graph belonging to 171 * the biconnected component represented by the parent of the respective 172 * BC-tree-vertex. 173 * 174 * - If \a vB is the root of the BC-tree, then m_bNode_hParNode[\a vB] is 175 * \a nullptr. 176 * - If \a vB is representing a B-component and \a vB is not the root of the 177 * BC-tree, then m_bNode_hParNode[\a vB] is the single isolated vertex 178 * of the biconnected components graph corresponding to the very cut-vertex, 179 * which the C-component represented by <em>the parent of</em> \a vB consists 180 * of. 181 * - If \a vB is representing to a C-component and \a vB is not the root of the 182 * BC-tree, then m_bNode_hParNode[\a vB] is the very vertex of the 183 * biconnected components graph, which is the duplicate of the cut-vertex, 184 * which the C-component consists of, <em>in the copy of the B-component 185 * represented by the parent of</em> \a vB. 186 */ 187 NodeArray<node> m_bNode_hParNode; 188 189 /** 190 * Array that contains for each BC-tree-vertex a linear list of the 191 * edges of the biconnected components graph belonging to the biconnected 192 * component represented by the respective BC-tree-vertex. 193 * 194 * For each vertex \a vB of the BC-tree: 195 * - If \a vB is representing a B-component, then m_bNode_hEdges[\a vB] is a 196 * linear list of the edges of the biconnected components graph corresponding 197 * to the edges of the original graph belonging to the B-component. 198 * - If \a vB is representing a C-component, then m_bNode_hEdges[\a vB] is an 199 * empty list. 200 */ 201 NodeArray<SList<edge> > m_bNode_hEdges; 202 203 /** 204 * Array that contains for each BC-tree-vertex the number of vertices 205 * belonging to the biconnected component represented by the respective 206 * BC-tree-vertex. 207 * 208 * For each vertex \a vB of the BC-tree: 209 * - If \a vB is representing a B-component, then m_bNode_numNodes[\a vB] is 210 * the number of vertices belonging to the B-component, cut-vertices 211 * inclusive. 212 * - If \a vB is representing a C-component, then m_bNode_numNodes[\a vB] is 1. 213 */ 214 NodeArray<int> m_bNode_numNodes; 215 //! @} 216 217 /** @{ 218 * A surjective mapping vertices(\a H) -> vertices(\a B). 219 * 220 * For each vertex \a vH of the biconnected components graph, 221 * m_hNode_bNode[\a vH] is the very BC-tree-vertex representing the B- or 222 * C-component with respect to the copy of the very block or representation 223 * of a cut-vertex, which vH is belonging to. 224 */ 225 mutable NodeArray<node> m_hNode_bNode; 226 227 /** 228 * A surjective mapping edges(\a H) -> vertices(\a B). 229 * 230 * For each edge \a eH of the biconnected components graph, 231 * m_hEdge_bNode[\a eH] is the very BC-tree-vertex representing the unambiguous 232 * B-component, which \a eH is belonging to. 233 */ 234 mutable EdgeArray<node> m_hEdge_bNode; 235 236 /** 237 * A surjective mapping vertices(\a H) -> vertices(\a G). 238 * 239 * For each vertex \a vH of the biconnected components graph, 240 * m_hNode_gNode[\a vH] is the vertex of the original graph which \a vH is 241 * corresponding to. 242 */ 243 NodeArray<node> m_hNode_gNode; 244 245 /** 246 * A bijective mapping edges(\a H) -> edges(\a G). 247 * 248 * For each edge \a eH of the biconnected components graph, 249 * m_hEdge_gEdge[\a eH] is the edge of the original graph which \a eH is 250 * corresponding to. 251 */ 252 EdgeArray<edge> m_hEdge_gEdge; 253 //! @} 254 255 /** @{ 256 * Temporary variable. 257 * 258 * It is needed for the generation of the BC-tree by DFS method. It has to be a 259 * member of class BCTree due to recursive calls to biComp(). 260 */ 261 int m_count; 262 263 /** 264 * Temporary array. 265 * 266 * It is needed for the generation of the BC-tree by DFS method. It has to be a 267 * member of class BCTree due to recursive calls to biComp(). 268 */ 269 270 NodeArray<int> m_number; 271 /** 272 * Temporary array. 273 * 274 * It is needed for the generation of the BC-tree by DFS method. It has to be a 275 * member of class BCTree due to recursive calls to biComp(). 276 */ 277 NodeArray<int> m_lowpt; 278 279 /** 280 * Temporary stack. 281 * 282 * It is needed for the generation of the BC-tree by DFS method. It has to be a 283 * member of class BCTree due to recursive calls to biComp(). 284 */ 285 ArrayBuffer<adjEntry> m_eStack; 286 287 /** 288 * Temporary array. 289 * 290 * It is needed for the generation of the BC-tree by DFS method. It has to be a 291 * member of class BCTree due to recursive calls to biComp(). 292 */ 293 NodeArray<node> m_gtoh; 294 295 /** 296 * Temporary list. 297 * 298 * It is needed for the generation of the BC-tree by DFS method. It has to be a 299 * member of class BCTree due to recursive calls to biComp(). 300 */ 301 SList<node> m_nodes; 302 303 /** @} 304 * Initialization. 305 * 306 * initializes all data structures and generates the BC-tree and the 307 * biconnected components graph by call to biComp(). 308 * \param vG is the vertex of the original graph which the DFS algorithm starts 309 * with. 310 */ 311 void init (node vG); 312 //! @} 313 314 /** 315 * Initialization for not connected graphs 316 * 317 * initializes all data structures and generates a forest of BC-trees and the 318 * biconnected components graph by call to biComp(). 319 * \param vG is the vertex of the original graph which the DFS algorithm starts 320 * first with. 321 */ 322 void initNotConnected (node vG); 323 324 /** 325 * Generates the BC-tree and the biconnected components graph 326 * recursively. 327 * 328 * The DFS algorithm is based on J. Hopcroft and R. E. Tarjan: Algorithm 447: 329 * Efficient algorithms for graph manipulation. <em>Comm. ACM</em>, 16:372-378 330 * (1973). 331 */ 332 void biComp (adjEntry adjuG, node vG); 333 334 /** @{ 335 * Returns the parent of a given BC-tree-vertex. 336 * \param vB is a vertex of the BC-tree or \a nullptr. 337 * \return the parent of \p vB in the BC-tree structure, if \p vB is not the 338 * root of the BC-tree, and \c nullptr, if \p vB is \c nullptr or the root of the 339 * BC-tree. 340 */ 341 virtual node parent (node vB) const; 342 343 /** 344 * Calculates the nearest common ancestor of two vertices of the 345 * BC-tree. 346 * \param uB is a vertex of the BC-tree. 347 * \param vB is a vertex of the BC-tree. 348 * \return the nearest common ancestor of \p uB and \p vB. 349 */ 350 node findNCA (node uB, node vB) const; 351 352 //! @} 353 354 public: 355 /** 356 * A constructor. 357 * 358 * This constructor does only call init() or initNotConnected(). 359 * BCTree(\p G) is equivalent to BCTree(\p G, \c G.firstNode()). 360 * \param G is the original graph. 361 * \param callInitConnected decides which init is called, default call is init() 362 */ m_G(G)363 explicit BCTree (Graph& G, bool callInitConnected = false) : m_G(G), m_eStack(G.numberOfEdges()) { 364 if (!callInitConnected) { 365 init(G.firstNode()); 366 } 367 else { 368 initNotConnected(G.firstNode()); 369 } 370 } 371 372 /** 373 * A constructor. 374 * 375 * This constructor does only call init() or initNotConnected(). 376 * \param G is the original graph. 377 * \param vG is the vertex of the original graph which the DFS algorithm starts 378 * \param callInitConnected decides which init is called, default call is init() 379 */ m_G(G)380 BCTree (Graph& G, node vG, bool callInitConnected = false) : m_G(G), m_eStack(G.numberOfEdges()) { 381 if (!callInitConnected) 382 init(vG); 383 else initNotConnected(vG); 384 } 385 386 //! Virtual destructor. ~BCTree()387 virtual ~BCTree () { } 388 389 //! Returns the original graph. originalGraph()390 const Graph& originalGraph () const { return m_G; } 391 //! Returns the BC-tree graph. bcTree()392 const Graph& bcTree () const { return m_B; } 393 //! Returns the biconnected components graph. auxiliaryGraph()394 const Graph& auxiliaryGraph () const { return m_H; } 395 //! @} 396 397 //! Returns the number of B-components. numberOfBComps()398 int numberOfBComps () const { return m_numB; } 399 //! Returns the number of C-components. numberOfCComps()400 int numberOfCComps () const { return m_numC; } 401 //! @} 402 403 /** @{ 404 * Returns the type of a vertex of the original graph. 405 * \param vG is a vertex of the original graph. 406 * \return the type of \p vG. 407 */ typeOfGNode(node vG)408 GNodeType typeOfGNode (node vG) const { return m_bNode_type[m_hNode_bNode[m_gNode_hNode[vG]]]==BNodeType::BComp ? GNodeType::Normal : GNodeType::CutVertex; } 409 410 /** 411 * Returns a BC-tree-vertex representing a biconnected component which a 412 * given vertex of the original graph is belonging to. 413 * \param vG is a vertex of the original graph. 414 * \return a vertex of the BC-tree: 415 * - If \p vG is not a cut-vertex, then typeOfGNode(\p vG) returns the very 416 * vertex of the BC-tree representing the unambiguous B-component which \p vG 417 * is belonging to. 418 * - If \p vG is a cut-vertex, then typeOfGNode(\p vG) returns the very vertex 419 * of the BC-tree representing the unambiguous C-component which \p vG is 420 * belonging to. 421 */ bcproper(node vG)422 virtual node bcproper (node vG) const { return m_hNode_bNode[m_gNode_hNode[vG]]; } 423 424 /** 425 * Returns the BC-tree-vertex representing the biconnected component 426 * which a given edge of the original graph is belonging to. 427 * \param eG is an edge of the original graph. 428 * \return the vertex of the BC-tree representing the B-component which \p eG 429 * is belonging to. 430 */ bcproper(edge eG)431 virtual node bcproper (edge eG) const { return m_hEdge_bNode[m_gEdge_hEdge[eG]]; } 432 433 /** 434 * Returns a vertex of the biconnected components graph corresponding to 435 * a given vertex of the original graph. 436 * \param vG is a vertex of the original graph. 437 * \return a vertex of the biconnected components graph: 438 * - If \p vG is not a cut-vertex, then rep(\p vG) returns the very vertex of 439 * the biconnected components graph corresponding to \p vG. 440 * - If \p vG is a cut-vertex, then rep(\p vG) returns the very vertex of the 441 * biconnected components graph representing the C-component which \p vG is 442 * belonging to. 443 */ rep(node vG)444 node rep (node vG) const { return m_gNode_hNode[vG]; } 445 446 /** 447 * Returns the edge of the biconnected components graph corresponding to 448 * a given edge of the original graph. 449 * \param eG is an edge of the original graph. 450 * \return the edge of the biconnected components graph corresponding to \p eG. 451 */ rep(edge eG)452 edge rep (edge eG) const { return m_gEdge_hEdge[eG]; } 453 //! @} 454 455 /** @{ 456 * returns the vertex of the original graph which a given vertex of the 457 * biconnected components graph is corresponding to. 458 * \param vH is a vertex of the biconnected components graph. 459 * \return the vertex of the original graph which \p vH is corresponding to. 460 */ original(node vH)461 node original (node vH) { return m_hNode_gNode[vH]; } 462 463 /** 464 * Returns the edge of the original graph which a given edge of the 465 * biconnected components graph is corresponding to. 466 * \param eH is an edge of the biconnected components graph. 467 * \return the edge of the original graph which \p eH is corresponding to. 468 */ original(edge eH)469 edge original (edge eH) const { return m_hEdge_gEdge[eH]; } 470 //! @} 471 472 /** @{ 473 * Returns the type of the biconnected component represented by a given 474 * BC-tree-vertex. 475 * \param vB is a vertex of the BC-tree. 476 * \return the type of the biconnected component represented by \p vB. 477 */ typeOfBNode(node vB)478 BNodeType typeOfBNode (node vB) const { return m_bNode_type[vB]; } 479 480 /** 481 * Returns a linear list of the edges of the biconnected components 482 * graph belonging to the biconnected component represented by a given 483 * BC-tree-vertex. 484 * \param vB is a vertex of the BC-tree. 485 * \return a linear list of edges of the biconnected components graph: 486 * - If \p vB is representing a B-component, then the edges in the list are the 487 * copies of the edges belonging to the B-component. 488 * - If \p vB is representing a C-component, then the list is empty. 489 */ hEdges(node vB)490 const SList<edge>& hEdges (node vB) const { return m_bNode_hEdges[vB]; } 491 492 /** 493 * Returns the number of edges belonging to the biconnected component 494 * represented by a given BC-tree-vertex. 495 * \param vB is a vertex of the BC-tree. 496 * \return the number of edges belonging to the B- or C-component represented 497 * by \p vB, particularly 0 if it is a C-component. 498 */ numberOfEdges(node vB)499 int numberOfEdges (node vB) const { return m_bNode_hEdges[vB].size(); } 500 501 /** 502 * Returns the number of vertices belonging to the biconnected component 503 * represented by a given BC-tree-vertex. 504 * \param vB is a vertex of the BC-tree. 505 * \return the number of vertices belonging to the B- or C-component 506 * represented by \p vB, cut-vertices inclusive, particularly 1 if it is a 507 * C-component. 508 */ numberOfNodes(node vB)509 int numberOfNodes (node vB) const { return m_bNode_numNodes[vB]; } 510 //! @} 511 512 /** @{ 513 * Returns the BC-tree-vertex representing the B-component which two 514 * given vertices of the original graph are belonging to. 515 * \param uG is a vertex of the original graph. 516 * \param vG is a vertex of the original graph. 517 * \return If \p uG and \p vG are belonging to the same B-component, the very 518 * vertex of the BC-tree representing this B-component is returned. Otherwise, 519 * \a nullptr is returned. This member function returns the representative of the 520 * correct B-component even if \p uG or \p vG or either are cut-vertices and 521 * are therefore belonging to C-components, too. 522 */ 523 node bComponent (node uG, node vG) const; 524 525 /** 526 * Calculates a path in the BC-tree. 527 * \param sG is a vertex of the original graph. 528 * \param tG is a vertex of the original graph. 529 * \return the path from bcproper(\p sG) to bcproper(\p tG) in the BC-tree as a 530 * linear list of vertices. 531 * \post <b>The SList<node> instance is created by this function and has to be 532 * destructed by the caller!</b> 533 */ 534 SList<node>& findPath (node sG, node tG) const; 535 536 /** 537 * Calculates a path in the BC-tree. 538 * \param sB is a vertex of the BC-tree. 539 * \param tB is a vertex of the BC-tree. 540 * \return the path from (\p sB) to bcproper(\p tB) in the BC-tree as a 541 * linear list of vertices. 542 * \post <b>The SList<node> instance is created by this function and has to be 543 * destructed by the caller!</b> 544 */ 545 SList<node>* findPathBCTree (node sB, node tB) const; 546 547 /** 548 * Returns a vertex of the biconnected components graph corresponding to 549 * a given vertex of the original graph and belonging to the representation of 550 * a certain biconnected component given by a vertex of the BC-tree. 551 * \param uG is a vertex of the original graph. 552 * \param vB is a vertex of the BC-tree. 553 * \return a vertex of the biconnected components graph: 554 * - If \p uG is belonging to the biconnected component represented by \p vB, 555 * then repVertex(\p uG, \p vB) returns the very vertex of the biconnected 556 * components graph corresponding to \p uG within the representation of 557 * \p vB. 558 * - Otherwise, repVertex(\p uG, \p vB) returns \a nullptr. 559 */ 560 virtual node repVertex (node uG, node vB) const; 561 562 /** 563 * Returns the copy of a cut-vertex in the biconnected components graph 564 * which belongs to a certain B-component and leads to another B-component. 565 * 566 * If two BC-tree-vertices are neighbours, then the biconnected components 567 * represented by them have exactly one cut-vertex in common. But there are 568 * several copies of this cut-vertex in the biconnected components graph, 569 * namely one copy for each biconnected component which the cut-vertex is 570 * belonging to. The member function rep() had been designed for returning the 571 * very copy of the cut-vertex belonging to the copy of the unambiguous 572 * C-component which it is belonging to, whereas this member function is 573 * designed to return the very copy of the cut-vertex connecting two 574 * biconnected components which belongs to the copy of the second one. 575 * \param uB is a vertex of the BC-tree. 576 * \param vB is a vertex of the BC-tree. 577 * \return a vertex of the biconnected components graph: 578 * - If \p uB == \p vB and they are representing a B-component, then 579 * cutVertex(\p uB, \p vB) returns \a nullptr. 580 * - If \p uB == \p vB and they are representing a C-component, then 581 * cutVertex(\p uB, \p vB) returns the single isolated vertex of the 582 * biconnected components graph which is the copy of the C-component. 583 * - If \p uB and \p vB are \a neighbours in the BC-tree, then there exists 584 * a cut-vertex leading from the biconnected component represented by \p vB 585 * to the biconnected component represented by \p uB. cutVertex(\p uB, \p vB) 586 * returns the very copy of this vertex within the biconnected components 587 * graph which belongs to the copy of the biconnected component represented 588 * by \p vB. 589 * - Otherwise, cutVertex(\p uB, \p vB) returns \a nullptr. 590 */ 591 virtual node cutVertex (node uB, node vB) const; 592 593 //! @} 594 private: 595 //! Copy constructor is undefined! 596 BCTree(const BCTree &) = delete; 597 598 //! Assignment operator is undefined! 599 BCTree &operator=(const BCTree &) = delete; 600 601 void initBasic(node vG); 602 void initEdges(); 603 }; 604 605 } 606