Path: blob/master/sage/graphs/planarity/graphK23Search.c
4091 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);4950extern int _GetNextVertexOnExternalFace(graphP theGraph, int curVertex, int *pPrevLink);51extern int _OrientVerticesInBicomp(graphP theGraph, int BicompRoot, int PreserveSigns);52extern int _JoinBicomps(graphP theGraph);5354extern int _MarkHighestXYPath(graphP theGraph);5556extern int _FindUnembeddedEdgeToAncestor(graphP theGraph, int cutVertex, int *pAncestor, int *pDescendant);57extern int _FindUnembeddedEdgeToCurVertex(graphP theGraph, int cutVertex, int *pDescendant);58extern int _FindUnembeddedEdgeToSubtree(graphP theGraph, int ancestor, int SubtreeRoot, int *pDescendant);5960extern int _MarkPathAlongBicompExtFace(graphP theGraph, int startVert, int endVert);6162extern int _AddAndMarkEdge(graphP theGraph, int ancestor, int descendant);6364extern int _DeleteUnmarkedVerticesAndEdges(graphP theGraph);6566extern int _ChooseTypeOfNonOuterplanarityMinor(graphP theGraph, int I, int R);67extern int _IsolateOuterplanarityObstructionA(graphP theGraph);68extern int _IsolateOuterplanarityObstructionB(graphP theGraph);6970/* Private function declarations for K_{2,3} searching */7172int _SearchForK23(graphP theGraph, int I);7374int _SearchForK23InBicomp(graphP theGraph, int I, int R);75int _IsolateOuterplanarityObstructionE1orE2(graphP theGraph);76int _IsolateOuterplanarityObstructionE3orE4(graphP theGraph);7778/****************************************************************************79_SearchForK23()80We begin by identifying all root copies of I on which the81Walkdown failed. We can do this via straightforward traversal of82descendant to ancestor DFS tree paths. This is an O(n) total cost in the83worst case. If we find a K_{2,3}, then we can afford the time. However,84if we find only a K_4, then the outerplanarity algorithm must continue so85we could not afford the worst case performance. Fortunately, this86operation is worst-case constant time if there are no K_{2,3} homeomorphs87to be found in step I because a non-outerplanar biconnected graph either88contains a K_{2,3} or is \emph{isomorphic} to K_4.89****************************************************************************/9091int _SearchForK23(graphP theGraph, int I)92{93int J, W, C, RetVal=OK;9495/* Traverse the edges of I to find the unembedded forward edges to96descendants. For each such edge (I, W), traverse the DFS tree97path up to mark the child of I that is the root of the subtree98containing W. Optimize with visitation flag. */99100/* Traverse each unembedded back edge to the descendant endpoint... */101102J = theGraph->V[I].fwdArcList;103104// Ensure we have at least one bicomp on which Walkdown failed, which105// should always be the case in an error free implementation106if (!gp_IsArc(theGraph, J))107return NOTOK;108109while (J != NIL)110{111W = theGraph->G[J].v;112113/* Go from the descendant endpoint to find the ancestor that114is a child of I, which in turn indicates the root of a115bicomp on which the Walkdown failed to embed all back edges */116117C = W;118while (theGraph->V[C].DFSParent != I)119C = theGraph->V[C].DFSParent;120121RetVal = _SearchForK23InBicomp(theGraph, I, C+theGraph->N);122123/* If something went wrong, NOTOK was returned;124If a K_{2,3} was found, NONEMBEDDABLE was returned;125If OK was returned, then an isolated K_4 was found, so126we continue searching any other bicomps on which the127Walkdown failed. */128129if (RetVal != OK)130break;131132/* Get the next unembedded back edge from I */133134J = gp_GetNextArc(theGraph, J);135if (J == theGraph->V[I].fwdArcList)136J = NIL;137}138139/* If we got through the loop with an OK value for each bicomp on140which the Walkdown failed, then we return OK to indicate that only141isolated K_4's were found. This allows the embedder to continue.142If a K_{2,3} is ever found (or if an error occurred), then RetVal143will not be OK, and the loop terminates immediately so we can144return the appropriate value. */145146return RetVal;147}148149/****************************************************************************150_SearchForK23InBicomp()151****************************************************************************/152153int _SearchForK23InBicomp(graphP theGraph, int I, int R)154{155isolatorContextP IC = &theGraph->IC;156int X, Y, XPrevLink, YPrevLink;157158/* Begin by determining whether minor A, B or E is detected */159160if (_ChooseTypeOfNonOuterplanarityMinor(theGraph, I, R) != OK)161return NOTOK;162163/* Minors A and B result in the desired K_{2,3} homeomorph,164so we isolate it and return NONEMBEDDABLE. */165166if (theGraph->IC.minorType & (MINORTYPE_A|MINORTYPE_B))167{168_FillVisitedFlags(theGraph, 0);169170if (theGraph->IC.minorType & MINORTYPE_A)171{172if (_FindUnembeddedEdgeToCurVertex(theGraph, IC->w, &IC->dw) != TRUE)173return NOTOK;174175if (_IsolateOuterplanarityObstructionA(theGraph) != OK)176return NOTOK;177}178else if (theGraph->IC.minorType & MINORTYPE_B)179{180int SubtreeRoot = LCGetPrev(theGraph->BicompLists,181theGraph->V[IC->w].pertinentBicompList, NIL);182183if (_FindUnembeddedEdgeToSubtree(theGraph, IC->v, SubtreeRoot, &IC->dw) != TRUE)184return NOTOK;185186if (_IsolateOuterplanarityObstructionB(theGraph) != OK)187return NOTOK;188}189190if (_DeleteUnmarkedVerticesAndEdges(theGraph) != OK)191return NOTOK;192193return NONEMBEDDABLE;194}195196/* For minor E (a K_4) , we run the additional tests to see if a K_{2,3} is197entangled with the K_4. If not, then we return OK to indicate that198the outerplanarity embedder should proceed as if the K_4 had not199been found. */200201/* If any vertices other than R, X, Y and W exist along the202external face, then we can obtain a K_{2,3} by minor E1 or E2 */203204X = IC->x;205Y = IC->y;206XPrevLink = 1;207YPrevLink = 0;208if (IC->w != _GetNextVertexOnExternalFace(theGraph, X, &XPrevLink) ||209IC->w != _GetNextVertexOnExternalFace(theGraph, Y, &YPrevLink))210{211_FillVisitedFlags(theGraph, 0);212213if (_IsolateOuterplanarityObstructionE1orE2(theGraph) != OK)214return NOTOK;215216if (_DeleteUnmarkedVerticesAndEdges(theGraph) != OK)217return NOTOK;218219return NONEMBEDDABLE;220}221222/* If X, Y or W make either a direct back edge connection or a223connection through a separated child bicomp to an ancestor of224the current vertex I, then we can obtain a K_{2,3} by minor225E3 or E4. Note that this question is query on X, Y and W is226equivalent to the planarity version of external activity. */227228if (FUTUREPERTINENT(theGraph, X, I) ||229FUTUREPERTINENT(theGraph, Y, I) ||230FUTUREPERTINENT(theGraph, IC->w, I))231{232_FillVisitedFlags(theGraph, 0);233234if (_IsolateOuterplanarityObstructionE3orE4(theGraph) != OK)235return NOTOK;236237if (_DeleteUnmarkedVerticesAndEdges(theGraph) != OK)238return NOTOK;239240return NONEMBEDDABLE;241}242243/* The extra cases for finding a K_{2,3} failed, so the bicomp rooted244by R is a separable subgraph of the input that is isomorphic245to K_4. So, we restore the original vertex orientation of246the bicomp (because it's polite, not because we really have to).247Then, we return OK to tell the outerplanarity embedder that it248can ignore this K_4 and keep processing. */249250if (_OrientVerticesInBicomp(theGraph, R, 1) != OK)251return NOTOK;252253return OK;254}255256/****************************************************************************257_IsolateOuterplanarityObstructionE1orE2()258****************************************************************************/259260int _IsolateOuterplanarityObstructionE1orE2(graphP theGraph)261{262isolatorContextP IC = &theGraph->IC;263int XPrevLink = 1;264265if (_MarkHighestXYPath(theGraph) != TRUE)266return NOTOK;267268/* Isolate E1 */269270if (theGraph->IC.px != theGraph->IC.x)271{272if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->w) != OK ||273_MarkPathAlongBicompExtFace(theGraph, IC->py, IC->r) != OK)274return NOTOK;275}276else if (theGraph->IC.py != theGraph->IC.y)277{278if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->x) != OK ||279_MarkPathAlongBicompExtFace(theGraph, IC->w, IC->r) != OK)280return NOTOK;281}282283/* Isolate E2 */284285else if (IC->w != _GetNextVertexOnExternalFace(theGraph, IC->x, &XPrevLink))286{287if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->y) != OK)288return NOTOK;289}290291else292{293if (_MarkPathAlongBicompExtFace(theGraph, IC->x, IC->r) != OK)294return NOTOK;295}296297/* Final bits are in common */298299if (_FindUnembeddedEdgeToCurVertex(theGraph, IC->w, &IC->dw) != TRUE ||300theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||301_JoinBicomps(theGraph) != OK ||302_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)303return NOTOK;304305return OK;306}307308/****************************************************************************309_IsolateOuterplanarityObstructionE3orE4()310****************************************************************************/311312int _IsolateOuterplanarityObstructionE3orE4(graphP theGraph)313{314isolatorContextP IC = &theGraph->IC;315int u, d, XorY;316317/* Minor E3 */318319if (FUTUREPERTINENT(theGraph, theGraph->IC.x, theGraph->IC.v) ||320FUTUREPERTINENT(theGraph, theGraph->IC.y, theGraph->IC.v))321{322if (_MarkHighestXYPath(theGraph) != TRUE)323return NOTOK;324325if (FUTUREPERTINENT(theGraph, theGraph->IC.x, theGraph->IC.v))326XorY = theGraph->IC.x;327else XorY = theGraph->IC.y;328329/* The cases of X externally active and Y externally active330are the same except for the bicomp external face marking331(because parameter order is important) */332333if (XorY == theGraph->IC.x)334{335if (_MarkPathAlongBicompExtFace(theGraph, IC->x, IC->w) != OK ||336_MarkPathAlongBicompExtFace(theGraph, IC->y, IC->r) != OK)337return NOTOK;338}339else340{341if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->x) != OK ||342_MarkPathAlongBicompExtFace(theGraph, IC->w, IC->y) != OK)343return NOTOK;344}345346if (_FindUnembeddedEdgeToCurVertex(theGraph, IC->w, &IC->dw) != TRUE)347return NOTOK;348349if (_FindUnembeddedEdgeToAncestor(theGraph, XorY, &u, &d) != TRUE)350return NOTOK;351352if (theGraph->functions.fpMarkDFSPath(theGraph, u, IC->v) != OK ||353theGraph->functions.fpMarkDFSPath(theGraph, XorY, d) != OK ||354theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||355_JoinBicomps(theGraph) != OK ||356_AddAndMarkEdge(theGraph, u, d) != OK ||357_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)358return NOTOK;359360return OK;361}362363/* Otherwise, isolate Minor E4 (reduce to minor A) */364365if (_FindUnembeddedEdgeToAncestor(theGraph, IC->w, &u, &d) != TRUE)366return NOTOK;367368IC->v = u;369IC->dw = d;370return _IsolateOuterplanarityObstructionA(theGraph);371}372373374