Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/planarity/planarity.c
4107 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
void ProjectTitle()
48
{
49
Message("\n=================================================="
50
"\nPlanarity version 2.2"
51
"\nCopyright (c) 2010 by John M. Boyer"
52
"\nContact info: jboyer at acm.org"
53
"\n=================================================="
54
"\n");
55
}
56
57
/****************************************************************************
58
MAIN
59
****************************************************************************/
60
61
int main(int argc, char *argv[])
62
{
63
int retVal=0;
64
65
if (argc <= 1)
66
retVal = menu();
67
68
else if (argv[1][0] == '-')
69
retVal = commandLine(argc, argv);
70
71
else
72
retVal = legacyCommandLine(argc, argv);
73
74
// Close the log file if logging
75
gp_Log(NULL);
76
77
return retVal;
78
}
79
80
/****************************************************************************
81
helpMessage()
82
****************************************************************************/
83
84
int helpMessage(char *param)
85
{
86
char *commandStr =
87
"C = command from menu\n"
88
" -p = Planar embedding and Kuratowski subgraph isolation\n"
89
" -o = Outerplanar embedding and obstruction isolation\n"
90
" -d = Planar graph drawing\n"
91
" -2 = Search for subgraph homeomorphic to K_{2,3}\n"
92
" -3 = Search for subgraph homeomorphic to K_{3,3}\n"
93
" -4 = Search for subgraph homeomorphic to K_4\n"
94
" -c = Color the vertices of the graph\n"
95
"\n";
96
97
ProjectTitle();
98
99
if (param == NULL)
100
{
101
Message(
102
"'planarity': menu-driven\n"
103
"'planarity (-h|-help)': this message\n"
104
"'planarity (-h|-help) -menu': more help with menu-based command line\n"
105
"'planarity -test [-q] [C]': runs tests (optional quiet mode, single test)\n"
106
"\n"
107
);
108
109
Message(
110
"Common usages\n"
111
"-------------\n"
112
"planarity -s -q -p infile.txt embedding.out [obstruction.out]\n"
113
"Process infile.txt in quiet mode (-q), putting planar embedding in \n"
114
"embedding.out or (optionally) a Kuratowski subgraph in Obstruction.out\n"
115
"Process returns 0=planar, 1=nonplanar, -1=error\n"
116
"\n"
117
"planarity -s -q -d infile.txt embedding.out [drawing.out]\n"
118
"If graph in infile.txt is planar, then put embedding in embedding.out \n"
119
"and (optionally) an ASCII art drawing in drawing.out\n"
120
"Process returns 0=planar, 1=nonplanar, -1=error\n"
121
"\n"
122
);
123
}
124
125
else if (strcmp(param, "-menu") == 0)
126
{
127
Message(
128
"'planarity -r [-q] C K N': Random graphs\n"
129
"'planarity -s [-q] C I O [O2]': Specific graph\n"
130
"'planarity -rm [-q] N O [O2]': Maximal planar random graph\n"
131
"'planarity -rn [-q] N O [O2]': Nonplanar random graph (maximal planar + edge)\n"
132
"'planarity I O [-n O2]': Legacy command-line (default -s -p)\n"
133
"\n"
134
);
135
136
Message("-q is for quiet mode (no messages to stdout and stderr)\n\n");
137
138
Message(commandStr);
139
140
Message(
141
"K = # of graphs to randomly generate\n"
142
"N = # of vertices in each randomly generated graph\n"
143
"I = Input file (for work on a specific graph)\n"
144
"O = Primary output file\n"
145
" For example, if C=-p then O receives the planar embedding\n"
146
" If C=-3, then O receives a subgraph containing a K_{3,3}\n"
147
"O2= Secondary output file\n"
148
" For -s, if C=-p or -o, then O2 receives the embedding obstruction\n"
149
" For -s, if C=-d, then O2 receives a drawing of the planar graph\n"
150
" For -m and -n, O2 contains the original randomly generated graph\n"
151
"\n"
152
);
153
154
Message(
155
"planarity process results: 0=OK, -1=NOTOK, 1=NONEMBEDDABLE\n"
156
" 1 result only produced by specific graph mode (-s)\n"
157
" with command -2,-3,-4: found K_{2,3}, K_{3,3} or K_4\n"
158
" with command -p,-d: found planarity obstruction\n"
159
" with command -o: found outerplanarity obstruction\n"
160
);
161
}
162
163
FlushConsole(stdout);
164
return 0;
165
}
166
167
/****************************************************************************
168
MENU-DRIVEN PROGRAM
169
****************************************************************************/
170
171
int menu()
172
{
173
char Choice;
174
175
do {
176
ProjectTitle();
177
178
Message("\n"
179
"P. Planar embedding and Kuratowski subgraph isolation\n"
180
"D. Planar graph drawing\n"
181
"O. Outerplanar embedding and obstruction isolation\n"
182
"2. Search for subgraph homeomorphic to K_{2,3}\n"
183
"3. Search for subgraph homeomorphic to K_{3,3}\n"
184
"4. Search for subgraph homeomorphic to K_4\n"
185
"C. Color the vertices of the graph\n"
186
"H. Help message for command line version\n"
187
"R. Reconfigure options\n"
188
"X. Exit\n"
189
"\n"
190
);
191
192
Prompt("Enter Choice: ");
193
fflush(stdin);
194
scanf(" %c", &Choice);
195
Choice = tolower(Choice);
196
197
if (Choice == 'h')
198
helpMessage(NULL);
199
200
else if (Choice == 'r')
201
Reconfigure();
202
203
else if (Choice != 'x')
204
{
205
char *secondOutfile = NULL;
206
if (Choice == 'p' || Choice == 'o' || Choice == 'd')
207
secondOutfile ="";
208
209
switch (tolower(Mode))
210
{
211
case 's' : SpecificGraph(Choice, NULL, NULL, secondOutfile); break;
212
case 'r' : RandomGraphs(Choice, 0, 0); break;
213
case 'm' : RandomGraph(Choice, 0, 0, NULL, NULL); break;
214
case 'n' : RandomGraph(Choice, 1, 0, NULL, NULL); break;
215
}
216
}
217
218
if (Choice != 'r' && Choice != 'x')
219
{
220
Prompt("\nPress a key then hit ENTER to continue...");
221
fflush(stdin);
222
scanf(" %*c");
223
fflush(stdin);
224
Message("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
225
FlushConsole(stdout);
226
}
227
228
} while (Choice != 'x');
229
230
// Certain debuggers don't terminate correctly with pending output content
231
FlushConsole(stdout);
232
FlushConsole(stderr);
233
234
return 0;
235
}
236
237