I have a problem that requires me to find the minimum spanning tree of a directed graph in Boost Graph Library.
My first try was to use the depth first search and DFS-visitor. My plan was to ignore all the edges except the tree edges callback. This doesn't work and I give the example below on why.
My question is if I can make my dfs-visitor create a minimum spanning tree of a directed graph in BGL.
There are algorithms for it and has been discussed here (Finding a minimum spanning tree on a directed graph) and I can't tell if it's been implemented for BGL or not or it's just a simple modification of something that's already in BGL.
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/depth_first_search.hpp>
struct my_visitor : public boost::dfs_visitor<>
{
template <class Edge, class Graph>
void back_edge(Edge e, const Graph&)
{
std::cout << "Back edge: " << e << std::endl;
}
template <class Edge, class Graph>
void forward_or_cross_edge(Edge u, const Graph& g)
{
std::cout << "Forward or cross edge: " << u << std::endl;
}
template <class Edge, class Graph>
void tree_edge(Edge u, const Graph& g)
{
std::cout << "Tree Edge: " << u << std::endl;
}
};
int main()
{
using namespace boost;
typedef boost::adjacency_list< listS, vecS, bidirectionalS > digraph;
// Construct the directed graph
digraph g(2);
add_edge(1, 0, g);
//add_edge(0, 1, g);
my_visitor vis2;
boost::depth_first_search(g, visitor(vis2));
return 0;
}
Here is the example that fails. If I have the following directed graph
digraph G {
0;
1;
1->0 ;
}
In depth first search dfs-visitor, 1->0 is classified as a forward edge.
If the graph was changed so that the edge is 0->1, then it is a tree edge.
From the strict definition of the forward edge and the source code of DFS, since vertex 0 is visited before vertex 1, it is a forward edge.
However, the edge 1->0 is still a tree edge in a technical sense and from the definition given in their page as http://www.boost.org/doc/libs/1_59_0/libs/graph/doc/graph_theory_review.html.
Forward edges are non-tree edges (u,v) that connect a vertex u to a descendant v in a search tree.
So, is there a simple solution in BGL or do I have to implement one of the algorithms in BGL for it?