我有一個問題,需要我在Boost圖庫中找到有向圖的最小生成樹。Boost Graph Library - 有向圖的最小生成樹
我第一次嘗試使用深度優先搜索和DFS訪問者。我的計劃是忽略除樹邊回調之外的所有邊。這不起作用,我在下面舉例說明爲什麼。
我的問題是,如果我可以讓我的dfs-visitor在BGL中創建一個有向圖的最小生成樹。
有一些算法,這裏已經討論過(Finding a minimum spanning tree on a directed graph),我不知道它是否已經實現了BGL,或者它只是對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;
}
這是失敗的例子。如果我有以下有向圖
digraph G {
0;
1;
1->0 ;
}
深度優先搜索dfs-visitor,1-> 0被分類爲前沿。
如果圖形發生了變化,使邊緣爲0-> 1,那麼它就是樹形邊緣。
從DFS的前沿和源代碼的嚴格定義來看,由於在頂點1之前訪問頂點0,所以它是前沿。
但是,從技術意義上來說,邊1-> 0仍然是樹邊緣,並且從其頁面中給出的定義爲http://www.boost.org/doc/libs/1_59_0/libs/graph/doc/graph_theory_review.html。
前部邊緣是非樹邊(U,V)連接一個頂點u到 後裔v在搜索樹。
那麼,在BGL中是否存在一個簡單的解決方案,還是我必須在BGL中實現其中的一種算法?