Path: blob/master/sage/graphs/planarity/planarityCommandLine.c
4069 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 "planarity.h"4546#include <unistd.h>4748int callRandomGraphs(int argc, char *argv[]);49int callSpecificGraph(int argc, char *argv[]);50int callRandomMaxPlanarGraph(int argc, char *argv[]);51int callRandomNonplanarGraph(int argc, char *argv[]);5253/****************************************************************************54Command Line Processor55****************************************************************************/5657int commandLine(int argc, char *argv[])58{59int Result = OK;6061if (argc >= 3 && strcmp(argv[2], "-q") == 0)62quietMode = 'y';6364if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0)65{66Result = helpMessage(argc >= 3 ? argv[2] : NULL);67}6869else if (strcmp(argv[1], "-r") == 0)70Result = callRandomGraphs(argc, argv);7172else if (strcmp(argv[1], "-s") == 0)73Result = callSpecificGraph(argc, argv);7475else if (strcmp(argv[1], "-rm") == 0)76Result = callRandomMaxPlanarGraph(argc, argv);7778else if (strcmp(argv[1], "-rn") == 0)79Result = callRandomNonplanarGraph(argc, argv);8081else82{83ErrorMessage("Unsupported command line. Here is the help for this program.\n");84helpMessage(NULL);85Result = NOTOK;86}8788return Result == OK ? 0 : (Result == NONEMBEDDABLE ? 1 : -1);89}9091/****************************************************************************92Legacy Command Line Processor from version 1.x93****************************************************************************/9495int legacyCommandLine(int argc, char *argv[])96{97graphP theGraph = gp_New();98int Result;99100Result = gp_Read(theGraph, argv[1]);101if (Result != OK)102{103if (Result != NONEMBEDDABLE)104{105if (strlen(argv[1]) > MAXLINE - 100)106sprintf(Line, "Failed to read graph\n");107else108sprintf(Line, "Failed to read graph %s\n", argv[1]);109ErrorMessage(Line);110return -2;111}112}113114Result = gp_Embed(theGraph, EMBEDFLAGS_PLANAR);115116if (Result == OK)117{118gp_SortVertices(theGraph);119gp_Write(theGraph, argv[2], WRITE_ADJLIST);120}121122else if (Result == NONEMBEDDABLE)123{124if (argc >= 5 && strcmp(argv[3], "-n")==0)125{126gp_SortVertices(theGraph);127gp_Write(theGraph, argv[4], WRITE_ADJLIST);128}129}130else131Result = NOTOK;132133gp_Free(&theGraph);134135// In the legacy 1.x versions, OK/NONEMBEDDABLE was 0 and NOTOK was -2136return Result==OK || Result==NONEMBEDDABLE ? 0 : -2;137}138139140/****************************************************************************141Quick regression test142****************************************************************************/143144int runSpecificGraphTests();145int runSpecificGraphTest(char *command, char *infileName);146147int runSpecificGraphTests()148{149char origDir[2049];150int retVal = 0;151152if (!getcwd(origDir, 2048))153return -1;154155if (chdir("samples") != 0)156{157if (chdir("..") != 0 || chdir("samples") != 0)158{159// Warn but give success result160printf("WARNING: Unable to change to samples directory to run tests on samples.\n");161return 0;162}163}164165if (runSpecificGraphTest("-p", "maxPlanar5.txt") < 0)166retVal = -1;167168if (runSpecificGraphTest("-d", "maxPlanar5.txt") < 0)169retVal = -1;170171if (runSpecificGraphTest("-d", "drawExample.txt") < 0)172retVal = -1;173174if (runSpecificGraphTest("-p", "Petersen.txt") < 0)175retVal = -1;176177if (runSpecificGraphTest("-o", "Petersen.txt") < 0)178retVal = -1;179180if (runSpecificGraphTest("-2", "Petersen.txt") < 0)181retVal = -1;182183if (runSpecificGraphTest("-3", "Petersen.txt") < 0)184retVal = -1;185186if (runSpecificGraphTest("-4", "Petersen.txt") < 0)187retVal = -1;188189if (runSpecificGraphTest("-c", "maxPlanar5.txt") < 0)190retVal = -1;191192if (runSpecificGraphTest("-c", "Petersen.txt") < 0)193retVal = -1;194195if (runSpecificGraphTest("-c", "drawExample.txt") < 0)196retVal = -1;197198chdir(origDir);199FlushConsole(stdout);200return retVal;201}202203int runSpecificGraphTest(char *command, char *infileName)204{205char *commandLine[] = {206"planarity", "-s", "C", "infile", "outfile", "outfile2"207};208char *outfileName = ConstructPrimaryOutputFilename(infileName, NULL, command[1]);209char *outfile2Name = "";210char *testfileName = strdup(outfileName);211int Result = 0;212213if (testfileName == NULL)214return -1;215216outfileName = strdup(strcat(outfileName, ".test.txt"));217if (outfileName == NULL)218{219free(testfileName);220return -1;221}222223// 'planarity -s [-q] C I O [O2]': Specific graph224commandLine[2] = command;225commandLine[3] = infileName;226commandLine[4] = outfileName;227commandLine[5] = outfile2Name;228229Result = callSpecificGraph(6, commandLine);230if (Result == OK || Result == NONEMBEDDABLE)231Result = 0;232else233{234ErrorMessage("Test failed (graph processor returned failure result).\n");235Result = -1;236}237238if (Result == 0)239{240if (FilesEqual(testfileName, outfileName) == TRUE)241{242Message("Test succeeded (result equal to exemplar).\n");243unlink(outfileName);244}245else246{247ErrorMessage("Test failed (result not equal to exemplar).\n");248Result = -1;249}250}251252// For graph drawing, secondary file is outfileName + ".render.txt"253254if (command[1] == 'd' && Result == 0)255{256outfile2Name = ConstructPrimaryOutputFilename(NULL, outfileName, command[1]);257free(outfileName);258outfileName = strdup(strcat(outfile2Name, ".render.txt"));259260free(testfileName);261testfileName = ConstructPrimaryOutputFilename(infileName, NULL, command[1]);262testfileName = strdup(strcat(testfileName, ".render.txt"));263264if (Result == 0)265{266if (FilesEqual(testfileName, outfileName) == TRUE)267{268Message("Test succeeded (secondary result equal to exemplar).\n");269unlink(outfileName);270}271else272{273ErrorMessage("Test failed (secondary result not equal to exemplar).\n");274Result = -1;275}276}277}278279Message("\n");280281free(outfileName);282free(testfileName);283return Result;284}285286287/****************************************************************************288callRandomGraphs()289****************************************************************************/290291// 'planarity -r [-q] C K N': Random graphs292int callRandomGraphs(int argc, char *argv[])293{294char Choice = 0;295int offset = 0, NumGraphs, SizeOfGraphs;296297if (argc < 5)298return -1;299300if (argv[2][0] == '-' && (Choice = argv[2][1]) == 'q')301{302Choice = argv[3][1];303if (argc < 6)304return -1;305offset = 1;306}307308NumGraphs = atoi(argv[3+offset]);309SizeOfGraphs = atoi(argv[4+offset]);310311return RandomGraphs(Choice, NumGraphs, SizeOfGraphs);312}313314/****************************************************************************315callSpecificGraph()316****************************************************************************/317318// 'planarity -s [-q] C I O [O2]': Specific graph319int callSpecificGraph(int argc, char *argv[])320{321char Choice=0, *infileName=NULL, *outfileName=NULL, *outfile2Name=NULL;322int offset = 0;323324if (argc < 5)325return -1;326327if (argv[2][0] == '-' && (Choice = argv[2][1]) == 'q')328{329Choice = argv[3][1];330if (argc < 6)331return -1;332offset = 1;333}334335infileName = argv[3+offset];336outfileName = argv[4+offset];337if (argc == 6+offset)338outfile2Name = argv[5+offset];339340return SpecificGraph(Choice, infileName, outfileName, outfile2Name);341}342343/****************************************************************************344callRandomMaxPlanarGraph()345****************************************************************************/346347// 'planarity -rm [-q] N O [O2]': Maximal planar random graph348int callRandomMaxPlanarGraph(int argc, char *argv[])349{350int offset = 0, numVertices;351char *outfileName = NULL, *outfile2Name = NULL;352353if (argc < 4)354return -1;355356if (argv[2][0] == '-' && argv[2][1] == 'q')357{358if (argc < 5)359return -1;360offset = 1;361}362363numVertices = atoi(argv[2+offset]);364outfileName = argv[3+offset];365if (argc == 5+offset)366outfile2Name = argv[4+offset];367368return RandomGraph('p', 0, numVertices, outfileName, outfile2Name);369}370371/****************************************************************************372callRandomNonplanarGraph()373****************************************************************************/374375// 'planarity -rn [-q] N O [O2]': Non-planar random graph (maximal planar plus edge)376int callRandomNonplanarGraph(int argc, char *argv[])377{378int offset = 0, numVertices;379char *outfileName = NULL, *outfile2Name = NULL;380381if (argc < 4)382return -1;383384if (argv[2][0] == '-' && argv[2][1] == 'q')385{386if (argc < 5)387return -1;388offset = 1;389}390391numVertices = atoi(argv[2+offset]);392outfileName = argv[3+offset];393if (argc == 5+offset)394outfile2Name = argv[4+offset];395396return RandomGraph('p', 1, numVertices, outfileName, outfile2Name);397}398399400