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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _spanning.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. *  SPANNING_TREE  (spanning tree)                                              *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23.  
  24. #include <LEDA/graph_alg.h>
  25.  
  26. #include <LEDA/node_partition.h>
  27.  
  28.  
  29. list(edge) SPANNING_TREE(const graph& G)
  30. { node v,w;
  31.   edge e;
  32.   list(edge) EL;
  33.   node_partition P(G);
  34.  
  35.   forall_edges(e,G)
  36.      { v = source(e);
  37.        w = target(e);
  38.        if (! P.same_block(v,w))
  39.         { EL.append(e);
  40.           P.union_blocks(v,w);
  41.          }
  42.       }
  43.  
  44.   return EL;
  45. }
  46.  
  47.  
  48. /*******************************************************************************
  49. *                                                                              *
  50. *  LEDA 1.0    (graph algorithms)                                              *
  51. *                                                                              *
  52. *  MIN_SPANNING_TREE   (minimum spanning tree)                                 *
  53. *                                                                              *
  54. *******************************************************************************/
  55.  
  56. static edge_array(int) edge_cost;
  57.  
  58. static int CMP_EDGES(edge& e1, edge& e2) 
  59. { return (edge_cost[e1] - edge_cost[e2]); }
  60.  
  61. list(edge) MIN_SPANNING_TREE(const graph& G, const edge_array(int)& cost)
  62. { node v,w;
  63.   edge e;
  64.   list(edge) EL;
  65.   node_partition P(G);
  66.  
  67.   list(edge) OEL = G.all_edges();
  68.  
  69.   edge_cost = cost;
  70.  
  71.   OEL.sort(CMP_EDGES);
  72.  
  73.   forall(e,OEL)
  74.      { v = source(e);
  75.        w = target(e);
  76.        if (! P.same_block(v,w))
  77.         { EL.append(e);
  78.           P.union_blocks(v,w);
  79.          }
  80.       }
  81.  
  82.   return EL;
  83. }
  84.  
  85.  
  86.  
  87.  
  88. #ifndef NO_REAL_GRAPH_ALG
  89.  
  90.  
  91. static edge_array(real) edge_cost1;
  92.  
  93. static int CMP_EDGES1(edge& e1, edge& e2) 
  94. { return (edge_cost[e1] - edge_cost[e2]); }
  95.  
  96. list(edge) MIN_SPANNING_TREE(const graph& G, const edge_array(real)& cost)
  97. { node v,w;
  98.   edge e;
  99.   list(edge) EL;
  100.   node_partition P(G);
  101.  
  102.   list(edge) OEL = G.all_edges();
  103.  
  104.   edge_cost1 = cost;
  105.  
  106.   OEL.sort(CMP_EDGES1);
  107.  
  108.   forall(e,OEL)
  109.      { v = source(e);
  110.        w = target(e);
  111.        if (! P.same_block(v,w))
  112.         { EL.append(e);
  113.           P.union_blocks(v,w);
  114.          }
  115.       }
  116.  
  117.   return EL;
  118. }
  119.  
  120.  
  121. #endif
  122.