2012-07-26 71 views
5

我想用提升breadth_first_visit方法,我想爲它提供我自己的「外部」彩色地圖。 我定義如下彩色地圖在提升圖形breadth_first_visit

typedef boost::adjacency_list<boost::setS, boost::listS, boost::undirectedS, 
    boost::property<boost::vertex_index_t, int, 
    boost::property<boost::vertex_color_t, boost::default_color_type, 
    Node_t>>> GraphType; 

其中Node_t是一個結構,限定用於頂點的屬性的圖表。但是,我無法瞭解如何使用我自己的色彩地圖提供BFS。我想頂點顏色存儲在一個矢量,所以我的定義看起來像

std::vector<boost::default_color_type> colors; 

,但我不能弄清楚,如何使用這個的BFS。

無論

boost::breadth_first_search(g, *boost::vertices(g).first, 
    boost::color_map(colors)); 

也不

boost::breadth_first_search(g, *boost::vertices(g).first, 
    boost::color_map(&colors[0])); 

工作。雖然第一給了我一堆不同的編譯器錯誤(不支持如默認int,「助推:: color_traits」使用類類型的需要類型參數列表)第二編譯只有C2664中止:「提高::把」不能將參數2從'void *'轉換爲'ptrdiff_t'。

所以現在的問題是:我該如何使用自己的顏色映射結構。另外一個問題是:我如何獲取特定vertex_descriptor的顏色值?

回答

4

好吧,我用另一種方法,但解決我的問題。對於那些誰的困惑,我正要升壓彩色地圖或那些有興趣誰:

顏色的地圖類型,如BFS使用它是:

typedef boost::property_map<GraphType, boost::vertex_color_t>::type color_map_t; 
color_map_t colorMap; //Create a color map 

這映射vertex_descriptor到(以我的情況)default_color_type。適當的呼叫提高的BFS將

boost::breadth_first_visit(g, *boost::vertices(g).first, boost::color_map(colorMap)); 

給定一個color_names結構映射像

const char* color_names[] = {"white", "gray", "green", "red", "black"}; 

一個通過遍歷所有頂點的圖形,並使用vertex_descriptor的通過顏色可以遍歷色號

GraphType::vertex_iterator it, itEnd; 
for (boost::tie(it, itEnd) = boost::vertices(g); it != itEnd; it++) 
{ 
    std::cout << "Color of node " << *it << " is " << color_names[colorMap[*it]] << std::endl; 
} 
+0

在'升壓:: breadth_first_visit(克,*升壓::頂點(克)。首先,升壓:: color_map顏色表(:當前頂點作爲[] - 運算符在彩色地圖的參數的));'你'不傳遞訪問者 - 通過訪問者和色彩映射的簽名是什麼? – 2016-11-09 16:06:52

+0

唷,那是相當長一段時間以前;)我沒有嘗試了這一點,但是這裏(http://www.boost.org/doc/libs/1_62_0/libs/graph/doc/breadth_first_visit.html)你用'BFSVisitor'類作爲參數找到第二個簽名。 – AquilaRapax 2016-11-09 20:23:19

+0

我想我已經明白了 - 它使用「命名參數」簽名。我不知道你應該知道哪些名字是有效的參數,但對於'breadth_first_search',似乎'visitor'是一個,'color_map'是另一個。 – 2016-11-09 21:31:33