for checking bipartiteness
authormarci
Wed, 28 Apr 2004 09:59:23 +0000
changeset 45514a1d11ddf21
parent 454 0cd33e3e60cb
child 456 02c28d3cf97b
for checking bipartiteness
src/work/marci/bipartite_graphs.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/marci/bipartite_graphs.h	Wed Apr 28 09:59:23 2004 +0000
     1.3 @@ -0,0 +1,39 @@
     1.4 +// -*- c++ -*-
     1.5 +#ifndef HUGO_BIPARTITE_GRAPHS_H
     1.6 +#define HUGO_BIPARTITE_GRAPHS_H
     1.7 +
     1.8 +#include <bfs_iterator.h>
     1.9 +#include <for_each_macros.h>
    1.10 +
    1.11 +namespace hugo {
    1.12 +
    1.13 +  /// This function eat a read-write \c BoolMap& bool_map, 
    1.14 +  /// which have to work well up 
    1.15 +  /// to its \c set and \c operator[]() method. Thus we have to deal 
    1.16 +  /// very carefully with an uninitialized \c IterableBoolMap.
    1.17 +  template<typename Graph, typename BoolMap> 
    1.18 +  bool isBipartite(const Graph& g, BoolMap& bool_map) {
    1.19 +    typedef typename Graph::template NodeMap<bool> ReachedMap;
    1.20 +    ReachedMap reached(g/*, false*/);
    1.21 +    BfsIterator<Graph, ReachedMap> bfs(g, reached);
    1.22 +    FOR_EACH_LOC(typename Graph::NodeIt, n, g) {
    1.23 +      if (!reached[n]) {
    1.24 +	bfs.pushAndSetReached(n);
    1.25 +	bool_map.set(n, false) {
    1.26 +	  while (!bfs.finished()) {
    1.27 +	    if (bfs.isBNodeNewlyReached()) {
    1.28 +	      bool_map.set(bfs.bNode())=!bfs.aNode();
    1.29 +	    } else {
    1.30 +	      if (bool_map[bfs.bNode()]==bool_map[bfs.aNode()]) {
    1.31 +		return false;
    1.32 +	      }
    1.33 +	    }
    1.34 +	    ++bfs;
    1.35 +	  }
    1.36 +	}
    1.37 +      }
    1.38 +    }
    1.39 +    return true;
    1.40 +  }
    1.41 +}
    1.42 +#endif //HUGO_BIPARTITE_GRAPHS_H