src/work/marci/bfs_dfs_misc.h
author marci
Thu, 06 May 2004 14:25:21 +0000
changeset 548 61898ac9e9dc
parent 543 2b031f790e7a
child 549 5531429143bc
permissions -rw-r--r--
(none)
marci@455
     1
// -*- c++ -*-
marci@543
     2
#ifndef HUGO_BFS_DFS_MISC_H
marci@543
     3
#define HUGO_BFS_DFS_MISC_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@540
    38
marci@540
    39
  /// experimental topsort, 
marci@540
    40
  /// I think the final version will work as an iterator
marci@540
    41
  template<typename Graph> 
marci@540
    42
  void topSort(Graph& g, std::list<typename Graph::Node>& l) {
marci@540
    43
    l.clear();
marci@540
    44
    typedef typename Graph::template NodeMap<bool> ReachedMap;
marci@540
    45
    ReachedMap reached(g/*, false*/);
marci@540
    46
    DfsIterator<Graph, ReachedMap> dfs(g, reached);
marci@540
    47
    FOR_EACH_LOC(typename Graph::NodeIt, n, g) {
marci@540
    48
      if (!reached[n]) {
marci@540
    49
	dfs.pushAndSetReached(n);
marci@543
    50
	while (!dfs.finished()) {
marci@543
    51
	  if (dfs.isANodeExamined()) {
marci@543
    52
	    l.push_back(dfs.aNode());
marci@540
    53
	  }
marci@548
    54
	  ++dfs;
marci@540
    55
	}
marci@540
    56
      }
marci@540
    57
    }
marci@540
    58
  }
marci@548
    59
} //namespace hugo
marci@548
    60
marci@543
    61
#endif //HUGO_BFS_DFS_MISC_H