eg_menger_app.c

Go to the documentation of this file.
00001 #include "eg_menger_app.h"
00002 
00003 EGmengerNodeData_t *EGnewMengerNodeData (EGmemPool_t * mem)
00004 {
00005 
00006   EGmengerNodeData_t *ndata;
00007 
00008   ndata =
00009     (EGmengerNodeData_t *) EGmemPoolMalloc (mem, sizeof (EGmengerNodeData_t));
00010   memset (ndata, 0, sizeof (EGmengerNodeData_t));
00011 
00012   ndata->dist = ndata->orig_dist = ndata->pi = EG_DIJKSTRA_COST_MAX;
00013 
00014   return ndata;
00015 
00016 }
00017 
00018 EGmengerEdgeData_t *EGnewMengerEdgeData (EGmemPool_t * mem)
00019 {
00020 
00021   EGmengerEdgeData_t *edata;
00022 
00023   edata =
00024     (EGmengerEdgeData_t *) EGmemPoolMalloc (mem, sizeof (EGmengerEdgeData_t));
00025 
00026   edata->cost = edata->reduced_cost = EG_DIJKSTRA_COST_MAX;
00027   edata->is_in_solution = 0;
00028   edata->data = 0;
00029 
00030   return edata;
00031 
00032 }
00033 
00034 void EGfreeMengerEdgeDataMP (void *v,
00035                              EGmemPool_t * mem)
00036 {
00037   EGmemPoolFree (v, sizeof (EGmengerEdgeData_t), mem);
00038   return;
00039 }
00040 
00041 void EGfreeMengerNodeDataMP (void *v,
00042                              EGmemPool_t * mem)
00043 {
00044   EGmemPoolFree (v, sizeof (EGmengerNodeData_t), mem);
00045   return;
00046 }
00047 
00048 void EGmengerDisplayEdgeData (void *v,
00049                               FILE * file)
00050 {
00051 
00052   EGmengerEdgeData_t *edata;
00053 
00054   edata = (EGmengerEdgeData_t *) v;
00055 
00056   fprintf (file, "c=%lf, rc=%lf, iis=%d", EGdijkstraCostToLf (edata->cost),
00057            EGdijkstraCostToLf (edata->reduced_cost),
00058            (int) (edata->is_in_solution));
00059 
00060   return;
00061 
00062 }
00063 
00064 void EGmengerDisplayNodeData (void *v,
00065                               FILE * file)
00066 {
00067 
00068   EGmengerNodeData_t *ndata;
00069 
00070   ndata = (EGmengerNodeData_t *) v;
00071 
00072   fprintf (file, "dist = %lf   odist = %lf   mark = %u   omark = %u",
00073            EGdijkstraCostToLf (ndata->dist),
00074            EGdijkstraCostToLf (ndata->orig_dist), ndata->marker,
00075            ndata->orig_marker);
00076 
00077   return;
00078 
00079 }
00080 
00081 
00082 EGdGraph_t *EGnewMengerGraph (EGmemPool_t * mem,
00083                               int nNodes,
00084                               int nEdges,
00085                               int *edges,
00086                               EGdijkstraCost_t * weight,
00087                               size_t ** os)
00088 {
00089 
00090   unsigned int i;
00091 
00092   EGdGraph_t *G;
00093   EGdGraphNode_t **pnodes;
00094 
00095   EGmengerNodeData_t *ndata;
00096   EGmengerEdgeData_t *edata;
00097 
00098   EXIT (*os, "*os must be NULL");
00099 
00100   /* Prepare the offset vector.                                               */
00101   EGmengerSetOs (os, mem);
00102 
00103   /* Prepare the graph.                                                       */
00104   G = EGnewDGraph (mem);
00105 
00106   pnodes =
00107     (EGdGraphNode_t **) EGmemPoolMalloc (mem,
00108                                          sizeof (EGdGraphNode_t *) * nNodes);
00109 
00110   for (i = 0; i < (unsigned) nNodes; i++)
00111   {
00112     ndata = EGnewMengerNodeData (mem);
00113     pnodes[i] = EGdGraphAddNode (G, ndata);
00114   }
00115   for (i = 0; i < (unsigned) nEdges; i++)
00116   {
00117     edata = EGnewMengerEdgeData (mem);
00118     edata->cost = weight[i];
00119     EGdGraphAddEdge (G, edata, pnodes[edges[2 * i]], pnodes[edges[2 * i + 1]]);
00120     edata = EGnewMengerEdgeData (mem);
00121     edata->cost = weight[i];
00122     EGdGraphAddEdge (G, edata, pnodes[edges[2 * i + 1]], pnodes[edges[2 * i]]);
00123   }
00124 
00125   EGmemPoolFree (pnodes, sizeof (EGdGraphNode_t *) * nNodes, mem);
00126 
00127   return G;
00128 
00129 }
00130 
00131 int EGmengerPathsBAS (unsigned int s,
00132                       unsigned int t,
00133                       EGdijkstraCost_t ubound,
00134                       EGdijkstraCost_t * menger_val,
00135                       unsigned int npaths,
00136                       unsigned int *iedges,
00137                       unsigned int *iedges_beg,
00138                       int nNodes,
00139                       int nEdges,
00140                       int *edges,
00141                       EGdijkstraCost_t * weight,
00142                       EGmemPool_t * mem)
00143 {
00144 
00145   int rval,
00146     i;
00147 
00148   EGlistNode_t *n_it;
00149   EGdGraphNode_t *snode,
00150    *tnode;
00151   size_t *os;
00152   EGdGraph_t *G;
00153 
00154   EGdGraphEdge_t **pedges;
00155 
00156   /* Initialize a graph with all of the entries required by the algorithm.    */
00157   os = 0;
00158   G = EGnewMengerGraph (mem, nNodes, nEdges, edges, weight, &os);
00159 
00160   /* Initialize the pedges structure.                                         */
00161   pedges =
00162     (EGdGraphEdge_t **) EGmemPoolMalloc (mem,
00163                                          sizeof (EGdGraphEdge_t *) * nEdges);
00164 
00165   /* Obtain the node pointer for 's'.                                         */
00166   for (n_it = G->nodes->begin; n_it; n_it = n_it->next)
00167   {
00168     snode = (EGdGraphNode_t *) (n_it->this);
00169     if (snode->id == s)
00170       break;
00171   }
00172   TEST (!n_it, "Node 's' not found");
00173 
00174   /* Obtain the node pointer for 't'.                                         */
00175   for (n_it = G->nodes->begin; n_it; n_it = n_it->next)
00176   {
00177     tnode = (EGdGraphNode_t *) (n_it->this);
00178     if (tnode->id == t)
00179       break;
00180   }
00181   TEST (!n_it, "Node 't' not found");
00182 
00183   /* Run the algorithm.                                                       */
00184   rval =
00185     EGmengerPaths (snode, tnode, ubound, menger_val, npaths, pedges, iedges_beg,
00186                    os, G);
00187   CHECKRVAL (rval);
00188 
00189   for (i = 0; (unsigned) i < iedges_beg[npaths]; i++)
00190     iedges[i] = pedges[i]->id;
00191 
00192   {
00193     int j;
00194     for (i = 0; (unsigned) i < npaths; i++)
00195     {
00196       fprintf (stderr, "path: %u\n", i);
00197       for (j = iedges_beg[i]; (unsigned) j < iedges_beg[i + 1]; j++)
00198         fprintf (stderr, "(%u %u) ", pedges[j]->tail->id, pedges[j]->head->id);
00199       fprintf (stderr, "\n");
00200     }
00201   }
00202 
00203   EGdGraphClearMP (G, EGfreeMengerEdgeDataMP, EGfreeMengerNodeDataMP, 0, mem,
00204                    mem, 0);
00205   EGfreeDGraph (G);
00206   EGmemPoolFree (pedges, sizeof (EGdGraphEdge_t *) * nEdges, mem);
00207   EGmemPoolFree (os, sizeof (size_t) * 13, mem);
00208 
00209   return 0;
00210 
00211 }
00212 
00213 int EGmengerPaths (EGdGraphNode_t * s,
00214                    EGdGraphNode_t * t,
00215                    EGdijkstraCost_t ubound,
00216                    EGdijkstraCost_t * menger_val,
00217                    unsigned int npaths,
00218                    EGdGraphEdge_t ** pedges,
00219                    unsigned int *pedges_beg,
00220                    size_t * os,
00221                    EGdGraph_t * G)
00222 {
00223 
00224   int rval;
00225   unsigned int found_paths;
00226   size_t dij_os[6];
00227   EGheap_t *my_heap;
00228   EGdijkstraCost_t dij_ubound;
00229 
00230   /* Prepare the offsets which will be used by dijkstra                       */
00231   dij_os[EG_DIJ_DIST] = os[EG_MENGER_ORIG_DIST];
00232   dij_os[EG_DIJ_NDIST] = os[EG_MENGER_ORIG_NDIST];
00233   dij_os[EG_DIJ_FATHER] = os[EG_MENGER_ORIG_FATHER];
00234   dij_os[EG_DIJ_MARKER] = os[EG_MENGER_ORIG_MARK];
00235   dij_os[EG_DIJ_HCONNECTOR] = os[EG_MENGER_HEAP_CONNECTOR];
00236   dij_os[EG_DIJ_ELENGTH] = os[EG_MENGER_ECOST];
00237 
00238   /* Prepare the heap                                                         */
00239   my_heap = EGnewHeap (G->mem, 2, G->nodes->size);
00240 
00241   /* Compute the ubound                                                       */
00242   dij_ubound = EG_DIJKSTRA_COST_MAX;
00243 
00244   /* Solve the first dijkstra                                                 */
00245   rval = EGpartialDijkstra (s, 0, dij_ubound, dij_os, my_heap, G);
00246   CHECKRVAL (rval);
00247   rval = EGdijkstraCheckOptimality (s, 0, dij_ubound, dij_os, G);
00248   TEST (rval, "failed to meet optimality conditions");
00249 
00250   /* Question: can we use a better dij_ubound and somehow 'omit' those nodes
00251    * which are too far from 's'?                                              */
00252 
00253   rval =
00254     EGmengerPathsADV (s, t, ubound, npaths, &found_paths, menger_val, my_heap,
00255                       os, G);
00256   CHECKRVAL (rval);
00257 
00258   if (found_paths)
00259   {
00260     rval =
00261       EGmengerRecoverGraphAndSolution (G, os, s, t, found_paths, pedges,
00262                                        pedges_beg);
00263     CHECKRVAL (rval);
00264   }
00265 
00266   EGfreeHeap (my_heap, G->mem);
00267 
00268   return 0;
00269 
00270 }
00271 
00272 void EGmengerSetOs (size_t ** os,
00273                     EGmemPool_t * mem)
00274 {
00275 
00276   *os = (size_t *) EGmemPoolMalloc (mem, sizeof (size_t) * EG_MENGER_NUMOS);
00277 
00278   (*os)[EG_MENGER_PI] = offsetof (EGmengerNodeData_t, pi);
00279   (*os)[EG_MENGER_DIST] = offsetof (EGmengerNodeData_t, dist);
00280   (*os)[EG_MENGER_ORIG_DIST] = offsetof (EGmengerNodeData_t, orig_dist);
00281   (*os)[EG_MENGER_NDIST] = offsetof (EGmengerNodeData_t, ndist);
00282   (*os)[EG_MENGER_ORIG_NDIST] = offsetof (EGmengerNodeData_t, orig_ndist);
00283   (*os)[EG_MENGER_MARK] = offsetof (EGmengerNodeData_t, marker);
00284   (*os)[EG_MENGER_ORIG_MARK] = offsetof (EGmengerNodeData_t, orig_marker);
00285   (*os)[EG_MENGER_FATHER] = offsetof (EGmengerNodeData_t, father);
00286   (*os)[EG_MENGER_ORIG_FATHER] = offsetof (EGmengerNodeData_t, orig_father);
00287   (*os)[EG_MENGER_HEAP_CONNECTOR] = offsetof (EGmengerNodeData_t, hc);
00288 
00289   (*os)[EG_MENGER_ECOST] = offsetof (EGmengerEdgeData_t, cost);
00290   (*os)[EG_MENGER_REDUCED_ECOST] = offsetof (EGmengerEdgeData_t, reduced_cost);
00291   (*os)[EG_MENGER_IS_IN_SOL] = offsetof (EGmengerEdgeData_t, is_in_solution);
00292   (*os)[EG_MENGER_EDATA] = offsetof (EGmengerEdgeData_t, data);
00293 
00294   return;
00295 
00296 }
00297 
00298 void EGmengerDisplayEdgeBasic (void *v,
00299                                FILE * file)
00300 {
00301   EGdGraphEdge_t *e;
00302   size_t dij_os[6];
00303 
00304   e = (EGdGraphEdge_t *) (v);
00305 
00306   dij_os[EG_DIJ_ELENGTH] = offsetof (EGmengerEdgeData_t, cost);
00307 
00308   fprintf (file, "(%u %u) [%lf] ", e->tail->id, e->head->id,
00309            EGdijkstraCostToLf (EGdijkstraGetEdgeLength (e, dij_os)));
00310 
00311   return;
00312 }
00313 
00314 EGdijkstraCost_t EGmengerEdgeCost (EGdGraphEdge_t * e)
00315 {
00316   size_t dij_os[6];
00317   dij_os[EG_DIJ_ELENGTH] = offsetof (EGmengerEdgeData_t, cost);
00318 
00319   return (EGdijkstraGetEdgeLength (e, dij_os));
00320 }

Generated on Thu Oct 20 14:58:41 2005 for DominoParitySeparator by  doxygen 1.4.5