Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在BGL c+中更新边的值+;?_C++_Algorithm_Boost_Graph_Edges - Fatal编程技术网

C++ 如何在BGL c+中更新边的值+;?

C++ 如何在BGL c+中更新边的值+;?,c++,algorithm,boost,graph,edges,C++,Algorithm,Boost,Graph,Edges,我正在尝试更新图形中边的值,但是,当我使用以下属性映射更新该值时: property_map<Graph, edge_weight_t>::type weight = get(edge_weight, g); weight[*edgeIndex] = new_value; property\u-map::type-weight=get(edge\u-weight,g); 重量[*边缘索引]=新的_值; 它会更新值,但是当我尝试使用dijkstra算法获得最短路径时,该算法会得到权

我正在尝试更新图形中边的值,但是,当我使用以下属性映射更新该值时:

property_map<Graph, edge_weight_t>::type weight = get(edge_weight, g);
weight[*edgeIndex] = new_value;
property\u-map::type-weight=get(edge\u-weight,g);
重量[*边缘索引]=新的_值;
它会更新值,但是当我尝试使用dijkstra算法获得最短路径时,该算法会得到权重的旧_值。但当我在图中列出所有边和权重时,更新的权重使用新的_值


我尝试使用boost::put();然而,也会出现同样的问题。仅当新的_值小于旧的_值时,它才起作用

谁能帮帮我吗

换句话说,我只想设置边的名称和权重,并动态更新这些权重

这是我的代码:

#include <iostream>                  // for std::cout
#include <utility>                   // for std::pair
#include <algorithm>                 // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/config.hpp>
#include <string>
#include <string.h>
#include <list>
#include <fstream>

using namespace boost;

//define de Edges and Vertex property
typedef property<edge_weight_t, float, property<edge_name_t, std::string> > EdgeWeightProperty;
typedef property<vertex_name_t, std::string> VertexProperty;

//define the type of the Graph
typedef adjacency_list<vecS, vecS, undirectedS, VertexProperty, EdgeWeightProperty> Graph;



