Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/planarity/graphOuterplanarObstruction.c
4097 views
1
/*
2
Planarity-Related Graph Algorithms Project
3
Copyright (c) 1997-2010, John M. Boyer
4
All rights reserved. Includes a reference implementation of the following:
5
6
* John M. Boyer. "Simplified O(n) Algorithms for Planar Graph Embedding,
7
Kuratowski Subgraph Isolation, and Related Problems". Ph.D. Dissertation,
8
University of Victoria, 2001.
9
10
* John M. Boyer and Wendy J. Myrvold. "On the Cutting Edge: Simplified O(n)
11
Planarity by Edge Addition". Journal of Graph Algorithms and Applications,
12
Vol. 8, No. 3, pp. 241-273, 2004.
13
14
* John M. Boyer. "A New Method for Efficiently Generating Planar Graph
15
Visibility Representations". In P. Eades and P. Healy, editors,
16
Proceedings of the 13th International Conference on Graph Drawing 2005,
17
Lecture Notes Comput. Sci., Volume 3843, pp. 508-511, Springer-Verlag, 2006.
18
19
Redistribution and use in source and binary forms, with or without modification,
20
are permitted provided that the following conditions are met:
21
22
* Redistributions of source code must retain the above copyright notice, this
23
list of conditions and the following disclaimer.
24
25
* Redistributions in binary form must reproduce the above copyright notice, this
26
list of conditions and the following disclaimer in the documentation and/or
27
other materials provided with the distribution.
28
29
* Neither the name of the Planarity-Related Graph Algorithms Project nor the names
30
of its contributors may be used to endorse or promote products derived from this
31
software without specific prior written permission.
32
33
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
*/
44
45
#include "graph.h"
46
47
/* Imported functions */
48
49
extern void _FillVisitedFlags(graphP, int);
50
extern void _ClearIsolatorContext(graphP theGraph);
51
52
extern int _GetNextVertexOnExternalFace(graphP theGraph, int curVertex, int *pPrevLink);
53
extern int _JoinBicomps(graphP theGraph);
54
55
extern int _InitializeNonplanarityContext(graphP theGraph, int I, int R);
56
extern int _MarkHighestXYPath(graphP theGraph);
57
58
extern int _FindUnembeddedEdgeToAncestor(graphP theGraph, int cutVertex, int *pAncestor, int *pDescendant);
59
extern int _FindUnembeddedEdgeToCurVertex(graphP theGraph, int cutVertex, int *pDescendant);
60
extern int _FindUnembeddedEdgeToSubtree(graphP theGraph, int ancestor, int SubtreeRoot, int *pDescendant);
61
62
extern int _MarkPathAlongBicompExtFace(graphP theGraph, int startVert, int endVert);
63
64
extern int _AddAndMarkEdge(graphP theGraph, int ancestor, int descendant);
65
66
extern int _DeleteUnmarkedVerticesAndEdges(graphP theGraph);
67
68
/* Private function declarations (exported to system) */
69
70
int _IsolateOuterplanarObstruction(graphP theGraph, int I, int R);
71
72
int _ChooseTypeOfNonOuterplanarityMinor(graphP theGraph, int I, int R);
73
74
int _IsolateOuterplanarityObstructionA(graphP theGraph);
75
int _IsolateOuterplanarityObstructionB(graphP theGraph);
76
int _IsolateOuterplanarityObstructionE(graphP theGraph);
77
78
/****************************************************************************
79
_ChooseTypeOfNonOuterplanarityMinor()
80
A constant time implementation is easily feasible but only constant amortized
81
time is needed for the outerplanarity obstruction isolation, which also
82
benefits from having the bicomp rooted by R oriented.
83
If an extension algorithm requires constant actual time, then this function
84
should not be used and instead the minor should be decided without orienting
85
the bicomp.
86
****************************************************************************/
87
88
int _ChooseTypeOfNonOuterplanarityMinor(graphP theGraph, int I, int R)
89
{
90
int N, X, Y, W;
91
92
// Create the initial non-outerplanarity obstruction isolator state.
93
if (_InitializeNonplanarityContext(theGraph, I, R) != OK)
94
return NOTOK;
95
96
N = theGraph->N;
97
R = theGraph->IC.r;
98
X = theGraph->IC.x;
99
Y = theGraph->IC.y;
100
W = theGraph->IC.w;
101
102
// If the root copy is not a root copy of the current vertex I,
103
// then the Walkdown terminated on a descendant bicomp, which is Minor A.
104
if (theGraph->V[R - N].DFSParent != I)
105
{
106
theGraph->IC.minorType |= MINORTYPE_A;
107
return OK;
108
}
109
110
// If W has a pertinent child bicomp, then we've found Minor B.
111
// Notice this is different from planarity, in which minor B is indicated
112
// only if the pertinent child bicomp is also externally active under the
113
// planarity processing model (i.e. future pertinent).
114
if (theGraph->V[W].pertinentBicompList != NIL)
115
{
116
theGraph->IC.minorType |= MINORTYPE_B;
117
return OK;
118
}
119
120
// The only other result is minor E (we will search for the X-Y path later)
121
theGraph->IC.minorType |= MINORTYPE_E;
122
return OK;
123
}
124
125
/****************************************************************************
126
_IsolateOuterplanarObstruction()
127
****************************************************************************/
128
129
int _IsolateOuterplanarObstruction(graphP theGraph, int I, int R)
130
{
131
int RetVal;
132
133
/* A subgraph homeomorphic to K_{2,3} or K_4 will be isolated by using the visited
134
flags, 1=keep edge/vertex and 0=omit. Here we initialize to omit all, then we
135
subsequently set visited to 1 on all edges and vertices in the homeomorph. */
136
137
_FillVisitedFlags(theGraph, 0);
138
139
/* Next we determineg which of the non-outerplanarity Minors was encountered
140
and the principal bicomp on which the isolator will focus attention. */
141
142
if (_ChooseTypeOfNonOuterplanarityMinor(theGraph, I, R) != OK)
143
return NOTOK;
144
145
/* Find the path connecting the pertinent vertex w with the current vertex v */
146
147
if (theGraph->IC.minorType & MINORTYPE_B)
148
{
149
isolatorContextP IC = &theGraph->IC;
150
int SubtreeRoot = LCGetPrev(theGraph->BicompLists,
151
theGraph->V[IC->w].pertinentBicompList, NIL);
152
153
if (_FindUnembeddedEdgeToSubtree(theGraph, IC->v, SubtreeRoot, &IC->dw) != TRUE)
154
return NOTOK;
155
}
156
else
157
{
158
isolatorContextP IC = &theGraph->IC;
159
160
if (_FindUnembeddedEdgeToCurVertex(theGraph, IC->w, &IC->dw) != TRUE)
161
return NOTOK;
162
}
163
164
/* For minor E, we need to find and mark an X-Y path */
165
166
if (theGraph->IC.minorType & MINORTYPE_E)
167
{
168
if (_MarkHighestXYPath(theGraph) != TRUE)
169
return NOTOK;
170
}
171
172
/* Call the appropriate isolator */
173
174
if (theGraph->IC.minorType & MINORTYPE_A)
175
RetVal = _IsolateOuterplanarityObstructionA(theGraph);
176
else if (theGraph->IC.minorType & MINORTYPE_B)
177
RetVal = _IsolateOuterplanarityObstructionB(theGraph);
178
else if (theGraph->IC.minorType & MINORTYPE_E)
179
RetVal = _IsolateOuterplanarityObstructionE(theGraph);
180
else
181
RetVal = NOTOK;
182
183
/* Delete the unmarked edges and vertices, and return */
184
185
if (RetVal == OK)
186
RetVal = _DeleteUnmarkedVerticesAndEdges(theGraph);
187
188
return RetVal;
189
}
190
191
/****************************************************************************
192
_IsolateOuterplanarityObstructionA(): Isolate a K2,3 homeomorph
193
****************************************************************************/
194
195
int _IsolateOuterplanarityObstructionA(graphP theGraph)
196
{
197
isolatorContextP IC = &theGraph->IC;
198
199
if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->r) != OK ||
200
theGraph->functions.fpMarkDFSPath(theGraph, IC->v, IC->r) != OK ||
201
theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||
202
_JoinBicomps(theGraph) != OK ||
203
_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)
204
return NOTOK;
205
206
return OK;
207
}
208
209
/****************************************************************************
210
_IsolateOuterplanarityObstructionB(): Isolate a K2,3 homeomorph
211
****************************************************************************/
212
213
int _IsolateOuterplanarityObstructionB(graphP theGraph)
214
{
215
isolatorContextP IC = &theGraph->IC;
216
217
if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->r) != OK ||
218
theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||
219
_JoinBicomps(theGraph) != OK ||
220
_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)
221
return NOTOK;
222
223
return OK;
224
}
225
226
/****************************************************************************
227
_IsolateOuterplanarityObstructionE(): Isolate a K4 homeomorph
228
****************************************************************************/
229
230
int _IsolateOuterplanarityObstructionE(graphP theGraph)
231
{
232
isolatorContextP IC = &theGraph->IC;
233
234
if (_MarkPathAlongBicompExtFace(theGraph, IC->r, IC->r) != OK ||
235
theGraph->functions.fpMarkDFSPath(theGraph, IC->w, IC->dw) != OK ||
236
_JoinBicomps(theGraph) != OK ||
237
_AddAndMarkEdge(theGraph, IC->v, IC->dw) != OK)
238
return NOTOK;
239
240
return OK;
241
}
242
243