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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _components.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. *  COMPONENTS  (connected components)                                          *
  18. *                                                                              *
  19. *******************************************************************************/
  20.  
  21.  
  22.  
  23. #include <LEDA/graph_alg.h>
  24.  
  25. #include <LEDA/node_partition.h>
  26.  
  27.  
  28. int COMPONENTS(const ugraph& G, node_array(int)& compnum)
  29. { // computes connected components of undirected graph G
  30.   // compnum[v] = i  iff v in component i
  31.   // number of components is returned
  32.  
  33.   node v,w;
  34.   list(node) S;
  35.   int count = 0;
  36.  
  37.   node_array(bool) reached(G,false);
  38.  
  39.   forall_nodes(v,G) 
  40.      if (!reached[v]) 
  41.       { S = DFS(G,v,reached);
  42.         forall(w,S) compnum[w] = count;
  43.         count++; 
  44.        }
  45.  
  46.   return count;
  47. }
  48.  
  49.  
  50.  
  51. int COMPONENTS1(const ugraph& G, node_array(int)& compnum)
  52.  
  53.   node_partition P(G);
  54.   edge e;
  55.   node v;
  56.  
  57.   forall_nodes(v,G) compnum[v] = 0;
  58.  
  59.   forall_edges(e,G) P.union_blocks(source(e),target(e));
  60.  
  61.   int count = 0;
  62.   forall_nodes(v,G) 
  63.    { node w = P.find(v);
  64.      if (compnum[w]==0) compnum[w] = ++count;
  65.     }
  66.  
  67.   forall_nodes(v,G) compnum[v] = compnum[P.find(v)];
  68.  
  69.   return count;
  70. }
  71.  
  72.  
  73.