Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/planarity/planaritySpecificGraph.c
4094 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 "planarity.h"
46
47
/****************************************************************************
48
SpecificGraph()
49
****************************************************************************/
50
51
int SpecificGraph(char command, char *infileName, char *outfileName, char *outfile2Name)
52
{
53
graphP theGraph, origGraph;
54
platform_time start, end;
55
int Result;
56
57
// Get the filename of the graph to test
58
if ((infileName = ConstructInputFilename(infileName)) == NULL)
59
return NOTOK;
60
61
// Create the graph and, if needed, attach the correct algorithm to it
62
theGraph = gp_New();
63
64
switch (command)
65
{
66
case 'd' : gp_AttachDrawPlanar(theGraph); break;
67
case '2' : gp_AttachK23Search(theGraph); break;
68
case '3' : gp_AttachK33Search(theGraph); break;
69
case '4' : gp_AttachK4Search(theGraph); break;
70
case 'c' : gp_AttachColorVertices(theGraph); break;
71
}
72
73
// Read the graph into memory
74
Result = gp_Read(theGraph, infileName);
75
if (Result == NONEMBEDDABLE)
76
{
77
Message("The graph contains too many edges.\n");
78
// Some of the algorithms will still run correctly with some edges removed.
79
if (strchr("pdo234", command))
80
{
81
Message("Some edges were removed, but the algorithm will still run correctly.\n");
82
Result = OK;
83
}
84
}
85
86
// If there was an unrecoverable error, report it
87
if (Result != OK)
88
ErrorMessage("Failed to read graph\n");
89
90
// Otherwise, call the correct algorithm on it
91
else
92
{
93
// Copy the graph for integrity checking
94
origGraph = gp_DupGraph(theGraph);
95
96
// Run the algorithm
97
if (strchr("pdo234", command))
98
{
99
int embedFlags = GetEmbedFlags(command);
100
platform_GetTime(start);
101
Result = gp_Embed(theGraph, embedFlags);
102
platform_GetTime(end);
103
Result = gp_TestEmbedResultIntegrity(theGraph, origGraph, Result);
104
}
105
else
106
{
107
platform_GetTime(start);
108
if (command == 'c')
109
{
110
if ((Result = gp_ColorVertices(theGraph)) == OK)
111
Result = gp_ColorVerticesIntegrityCheck(theGraph, origGraph);
112
}
113
else
114
Result = NOTOK;
115
platform_GetTime(end);
116
}
117
118
// Write what the algorithm determined and how long it took
119
WriteAlgorithmResults(theGraph, Result, command, start, end, infileName);
120
121
// Free the graph obtained for integrity checking.
122
gp_Free(&origGraph);
123
}
124
125
// Report an error, if there was one, free the graph, and return
126
if (Result != OK && Result != NONEMBEDDABLE)
127
{
128
ErrorMessage("AN ERROR HAS BEEN DETECTED\n");
129
Result = NOTOK;
130
}
131
132
// Provide the output file(s)
133
else
134
{
135
// Restore the vertex ordering of the original graph (undo DFS numbering)
136
if (strchr("pdo234", command))
137
gp_SortVertices(theGraph);
138
139
// Determine the name of the primary output file
140
outfileName = ConstructPrimaryOutputFilename(infileName, outfileName, command);
141
142
// For some algorithms, the primary output file is not always written
143
if ((strchr("pdo", command) && Result == NONEMBEDDABLE) ||
144
(strchr("234", command) && Result == OK))
145
;
146
147
// Write the primary output file, if appropriate to do so
148
else
149
gp_Write(theGraph, outfileName, WRITE_ADJLIST);
150
151
// NOW WE WANT TO WRITE THE SECONDARY OUTPUT FILE
152
153
// When called from the menu system, we want to write the planar or outerplanar
154
// obstruction, if one exists. For planar graph drawing, we want the character
155
// art rendition. An empty but non-NULL string is passed to indicate the necessity
156
// of selecting a default name for the second output file.
157
if (outfile2Name != NULL)
158
{
159
if ((command == 'p' || command == 'o') && Result == NONEMBEDDABLE)
160
{
161
// By default, use the same name as the primary output filename
162
if (strlen(outfile2Name) == 0)
163
outfile2Name = outfileName;
164
gp_Write(theGraph, outfile2Name, WRITE_ADJLIST);
165
}
166
else if (command == 'd' && Result == OK)
167
{
168
// By default, add ".render.txt" to the primary output filename
169
if (strlen(outfile2Name) == 0)
170
strcat((outfile2Name = outfileName), ".render.txt");
171
gp_DrawPlanar_RenderToFile(theGraph, outfile2Name);
172
}
173
}
174
}
175
176
// Free the graph
177
gp_Free(&theGraph);
178
179
// Flush any remaining message content to the user, and return the result
180
FlushConsole(stdout);
181
return Result;
182
}
183
184
/****************************************************************************
185
WriteAlgorithmResults()
186
****************************************************************************/
187
188
void WriteAlgorithmResults(graphP theGraph, int Result, char command, platform_time start, platform_time end, char *infileName)
189
{
190
if (infileName)
191
sprintf(Line, "The graph '%s' ", infileName);
192
else sprintf(Line, "The graph ");
193
Message(Line);
194
195
switch (command)
196
{
197
case 'p' : sprintf(Line, "is%s planar.\n", Result==OK ? "" : " not"); break;
198
case 'd' : sprintf(Line, "is%s planar.\n", Result==OK ? "" : " not"); break;
199
case 'o' : sprintf(Line, "is%s outerplanar.\n", Result==OK ? "" : " not"); break;
200
case '2' : sprintf(Line, "has %s subgraph homeomorphic to K_{2,3}.\n", Result==OK ? "no" : "a"); break;
201
case '3' : sprintf(Line, "has %s subgraph homeomorphic to K_{3,3}.\n", Result==OK ? "no" : "a"); break;
202
case '4' : sprintf(Line, "has %s subgraph homeomorphic to K_4.\n", Result==OK ? "no" : "a"); break;
203
case 'c' : sprintf(Line, "has been %d-colored.\n", gp_GetNumColorsUsed(theGraph)); break;
204
default : sprintf(Line, "nas not been processed due to unrecognized command.\n"); break;
205
}
206
Message(Line);
207
208
sprintf(Line, "Algorithm '%s' executed in %.3lf seconds.\n",
209
GetAlgorithmName(command), platform_GetDuration(start,end));
210
Message(Line);
211
}
212
213