【问题标题】:How do I change the edge weight in a graph using the Boost Graph Library?如何使用 Boost Graph Library 更改图中的边权重?
【发布时间】:2014-08-13 12:51:22
【问题描述】:

我已经使用 Boost 图形库定义了一个 Graph,

typedef boost::property<boost::edge_weight_t, int> EdgeWeightProperty;
typedef boost::adjacency_list<boost::listS, boost::vecS,boost::undirectedS,boost::no_property,EdgeWeightProperty> Graph;

使用添加边相当简单

boost::add_edge(vertice1, vertice2, weight, graph);

我还没有弄清楚如何在设置边缘权重后更改它。一种可能的解决方案是删除边缘并使用更新的权重值重新添加它,但是,这似乎有点过分。

【问题讨论】:

    标签: c++ boost graph boost-graph


    【解决方案1】:

    一种解决方案是执行以下操作

    typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS,boost::no_property,EdgeWeightProperty> Graph;
    typedef Graph::edge_descriptor Edge;
    Graph g;
    std::pair<Edge, bool> ed = boost::edge(v1,v2,g);
    int weight = get(boost::edge_weight_t(), g, ed.first);
    int weightToAdd = 10;
    boost::put(boost::edge_weight_t(), g, ed.first, weight+weightToAdd);
    

    【讨论】:

    • 如果您将边属性定义为带有元素 weight 的结构,则 int weight = g[ed.first].weight;g[ed.first].weight = weight + weightToAdd; 应该也可以分别工作,而不是 boost::getboost::put 函数
    • @agy 是否可以轻松地在边缘属性结构中使用带有权重的内置算法而不是 edge_weight_t()
    【解决方案2】:

    另一种解决方案是使用属性映射。这是一个例子。

    // Edge weight.
    typedef boost::property<boost::edge_weight_t, int> EdgeWeightProperty;
    
    // Graph.
    typedef boost::adjacency_list< boost::listS,
                                   boost::vecS,
                                   boost::undirectedS,
                                   boost::no_property,
                                   EdgeWeightProperty > Graph;
    
    // Vertex descriptor.
    typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
    
    // The Graph object
    Graph g;
    
    // Populates the graph.
    Vertex v1 = boost::add_vertex(g);
    Vertex v2 = boost::add_vertex(g);
    Vertex v3 = boost::add_vertex(g);
    boost::add_edge(v1, v2, EdgeWeightProperty(2), g);
    boost::add_edge(v1, v3, EdgeWeightProperty(4), g);
    boost::add_edge(v2, v3, EdgeWeightProperty(5), g);
    
    // The property map associated with the weights.
    boost::property_map < Graph,
                          boost::edge_weight_t >::type EdgeWeightMap = get(boost::edge_weight, g);
    
    // Loops over all edges and add 10 to their weight.
    boost::graph_traits< Graph >::edge_iterator e_it, e_end;
    for(std::tie(e_it, e_end) = boost::edges(g); e_it != e_end; ++e_it)
    {
      EdgeWeightMap[*e_it] += 10;
    }
    
    // Prints the weighted edgelist.
    for(std::tie(e_it, e_end) = boost::edges(g); e_it != e_end; ++e_it)
    {
      std::cout << boost::source(*e_it, g) << " "
                << boost::target(*e_it, g) << " "
                << EdgeWeightMap[*e_it] << std::endl;
    }
    

    【讨论】:

    • 如果我的权重是边的源顶点和结束顶点的函数怎么办?
    • @Jim 你的意思是:如果权重是顶点的某个变量(例如状态)的函数怎么办?因为现在,由于每条边都连接到两个节点,所以权重在技术上是两个顶点的函数。如果“源/目标”的顺序很重要,您可以使用双向图。
    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多