Path: blob/master/sage/graphs/planarity/graphOuterplanarObstruction.c
4097 views
/*1Planarity-Related Graph Algorithms Project2Copyright (c) 1997-2010, John M. Boyer3All rights reserved. Includes a reference implementation of the following:45* John M. Boyer. "Simplified O(n) Algorithms for Planar Graph Embedding,6Kuratowski Subgraph Isolation, and Related Problems". Ph.D. Dissertation,7University of Victoria, 2001.89* John M. Boyer and Wendy J. Myrvold. "On the Cutting Edge: Simplified O(n)10Planarity by Edge Addition". Journal of Graph Algorithms and Applications,11Vol. 8, No. 3, pp. 241-273, 2004.1213* John M. Boyer. "A New Method for Efficiently Generating Planar Graph14Visibility Representations". In P. Eades and P. Healy, editors,15Proceedings of the 13th International Conference on Graph Drawing 2005,16Lecture Notes Comput. Sci., Volume 3843, pp. 508-511, Springer-Verlag, 2006.1718Redistribution and use in source and binary forms, with or without modification,19are permitted provided that the following conditions are met:2021* Redistributions of source code must retain the above copyright notice, this22list of conditions and the following disclaimer.2324* Redistributions in binary form must reproduce the above copyright notice, this25list of conditions and the following disclaimer in the documentation and/or26other materials provided with the distribution.2728* Neither the name of the Planarity-Related Graph Algorithms Project nor the names29of its contributors may be used to endorse or promote products derived from this30software without specific prior written permission.3132THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"33AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE34IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE35DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR36ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES37(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;38LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON39ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT40(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS41SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.42*/4344#include "graph.h"4546/* Imported functions */4748extern void _FillVisitedFlags(graphP, int);49extern void _ClearIsolatorContext(graphP theGraph);5051extern int _GetNextVertexOnExternalFace(graphP theGraph, int curVertex, int *pPrevLink);52extern int _JoinBicomps(graphP theGraph);5354extern int _InitializeNonplanarityContext(graphP theGraph, int I, int R);55extern int _MarkHighestXYPath(graphP theGraph);5657extern int _FindUnembeddedEdgeToAncestor(graphP theGraph, int cutVertex, int *pAncestor, int *pDescendant);58extern int _FindUnembeddedEdgeToCurVertex(graphP theGraph, int cutVertex, int *pDescendant);59extern int _FindUnembeddedEdgeToSubtree(graphP theGraph, int ancestor, int SubtreeRoot, int *pDescendant);6061extern int _MarkPathAlongBicompExtFace(graphP theGraph, int startVert, int endVert);6263extern int _AddAndMarkEdge(graphP theGraph, int ancestor, int descendant);6465extern int _DeleteUnmarkedVerticesAndEdges(graphP theGraph);6667/* Private function declarations (exported to system) */6869int _IsolateOuterplanarObstruction(graphP theGraph, int I, int R);7071int _ChooseTypeOfNonOuterplanarityMinor(graphP theGraph, int I, int R);7273int _IsolateOuterplanarityObstructionA(graphP theGraph);74int _IsolateOuterplanarityObstructionB(graphP theGraph);75int _IsolateOuterplanarityObstructionE(graphP theGraph);7677/****************************************************************************78_ChooseTypeOfNonOuterplanarityMinor()79A constant time implementation is easily feasible but only constant amortized80time is needed for the outerplanarity obstruction isolation, which also81benefits from having the bicomp rooted by R oriented.82If an extension algorithm requires constant actual time, then this function83should not be used and instead the minor should be decided without orienting84the bicomp.85****************************************************************************/8687int _ChooseTypeOfNonOuterplanarityMinor(graphP theGraph, int I, int R)88{89int N, X, Y, W;9091// Create the initial non-outerplanarity obstruction isolator state.92if (_InitializeNonplanarityContext(theGraph, I, R) != OK)93return NOTOK;9495N = theGraph->N;96R = theGraph->IC.r;97X = theGraph->IC.x;98Y = theGraph->IC.y;99W = theGraph->IC.w;100101// If the root copy is not a root copy of the current vertex I,102// then the Walkdown terminated on a descendant bicomp, which is Minor A.103if (theGraph->V[R - N].DFSParent != I)104{105theGraph->IC.minorType |= MINORTYPE_A;106return OK;107}108109// If W has a pertinent child bicomp, then we've found Minor B.110// Notice this is different from planarity, in which minor B is indicated111// only if the pertinent child bicomp is also externally active under the112// planarity processing model (i.e. future pertinent).113if (theGraph->V[W].pertinentBicompList != NIL)114{115theGraph->IC.minorType |= MINORTYPE_B;116return OK;117}118119// The only other result is minor E (we will search for the X-Y path later)120theGraph->IC.minorType |= MINORTYPE_E;121return OK;122}123124/****************************************************************************125_IsolateOuterplanarObstruction()126****************************************************************************/127128int _IsolateOuterplanarObstruction(graphP theGraph, int I, int R)129{130int RetVal;131132/* A subgraph homeomorphic to K_{2,3} or K_4 will be isolated by using the visited133flags, 1=keep edge/vertex and 0=omit. Here we initialize to omit all, then we134subsequently set visited to 1 on all edges and vertices in the homeomorph. */135136_FillVisitedFlags(theGraph, 0);137138/* Next we determineg which of the non-outerplanarity Minors was encountered139and the principal bicomp on which the isolator will focus attention. */140141if (_ChooseTypeOfNonOuterplanarityMinor(theGraph, I, R) != OK)142return NOTOK;143144/* Find the path connecting the pertinent vertex w with the current vertex v */145146if (theGraph->IC.minorType & MINORTYPE_B)147{148isolatorContextP IC = &theGraph->IC;149int SubtreeRoot = LCGetPrev(theGraph->BicompLists,150theGraph->V[IC->w].pertinentBicompList, NIL);151152if (_FindUnembeddedEdgeToSubtree(theGraph, IC->v, SubtreeRoot, &IC->dw) != TRUE)153return NOTOK;154}155else156{157isolatorContextP IC = &theGraph->IC;158159if (_FindUnembeddedEdgeToCurVertex(theGraph, IC->w, &IC->dw) != TRUE)160return NOTOK;161}162163/* For minor E, we need to find and mark an X-Y path */164165if (theGraph->IC.minorType & MINORTYPE_E)166{167if (_MarkHighestXYPath(theGraph) != TRUE)168return NOTOK;169}170171/* Call the appropriate isolator */172173if (theGraph->IC.minorType & MINORTYPE_A)174RetVal = _IsolateOuterplanarityObstructionA(theGraph);175else if (theGraph->IC.minorType & MINORTYPE_B)176RetVal = _IsolateOuterplanarityObstructionB(theGraph);177else if (theGraph->IC.minorType & MINORTYPE_E)178RetVal = _IsolateOuterplanarityObstructionE(theGraph);179else180RetVal = NOTOK;181182/* Delete the unmarked edges and vertices, and return */183184if (RetVal == OK)185RetVal = _DeleteUnmarkedVerticesAndEdges(theGraph);186187return RetVal;188}189190/****************************************************************************191_IsolateOuterplanarityObstructionA(): Isolate a K2,3 homeomorph192****************************************************************************/193194int _IsolateOuterplanarityObstructionA(graphP theGraph)195{196isolatorContextP IC = &theGraph->IC;197198if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->r) != OK ||199theGraph->functions.fpMarkDFSPath(theGraph, IC->v, IC->r) != OK ||200theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||201_JoinBicomps(theGraph) != OK ||202_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)203return NOTOK;204205return OK;206}207208/****************************************************************************209_IsolateOuterplanarityObstructionB(): Isolate a K2,3 homeomorph210****************************************************************************/211212int _IsolateOuterplanarityObstructionB(graphP theGraph)213{214isolatorContextP IC = &theGraph->IC;215216if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->r) != OK ||217theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||218_JoinBicomps(theGraph) != OK ||219_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)220return NOTOK;221222return OK;223}224225/****************************************************************************226_IsolateOuterplanarityObstructionE(): Isolate a K4 homeomorph227****************************************************************************/228229int _IsolateOuterplanarityObstructionE(graphP theGraph)230{231isolatorContextP IC = &theGraph->IC;232233if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->r) != OK ||234theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||235_JoinBicomps(theGraph) != OK ||236_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)237return NOTOK;238239return OK;240}241242243