[英]BGL: vertices in listS and index_map
我是BGL的新手,我已經將頭撞牆了幾天,試圖實現帶有頂點去除的圖形,直到我發現一個特別有用的SO問題( 該問題)節省了我的時間。
現在,我的代碼以這種方式構造(下面是簡化的代碼):
struct NodeProperties
{
int32_t data;
};
typedef property<edge_weight_t, uint32_t> EdgeProperties;
typedef adjacency_list<listS, listS, bidirectionalS,
property<vertex_index_t, int, NodeProperties>,
EdgeProperties> Graph;
/* In the graph initialization function */
Graph graph;
/* many add_vertex() and add_edge() here */
/* Assign indices */
int i = 0;
BGL_FORALL_VERTICES(v, graph, Graph) {
get(vertex_index, graph)[v] = i++;
}
/* Make many manipulations, in particular, edge/vertex removal according
to certain criteria, NO add_vertex (though some edge are created) */
/* In another function */
vector<int32_t> component(num_vertices(graph));
int num = connected_components(graph, make_iterator_property_map(component.begin(), get(vertex_index, graph), component[0]));
就目前為止我所了解的那樣,對頂點使用listS可以防止boost使用每個節點的位置作為索引,因此我必須使用一種“添加屬性”為自己提供索引。
我很好這一點,但上述方法無效代碼-雲中的connected_components線分段錯誤- 除非我重做指標分配。 這樣做可以使一切正常運行,但是在我創建的心理圖片中沒有任何意義。
有人可以嗎
在此先感謝您,祝您愉快!
[R
connected_components函數構造一個默認的color_map,其大小為num_vertices(g),在您的圖形中該值小於您分配的最大頂點索引。 當算法嘗試為索引大於num_vertices(g)的頂點之一寫入顏色時,將訪問無效的內存。
當您重新分配所有索引時,它們都位於num_vertices(g)之內。
有關屬性的快速參考,您應該閱讀http://www.boost.org/doc/libs/1_63_0/libs/graph/doc/quick_tour.html中的 “向圖形添加顏色”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.