home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / graph_al / _bellman.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.0 KB  |  147 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _bellman_ford.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. /*******************************************************************************
  17. *                                                                              *
  18. *  BELLMAN FORD                                                                *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23.  
  24.  
  25. #include <LEDA/graph_alg.h>
  26. #include <LEDA/b_queue.h>
  27.  
  28. declare(b_queue,node)
  29.  
  30. bool BELLMAN_FORD(const graph& G, node s, const edge_array(int)& cost, 
  31.                                                 node_array(int)& dist, 
  32.                                                 node_array(edge)& pred ) 
  33.  
  34. /* single source shortest paths from s using a queue (breadth first search)
  35.    computes for all nodes v:
  36.    a) dist[v] = cost of shortest path from s to v
  37.    b) pred[v] = predecessor edge of v in shortest paths tree
  38. */
  39.  
  40.   node_array(bool) in_Q(G,false);
  41.   node_array(int)  count(G,0);
  42.  
  43.   int n = G.number_of_nodes();
  44.   b_queue(node) Q(n);
  45.  
  46.   node u,v;
  47.   edge e;
  48.   int c;
  49.  
  50.   forall_nodes(v,G) 
  51.     { pred[v] = 0;
  52.       dist[v] =  Infinity; 
  53.      }
  54.  
  55.   dist[s] = 0;
  56.   Q.append(s);
  57.   in_Q[s] = true;
  58.  
  59.   while (!Q.empty())
  60.     { u = Q.pop();
  61.       in_Q[u] = false;
  62.  
  63.       if (++count[u] > n) return false;
  64.  
  65.       forall_adj_edges(e,u) 
  66.         { v = target(e);
  67.           c = dist[u] + cost[e];
  68.  
  69.           if (c < dist[v]) 
  70.            { dist[v] = c; 
  71.              pred[v] = e;
  72.              if (!in_Q[v])  
  73.               { Q.append(v);
  74.                 in_Q[v]=true;
  75.                }
  76.  
  77.             }
  78.  
  79.          } /* forall */
  80.  
  81.      } // while
  82.  
  83.   return true;
  84.  
  85. }
  86.  
  87.  
  88.  
  89. #ifndef NO_REAL_GRAPH_ALG
  90.  
  91.  
  92. // BELLMAN_FORD for real valued edge costs:
  93.  
  94. bool BELLMAN_FORD(const graph& G, node s, const edge_array(real)& cost, 
  95.                                                 node_array(real)& dist, 
  96.                                                 node_array(edge)& pred ) 
  97.  
  98. { int n = G.number_of_nodes();
  99.   b_queue(node) Q(n);
  100.   node_array(bool) in_Q(G,false);
  101.   node_array(int) count(G,0);
  102.  
  103.   node u,v;
  104.   edge e;
  105.   real c;
  106.  
  107.   forall_nodes(v,G) 
  108.     { pred[v] = 0;
  109.       dist[v] =  Infinity; 
  110.      }
  111.  
  112.   dist[s] = 0;
  113.   Q.append(s);
  114.   in_Q[s] = true;
  115.  
  116.   while (!Q.empty())
  117.     { u = Q.pop();
  118.       in_Q[u] = false;
  119.  
  120.       if (++count[u] > n)  return false;
  121.  
  122.       forall_adj_edges(e,u) 
  123.         { v = target(e);
  124.           c = dist[u] + cost[e];
  125.  
  126.           if (c < dist[v]) 
  127.            { dist[v] = c; 
  128.              pred[v] = e;
  129.              if (!in_Q[v])  
  130.               { Q.append(v);
  131.                 in_Q[v] = true;
  132.                }
  133.  
  134.             }
  135.  
  136.          } /* forall */
  137.  
  138.      } // while
  139.  
  140.   return true;
  141.  
  142. }
  143.  
  144. #endif
  145.