C++ 复制图形并在boost c+中对复制的图形运行最大流量后,图形的剩余容量将发生变化+;!

C++ 复制图形并在boost c+中对复制的图形运行最大流量后,图形的剩余容量将发生变化+;!,c++,boost,max-flow,C++,Boost,Max Flow,我通过以下方式在boost中创建一个图形: typedef adjacency_list_traits<vecS, vecS, directedS> Traits; typedef adjacency_list<setS, vecS, directedS, property<vertex_name_t, std::string>, property<edge_capacity_t, double, property<edge_r

我通过以下方式在boost中创建一个图形:

typedef adjacency_list_traits<vecS, vecS, directedS> Traits;
typedef adjacency_list<setS, vecS, directedS, 
    property<vertex_name_t, std::string>,
    property<edge_capacity_t, double,
    property<edge_residual_capacity_t, double,
    property<edge_reverse_t, Traits::edge_descriptor> > >
    > Graph;
Graph g; 
Traits::vertex_descriptor s, t;  
在这之后,我创建了这个图的一个副本,g_copy.H:

class CG{
  public:
  CG( Graph G, Traits::vertex_descriptor s, Traits::vertex_descriptor t)
    : H(G), src(s), sink(t) {

  Graph H;
  Traits::vertex_descriptor src;
  Traits::vertex_descriptor sink;
  property_map<Graph, boost::edge_capacity_t>::type capacity ;
  property_map<Graph, boost::edge_reverse_t>::type rev;
  property_map<Graph, boost::edge_residual_capacity_t>::type residual_capacity;
  }
}
new_flow = boost::edmonds_karp_max_flow(g_copy.H, s', t');
如果我查看原始图形g的剩余容量,它们在新流=升压::edmonds_karp_max_flow(g_copy.H,s',t')一行之后发生了变化; 以前有人有过同样的问题吗?我在这里做错了什么,导致原始图形的剩余容量发生意外变化吗?感谢任何帮助或反馈

这就是我复制图表的方法:

CG( Graph G, Traits::vertex_descriptor s, Traits::vertex_descriptor t ){

        //add the vertices to h
        std::vector<vertex_descriptor> verts;
        for(long vi = 0;  vi < num_vertices(G); ++vi)
            verts.push_back(add_vertex(H));


        // get the property map for vertex indices
        typedef property_map<Graph, vertex_index_t>::type IndexMap;
        IndexMap index = get(vertex_index, G);

        this->rev = get(edge_reverse, H);
        //get the edges
        boost::graph_traits<Graph>::vertex_iterator u_iter, u_end;
        boost::graph_traits<Graph>::out_edge_iterator ei, e_end;
        for (boost::tie(u_iter, u_end) = vertices(G); u_iter != u_end; ++u_iter)
            for (boost::tie(ei, e_end) = out_edges(*u_iter, G); ei != e_end; ++ei){

                edge_descriptor e1,e2;
                bool in1, in2;
                int idx_s = index[source(*ei,G)];
                int idx_t = index[target(*ei, G)];
                boost::tie(e1, in1) = add_edge(idx_s, idx_t, H);    
                boost::tie(e2, in2) = add_edge(idx_t, idx_s, H);
                if(in1)
                    this->rev[e1] = e2; 
                if(in2)
                    this->rev[e2] = e1;
            }

        //get the properties of G
        property_map<Graph, edge_capacity_t>::type 
            cap = get(edge_capacity, G);
        property_map<Graph, edge_reverse_t>::type 
            reverse = get(edge_reverse, G);
        property_map<Graph, edge_residual_capacity_t>::type 
            residual_cap = get(edge_residual_capacity, G);

        //initiate the properties of H
        this->capacity = get(edge_capacity, H);
        this->residual_capacity = get(edge_residual_capacity, H);

        boost::graph_traits<Graph>::vertex_iterator u_iter1, u_end1, u_iter_2, u_end_2;
        boost::graph_traits<Graph>::out_edge_iterator ei1, e_end1, ei_2, e_end_2;
        for (boost::tie(u_iter1, u_end1) = vertices(G), boost::tie(u_iter_2, u_end_2) = vertices(H) ; 
                u_iter1 != u_end1 && u_iter_2 != u_end_2; ++u_iter1, ++u_iter_2)
            for (boost::tie(ei1, e_end1) = out_edges(*u_iter1, G), boost::tie(ei_2, e_end_2) = out_edges(*u_iter_2, H); ei1 != e_end1 && ei_2 != e_end_2;
                    ++ei1, ++ei_2){

                double temp_res = residual_cap[*ei1];
                this->residual_capacity[*ei_2] = temp_res;
                double temp_cap = cap[*ei1];
                this->capacity[*ei_2] = temp_cap;

            }
    };
