[455] | 1 | // -*- c++ -*- |
---|
[543] | 2 | #ifndef HUGO_BFS_DFS_MISC_H |
---|
| 3 | #define HUGO_BFS_DFS_MISC_H |
---|
[455] | 4 | |
---|
| 5 | #include <bfs_iterator.h> |
---|
| 6 | #include <for_each_macros.h> |
---|
| 7 | |
---|
| 8 | namespace hugo { |
---|
| 9 | |
---|
| 10 | /// This function eat a read-write \c BoolMap& bool_map, |
---|
| 11 | /// which have to work well up |
---|
| 12 | /// to its \c set and \c operator[]() method. Thus we have to deal |
---|
| 13 | /// very carefully with an uninitialized \c IterableBoolMap. |
---|
| 14 | template<typename Graph, typename BoolMap> |
---|
| 15 | bool isBipartite(const Graph& g, BoolMap& bool_map) { |
---|
| 16 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
| 17 | ReachedMap reached(g/*, false*/); |
---|
| 18 | BfsIterator<Graph, ReachedMap> bfs(g, reached); |
---|
| 19 | FOR_EACH_LOC(typename Graph::NodeIt, n, g) { |
---|
| 20 | if (!reached[n]) { |
---|
| 21 | bfs.pushAndSetReached(n); |
---|
[549] | 22 | bool_map.set(n, false); |
---|
| 23 | while (!bfs.finished()) { |
---|
| 24 | if (bfs.isBNodeNewlyReached()) { |
---|
| 25 | bool_map.set(bfs.bNode())=!bfs.aNode(); |
---|
| 26 | } else { |
---|
| 27 | if (bool_map[bfs.bNode()]==bool_map[bfs.aNode()]) { |
---|
| 28 | return false; |
---|
[455] | 29 | } |
---|
| 30 | } |
---|
[549] | 31 | ++bfs; |
---|
[455] | 32 | } |
---|
| 33 | } |
---|
| 34 | } |
---|
[549] | 35 | |
---|
[455] | 36 | return true; |
---|
| 37 | } |
---|
[540] | 38 | |
---|
| 39 | /// experimental topsort, |
---|
| 40 | /// I think the final version will work as an iterator |
---|
[549] | 41 | /// if the graph is not a acyclic, the na pre-topological order is obtained |
---|
[577] | 42 | /// (see Schrijver's book). |
---|
| 43 | /// PredMap have to be a writtable node-map. |
---|
| 44 | /// If the graph is directed and not acyclic, |
---|
| 45 | /// then going back from the returned node via the pred information, a |
---|
| 46 | /// cycle is obtained. |
---|
| 47 | template<typename Graph, typename PredMap> |
---|
| 48 | typename Graph::Node |
---|
| 49 | topSort(const Graph& g, std::list<typename Graph::Node>& l, |
---|
| 50 | PredMap& pred) { |
---|
[540] | 51 | l.clear(); |
---|
| 52 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
[577] | 53 | typedef typename Graph::template NodeMap<bool> ExaminedMap; |
---|
[540] | 54 | ReachedMap reached(g/*, false*/); |
---|
[577] | 55 | ExaminedMap examined(g/*, false*/); |
---|
[540] | 56 | DfsIterator<Graph, ReachedMap> dfs(g, reached); |
---|
| 57 | FOR_EACH_LOC(typename Graph::NodeIt, n, g) { |
---|
| 58 | if (!reached[n]) { |
---|
| 59 | dfs.pushAndSetReached(n); |
---|
[577] | 60 | pred.set(n, INVALID); |
---|
[543] | 61 | while (!dfs.finished()) { |
---|
[552] | 62 | ++dfs; |
---|
[577] | 63 | if (dfs.isBNodeNewlyReached()) { |
---|
| 64 | ///\bug hugo 0.2-ben Edge kell |
---|
| 65 | pred.set(dfs.aNode(), typename Graph::OutEdgeIt(dfs)); |
---|
| 66 | } else { |
---|
| 67 | ///\bug ugyanaz |
---|
| 68 | if (g.valid(typename Graph::OutEdgeIt(dfs)) && |
---|
| 69 | !examined[dfs.bNode()]) { |
---|
| 70 | ///\bug hugo 0.2-ben Edge kell |
---|
| 71 | pred.set(dfs.bNode(), typename Graph::OutEdgeIt(dfs)); |
---|
| 72 | return dfs.aNode(); |
---|
| 73 | } |
---|
| 74 | } |
---|
[543] | 75 | if (dfs.isANodeExamined()) { |
---|
| 76 | l.push_back(dfs.aNode()); |
---|
[577] | 77 | examined.set(dfs.aNode(), true); |
---|
[540] | 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
[577] | 82 | return INVALID; |
---|
[540] | 83 | } |
---|
[548] | 84 | } //namespace hugo |
---|
| 85 | |
---|
[543] | 86 | #endif //HUGO_BFS_DFS_MISC_H |
---|