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@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@540
|
50 |
while (!bfs.finished()) {
|
marci@540
|
51 |
if (bfs.isANodeExamined()) {
|
marci@540
|
52 |
l.push_back(bfs.aNode());
|
marci@540
|
53 |
}
|
marci@540
|
54 |
++bfs;
|
marci@540
|
55 |
}
|
marci@540
|
56 |
}
|
marci@540
|
57 |
}
|
marci@540
|
58 |
}
|
marci@455
|
59 |
}
|
marci@455
|
60 |
#endif //HUGO_BIPARTITE_GRAPHS_H
|