CG(图G,特征::顶点描述符s,特征::顶点描述符t){
//将顶点添加到h
向量顶点;
对于(长vi=0;virev=get(反向边缘,H);
//获得优势
boost::graph_traits::顶点迭代器u_iter,u_end;
boost::graph\u traits::out\u edge\u迭代器ei,e\u end;
对于(boost::tie(u_iter,u_end)=顶点(G);u_iter!=u_end;+u_iter)
对于(boost::tie(ei,e_end)=外边缘(*u_iter,G);ei!=e_end;++ei){
边描述符e1,e2;
bool-in1,in2;
int idx_s=索引[来源(*ei,G)];
int idx_t=指数[目标(*ei,G)];
boost::tie(e1,in1)=add_edge(idx_s,idx_t,H);
boost::tie(e2,in2)=add_edge(idx_t,idx_s,H);
如果(in1)
本->版本[e1]=e2;
如果(in2)
本->版次[e2]=e1;
}
//得到G的性质
属性映射::类型
cap=get(边缘容量,G);
属性映射::类型
反向=获取(反向边缘,G);
属性映射::类型
剩余容量=get(边缘剩余容量,G);
//初始化H的性质
此->容量=获取(边缘容量,H);
此->剩余容量=获取(边缘剩余容量,H);
boost::graph_traits::顶点迭代器u_iter1,u_end1,u_iter_2,u_end_2;
boost::graph_traits::out_edge_迭代器ei1、e_end1、ei_2、e_end_2;
对于(boost::tie(u_iter1,u_end1)=顶点(G),boost::tie(u_iter_2,u_end_2)=顶点(H);
u_iter1!=u_end1和&u_iter_2!=u_end2;++u_iter1,++u_iter_2)
对于(boost::tie(ei1,e_end1)=out_边(*u_iter1,G),boost::tie(ei_2,e_end_2)=out_边(*u iter_2,H);ei1!=e_end1和&ei_2!=e_end_2;
++ei1,++EIU 2){
双温度=剩余上限[*ei1];
此->剩余容量[*ei\U 2]=临时存储;
双温度上限=上限[*ei1];
此->容量[*ei_2]=温度上限;
}
};

能否将代码扩展为SSCCE?很明显,您正在共享属性映射,但没有显示该代码。假设顶点描述符可以与图的副本一起使用是。。。危险(虽然在这种情况下它应该像你预期的那样工作)。我设法找到了原因!在CG类的构造函数中将G分配给H是一个问题,这是一个浅拷贝,我与H共享了G的一些对象(在本例中,它是图G的反向边)。我设法手动复制了这个图表,现在它开始工作了。您还可以通过boost查看以下示例,了解Edmonds Karp max flow算法以及剩余容量和反向边的概念。TBH我不认为复制构造或分配所示的
图形
类型可能会导致共享数据。如果更容易分享,你现在用什么“魔药”来代替图形复制/分配?我刚刚用我的解决方案更新了它。你能将代码扩展为SSCE吗?很明显,您正在共享属性映射,但没有显示该代码。假设顶点描述符可以与图的副本一起使用是。。。危险(虽然在这种情况下它应该像你预期的那样工作)。我设法找到了原因!在CG类的构造函数中将G分配给H是一个问题,这是一个浅拷贝,我与H共享了G的一些对象(在本例中,它是图G的反向边)。我设法手动复制了这个图表,现在它开始工作了。您还可以通过boost查看以下示例,了解Edmonds Karp max flow算法以及剩余容量和反向边的概念。TBH我不认为复制构造或分配所示的
图形
类型可能会导致共享数据。如果更容易分享,你现在用什么“魔药”来代替图形复制/分配?我刚刚用我的解决方案更新了它
new_flow = boost::edmonds_karp_max_flow(g_copy.H, s', t');
CG( Graph G, Traits::vertex_descriptor s, Traits::vertex_descriptor t ){

        //add the vertices to h
        std::vector<vertex_descriptor> verts;
        for(long vi = 0;  vi < num_vertices(G); ++vi)
            verts.push_back(add_vertex(H));


        // get the property map for vertex indices
        typedef property_map<Graph, vertex_index_t>::type IndexMap;
        IndexMap index = get(vertex_index, G);

        this->rev = get(edge_reverse, H);
        //get the edges
        boost::graph_traits<Graph>::vertex_iterator u_iter, u_end;
        boost::graph_traits<Graph>::out_edge_iterator ei, e_end;
        for (boost::tie(u_iter, u_end) = vertices(G); u_iter != u_end; ++u_iter)
            for (boost::tie(ei, e_end) = out_edges(*u_iter, G); ei != e_end; ++ei){

                edge_descriptor e1,e2;
                bool in1, in2;
                int idx_s = index[source(*ei,G)];
                int idx_t = index[target(*ei, G)];
                boost::tie(e1, in1) = add_edge(idx_s, idx_t, H);    
                boost::tie(e2, in2) = add_edge(idx_t, idx_s, H);
                if(in1)
                    this->rev[e1] = e2; 
                if(in2)
                    this->rev[e2] = e1;
            }

        //get the properties of G
        property_map<Graph, edge_capacity_t>::type 
            cap = get(edge_capacity, G);
        property_map<Graph, edge_reverse_t>::type 
            reverse = get(edge_reverse, G);
        property_map<Graph, edge_residual_capacity_t>::type 
            residual_cap = get(edge_residual_capacity, G);

        //initiate the properties of H
        this->capacity = get(edge_capacity, H);
        this->residual_capacity = get(edge_residual_capacity, H);

        boost::graph_traits<Graph>::vertex_iterator u_iter1, u_end1, u_iter_2, u_end_2;
        boost::graph_traits<Graph>::out_edge_iterator ei1, e_end1, ei_2, e_end_2;
        for (boost::tie(u_iter1, u_end1) = vertices(G), boost::tie(u_iter_2, u_end_2) = vertices(H) ; 
                u_iter1 != u_end1 && u_iter_2 != u_end_2; ++u_iter1, ++u_iter_2)
            for (boost::tie(ei1, e_end1) = out_edges(*u_iter1, G), boost::tie(ei_2, e_end_2) = out_edges(*u_iter_2, H); ei1 != e_end1 && ei_2 != e_end_2;
                    ++ei1, ++ei_2){

                double temp_res = residual_cap[*ei1];
                this->residual_capacity[*ei_2] = temp_res;
                double temp_cap = cap[*ei1];
                this->capacity[*ei_2] = temp_cap;

            }
    };