eg_dgraph.ex.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /*****************************************************************************
00021  *                                                                           *
00022  * This program reads a weighted graph from a file and displays it on        * 
00023  * screen.                                                                   * 
00024  *                                                                           * 
00025  * The input file format is:                                                 * 
00026  *                                                                           * 
00027  * nnodes nedges                                                             * 
00028  * head0 tail0 weight0                                                       * 
00029  * head1 tail1 weight1                                                       * 
00030  * head2 tail2 weight2                                                       * 
00031  *   ...                                                                     * 
00032  * headn tailn weightn                                                       * 
00033  *                                                                           * 
00034  *****************************************************************************/
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 
00039 #ifdef LINUX
00040 #include <getopt.h>
00041 #endif
00042 
00043 #include "eg_mempool.h"
00044 #include "eg_dgraph.h"
00045 
00046 void usage (char *program)
00047 {
00048   fprintf (stdout, "Usage: %s [options]\n", program);
00049   fprintf (stdout, "Options:\n");
00050   fprintf (stdout, "     -f n   file name.\n");
00051 }
00052 
00053 int parseargs (int argc,
00054                char **argv,
00055                char **file_name)
00056 {
00057 
00058   int c;
00059 
00060   while ((c = getopt (argc, argv, "f:")) != EOF)
00061   {
00062     switch (c)
00063     {
00064     case 'f':
00065       *file_name = optarg;
00066       break;
00067     default:
00068       usage (argv[0]);
00069       return 1;
00070     }
00071   }
00072 
00073   /* reporting the options */
00074   fprintf (stdout, "Parsed Options:\n");
00075   fprintf (stdout, "input         : %s\n", *file_name);
00076 
00077   return 0;
00078 
00079 }
00080 
00081 int main (int argc,
00082           char **argv)
00083 {
00084 
00085   int rval;
00086   unsigned int i;
00087   char *file_name = 0;
00088   FILE *file;
00089 
00090   unsigned int nedges,
00091     nnodes;
00092   unsigned int *edges = 0;
00093 
00094   double *weight = 0;
00095 
00096   EGmemPool_t *mem = 0;
00097   EGdGraph_t *G = 0;
00098   EGdGraphNode_t **nodes = 0;
00099 
00100   /* Parse command line input to get 'file name' and 'd'. */
00101   rval = parseargs (argc, argv, &file_name);
00102   CHECKRVAL (rval);
00103 
00104   /* Read the objects to sort from the file */
00105   file = fopen (file_name, "r");
00106   TEST (!file, "unable to open file %s", file_name);
00107 
00108   fscanf (file, "%u %u", &nnodes, &nedges);
00109 
00110   edges = (unsigned int *) malloc (sizeof (unsigned int) * 2 * nedges);
00111   weight = (double *) malloc (sizeof (double) * nedges);
00112 
00113   for (i = 0; i < nedges; i++)
00114   {
00115     fscanf (file, "%u %u %lf", &edges[2 * i], &edges[2 * i + 1], &weight[i]);
00116     TEST (edges[2 * i] > (nnodes - 1), "Edge %u (head) out of bounds", i);
00117     TEST (edges[2 * i + 1] > (nnodes - 1), "Edge %u (tail) out of bounds", i);
00118   }
00119 
00120   fclose (file);
00121 
00122   /* Create a new memory pool and heap */
00123   mem = EGnewMemPool (100, EGmemPoolNewSize, EGmemPoolNewSize (1));
00124 
00125   /* Create a new di-graph */
00126   G = EGnewDGraph (mem);
00127 
00128   /* Allocate space for the node objects */
00129   nodes =
00130     (EGdGraphNode_t **) EGmemPoolMalloc (mem,
00131                                          sizeof (EGdGraphNode_t *) * nnodes);
00132 
00133   /* Add the nodes into the graph */
00134   fprintf (stderr, "\nAdding Nodes... ");
00135   for (i = 0; i < nnodes; i++)
00136     nodes[i] = EGdGraphAddNode (G, 0);
00137 
00138   /* Add the edges into the graph */
00139   fprintf (stderr, "done.\n\nAdding Edges... ");
00140   for (i = 0; i < nedges; i++)
00141     EGdGraphAddEdge (G, &weight[i], nodes[edges[2 * i]],
00142                      nodes[edges[2 * i + 1]]);
00143 
00144   fprintf (stderr, "done.\n\n");
00145 
00146   /* Liberating allocated memory */
00147 
00148   free (edges);
00149   free (weight);
00150   EGmemPoolFree (nodes, sizeof (EGdGraphNode_t *) * nnodes, mem);
00151   EGdGraphClear (G, 0, 0, 0);
00152   EGfreeDGraph (G);
00153   EGfreeMemPool (mem);
00154 
00155   return 0;
00156 
00157 }

Generated on Mon Jan 30 08:48:52 2006 for EGlib by  doxygen 1.4.5