簡體   English   中英

Boost Graph CRS:散裝重量和Dijkstra

[英]Boost Graph CRS: bulk weights and Dijkstra

我正在嘗試盡可能高效地構建圖形,並且由於不需要在運行時更改圖形,因此我選擇了boost::compressed_sparse_row_graph 現在的問題很簡單:如何向邊緣添加權重並調用boost::dijkstra_shortest_paths

到目前為止,我已經完成了創建圖形的操作,但是我不知道該如何進行。

我的要求是:浪費盡可能少的內存和時間。 我面對的圖的節點數可能達到10 ^ 6。 我正在關注此Wiki條目,但恐怕我在Wiki中看到的屬性映射,索引映射和Co.將對我的程序造成額外負擔。

您是否認為有最小化內存占用的方法?

謝謝你的幫助!

// Properties: weights
typedef boost::property<boost::edge_weight_t, int> edge_weight;

// The graph itself as a compressed sparse row matrix
typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, edge_weight> boost_graph;

// Vertex iterator
typedef boost::graph_traits<boost_graph>::vertex_iterator vertex_iterator;

// Edge iterator
typedef boost::graph_traits<boost_graph>::edge_iterator edge_iterator;

// Adjacent nodes iterator
typedef boost::graph_traits<boost_graph>::adjacency_iterator adjacency_iterator;
typedef boost::graph_traits<boost_graph>::out_edge_iterator  outward_iterator;
typedef boost::graph_traits<boost_graph>::in_edge_iterator   inward_iterator;

int main(int argc, const char * argv[])
{
    std::vector<std::pair<std::size_t, std::size_t>> graph_edges;
    std::vector<int>                                 edge_weight;

    graph_edges.push_back(std::make_pair( 0,  1)); edge_weight.push_back(1);
    graph_edges.push_back(std::make_pair( 0,  3)); edge_weight.push_back(2);
    graph_edges.push_back(std::make_pair( 1,  4)); edge_weight.push_back(2);
    graph_edges.push_back(std::make_pair( 2,  4)); edge_weight.push_back(3);
    graph_edges.push_back(std::make_pair( 3,  4)); edge_weight.push_back(1);
    graph_edges.push_back(std::make_pair( 4,  5)); edge_weight.push_back(1);
    graph_edges.push_back(std::make_pair( 4,  6)); edge_weight.push_back(5);
    graph_edges.push_back(std::make_pair( 5,  7)); edge_weight.push_back(4);
    graph_edges.push_back(std::make_pair( 7,  8)); edge_weight.push_back(1);
    graph_edges.push_back(std::make_pair( 8,  9)); edge_weight.push_back(3);
    graph_edges.push_back(std::make_pair( 8, 11)); edge_weight.push_back(2);
    graph_edges.push_back(std::make_pair( 8, 12)); edge_weight.push_back(3);
    graph_edges.push_back(std::make_pair( 9, 10)); edge_weight.push_back(2);
    graph_edges.push_back(std::make_pair(12, 10)); edge_weight.push_back(4);

    // Create the graph
    boost_graph graph(boost::edges_are_unsorted_multi_pass, graph_edges.begin(), graph_edges.end(), 13);
    // ...

}

使用連續容器(例如std::vector )可以批量定義圖:

    boost_graph graph(boost::edges_are_unsorted_multi_pass, graph_edges.begin(), graph_edges.end(), edge_weight.data(), 13);

暫無
暫無

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

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