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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _bicomp.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. *  BICONNECTED COMPONENTS                                                      *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23.  
  24. #include <LEDA/graph_alg.h>
  25.  
  26. declare(node_array,node)
  27.  
  28. void bcc_dfs(const ugraph& G, node v, node_array(int)&, 
  29.                                       node_array(int)&,
  30.                                       node_array(int)&,
  31.                                       node_array(node)&,
  32.                                       list(node)&,
  33.                                       int&,
  34.                                       int&);
  35.  
  36.  
  37. int BICONNECTED_COMPONENTS(const ugraph& G, node_array(int)& compnum)
  38. {
  39.   // computes the biconnected components of undirected graph G
  40.   // returns m = number of biconnected components
  41.   // returns in edge_array(int) compnum for each edge an integer with
  42.   // compnum[x] = compnum[y] iff x and y belong to the same component 
  43.   // 0 <= compnum[e] <= m-1 for all edges e
  44.  
  45.   list(node)       unfinished;
  46.   node_array(int)  dfsnum(G,-1);
  47.   node_array(int)  lowpt(G);
  48.   node_array(node) father(G);
  49.  
  50.   int count1 = 0; 
  51.   int count2 = 0;
  52.  
  53.   node v;
  54.  
  55.   forall_nodes(v,G) 
  56.       if (dfsnum[v] == (-1)) 
  57.        bcc_dfs(G,v,compnum,dfsnum,lowpt,father,unfinished,count1,count2);
  58.  
  59.   return count2;
  60. }
  61.  
  62.  
  63.  
  64. void bcc_dfs(const ugraph& G, node v, node_array(int)&  compnum,
  65.                               node_array(int)&  dfsnum,
  66.                               node_array(int)&  lowpt,
  67.                               node_array(node)& father,
  68.                               list(node)&       unfinished,
  69.                               int& count1, int& count2) 
  70. {
  71.   node w;
  72.  
  73.   dfsnum[v] = ++count1;
  74.  
  75.   lowpt[v] = dfsnum[v];
  76.  
  77.   unfinished.push(v);
  78.  
  79.   forall_adj_nodes(w,v)
  80.     if (dfsnum[w]==-1) 
  81.       { father[w] = v;
  82.         bcc_dfs(G,w,compnum,dfsnum,lowpt,father,unfinished,count1,count2);
  83.         lowpt[v] = Min(lowpt[v],lowpt[w]);
  84.        }
  85.     else lowpt[v] = Min(lowpt[v],dfsnum[w]);
  86.  
  87.  
  88.   if ((dfsnum[v] >= 2) && (lowpt[v] == dfsnum[father[v]]))
  89.    { do { w = unfinished.pop();
  90.           compnum[w] = count2;
  91.       } while (v!=w);
  92.  
  93.       count2++;
  94.     }
  95.  
  96. }
  97.  
  98.