minspan.c

Go to the documentation of this file.
00001 /* LIBDGL -- a Directed Graph Library implementation
00002  * Copyright (C) 2002 Roberto Micarelli
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 /*
00020  * Source best viewed with tabstop=4
00021  */
00022 
00023 #include <stdio.h>
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <fcntl.h>
00029 #include <time.h>
00030 #include <errno.h>
00031 #include <math.h>
00032 
00033 #include "../type.h"
00034 #include "../graph.h"
00035 
00036 #include "opt.h"
00037 
00038 static int _clipper(dglGraph_s * pgraphIn,
00039                                         dglGraph_s * pgraphOut,
00040                                         dglSpanClipInput_s * pArgIn,
00041                                         dglSpanClipOutput_s * pArgOut,
00042                                         void * pvArg)
00043 {
00044         return 0;
00045 }
00046 
00047 int main( int argc , char ** argv )
00048 {
00049         dglGraph_s      graph , graphOut;
00050         dglInt32_t              nVertex;
00051         int                             nret , fd;
00052 
00053         /* program options
00054          */
00055         char    *       pszGraph;
00056         char    *       pszGraphOut;
00057         char    *       pszVertex;
00058  
00059         GNO_BEGIN/* short   long                default     variable        help */
00060         GNO_OPTION( "g",        "graph",                NULL ,          & pszGraph ,    "Input Graph file" )
00061         GNO_OPTION( "o",        "graphout",     NULL ,          & pszGraphOut , "Output Graph file" )
00062         GNO_OPTION( "v",        "vertex",               NULL ,          & pszVertex ,   "Vertex Node Id" )
00063         GNO_END
00064  
00065 
00066         if ( GNO_PARSE( argc , argv ) < 0 )
00067         {
00068                 return 1;
00069         }
00070         /*
00071          * options parsed
00072          */
00073 
00074         if ( pszVertex == NULL ) {
00075                 GNO_HELP("minspan usage");
00076                 return 1;
00077         }
00078         nVertex = atol(pszVertex);
00079 
00080         printf( "Graph read:\n" );
00081         if ( (fd = open( pszGraph , O_RDONLY )) < 0 )
00082         {
00083                 perror( "open" ); return 1;
00084         }
00085         nret = dglRead( & graph , fd );
00086         if ( nret < 0 ) {
00087                 fprintf( stderr , "dglRead error: %s\n", dglStrerror( & graph ) );
00088                 return 1;
00089         }
00090         close( fd );
00091         printf( "Done.\n" );
00092 
00093         printf( "Graph minimum spanning:\n" );
00094         nret = dglMinimumSpanning( & graph , & graphOut , nVertex , _clipper , NULL );
00095         if ( nret < 0 ) {
00096                 fprintf( stderr , "dglMinimumSpanning error: %s\n", dglStrerror( & graph ) );
00097                 return 1;
00098         }
00099         printf( "Done.\n" );
00100 
00101 
00102         printf( "Graph flatten:\n" );
00103         nret = dglFlatten( & graphOut );
00104         printf( "Done.\n" );
00105 
00106         if ( dglGet_EdgeCount( & graphOut ) > 0 ) {
00107 
00108 
00109                 if ( pszGraphOut ) {
00110                         printf( "Graph write:\n" );
00111                         if ( (fd = open( pszGraphOut , O_WRONLY | O_CREAT | O_TRUNC, 0666 )) < 0 )
00112                         {
00113                                 perror( "open" ); return 1;
00114                         }
00115                         dglWrite( & graphOut, fd );
00116                         if ( nret < 0 )
00117                         {
00118                                 fprintf( stderr , "dglWrite error: %s\n" , dglStrerror( & graphOut ) );
00119                                 return 1;
00120                         }
00121                         close( fd );
00122                         printf( "Done.\n" );
00123                 }
00124         }
00125         else {
00126                 printf( "Empty span. No output produced.\n" );
00127         }
00128 
00129         dglRelease( & graph );
00130         dglRelease( & graphOut );
00131         return 0;
00132 }

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5