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

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _strongcomp.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. *  STRONG_COMPONENTS (strong connected components)                             *
  19. *                                                                              *
  20. *******************************************************************************/
  21.  
  22.  
  23.  
  24. #include <LEDA/graph_alg.h>
  25.  
  26. void scc_dfs(const graph& G, node v, node_array(int)& compnum,
  27.                                      node_array(int)& dfsnum,
  28.                                      list(node)&      unfinished,
  29.                                      list(node)&      roots,
  30.                                      node_array(bool)& in_unfinished,
  31.                                      int& count1, int& count2 );
  32.  
  33.  
  34. int STRONG_COMPONENTS(const graph& G, node_array(int)& compnum)
  35. {
  36.   // int STRONG_COMPONENTS(graph& G, node_array(int)& compnum)
  37.   // computes strong connected components (scc) of digraph G
  38.   // returns m = number of scc 
  39.   // returns in node_array(int) compnum for each node an integer with
  40.   // compnum[v] = compnum[w] iff v and w belong to the same scc
  41.   // 0 <= compnum[v] <= m-1 for all nodes v
  42.  
  43.   list(node)       unfinished;
  44.   list(node)       roots;
  45.   node_array(bool) in_unfinished(G,false);
  46.   node_array(int)  dfsnum(G,-1);
  47.  
  48.   int count1 = 0; 
  49.   int count2 = 0;
  50.  
  51.   node v;
  52.  
  53.   forall_nodes(v,G) 
  54.       if (dfsnum[v] == -1) 
  55.        scc_dfs(G,v,compnum,dfsnum,unfinished,roots,in_unfinished,count1,count2);
  56.  
  57.   return count2;
  58. }
  59.  
  60.  
  61. void scc_dfs(const graph& G, node v, node_array(int)& compnum,
  62.                                      node_array(int)& dfsnum,
  63.                                      list(node)&      unfinished,
  64.                                      list(node)&      roots,
  65.                                      node_array(bool)& in_unfinished,
  66.                                      int& count1, int& count2 )
  67. {
  68.   node w;
  69.  
  70.   dfsnum[v] = ++count1;
  71.   in_unfinished[v] = true;
  72.   unfinished.push(v);
  73.   roots.push(v);
  74.  
  75.   forall_adj_nodes(w,v)
  76.     { if (dfsnum[w]==-1) 
  77.        scc_dfs(G,w,compnum,dfsnum,unfinished,roots,in_unfinished,count1,count2);
  78.       else 
  79.        if (in_unfinished[w])
  80.         while (dfsnum[roots.head()]>dfsnum[w])  roots.pop();
  81.      }
  82.  
  83.   if (v==roots.head()) 
  84.    { do { w=unfinished.pop();
  85.           /* w is an element of the scc with root v */
  86.           in_unfinished[w] = false;
  87.           compnum[w] = count2;
  88.          } while (v!=w);
  89.      count2++;
  90.      roots.pop(); 
  91.     }
  92. }
  93.  
  94.  
  95. #include <LEDA/array.h>
  96.  
  97. declare(array,node)
  98.  
  99. int STRONG_COMPONENTS1(graph& G, node_array(int)& compnum)
  100.   node v,w;
  101.   int n = G.number_of_nodes();
  102.   int count = 0;
  103.  
  104.   array(node) V(1,n);
  105.   list(node) S;
  106.   node_array(int)  dfs_num(G), compl_num(G);
  107.   node_array(bool) reached(G,false);
  108.  
  109.   DFS_NUM(G,dfs_num,compl_num);
  110.  
  111.   forall_nodes(v,G) V[compl_num[v]] = v;
  112.  
  113.   G.rev();
  114.  
  115.  
  116.   int i;
  117.   for(i=n;i>0;i--)
  118.    { if ( !reached[V[i]] ) 
  119.       { S = DFS(G,V[i],reached);
  120.         forall(w,S) compnum[w] = count;
  121.         count++;
  122.        }
  123.     }
  124.  
  125.  return count;
  126.    
  127.  }
  128.  
  129.