[Lemon-commits] [lemon_svn] alpar: r2267 - hugo/trunk/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:51:22 CET 2006


Author: alpar
Date: Mon Oct 24 17:59:38 2005
New Revision: 2267

Modified:
   hugo/trunk/lemon/topology.h

Log:
Computing the number of the connected components and the components themselves.

Modified: hugo/trunk/lemon/topology.h
==============================================================================
--- hugo/trunk/lemon/topology.h	(original)
+++ hugo/trunk/lemon/topology.h	Mon Oct 24 17:59:38 2005
@@ -29,6 +29,7 @@
 /// \brief Topology related algorithms
 ///
 /// Topology related algorithms
+///\todo Place the file contents is the module tree.
 
 namespace lemon {
 
@@ -213,6 +214,56 @@
     return true;
   }
  
+  ///Count the number of connected components of an undirected graph
+
+  ///Count the number of connected components of an undirected graph
+  ///
+  ///\param g The graph. In must be undirected.
+  ///\return The number of components
+  ///\todo Test required
+  template<class UGraph>
+  int numberOfComponents(const UGraph &g)
+  {
+    int c=0;
+    Bfs<Graph> bfs(g);
+    bfs.init();
+    for(typename Graph::NodeIt n(g);n!=INVALID;++n)
+      if(!bfs.reached(n)) {
+	c++;
+	bfs.addSource(n);
+	bfs.start();
+      }
+    return c;
+  }
+
+
+  ///Find the connected components of an undirected graph
+
+  ///Find the connected components of an undirected graph
+  ///
+  ///\param g The graph. In must be undirected.
+  ///\retval comp A writable node map. The values will be set from 0 to
+  ///the number of the connected components minus one. Each values of the map
+  ///will be set exactly once, the values of a certain component will be
+  ///set continuously.
+  ///\return The number of components
+  ///\todo Test required
+  template<class UGraph, class WMap>
+  int connectedComponents(const UGraph &g, WMap &comp)
+  {
+    int c=0;
+    Bfs<Graph> bfs(g);
+    bfs.init();
+    for(typename Graph::NodeIt n(g);n!=INVALID;++n)
+      if(!bfs.reached(n)) {
+	bfs.addSource(n);
+	while ( bfs.nextNode()!=INVALID ) {
+	  comp[bfs.nextNode()]=c;
+	  processNextNode();
+	  c++;
+      }
+    return c;
+  }
 
 } //namespace lemon
 



More information about the Lemon-commits mailing list