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

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