Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/planarity/planarityCommandLine.c
4069 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
#include <unistd.h>
48
49
int callRandomGraphs(int argc, char *argv[]);
50
int callSpecificGraph(int argc, char *argv[]);
51
int callRandomMaxPlanarGraph(int argc, char *argv[]);
52
int callRandomNonplanarGraph(int argc, char *argv[]);
53
54
/****************************************************************************
55
Command Line Processor
56
****************************************************************************/
57
58
int commandLine(int argc, char *argv[])
59
{
60
int Result = OK;
61
62
if (argc >= 3 && strcmp(argv[2], "-q") == 0)
63
quietMode = 'y';
64
65
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0)
66
{
67
Result = helpMessage(argc >= 3 ? argv[2] : NULL);
68
}
69
70
else if (strcmp(argv[1], "-r") == 0)
71
Result = callRandomGraphs(argc, argv);
72
73
else if (strcmp(argv[1], "-s") == 0)
74
Result = callSpecificGraph(argc, argv);
75
76
else if (strcmp(argv[1], "-rm") == 0)
77
Result = callRandomMaxPlanarGraph(argc, argv);
78
79
else if (strcmp(argv[1], "-rn") == 0)
80
Result = callRandomNonplanarGraph(argc, argv);
81
82
else
83
{
84
ErrorMessage("Unsupported command line. Here is the help for this program.\n");
85
helpMessage(NULL);
86
Result = NOTOK;
87
}
88
89
return Result == OK ? 0 : (Result == NONEMBEDDABLE ? 1 : -1);
90
}
91
92
/****************************************************************************
93
Legacy Command Line Processor from version 1.x
94
****************************************************************************/
95
96
int legacyCommandLine(int argc, char *argv[])
97
{
98
graphP theGraph = gp_New();
99
int Result;
100
101
Result = gp_Read(theGraph, argv[1]);
102
if (Result != OK)
103
{
104
if (Result != NONEMBEDDABLE)
105
{
106
if (strlen(argv[1]) > MAXLINE - 100)
107
sprintf(Line, "Failed to read graph\n");
108
else
109
sprintf(Line, "Failed to read graph %s\n", argv[1]);
110
ErrorMessage(Line);
111
return -2;
112
}
113
}
114
115
Result = gp_Embed(theGraph, EMBEDFLAGS_PLANAR);
116
117
if (Result == OK)
118
{
119
gp_SortVertices(theGraph);
120
gp_Write(theGraph, argv[2], WRITE_ADJLIST);
121
}
122
123
else if (Result == NONEMBEDDABLE)
124
{
125
if (argc >= 5 && strcmp(argv[3], "-n")==0)
126
{
127
gp_SortVertices(theGraph);
128
gp_Write(theGraph, argv[4], WRITE_ADJLIST);
129
}
130
}
131
else
132
Result = NOTOK;
133
134
gp_Free(&theGraph);
135
136
// In the legacy 1.x versions, OK/NONEMBEDDABLE was 0 and NOTOK was -2
137
return Result==OK || Result==NONEMBEDDABLE ? 0 : -2;
138
}
139
140
141
/****************************************************************************
142
Quick regression test
143
****************************************************************************/
144
145
int runSpecificGraphTests();
146
int runSpecificGraphTest(char *command, char *infileName);
147
148
int runSpecificGraphTests()
149
{
150
char origDir[2049];
151
int retVal = 0;
152
153
if (!getcwd(origDir, 2048))
154
return -1;
155
156
if (chdir("samples") != 0)
157
{
158
if (chdir("..") != 0 || chdir("samples") != 0)
159
{
160
// Warn but give success result
161
printf("WARNING: Unable to change to samples directory to run tests on samples.\n");
162
return 0;
163
}
164
}
165
166
if (runSpecificGraphTest("-p", "maxPlanar5.txt") < 0)
167
retVal = -1;
168
169
if (runSpecificGraphTest("-d", "maxPlanar5.txt") < 0)
170
retVal = -1;
171
172
if (runSpecificGraphTest("-d", "drawExample.txt") < 0)
173
retVal = -1;
174
175
if (runSpecificGraphTest("-p", "Petersen.txt") < 0)
176
retVal = -1;
177
178
if (runSpecificGraphTest("-o", "Petersen.txt") < 0)
179
retVal = -1;
180
181
if (runSpecificGraphTest("-2", "Petersen.txt") < 0)
182
retVal = -1;
183
184
if (runSpecificGraphTest("-3", "Petersen.txt") < 0)
185
retVal = -1;
186
187
if (runSpecificGraphTest("-4", "Petersen.txt") < 0)
188
retVal = -1;
189
190
if (runSpecificGraphTest("-c", "maxPlanar5.txt") < 0)
191
retVal = -1;
192
193
if (runSpecificGraphTest("-c", "Petersen.txt") < 0)
194
retVal = -1;
195
196
if (runSpecificGraphTest("-c", "drawExample.txt") < 0)
197
retVal = -1;
198
199
chdir(origDir);
200
FlushConsole(stdout);
201
return retVal;
202
}
203
204
int runSpecificGraphTest(char *command, char *infileName)
205
{
206
char *commandLine[] = {
207
"planarity", "-s", "C", "infile", "outfile", "outfile2"
208
};
209
char *outfileName = ConstructPrimaryOutputFilename(infileName, NULL, command[1]);
210
char *outfile2Name = "";
211
char *testfileName = strdup(outfileName);
212
int Result = 0;
213
214
if (testfileName == NULL)
215
return -1;
216
217
outfileName = strdup(strcat(outfileName, ".test.txt"));
218
if (outfileName == NULL)
219
{
220
free(testfileName);
221
return -1;
222
}
223
224
// 'planarity -s [-q] C I O [O2]': Specific graph
225
commandLine[2] = command;
226
commandLine[3] = infileName;
227
commandLine[4] = outfileName;
228
commandLine[5] = outfile2Name;
229
230
Result = callSpecificGraph(6, commandLine);
231
if (Result == OK || Result == NONEMBEDDABLE)
232
Result = 0;
233
else
234
{
235
ErrorMessage("Test failed (graph processor returned failure result).\n");
236
Result = -1;
237
}
238
239
if (Result == 0)
240
{
241
if (FilesEqual(testfileName, outfileName) == TRUE)
242
{
243
Message("Test succeeded (result equal to exemplar).\n");
244
unlink(outfileName);
245
}
246
else
247
{
248
ErrorMessage("Test failed (result not equal to exemplar).\n");
249
Result = -1;
250
}
251
}
252
253
// For graph drawing, secondary file is outfileName + ".render.txt"
254
255
if (command[1] == 'd' && Result == 0)
256
{
257
outfile2Name = ConstructPrimaryOutputFilename(NULL, outfileName, command[1]);
258
free(outfileName);
259
outfileName = strdup(strcat(outfile2Name, ".render.txt"));
260
261
free(testfileName);
262
testfileName = ConstructPrimaryOutputFilename(infileName, NULL, command[1]);
263
testfileName = strdup(strcat(testfileName, ".render.txt"));
264
265
if (Result == 0)
266
{
267
if (FilesEqual(testfileName, outfileName) == TRUE)
268
{
269
Message("Test succeeded (secondary result equal to exemplar).\n");
270
unlink(outfileName);
271
}
272
else
273
{
274
ErrorMessage("Test failed (secondary result not equal to exemplar).\n");
275
Result = -1;
276
}
277
}
278
}
279
280
Message("\n");
281
282
free(outfileName);
283
free(testfileName);
284
return Result;
285
}
286
287
288
/****************************************************************************
289
callRandomGraphs()
290
****************************************************************************/
291
292
// 'planarity -r [-q] C K N': Random graphs
293
int callRandomGraphs(int argc, char *argv[])
294
{
295
char Choice = 0;
296
int offset = 0, NumGraphs, SizeOfGraphs;
297
298
if (argc < 5)
299
return -1;
300
301
if (argv[2][0] == '-' && (Choice = argv[2][1]) == 'q')
302
{
303
Choice = argv[3][1];
304
if (argc < 6)
305
return -1;
306
offset = 1;
307
}
308
309
NumGraphs = atoi(argv[3+offset]);
310
SizeOfGraphs = atoi(argv[4+offset]);
311
312
return RandomGraphs(Choice, NumGraphs, SizeOfGraphs);
313
}
314
315
/****************************************************************************
316
callSpecificGraph()
317
****************************************************************************/
318
319
// 'planarity -s [-q] C I O [O2]': Specific graph
320
int callSpecificGraph(int argc, char *argv[])
321
{
322
char Choice=0, *infileName=NULL, *outfileName=NULL, *outfile2Name=NULL;
323
int offset = 0;
324
325
if (argc < 5)
326
return -1;
327
328
if (argv[2][0] == '-' && (Choice = argv[2][1]) == 'q')
329
{
330
Choice = argv[3][1];
331
if (argc < 6)
332
return -1;
333
offset = 1;
334
}
335
336
infileName = argv[3+offset];
337
outfileName = argv[4+offset];
338
if (argc == 6+offset)
339
outfile2Name = argv[5+offset];
340
341
return SpecificGraph(Choice, infileName, outfileName, outfile2Name);
342
}
343
344
/****************************************************************************
345
callRandomMaxPlanarGraph()
346
****************************************************************************/
347
348
// 'planarity -rm [-q] N O [O2]': Maximal planar random graph
349
int callRandomMaxPlanarGraph(int argc, char *argv[])
350
{
351
int offset = 0, numVertices;
352
char *outfileName = NULL, *outfile2Name = NULL;
353
354
if (argc < 4)
355
return -1;
356
357
if (argv[2][0] == '-' && argv[2][1] == 'q')
358
{
359
if (argc < 5)
360
return -1;
361
offset = 1;
362
}
363
364
numVertices = atoi(argv[2+offset]);
365
outfileName = argv[3+offset];
366
if (argc == 5+offset)
367
outfile2Name = argv[4+offset];
368
369
return RandomGraph('p', 0, numVertices, outfileName, outfile2Name);
370
}
371
372
/****************************************************************************
373
callRandomNonplanarGraph()
374
****************************************************************************/
375
376
// 'planarity -rn [-q] N O [O2]': Non-planar random graph (maximal planar plus edge)
377
int callRandomNonplanarGraph(int argc, char *argv[])
378
{
379
int offset = 0, numVertices;
380
char *outfileName = NULL, *outfile2Name = NULL;
381
382
if (argc < 4)
383
return -1;
384
385
if (argv[2][0] == '-' && argv[2][1] == 'q')
386
{
387
if (argc < 5)
388
return -1;
389
offset = 1;
390
}
391
392
numVertices = atoi(argv[2+offset]);
393
outfileName = argv[3+offset];
394
if (argc == 5+offset)
395
outfile2Name = argv[4+offset];
396
397
return RandomGraph('p', 1, numVertices, outfileName, outfile2Name);
398
}
399
400