簡體   English   中英

BGL:listS和index_map中的頂點

[英]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線分段錯誤- 除非我重做指標分配。 這樣做可以使一切正常運行,但是在我創建的心理圖片中沒有任何意義。

有人可以嗎

  • 給我一個很好的參考,以通俗易懂的方式解釋所有這個index_map“ trickery”? 好吧,有噸的官方文檔中,但它們看起來復雜,我(我是一個C程序員)。 我打算學習高級C ++,但是目前我必須為自己的工作實現此圖算法,在開始真正的代碼之前,我不能花3個月的時間學習C ++ ...(已經使代碼勝任工作了大約10個小時,那么在這段時間內,我很容易在C中重新實現以上所有功能……)
  • 向我解釋為什么我必須這樣做(或者我在這里做錯了什么)?

在此先感謝您,祝您愉快!

[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM