src/work/marci/bipartite_graphs.h
author athos
Tue, 04 May 2004 16:52:15 +0000
changeset 530 d9c06ac0b3a3
child 540 405ccc3105e1
permissions -rw-r--r--
Minimum cost flows of small values: algorithm from Andras Frank's lecture notes (approximately)
marci@455
     1
// -*- c++ -*-
marci@455
     2
#ifndef HUGO_BIPARTITE_GRAPHS_H
marci@455
     3
#define HUGO_BIPARTITE_GRAPHS_H
marci@455
     4
marci@455
     5
#include <bfs_iterator.h>
marci@455
     6
#include <for_each_macros.h>
marci@455
     7
marci@455
     8
namespace hugo {
marci@455
     9
marci@455
    10
  /// This function eat a read-write \c BoolMap& bool_map, 
marci@455
    11
  /// which have to work well up 
marci@455
    12
  /// to its \c set and \c operator[]() method. Thus we have to deal 
marci@455
    13
  /// very carefully with an uninitialized \c IterableBoolMap.
marci@455
    14
  template<typename Graph, typename BoolMap> 
marci@455
    15
  bool isBipartite(const Graph& g, BoolMap& bool_map) {
marci@455
    16
    typedef typename Graph::template NodeMap<bool> ReachedMap;
marci@455
    17
    ReachedMap reached(g/*, false*/);
marci@455
    18
    BfsIterator<Graph, ReachedMap> bfs(g, reached);
marci@455
    19
    FOR_EACH_LOC(typename Graph::NodeIt, n, g) {
marci@455
    20
      if (!reached[n]) {
marci@455
    21
	bfs.pushAndSetReached(n);
marci@455
    22
	bool_map.set(n, false) {
marci@455
    23
	  while (!bfs.finished()) {
marci@455
    24
	    if (bfs.isBNodeNewlyReached()) {
marci@455
    25
	      bool_map.set(bfs.bNode())=!bfs.aNode();
marci@455
    26
	    } else {
marci@455
    27
	      if (bool_map[bfs.bNode()]==bool_map[bfs.aNode()]) {
marci@455
    28
		return false;
marci@455
    29
	      }
marci@455
    30
	    }
marci@455
    31
	    ++bfs;
marci@455
    32
	  }
marci@455
    33
	}
marci@455
    34
      }
marci@455
    35
    }
marci@455
    36
    return true;
marci@455
    37
  }
marci@455
    38
}
marci@455
    39
#endif //HUGO_BIPARTITE_GRAPHS_H