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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _all_pairs.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. *  ALL PAIRS SHORTEST PATHS                                                    *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23. #include <LEDA/graph_alg.h>
  24.  
  25. void ALL_PAIRS_SHORTEST_PATHS(graph&G, const edge_array(int)& cost, 
  26.                                              node_matrix(int)& DIST)
  27.   // computes for every node pair (v,w) DIST(v,w) = cost of the least cost
  28.   // path from v to w, the single source shortest paths algorithms BELLMAN_FORD
  29.   // and DIJKSTRA are used as subroutines
  30.  
  31.   edge e;
  32.   node v,w;
  33.  
  34.   int C = 0;
  35.   forall_edges(e,G)  C += ((cost[e]>0) ? cost[e] : -cost[e]);
  36.  
  37.   node s = G.new_node();
  38.   forall_nodes(v,G) G.new_edge(s,v);
  39.  
  40.   edge_array(int) cost1(G);
  41.   node_array(int) dist1(G);
  42.  
  43.   node_array(edge)  pred(G);
  44.  
  45.   forall_edges(e,G)  
  46.     if (source(e)==s) cost1[e] = C;
  47.     else cost1[e] =  cost[e];
  48.  
  49.   BELLMAN_FORD(G,s,cost1,dist1,pred);
  50.  
  51.   G.del_node(s);
  52.  
  53.   forall_edges(e,G) cost1[e] = dist1[source(e)] + cost[e] - dist1[target(e)];
  54.  
  55.   forall_nodes(v,G) DIJKSTRA(G,v,cost1,DIST[v],pred);
  56.  
  57.   forall_nodes(v,G)
  58.     forall_nodes(w,G)
  59.       { int d = dist1[w] - dist1[v];
  60.         DIST(v,w) += d;
  61.        }
  62.  
  63. }
  64.  
  65.  
  66.  
  67. #ifndef NO_REAL_GRAPH_ALG
  68.  
  69. // For real valued edge costs:
  70.  
  71. void ALL_PAIRS_SHORTEST_PATHS(graph&G, const edge_array(real)& cost, 
  72.                                              node_matrix(real)& DIST)
  73.  
  74.   edge e;
  75.   node v,w;
  76.   real C = 0;
  77.  
  78.   forall_edges(e,G) 
  79.    if (cost[e]>0) C += cost[e];
  80.    else  C -= cost[e];
  81.  
  82.   node s = G.new_node();
  83.   forall_nodes(v,G) G.new_edge(s,v);
  84.  
  85.   edge_array(real) cost1(G);
  86.   node_array(real) dist1(G);
  87.  
  88.   node_array(edge)  pred(G);
  89.  
  90.   forall_edges(e,G)  
  91.     if (source(e)==s) cost1[e] = C;
  92.     else cost1[e] =  cost[e];
  93.  
  94.   BELLMAN_FORD(G,s,cost1,dist1,pred);
  95.  
  96.   G.del_node(s);
  97.  
  98.   forall_edges(e,G) cost1[e] = dist1[source(e)] + cost[e] - dist1[target(e)];
  99.  
  100.   forall_nodes(v,G) DIJKSTRA(G,v,cost1,DIST[v],pred);
  101.  
  102.   forall_nodes(v,G)
  103.     forall_nodes(w,G)
  104.       { real d = dist1[w] - dist1[v];
  105.         DIST(v,w) += d;
  106.        }
  107. }
  108.  
  109.  
  110.  
  111. #endif
  112.