簡體   English   中英

外部屬性映射綁定到Boost圖形庫中的std :: vector

[英]External property map tied to std::vector in boost graph library

我目前正在嘗試定義增強圖的外部屬性。 我將一些捆綁屬性用作內部屬性:

struct VertexProperties
{
  int demand;
};

struct EdgeProperties
{ 
  uint capacity;
  int cost;
};

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

但是,在算法過程中,我需要一些外部屬性,也就是說,我希望能夠將圖形的邊/頂點映射到std :: vector中存儲的元素,以便可以通過operator [](Edge E)。 我毫無頭緒地站在boost文檔的前面。 似乎我需要一個property_map,但我不知道如何將它們與向量一起使用。 到目前為止,我發現的唯一示例涉及從頂點到向量的映射,但是由於頂點是無符號整數,所以這很簡單。

到目前為止,我真的對提升感到沮喪,我認為這將為我節省很多時間來獨自實現和測試圖類,我真的沒有得到這個瘋狂的模板元編程的東西...

無論圖形中有哪些內部和/或捆綁的屬性,都可以創建外部屬性映射。 在邊緣上創建屬性映射要困難一些,因為您需要一個edge_index映射,而adjacency_list默認不包含那些屬性。 compressed_sparse_row_graph可以,但是構造后大部分都是只讀的。 您可以在邊緣上使用associative_property_map ,或者將邊緣索引圖創建為內部屬性(如果您不經常更改圖形),填充它,然后使用它來構建外部屬性圖(例如,使用shared_array_property_map ) )。

我最近遇到了同樣的問題,這就是我最終如何附加頂點屬性(在此代碼段中稱為 )的方法:

// 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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