eg_edgraph.ex.c

This is a more detailed example on how to use this module

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 /** @file
00021  * @ingroup EGeDgraph
00022  * */
00023 /** @addtogroup EGeDgraph */
00024 /** @{ */
00025 #include "eg_elist.h"
00026 #include "eg_edgraph.h"
00027 /* ========================================================================= */
00028 /** @brief example of a graph structure using the embeded substructures. */
00029 typedef struct my_dgraph_t
00030 {
00031   unsigned id;
00032   EGeDgraph_t G;
00033 }
00034 my_dgraph_t;
00035 
00036 /* ========================================================================= */
00037 /** @brief example of a node structure using the embeded substructures. */
00038 typedef struct my_dnode_t
00039 {
00040   unsigned id;
00041   EGeDgraphNode_t v;
00042 }
00043 my_dnode_t;
00044 
00045 /* ========================================================================= */
00046 /** @brief example of an edge structure using the embeded substructures. */
00047 typedef struct my_dedge_t
00048 {
00049   unsigned id;
00050   EGeDgraphEdge_t e;
00051 }
00052 my_dedge_t;
00053 
00054 /* ========================================================================= */
00055 /** @brief Display the contents of our graph structure */
00056 static inline void display_DG (my_dgraph_t * myG)
00057 {
00058   EGeDgraph_t *G = &(myG->G);
00059   my_dnode_t *cn;
00060   my_dedge_t *ce;
00061   EGeList_t *node_it,
00062    *edge_it;
00063   fprintf (stderr, "Graph %d (%d nodes, %d edges):\n", myG->id, G->n_nodes,
00064            G->n_edges);
00065   /* we display the nodes and it's contents only if it is not empty */
00066   if (!EGeListIsEmpty (&(G->nodes)))
00067   {
00068     fprintf (stderr, "Nodes:\n");
00069     for (node_it = G->nodes.next; node_it != &(G->nodes);
00070          node_it = node_it->next)
00071     {
00072       cn = EGcontainerOf (node_it, my_dnode_t, v.node_cn);
00073       fprintf (stderr, "\t%d: ", cn->id);
00074       if (!EGeListIsEmpty (&(cn->v.in_edge)))
00075       {
00076         fprintf (stderr, "(in edges) ");
00077         for (edge_it = cn->v.in_edge.next; edge_it != &(cn->v.in_edge);
00078              edge_it = edge_it->next)
00079         {
00080           ce = EGcontainerOf (edge_it, my_dedge_t, e.head_cn);
00081           fprintf (stderr, "%d ", ce->id);
00082         }
00083       }
00084       if (!EGeListIsEmpty (&(cn->v.out_edge)))
00085       {
00086         fprintf (stderr, "(out edges) ");
00087         for (edge_it = cn->v.out_edge.next; edge_it != &(cn->v.out_edge);
00088              edge_it = edge_it->next)
00089         {
00090           ce = EGcontainerOf (edge_it, my_dedge_t, e.tail_cn);
00091           fprintf (stderr, "%d ", ce->id);
00092         }
00093       }
00094       fprintf (stderr, "\n");
00095     }
00096   }
00097 
00098 }
00099 
00100 /* ========================================================================= */
00101 /** @brief A simple example of a directed graph using (EGdEgraph) structures.
00102  * @return zero on success, non-zero- otherwise.
00103  * @par Description:
00104  * Show how to use a directed graph, modify it and display it's contents */
00105 int main (void)
00106 {
00107   int rval = 0;
00108   my_dgraph_t myG;
00109   my_dnode_t nodes[5];
00110   my_dedge_t edges[5];
00111   int i;
00112 
00113   /* initialize all structures */
00114   EGeDgraphInit (&(myG.G));
00115   myG.id = 0;
00116   display_DG (&myG);
00117   /* note that this is a backward loop that should be more eficient than the
00118    * regular forward loop (you save the last update condition ) */
00119   for (i = 5; i--;)
00120   {
00121     EGeDgraphNodeInit (&(nodes[i].v));
00122     EGeDgraphEdgeInit (&(edges[i].e));
00123     nodes[i].id = i;
00124     edges[i].id = i;
00125   }
00126   /* now we can use all edges and nodes and add them to the graph, we will
00127    * create a circular graph on 5 edges */
00128   for (i = 5; i--;)
00129     EGeDgraphAddNode (&(myG.G), &(nodes[i].v));
00130   display_DG (&myG);
00131   for (i = 5; i--;)
00132     EGeDgraphAddEdge (&(myG.G), &(nodes[i].v), &(nodes[(i + 1) % 5].v),
00133                       &(edges[i].e));
00134   display_DG (&myG);
00135   /* now we will push all incomming edges to a previous node, and all outgoing
00136    * edges to the next edge */
00137   for (i = 5; i--;)
00138   {
00139     EGeDgraphChangeHead (&(myG.G), &(edges[i].e), &(nodes[(i + 4) % 5].v));
00140     EGeDgraphChangeTail (&(myG.G), &(edges[i].e), &(nodes[(i + 2) % 5].v));
00141   }
00142   display_DG (&myG);
00143   /* now we delete one edge at a time */
00144   for (i = 5; i--;)
00145   {
00146     EGeDgraphDelEdge (&(myG.G), &(edges[i].e));
00147     display_DG (&myG);
00148   }
00149   /* and delete nodes one by one */
00150   for (i = 5; i--;)
00151   {
00152     EGeDgraphDelNode (&(myG.G), &(nodes[i].v));
00153     display_DG (&myG);
00154   }
00155   /* ending */
00156   EGeDgraphClear (&myG);
00157   for (i = 5; i--;)
00158   {
00159     EGeDgraphNodeClear (&(nodes[i].v));
00160     EGeDgraphEdgeClear (&(edges[i].e));
00161   }
00162   return rval;
00163 }
00164 
00165 /* ========================================================================= */
00166 /** @} */

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