It can be used to solve orientation problems on directed graphs. By example how can we orient an undirected graph to get the minimum number of strongly connected components. If we orient the edges with the dfs algorithm out from the source then we will get such an orientation.
We use the visitor interface of the dfs algorithm:
template <typename DirMap> class OrientVisitor : public DfsVisitor<UGraph> { public: OrientVisitor(const UGraph& ugraph, DirMap& dirMap) : _ugraph(ugraph), _dirMap(dirMap), _processed(ugraph, false) {} void discover(const Edge& edge) { _processed.set(edge, true); _dirMap.set(edge, _ugraph.direction(edge)); } void examine(const Edge& edge) { if (_processed[edge]) return; _processed.set(edge, true); _dirMap.set(edge, _ugraph.direction(edge)); } private: const UGraph& _ugraph; DirMap& _dirMap; UGraph::UEdgeMap<bool> _processed; };
And now we can use the orientation:
UGraph::UEdgeMap<bool> dmap(ugraph); typedef OrientVisitor<UGraph::UEdgeMap<bool> > Visitor; Visitor visitor(ugraph, dmap); DfsVisit<UGraph, Visitor> dfs(ugraph, visitor); dfs.run(); typedef DirUGraphAdaptor<UGraph> DGraph; DGraph dgraph(ugraph, dmap); LEMON_ASSERT(countStronglyConnectedComponents(dgraph) == countBiEdgeConnectedComponents(ugraph), "Wrong Orientation");
The number of the bi-connected components is a lower bound for the number of the strongly connected components in the directed graph because if we contract the bi-connected components to nodes we will get a tree therefore we cannot orient edges in both direction between bi-connected components. In the other way the algorithm will orient one component to be strongly connected. The two relations proof that the assertion will be always true and the found solution is optimal.
#include <lemon/ugraph_adaptor.h>
Public Member Functions | |
DirUGraphAdaptor (_Graph &_graph, DirectionMap &_direction_map) | |
Constructor of the adaptor. |
DirUGraphAdaptor | ( | _Graph & | _graph, | |
DirectionMap & | _direction_map | |||
) | [inline] |
Constructor of the adaptor