// -*- c++ -*- #ifndef HUGO_BFS_DFS_MISC_H #define HUGO_BFS_DFS_MISC_H #include #include namespace hugo { /// This function eat a read-write \c BoolMap& bool_map, /// which have to work well up /// to its \c set and \c operator[]() method. Thus we have to deal /// very carefully with an uninitialized \c IterableBoolMap. template bool isBipartite(const Graph& g, BoolMap& bool_map) { typedef typename Graph::template NodeMap ReachedMap; ReachedMap reached(g/*, false*/); BfsIterator bfs(g, reached); FOR_EACH_LOC(typename Graph::NodeIt, n, g) { if (!reached[n]) { bfs.pushAndSetReached(n); bool_map.set(n, false); while (!bfs.finished()) { if (bfs.isBNodeNewlyReached()) { bool_map.set(bfs.bNode())=!bfs.aNode(); } else { if (bool_map[bfs.bNode()]==bool_map[bfs.aNode()]) { return false; } } ++bfs; } } } return true; } /// experimental topsort, /// I think the final version will work as an iterator /// if the graph is not a acyclic, the na pre-topological order is obtained /// (see Schrijver's book) template void topSort(const Graph& g, std::list& l) { l.clear(); typedef typename Graph::template NodeMap ReachedMap; ReachedMap reached(g/*, false*/); DfsIterator dfs(g, reached); FOR_EACH_LOC(typename Graph::NodeIt, n, g) { if (!reached[n]) { dfs.pushAndSetReached(n); while (!dfs.finished()) { if (dfs.isANodeExamined()) { l.push_back(dfs.aNode()); } ++dfs; } } } } } //namespace hugo #endif //HUGO_BFS_DFS_MISC_H