int main(){
    typedef float Weight;

    //Graph instance
    Graph g;

    //property accessors
    property_map<Graph, vertex_name_t>::type node = get(vertex_name, g);
    property_map<Graph, edge_weight_t>::type weight = get(edge_weight, g);
    property_map<Graph, edge_name_t>::type edge = get(edge_name, g);

    // Create the vertices  
    typedef graph_traits<Graph>::vertex_descriptor Vertex;
    std::vector<Vertex> vertex_list;

    typedef graph_traits<Graph>::edge_descriptor Edge;
    std::vector<Edge> edges_list;

    typedef boost::property_map <Graph, vertex_index_t >::type IndexMap;
    typedef boost::property_map <Graph, vertex_name_t >::type NameMap;

    typedef boost::iterator_property_map <Vertex*, IndexMap, Vertex, Vertex& > PredecessorMap;

    typedef boost::iterator_property_map < Weight*, IndexMap, Weight, Weight& > DistanceMap;

    std::string vertex_name;
    std::ifstream vertex_file;
    vertex_file.open("vertex_file.txt");

    if (vertex_file.is_open()){
            for(int index = 0; vertex_file.peek() != EOF; index++){

                vertex_file >> vertex_name;
                vertex_list.push_back(add_vertex(g));
                node[vertex_list.at(index)] = vertex_name;

            }
            vertex_file.close();
    }

    std::string edge_name, from, to;
    std::ifstream edges_file;
    edges_file.open("edge.txt");

    int index_from, index_to;

    if(edges_file.is_open()){
        for(int index=0; edges_file.peek() != EOF; index++){
            edges_file >> edge_name;
            edges_file >> from;
            edges_file >> to;

            for(index_from=0; index_from < vertex_list.size(); index_from++){
                if(strcmp(from.c_str(), node[vertex_list.at(index_from)].c_str()) == 0){
                    break;
                }
            }

            for(index_to=0; index_to < vertex_list.size(); index_to++){
                if(strcmp(to.c_str(), node[vertex_list.at(index_to)].c_str()) == 0){
                    break;
                }
            }


            edges_list.push_back((add_edge(vertex_list.at(index_from), vertex_list.at(index_to), g)).first);
            edge[edges_list.at(index)] = edge_name;
            //std::cout << edges_list.at(index) << std::endl;
            weight[edges_list.at(index)] = 10;


        }
    }


    typedef graph_traits<Graph>::edge_iterator edge_iter;
    std::pair<edge_iter, edge_iter> ep;
    edge_iter ei, ei_end;
    std::string teste = "0/0to0/1";

    for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei){
        //std::cout << edge[*ei] << std::endl;
        if(strcmp(edge[*ei].c_str(), teste.c_str()) == 0){
            weight[*ei] = 700;

        }
    }


    std::vector<Vertex> predecessors(boost::num_vertices(g)); // To store parents
    std::vector<Weight> distances(boost::num_vertices(g)); // To store distances

    IndexMap indexMap = boost::get(boost::vertex_index, g);

    PredecessorMap predecessorMap(&predecessors[0], indexMap);
    DistanceMap distanceMap(&distances[0], indexMap);

    boost::dijkstra_shortest_paths(g, vertex_list.at(0), boost::distance_map(distanceMap).predecessor_map(predecessorMap));

    std::cout << "distances and parents:" << std::endl;
    NameMap nameMap = boost::get(boost::vertex_name, g);


      std::cout << std::endl;

    typedef std::vector<Graph::edge_descriptor> PathType;

    PathType path;

  Vertex v = vertex_list.at(1); // We want to start at the destination and work our way back to the source
  for(Vertex u = predecessorMap[v]; // Start by setting 'u' to the destintaion node's predecessor
      u != v; // Keep tracking the path until we get to the source
      v = u, u = predecessorMap[v]) // Set the current vertex to the current predecessor, and the predecessor to one level up
  {
    std::pair<Graph::edge_descriptor, bool> edgePair = boost::edge(u, v, g);
    Graph::edge_descriptor edge = edgePair.first;

    path.push_back( edge );
  }

  // Write shortest path
  //std::cout << "Shortest path from v0 to v3:" << std::endl;
  float totalDistance = 0;
  for(PathType::reverse_iterator pathIterator = path.rbegin(); pathIterator != path.rend(); ++pathIterator)
  {
    std::cout << nameMap[boost::source(*pathIterator, g)] << " to " << nameMap[boost::target(*pathIterator, g)]
              << " = " << boost::get( boost::edge_weight, g, *pathIterator ) << std::endl;

  }

  std::cout << std::endl;

  std::cout << "Distance: " << distanceMap[vertex_list.at(1)] << std::endl;



  return 0;
}
#包含//用于std::cout
#include//for std::pair
#包括//对于std::对于每个
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间boost;
//定义边和顶点属性
typedef属性EdgeWightProperty;
typedef属性VertexProperty;
//定义图形的类型
typedef邻接列表图;
int main(){
类型DEF浮子重量;
//图形实例
图g;
//属性访问器
属性映射::类型节点=get(顶点名称,g);
属性映射::类型权重=获取(边权重,g);
属性映射::type edge=get(edge\u name,g);
//创建顶点
typedef图形特征::顶点描述符顶点;
std::向量顶点列表;
typedef graph_traits::edge_描述符edge;
std::向量边列表;
typedef boost::property_map::type IndexMap;
typedef boost::property_map::type NameMap;
typedef boost::迭代器属性映射前置映射;
typedef boost::迭代器属性映射DistanceMap;
std::字符串顶点名称;
std::ifstream顶点文件;
打开(“vertex_file.txt”);
如果(顶点文件是打开的()){
对于(int index=0;vertex_file.peek()!=EOF;index++){
顶点文件>>顶点名称;
顶点列表。向后推(添加顶点(g));
节点[顶点列表.at(索引)]=顶点名称;
}
vertex_file.close();
}
std::字符串边缘_名称,从,到;
std::IFU文件;
edges_file.open(“edge.txt”);
int index_from,index_to;
如果(边\u文件.is\u open()){
对于(int index=0;edges_file.peek()!=EOF;index++){
边缘\u文件>>边缘\u名称;
边缘\u文件>>来自;
边缘\u文件>>至;
for(index_from=0;index_from//std::cout“仅当新的_值小于旧的_值时才起作用”。您只在最短路径中打印边,因此(只要有替代路径)是有意义的权重较大的边不会出现在您的列表中。/谢谢您的回答,我用一个简单的图形进行了测试,它是有效的,但是使用这个顶点文件和这个边文件,我得到了一个错误的答案,答案如下->距离和父项:0/0到0/1=700距离:10,但是如果我排除边0/0到0/1,它会返回其他短值st路径0/0到1/0=10 1/0到1/1=10 1/1到0/1=10距离:30我认为如果我将边0/0到0/1设置为大于30的weith,这是正确的最短路径。你能帮我吗?你有一个无向图,一个边从0/0到0/1,另一个边从0/1到0/0。这似乎是你的问题,我认为使用a会像你期望的那样工作(或者可能)。是的,它也能工作。非常感谢:)“它只在新的_值小于旧的_值时工作”。您只在最短路径中打印边,因此(只要有其他路径)是有意义的权重较大的边不会出现在您的列表中。/谢谢您的回答,我用一个简单的图形进行了测试,它是有效的,但是使用这个顶点文件和这个边文件,我得到了一个错误的答案,答案如下->距离和父项:0/0到0/1=700距离:10,但是如果我排除边0/0到0/1,它会返回其他短值st路径0/0到1/0=10 1/0到1/1=10 1/1到0/1=10距离:30我认为如果我将边0/0到0/1设置为大于30的weith,这是正确的最短路径。你能帮我吗?你有一个无向图,一个边从0/0到0/1,另一个边从0/1到0/0。这似乎是你的问题,我认为使用a会像你期望的那样工作(或者可能)。是的,它也很有效。非常感谢:)