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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _dijkstra.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. *  DIJKSTRA  (single source shortest paths)                                    *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23.  
  24. #include <LEDA/graph_alg.h>
  25.  
  26. #include <LEDA/node_pq.h>
  27.  
  28. declare(node_pq,int)
  29.  
  30.  
  31. void DIJKSTRA(const graph& G, node s, const edge_array(int)&  cost, 
  32.                                             node_array(int)&  dist,
  33.                                             node_array(edge)& pred ) 
  34. {
  35.   /*
  36.      Dijkstra's Algorithms for integer edge costs,
  37.      computes single source shortest paths from node s for 
  38.      a non-negative network (G,cost), computes for all nodes v:
  39.      a) dist[v] = cost of shortest path from s to v
  40.      b) pred[v] = predecessor edge of v in shortest paths tree
  41.   */
  42.  
  43.  
  44.   node_pq(int) PQ(G);
  45.  
  46.   int c;
  47.   node u,v;                                                                    
  48.   edge e;                                                                      
  49.                                                                                
  50.   forall_nodes(v,G)                                                            
  51.     { pred[v] = 0;                                                             
  52.       dist[v] = Infinity;                                                      
  53.       PQ.insert(v,dist[v]);
  54.      }                                                                         
  55.  
  56.   dist[s] = 0;
  57.   PQ.decrease_inf(s,0);                                               
  58.                                                                                
  59.   while (! PQ.empty())
  60.    { u = PQ.del_min();
  61.      if (dist[u] == Infinity) break;
  62.  
  63.      forall_adj_edges(e,u)                                                    
  64.         { v = target(e);                                                      
  65.           c = dist[u] + cost[e];                                              
  66.           if (c < dist[v])                                                    
  67.            { dist[v] = c;                                                     
  68.              pred[v] = e;                                                     
  69.              PQ.decrease_inf(v,c);
  70.             }                                                                 
  71.          }                                                                    
  72.  
  73.     } // while                                                                
  74.  
  75. }
  76.  
  77.  
  78.  
  79. #ifndef NO_REAL_GRAPH_ALG
  80.  
  81. // Dijkstra Algorithms for real valued edge costs 
  82.  
  83. declare(node_pq,real)
  84.  
  85. void DIJKSTRA(const graph& G, node s, const edge_array(real)&  cost, 
  86.                                             node_array(real)&  dist,
  87.                                             node_array(edge)& pred ) 
  88.  
  89. { node_pq(real) PQ(G);
  90.  
  91.   real c;
  92.   node u,v;                                                                    
  93.   edge e;                                                                      
  94.                                                                                
  95.   forall_nodes(v,G)                                                            
  96.     { pred[v] = 0;                                                             
  97.       dist[v] = Infinity;                                                      
  98.       PQ.insert(v,dist[v]);
  99.      }                                                                         
  100.  
  101.   dist[s] = 0;
  102.   PQ.decrease_inf(s,0);                                               
  103.                                                                                
  104.   while (! PQ.empty())
  105.    { u = PQ.del_min();
  106.      if (dist[u] == Infinity) break;
  107.  
  108.      forall_adj_edges(e,u)                                                    
  109.         { v = target(e);                                                      
  110.           c = dist[u] + cost[e];                                              
  111.           if (c < dist[v])                                                    
  112.            { dist[v] = c;                                                     
  113.              pred[v] = e;                                                     
  114.              PQ.decrease_inf(v,c);
  115.             }                                                                 
  116.          }                                                                    
  117.     } // while                                                                
  118.  
  119. }
  120.  
  121.  
  122. #endif
  123.