2011-11-22 49 views
2

我目前正在嘗試定義boost圖的外部屬性。我使用一些捆綁的屬性內部的:在boost圖庫中綁定std :: vector的外部屬性映射

struct VertexProperties 
{ 
    int demand; 
}; 

struct EdgeProperties 
{ 
    uint capacity; 
    int cost; 
}; 

typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph; 

然而,該算法時,我需要一些外部的屬性,那就是我希望能夠到我的圖的邊/頂點映射到存儲在一個std ::元素矢量,以便我可以通過運算符[](Edge e)訪問它們。毫無頭緒地站在boost文檔的前面。似乎我需要一個property_map,但我不知道如何將這些與矢量一起使用。迄今爲止我發現的唯一例子涉及從頂點到矢量的映射,但是由於頂點是無符號整數,這很簡單。

我真的升壓沮喪我到目前爲止,我認爲這將有救了我一很多的時間來執行,並通過自己的測試圖表類,我真不得到這個瘋狂的模板元編程的東西...

+0

Boost.Graph是邊緣無法使用;它試圖過於普遍,文件很差。我建議寫你自己的班級。 –

+0

那麼,我已經寫了很多使用boost圖的代碼,我不想重寫所有這些... – Exp

回答

5

您可以創建外部屬性地圖,而不管圖中的內部和/或捆綁屬性如何。在邊上創建屬性貼圖比較困難,因爲您需要一個edge_index貼圖,而adjacency_list默認情況下沒有這些貼圖。 compressed_sparse_row_graph確實,但其結構大多是隻讀後施工。您可以在邊上使用associative_property_map,或者創建邊緣索引圖作爲內部屬性(如果您不經常更改圖形),填充它,然後使用它構建外部屬性圖(例如使用shared_array_property_map,例如)。

1

我遇到了同樣的問題,最近,這裏是我是如何結束的附加頂點屬性(稱爲此代碼段中):

// Graph has to have _index_ property (vector-based graphs get it implicitly) 
typedef typename property_map<Graph, vertex_index_t>::type IndexMap; 
// Storage type for the _degree_ property 
typedef std::vector<uint> DegreeVector; 
// Type of the _degree_ property map 
typedef iterator_property_map<typename DegreeVector::iterator, IndexMap> DegreeMap; 

// Graph in question 
Graph g(5); 
// Actual storage 
DegreeVector degree_storage(num_vertices(g)); 
// This is needed to construct _degree_ property map 
IndexMap index_map = get(vertex_index, g); 
// Create _degree_ property map 
DegreeMap degree_map = make_iterator_property_map(degree_storage.begin(), index_map); 

// Now degree_map is ready to be used 
degree_map[some_vertex_id] = 10; 
+0

如果圖的頂點被刪除,這會正確地工作嗎?謝謝。 – Agostino