Path: blob/master/sage/graphs/planarity/planaritySpecificGraph.c
4094 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/****************************************************************************47SpecificGraph()48****************************************************************************/4950int SpecificGraph(char command, char *infileName, char *outfileName, char *outfile2Name)51{52graphP theGraph, origGraph;53platform_time start, end;54int Result;5556// Get the filename of the graph to test57if ((infileName = ConstructInputFilename(infileName)) == NULL)58return NOTOK;5960// Create the graph and, if needed, attach the correct algorithm to it61theGraph = gp_New();6263switch (command)64{65case 'd' : gp_AttachDrawPlanar(theGraph); break;66case '2' : gp_AttachK23Search(theGraph); break;67case '3' : gp_AttachK33Search(theGraph); break;68case '4' : gp_AttachK4Search(theGraph); break;69case 'c' : gp_AttachColorVertices(theGraph); break;70}7172// Read the graph into memory73Result = gp_Read(theGraph, infileName);74if (Result == NONEMBEDDABLE)75{76Message("The graph contains too many edges.\n");77// Some of the algorithms will still run correctly with some edges removed.78if (strchr("pdo234", command))79{80Message("Some edges were removed, but the algorithm will still run correctly.\n");81Result = OK;82}83}8485// If there was an unrecoverable error, report it86if (Result != OK)87ErrorMessage("Failed to read graph\n");8889// Otherwise, call the correct algorithm on it90else91{92// Copy the graph for integrity checking93origGraph = gp_DupGraph(theGraph);9495// Run the algorithm96if (strchr("pdo234", command))97{98int embedFlags = GetEmbedFlags(command);99platform_GetTime(start);100Result = gp_Embed(theGraph, embedFlags);101platform_GetTime(end);102Result = gp_TestEmbedResultIntegrity(theGraph, origGraph, Result);103}104else105{106platform_GetTime(start);107if (command == 'c')108{109if ((Result = gp_ColorVertices(theGraph)) == OK)110Result = gp_ColorVerticesIntegrityCheck(theGraph, origGraph);111}112else113Result = NOTOK;114platform_GetTime(end);115}116117// Write what the algorithm determined and how long it took118WriteAlgorithmResults(theGraph, Result, command, start, end, infileName);119120// Free the graph obtained for integrity checking.121gp_Free(&origGraph);122}123124// Report an error, if there was one, free the graph, and return125if (Result != OK && Result != NONEMBEDDABLE)126{127ErrorMessage("AN ERROR HAS BEEN DETECTED\n");128Result = NOTOK;129}130131// Provide the output file(s)132else133{134// Restore the vertex ordering of the original graph (undo DFS numbering)135if (strchr("pdo234", command))136gp_SortVertices(theGraph);137138// Determine the name of the primary output file139outfileName = ConstructPrimaryOutputFilename(infileName, outfileName, command);140141// For some algorithms, the primary output file is not always written142if ((strchr("pdo", command) && Result == NONEMBEDDABLE) ||143(strchr("234", command) && Result == OK))144;145146// Write the primary output file, if appropriate to do so147else148gp_Write(theGraph, outfileName, WRITE_ADJLIST);149150// NOW WE WANT TO WRITE THE SECONDARY OUTPUT FILE151152// When called from the menu system, we want to write the planar or outerplanar153// obstruction, if one exists. For planar graph drawing, we want the character154// art rendition. An empty but non-NULL string is passed to indicate the necessity155// of selecting a default name for the second output file.156if (outfile2Name != NULL)157{158if ((command == 'p' || command == 'o') && Result == NONEMBEDDABLE)159{160// By default, use the same name as the primary output filename161if (strlen(outfile2Name) == 0)162outfile2Name = outfileName;163gp_Write(theGraph, outfile2Name, WRITE_ADJLIST);164}165else if (command == 'd' && Result == OK)166{167// By default, add ".render.txt" to the primary output filename168if (strlen(outfile2Name) == 0)169strcat((outfile2Name = outfileName), ".render.txt");170gp_DrawPlanar_RenderToFile(theGraph, outfile2Name);171}172}173}174175// Free the graph176gp_Free(&theGraph);177178// Flush any remaining message content to the user, and return the result179FlushConsole(stdout);180return Result;181}182183/****************************************************************************184WriteAlgorithmResults()185****************************************************************************/186187void WriteAlgorithmResults(graphP theGraph, int Result, char command, platform_time start, platform_time end, char *infileName)188{189if (infileName)190sprintf(Line, "The graph '%s' ", infileName);191else sprintf(Line, "The graph ");192Message(Line);193194switch (command)195{196case 'p' : sprintf(Line, "is%s planar.\n", Result==OK ? "" : " not"); break;197case 'd' : sprintf(Line, "is%s planar.\n", Result==OK ? "" : " not"); break;198case 'o' : sprintf(Line, "is%s outerplanar.\n", Result==OK ? "" : " not"); break;199case '2' : sprintf(Line, "has %s subgraph homeomorphic to K_{2,3}.\n", Result==OK ? "no" : "a"); break;200case '3' : sprintf(Line, "has %s subgraph homeomorphic to K_{3,3}.\n", Result==OK ? "no" : "a"); break;201case '4' : sprintf(Line, "has %s subgraph homeomorphic to K_4.\n", Result==OK ? "no" : "a"); break;202case 'c' : sprintf(Line, "has been %d-colored.\n", gp_GetNumColorsUsed(theGraph)); break;203default : sprintf(Line, "nas not been processed due to unrecognized command.\n"); break;204}205Message(Line);206207sprintf(Line, "Algorithm '%s' executed in %.3lf seconds.\n",208GetAlgorithmName(command), platform_GetDuration(start,end));209Message(Line);210}211212213