![](/img/trans.png)
[英]boost graph library example six degrees of Kevin Bacon: what is Vertex() in the example
[英]What is a Simple Example for using the Boost Graph Library
我正在嘗試使用BGL ,我發現文檔很准確,但缺少更多簡單案例的示例。 我的目標如下所述(閱讀文檔后我仍然無法做到這一點):
struct Vertex
{
double m_d;
std::size_t m_id;
};
//or
struct Vertex
{
double m_d;
std::size_t id() const;
};
目標:
你能給我寫一個關於如何用 BGL 做到這一點的例子嗎? 我相信我需要MutableBidirectionalGraph
?
有向圖
G
馬上:
struct Vertex { double m_d = 0; size_t m_id = -1; // or std::size_t id() const; }; struct Edge { double cost = 0; }; using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, Vertex, Edge>;
(除了
in_edges
之外,雙向和定向之間有什么區別?)
沒有其他區別,當然除了枚舉傳入邊的復雜性保證以及插入邊時的線性開銷
G
可以保存頂點類型Vertex
。
見 0。
從
G
中通過 id 獲取頂點
auto find_by_id = [&g](size_t id) -> Vertex& { auto vv = boost::make_iterator_range(vertices(g)); auto vd = find_if(vv, [&, id](auto vd) { return g[vd].m_id == id; }); return g[*vd]; };
並在需要時更改
Vertex
結構中的值m_d
。
if (i_want()) { g[vd].m_id += 1; }
要么,
auto idmap = boost::get(&Vertex::m_id, g); if (i_want()) { idmap[vd] += 1; }
甚至
put(idmap, vd, 42);
甚至更多未標記:
get(boost::vertex_bundle, g, vd).m_id = 999;
添加、刪除頂點
remove_vertex(vd, g);
和頂點之間的邊
clear_vertex(vd, g);
並且還支持成本,即成本(邊緣)。
哇,這真的與以上任何一項都無關。 但它實際上與頂點 ID 相同:
if (i_want()) { g[ed].cost = new_cost; }
要么,
auto cost = boost::get(&Edge::cost, g); if (i_want()) { cost[ed] = new_cost; }
甚至
put(cost, ed, new_cost);
甚至更多未標記:
get(boost::edge_bundle, g, ed).cost = new_cost;
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>
struct Vertex {
double m_d = 0;
size_t m_id = -1;
// or std::size_t id() const;
};
struct Edge {
double cost = 0;
};
using Graph =
boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, Vertex, Edge>;
using boost::make_iterator_range;
int main(){
Graph g;
auto v0 = add_vertex({0.1, 100}, g);
auto v1 = add_vertex({0.2, 200}, g);
auto v2 = add_vertex({0.3, 300}, g);
auto v3 = add_vertex({0.4, 400}, g);
auto v4 = add_vertex({0.5, 500}, g);
auto v5 = add_vertex({0.6, 600}, g);
add_edge(v0, v2, Edge{1.5}, g);
add_edge(v1, v3, Edge{2.5}, g);
add_edge(v4, v1, Edge{3.5}, g);
add_edge(v2, v5, Edge{4.5}, g);
auto idmap = boost::get(&Vertex::m_id, g);
auto cost = boost::get(&Edge::cost, g);
auto find_by_id = [&g](size_t id) -> Vertex& {
auto vv = boost::make_iterator_range(vertices(g));
auto vd = find_if(vv, [&, id](auto vd) { return g[vd].m_id == id; });
return g[*vd];
};
print_graph(g, idmap, std::cout << "original: ");
auto i_want = [](auto vd) {
return (vd % 2); // when I want
};
for (auto vd : make_iterator_range(vertices(g))) {
if (i_want(vd))
g[vd].m_id += 1;
if (i_want(vd))
idmap[vd] += 1;
//put(idmap, vd, 42);
//get(boost::vertex_bundle, g, vd).m_id = 999;
}
print_graph(g, idmap, std::cout << "altered: ");
clear_vertex(v3, g);
remove_vertex(v3, g); // undefined behaviour unless edges cleared
print_graph(g, idmap, std::cout << "removed: ");
for (auto ed : make_iterator_range(edges(g))) {
std::cout << ed << " cost " << cost[ed] << "\n";
}
for (auto ed : make_iterator_range(edges(g))) {
cost[ed] *= 111;
}
for (auto ed : make_iterator_range(edges(g))) {
std::cout << ed << " cost " << cost[ed] << "\n";
}
};
印刷
original: 100 --> 300
200 --> 400
300 --> 600
400 -->
500 --> 200
600 -->
altered: 100 --> 300
202 --> 402
300 --> 602
402 -->
500 --> 202
602 -->
removed: 100 --> 300
202 -->
300 --> 602
500 --> 202
602 -->
(0,2) cost 1.5
(3,1) cost 3.5
(2,4) cost 4.5
(0,2) cost 166.5
(3,1) cost 388.5
(2,4) cost 499.5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.