在boost :: graph中访问特定边并修改其属性

时间:2017-09-27 07:19:48

标签: c++ boost boost-graph

我使用以下代码初始化下面表示的图表:

enter image description here

#include <boost/config.hpp>
#include <iostream>
#include <fstream>

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;

typedef adjacency_list_traits<vecS, vecS, directedS> Traits;

typedef adjacency_list<
    vecS, vecS, directedS,

    property<
    vertex_name_t, std::string,
    property<vertex_index_t, int,
    property<vertex_color_t, boost::default_color_type,
    property<vertex_distance_t, double,
    property<vertex_predecessor_t, Traits::edge_descriptor>
    > > > >,

    property<
    edge_index_t, int,
    property<edge_capacity_t, double,
    property<edge_weight_t, double,
    property<edge_residual_capacity_t, double,
    property<edge_reverse_t, Traits::edge_descriptor>
> > > > >
Graph;

int main() {
    Graph g(4);

    property_map<Graph, edge_index_t>::type             E = get(edge_index, g);
    property_map<Graph, edge_weight_t>::type            cost = get(edge_weight, g);



    // Create edges
    Traits::edge_descriptor ed;

    int eindex = 0;

    ed = (add_edge(0, 1, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;


    ed = (add_edge(0, 2, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;

    ed = (add_edge(1, 3, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;

    ed = (add_edge(2, 3, g)).first;
    cost[ed] = 1;
    E[ed] = eindex++;

    //At this point, how can I change the cost of the second edge, (0,2), to 2 most efficiently?

}

添加四条边后,最初成本为1,我想将第二条边的成本(0,2)更改为2.

如何最有效地获得适当的边缘描述符?

我尝试过的一种方法如下:

graph_traits < Graph >::out_edge_iterator ei, e_end;
for (tie(ei, e_end) = out_edges(0, g); ei != e_end; ++ei) {//search through out edges from vertex 0
    if (target(*ei, g) == 2) {//if the edge ends at vertex 2
        cost[*ei] = 2;
    }
}

这似乎有点低效,因为它必须遍历从节点开始的所有边。有没有更有效的方法?

0 个答案:

没有答案