src/work/marci/bfs_dfs_misc.h
author marci
Fri, 07 May 2004 10:57:31 +0000
changeset 576 d00c33d07114
parent 549 5531429143bc
child 577 e8703f0a6e2f
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@549
    22
	bool_map.set(n, false);
marci@549
    23
	while (!bfs.finished()) {
marci@549
    24
	  if (bfs.isBNodeNewlyReached()) {
marci@549
    25
	    bool_map.set(bfs.bNode())=!bfs.aNode();
marci@549
    26
	  } else {
marci@549
    27
	    if (bool_map[bfs.bNode()]==bool_map[bfs.aNode()]) {
marci@549
    28
	      return false;
marci@455
    29
	    }
marci@455
    30
	  }
marci@549
    31
	  ++bfs;
marci@455
    32
	}
marci@455
    33
      }
marci@455
    34
    }
marci@549
    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@549
    41
  /// if the graph is not a acyclic, the na pre-topological order is obtained 
marci@549
    42
  /// (see Schrijver's book)
marci@540
    43
  template<typename Graph> 
marci@549
    44
  void topSort(const Graph& g, std::list<typename Graph::Node>& l) {
marci@540
    45
    l.clear();
marci@540
    46
    typedef typename Graph::template NodeMap<bool> ReachedMap;
marci@540
    47
    ReachedMap reached(g/*, false*/);
marci@540
    48
    DfsIterator<Graph, ReachedMap> dfs(g, reached);
marci@540
    49
    FOR_EACH_LOC(typename Graph::NodeIt, n, g) {
marci@540
    50
      if (!reached[n]) {
marci@540
    51
	dfs.pushAndSetReached(n);
marci@543
    52
	while (!dfs.finished()) {
marci@552
    53
	  ++dfs;
marci@543
    54
	  if (dfs.isANodeExamined()) {
marci@543
    55
	    l.push_back(dfs.aNode());
marci@540
    56
	  }
marci@540
    57
	}
marci@540
    58
      }
marci@540
    59
    }
marci@540
    60
  }
marci@548
    61
} //namespace hugo
marci@548
    62
marci@543
    63
#endif //HUGO_BFS_DFS_MISC_H