Path: blob/master/src/sage/graphs/planarity_c/planarity.c
8815 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"4546void ProjectTitle()47{48Message("\n=================================================="49"\nPlanarity version 2.2"50"\nCopyright (c) 2010 by John M. Boyer"51"\nContact info: jboyer at acm.org"52"\n=================================================="53"\n");54}5556/****************************************************************************57MAIN58****************************************************************************/5960int main(int argc, char *argv[])61{62int retVal=0;6364if (argc <= 1)65retVal = menu();6667else if (argv[1][0] == '-')68retVal = commandLine(argc, argv);6970else71retVal = legacyCommandLine(argc, argv);7273// Close the log file if logging74gp_Log(NULL);7576return retVal;77}7879/****************************************************************************80helpMessage()81****************************************************************************/8283int helpMessage(char *param)84{85char *commandStr =86"C = command from menu\n"87" -p = Planar embedding and Kuratowski subgraph isolation\n"88" -o = Outerplanar embedding and obstruction isolation\n"89" -d = Planar graph drawing\n"90" -2 = Search for subgraph homeomorphic to K_{2,3}\n"91" -3 = Search for subgraph homeomorphic to K_{3,3}\n"92" -4 = Search for subgraph homeomorphic to K_4\n"93" -c = Color the vertices of the graph\n"94"\n";9596ProjectTitle();9798if (param == NULL)99{100Message(101"'planarity': menu-driven\n"102"'planarity (-h|-help)': this message\n"103"'planarity (-h|-help) -menu': more help with menu-based command line\n"104"'planarity -test [-q] [C]': runs tests (optional quiet mode, single test)\n"105"\n"106);107108Message(109"Common usages\n"110"-------------\n"111"planarity -s -q -p infile.txt embedding.out [obstruction.out]\n"112"Process infile.txt in quiet mode (-q), putting planar embedding in \n"113"embedding.out or (optionally) a Kuratowski subgraph in Obstruction.out\n"114"Process returns 0=planar, 1=nonplanar, -1=error\n"115"\n"116"planarity -s -q -d infile.txt embedding.out [drawing.out]\n"117"If graph in infile.txt is planar, then put embedding in embedding.out \n"118"and (optionally) an ASCII art drawing in drawing.out\n"119"Process returns 0=planar, 1=nonplanar, -1=error\n"120"\n"121);122}123124else if (strcmp(param, "-menu") == 0)125{126Message(127"'planarity -r [-q] C K N': Random graphs\n"128"'planarity -s [-q] C I O [O2]': Specific graph\n"129"'planarity -rm [-q] N O [O2]': Maximal planar random graph\n"130"'planarity -rn [-q] N O [O2]': Nonplanar random graph (maximal planar + edge)\n"131"'planarity I O [-n O2]': Legacy command-line (default -s -p)\n"132"\n"133);134135Message("-q is for quiet mode (no messages to stdout and stderr)\n\n");136137Message(commandStr);138139Message(140"K = # of graphs to randomly generate\n"141"N = # of vertices in each randomly generated graph\n"142"I = Input file (for work on a specific graph)\n"143"O = Primary output file\n"144" For example, if C=-p then O receives the planar embedding\n"145" If C=-3, then O receives a subgraph containing a K_{3,3}\n"146"O2= Secondary output file\n"147" For -s, if C=-p or -o, then O2 receives the embedding obstruction\n"148" For -s, if C=-d, then O2 receives a drawing of the planar graph\n"149" For -m and -n, O2 contains the original randomly generated graph\n"150"\n"151);152153Message(154"planarity process results: 0=OK, -1=NOTOK, 1=NONEMBEDDABLE\n"155" 1 result only produced by specific graph mode (-s)\n"156" with command -2,-3,-4: found K_{2,3}, K_{3,3} or K_4\n"157" with command -p,-d: found planarity obstruction\n"158" with command -o: found outerplanarity obstruction\n"159);160}161162FlushConsole(stdout);163return 0;164}165166/****************************************************************************167MENU-DRIVEN PROGRAM168****************************************************************************/169170int menu()171{172char Choice;173174do {175ProjectTitle();176177Message("\n"178"P. Planar embedding and Kuratowski subgraph isolation\n"179"D. Planar graph drawing\n"180"O. Outerplanar embedding and obstruction isolation\n"181"2. Search for subgraph homeomorphic to K_{2,3}\n"182"3. Search for subgraph homeomorphic to K_{3,3}\n"183"4. Search for subgraph homeomorphic to K_4\n"184"C. Color the vertices of the graph\n"185"H. Help message for command line version\n"186"R. Reconfigure options\n"187"X. Exit\n"188"\n"189);190191Prompt("Enter Choice: ");192fflush(stdin);193scanf(" %c", &Choice);194Choice = tolower(Choice);195196if (Choice == 'h')197helpMessage(NULL);198199else if (Choice == 'r')200Reconfigure();201202else if (Choice != 'x')203{204char *secondOutfile = NULL;205if (Choice == 'p' || Choice == 'o' || Choice == 'd')206secondOutfile ="";207208switch (tolower(Mode))209{210case 's' : SpecificGraph(Choice, NULL, NULL, secondOutfile); break;211case 'r' : RandomGraphs(Choice, 0, 0); break;212case 'm' : RandomGraph(Choice, 0, 0, NULL, NULL); break;213case 'n' : RandomGraph(Choice, 1, 0, NULL, NULL); break;214}215}216217if (Choice != 'r' && Choice != 'x')218{219Prompt("\nPress a key then hit ENTER to continue...");220fflush(stdin);221scanf(" %*c");222fflush(stdin);223Message("\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");224FlushConsole(stdout);225}226227} while (Choice != 'x');228229// Certain debuggers don't terminate correctly with pending output content230FlushConsole(stdout);231FlushConsole(stderr);232233return 0;234}235236237