BOOST图形库的edmonds_karp最大流量算法未返回

时间:2019-03-30 00:03:37

标签: c++ boost graph

我已经编写了一些代码来使用BOOST Graph库计算测试图的最大流量。当图形设置正确时,edmunds_karp_max_flow()函数调用未返回值。它似乎永远持续下去。

我已经尝试调试文件,但是调试器在库的edmonds_karp_max_flow文件中的一行模糊之后停止工作。它只是崩溃了。

代码:

#include "pch.h"
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/read_dimacs.hpp>
#include <boost/graph/edmonds_karp_max_flow.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/config.hpp>
#include <string>

using namespace boost;
using namespace std;
typedef adjacency_list_traits<vecS, vecS, undirectedS> Traits;
typedef adjacency_list<vecS, vecS, undirectedS,
    property<vertex_name_t, string>, 
    property<edge_capacity_t, double, property <edge_residual_capacity_t, double,
    property < edge_reverse_t, Traits::edge_descriptor > > >> myGraphType;

int main() {

    myGraphType g;

    auto v0 = add_vertex(g);
    auto v1 = add_vertex(g);
    auto v2 = add_vertex(g);
    auto v3 = add_vertex(g);
    auto v4 = add_vertex(g);
    auto v5 = add_vertex(g);

    //add some edges
    auto e1 = add_edge(0, 1, g).first;
    auto e2 = add_edge(0, 2, g).first;
    auto e3 = add_edge(0, 3, g).first;
    auto e4 = add_edge(1, 4, g).first;
    auto e5 = add_edge(1, 3, g).first;
    auto e6 = add_edge(2, 4, g).first;
    auto e7 = add_edge(3, 5, g).first;
    auto e8 = add_edge(4, 5, g).first;

    // add reverse edges
    auto e1r = add_edge(1, 0, g).first;
    auto e2r = add_edge(2, 0, g).first;
    auto e3r = add_edge(3, 0, g).first;
    auto e4r = add_edge(4, 1, g).first;
    auto e5r = add_edge(3, 1, g).first;
    auto e6r = add_edge(4, 2, g).first;
    auto e7r = add_edge(5, 3, g).first;
    auto e8r = add_edge(5, 4, g).first;


    //add edge capacities to property map
    property_map<myGraphType, edge_capacity_t>::type capacityMap = get(edge_capacity, g);
    capacityMap[e1] = 2;
    capacityMap[e2] = 4;
    capacityMap[e3] = 3;
    capacityMap[e4] = 1;
    capacityMap[e5] = 5;
    capacityMap[e6] = 7;
    capacityMap[e7] = 6;
    capacityMap[e8] = 8;

    // add reverse edge capacities (all 0) to property map
    capacityMap[e1r] = 0;
    capacityMap[e2r] = 0;
    capacityMap[e3r] = 0;
    capacityMap[e4r] = 0;
    capacityMap[e5r] = 0;
    capacityMap[e6r] = 0;
    capacityMap[e7r] = 0;
    capacityMap[e8r] = 0;

    // fill reverse edge property map
    //first for the forward edges
    property_map<myGraphType, edge_reverse_t>::type revMap = get(edge_reverse, g);
    revMap[e1] = e1r;
    revMap[e2] = e2r;
    revMap[e3] = e3r;
    revMap[e4] = e4r;
    revMap[e5] = e5r;
    revMap[e6] = e6r;
    revMap[e7] = e7r;
    revMap[e8] = e8r;

    //now do the same for the reverse edges
    revMap[e1r] = e1;
    revMap[e2r] = e2;
    revMap[e3r] = e3;
    revMap[e4r] = e4;
    revMap[e5r] = e5;
    revMap[e6r] = e6;
    revMap[e7r] = e7;
    revMap[e8r] = e8;

    //no need to add edge residual capacities as algo will do that



    //some printing
    auto vpair = vertices(g);
    for (auto iter = vpair.first; iter != vpair.second; ++iter) {
        cout << "vertex" << *iter << std::endl;
    }
    auto epair = edges(g);
    cout << "following format is used: Edge(source, sink) : weight / reverse Edge(source sink)" <<endl;
    for (auto iter = epair.first; iter != epair.second; ++iter) {
        cout << "edge" << *iter << ": " << capacityMap[*iter] << " / " << "Edge" << revMap[*iter] << endl;

    }
    Traits::vertex_descriptor s = v0;
    Traits::vertex_descriptor t = v5;
    property_map<myGraphType, edge_residual_capacity_t>::type resCapacityMap = get(edge_residual_capacity, g);
    vector<default_color_type> color(num_vertices(g));
    vector<Traits::edge_descriptor> pred(num_vertices(g));


//BELOW LINE IS THE ONE THAT CAUSES TROUBLE. EVERYTHING ABOVE THIS 
        LINE WORKS (MINIMUM WORKING EXAMPLE)
    auto flow = edmonds_karp_max_flow(g, s, t, capacityMap, resCapacityMap, revMap, &color[0], &pred[0]);
    cout << "Flow : " << flow <<endl;

    return 1;
}

所需结果是最大流量值。拥有BOOST图形库经验的任何人都可以通过检查简单地解决此问题,因为我认为这是一个相对简单的问题。 (我是BOOST新手!)

0 个答案:

没有答案