简体   繁体   English

什么是使用 Boost 图形库的简单示例

[英]What is a Simple Example for using the Boost Graph Library

I am trying to use theBGL , I find the documentation precise but lacks more examples for simple cases.我正在尝试使用BGL ,我发现文档很准确,但缺少更多简单案例的示例。 My goal is described below (after reading the documentation I still couldn't do this):我的目标如下所述(阅读文档后我仍然无法做到这一点):

struct Vertex
{
    double m_d;
    std::size_t m_id;
};

//or

struct Vertex
{
    double m_d;
    std::size_t id() const;
};

Goals :目标

  1. A directed graph G (what is the difference between a bidirectional and directed other than in_edges please?)有向图 G(除了 in_edges,双向图和有向图有什么区别?)
  2. G can hold the vertex type Vertex. G 可以持有顶点类型 Vertex。
  3. get the vertex by id from G and change the value m_d in the Vertex struct when I want.通过 id 从 G 获取顶点,并在需要时更改 Vertex 结构中的值 m_d。
  4. add, remove verticies and edges between verticies and also supports costs ie cost(edge).添加、删除顶点和顶点之间的边,还支持成本,即成本(边)。

Could you please write me an example on how to do this with BGL please?你能给我写一个关于如何用 BGL 做到这一点的例子吗? I beleive I need MutableBidirectionalGraph ?我相信我需要MutableBidirectionalGraph

  1. A directed graph G有向图G

    Straight-away:马上:

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

    (what is the difference between a bidirectional and directed other than in_edges please?) (除了in_edges之外,双向和定向之间有什么区别?)

    There is no other difference, except of course the complexity guarantees for enumerating incoming edges, and a linear overhead upon insertion of edges没有其他区别,当然除了枚举传入边的复杂性保证以及插入边时的线性开销

  2. G can hold the vertex type Vertex . G可以保存顶点类型Vertex

    See 0.见 0。

  3. get the vertex by id from GG中通过 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]; };

    and change the value m_d in the Vertex struct when I want.并在需要时更改Vertex结构中的值m_d

     if (i_want()) { g[vd].m_id += 1; }

    Or,要么,

     auto idmap = boost::get(&Vertex::m_id, g); if (i_want()) { idmap[vd] += 1; }

    or even甚至

    put(idmap, vd, 42);

    or even more unmarked:甚至更多未标记:

     get(boost::vertex_bundle, g, vd).m_id = 999;
  4. add, remove vertices添加、删除顶点

     remove_vertex(vd, g);

    and edges between vertices和顶点之间的边

     clear_vertex(vd, g);

    and also supports costs ie cost(edge).并且还支持成本,即成本(边缘)。

    Wow that really has nothing to do with any of the above.哇,这真的与以上任何一项都无关。 But it's really the same as with vertex ids:但它实际上与顶点 ID 相同:

     if (i_want()) { g[ed].cost = new_cost; }

    Or,要么,

     auto cost = boost::get(&Edge::cost, g); if (i_want()) { cost[ed] = new_cost; }

    or even甚至

    put(cost, ed, new_cost);

    or even more unmarked:甚至更多未标记:

     get(boost::edge_bundle, g, ed).cost = new_cost;

Live Demo现场演示

Live On Coliru生活在 Coliru

#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";
    }
};

Prints印刷

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM