我主要使用自己的网络流算法。但是,我刚刚开始使用boost,但我正在努力定义图形。更具体地说,我自己代码中的顶点编号为0到n-1。边缘编号为0到m-1。我正在尝试建立一个有4个边缘的非常简单的网络。
所有四条边的容量为4个单位。我正在寻找提升以找到从s = 0到t = 3的最大流量。(答案是8。)
为了让它运行,我有以下代码,但它编译和构建没有错误,代码没有做我期望的。有关我的具体问题,请参阅代码中的注释。有两个问题(Q1)和(Q2)。
v[vd] = i;
关于Q1)我查找了On C++ Boost Graph Creation and the vertex_index Property.提供的解决方案。但是,我不清楚为什么e[edf] = 0;
导致编译时错误,但稍后add_edge()
不会导致编译时错误。
关于Q2)我真正想要的是一种访问顶点以传递给vertex[2]
函数的方法。更一般地说,有没有办法通过某种机制(如edge[3]
)访问第二条边(从0开始计数),或通过{{1}}等某种机制访问第三条边(从0开始计数) ?
答案 0 :(得分:1)
i); //(Q1)Here, v[vd] = i; produces an error. Is there any other way to assign integer indices to vertices?
使用vecS
时,顶点索引是隐式,并且是顶点向量的整数索引。因此,如果不对顶点进行物理混洗,则无法进行分配。
但是,通过选择非随机访问顶点容器,例如,您可以自由地进行内置隐式顶点索引。 listS
。
HOWEVER :如果你这样做,你可以(显然)不再使用ID作为顶点描述符,使所有代码都像
edf = (add_edge(*(vertexIt + 0), *(vertexIt + 1), g)).first;
edb = (add_edge(*(vertexIt + 1), *(vertexIt + 0), g)).first;
非常笨拙,更像是
auto find_vertex_by_id = [&g](size_t id) {
for(auto vd : boost::make_iterator_range(boost::vertices(g)))
if (id == g[vd])
return vd;
throw std::range_error("vertex id " + std::to_string(id));
};
edf = (add_edge(find_vertex_by_id(g[*vertexIt].id + 0), find_vertex_by_id(g[*vertexIt].id + 1), g)).first;
edb = (add_edge(find_vertex_by_id(g[*vertexIt].id + 1), find_vertex_by_id(g[*vertexIt].id + 0), u)).first;
请参阅 Live On Coliru ,我希望您同意这一点加重。
注意它也会崩溃。别担心,这是一个小问题(主要是因为反向边缘地图没有正确构建)。快速修复,让您难以捉摸
Flow: 8
!的 Sneak Preview 强>
我建议走另一条路!包含隐式顶点索引,它与描述符类型一致。然后,不要使用迭代器的迂回方式,只需要解决:
<强> Live On Coliru 强>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/boykov_kolmogorov_max_flow.hpp>
#include <boost/graph/edmonds_karp_max_flow.hpp>
#include <boost/graph/push_relabel_max_flow.hpp>
#include <iostream>
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;
property_map<Graph, edge_index_t>::type e = get(edge_index, g);
property_map<Graph, edge_capacity_t>::type cap = get(edge_capacity, g);
//property_map<Graph, edge_weight_t>::type cost = get(edge_weight, g);
//property_map<Graph, edge_residual_capacity_t>::type rescap = get(edge_residual_capacity, g);
property_map<Graph, edge_reverse_t>::type rev = get(edge_reverse, g);
int nonodes = 4;
for (int i = 0; i < nonodes; i++) {
Traits::vertex_descriptor vd;
vd = add_vertex(g);
assert(vd == i);
}
// Create edges
Traits::edge_descriptor edf, edb; // Max flow algorithms seem to want both forward and backward edges. edf is for
// forward, edb is for backward
// Q2. All of the add_edge() functions below do not seem to add any edges to the graph, leading to a run time error
// when boykov_kolmogorov_max_flow() is finally called.
edf = (add_edge(0, 1, g)).first;
edb = (add_edge(1, 0, g)).first;
e[edf] = 0;
e[edb] = 1;
cap[edf] = 4;
cap[edb] = 4;
rev[edf] = edb;
rev[edb] = edf;
edf = (add_edge(0, 2, g)).first;
edb = (add_edge(2, 0, g)).first;
e[edf] = 2;
e[edb] = 3;
cap[edf] = 4;
cap[edb] = 4;
rev[edf] = edb;
rev[edb] = edf;
edf = (add_edge(1, 3, g)).first;
edb = (add_edge(3, 1, g)).first;
e[edf] = 4;
e[edb] = 5;
cap[edf] = 4;
cap[edb] = 4;
rev[edf] = edb;
rev[edb] = edf;
edf = (add_edge(2, 3, g)).first;
edb = (add_edge(3, 2, g)).first;
e[edf] = 6;
e[edb] = 7;
cap[edf] = 4;
cap[edb] = 4;
rev[edf] = edb;
rev[edb] = edf;
double flow = boykov_kolmogorov_max_flow(g, 0, 3);
std::cout << "Flow: " << flow << "\n";
}
打印
Flow: 8
不是更好吗?现在,让我们继续扼杀任何仍然丑陋/烦人的事情
add_vertex
循环现在似乎无能为力:
for (int i = 0; i < nonodes; i++) {
Traits::vertex_descriptor vd;
vd = add_vertex(g);
assert(vd == i);
}
确实,让我们写一下:
Graph g(nonodes);
以使得必须将属性映射传递给算法为代价,您可以使用Bundled Properties使构建图表更加可口:
<强> Live On Coliru 强>
等等 - 什么?这不是改进,是吗?好吧,等到你看到这个:struct { int from,to; } edges[] = { { 0, 1 }, { 0, 2 }, { 1, 3 }, { 2, 3 }, };
int edge_id = 0;
for (auto& edge : edges) {
auto edf = add_edge(edge.from, edge.to, EdgeProperty{edge_id++, 4}, g).first,
edb = add_edge(edge.to, edge.from, EdgeProperty{edge_id++, 4}, g).first;
rev[edf] = edb;
rev[edb] = edf;
}
<强> Live On Coliru 强>
耶!
那些特定于流算法的属性并不真正属于图表,为什么要将它们包含在内?让我们做一些花哨的步法并使用外部地图。这已经开始变得先进,但真正推动了房产地图的重点:它们就像 C ++镜头。
未经评论发表:
<强> Live On Coliru 强>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <boost/graph/boykov_kolmogorov_max_flow.hpp>
#include <functional>
#include <iostream>
using namespace boost;
struct VertexProperty { std::string name; };
struct EdgeProperty {
int id;
double capacity, residual_capacity;
EdgeProperty(int id, double cap, double res = 0)
: id(id), capacity(cap), residual_capacity(res)
{ }
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperty, EdgeProperty> Graph;
int main() {
int nonodes = 4;
Graph g(nonodes);
// reverse edge map
auto e = get(&EdgeProperty::id, g);
auto rev = make_vector_property_map<Graph::edge_descriptor>(e);
// Create edges
struct { int from,to; } edges[] = { { 0, 1 }, { 0, 2 }, { 1, 3 }, { 2, 3 }, };
int edge_id = 0;
for (auto& pair : edges) {
auto a = add_edge(pair.from, pair.to, EdgeProperty { edge_id++, 4 }, g).first;
auto b = add_edge(pair.to, pair.from, EdgeProperty { edge_id++, 4 }, g).first;
rev[a] = b;
rev[b] = a;
}
// property maps
struct VertexEx {
default_color_type color;
double distance;
Graph::edge_descriptor pred;
};
auto idx = get(vertex_index, g);
auto vex = make_vector_property_map<VertexEx>(idx);
auto pred = make_transform_value_property_map(std::mem_fn(&VertexEx::pred), vex);
auto color = make_transform_value_property_map(std::mem_fn(&VertexEx::color), vex);
auto dist = make_transform_value_property_map(std::mem_fn(&VertexEx::distance), vex);
auto cap = get(&EdgeProperty::capacity, g);
auto rescap = get(&EdgeProperty::residual_capacity, g);
// algorithm
double flow = boykov_kolmogorov_max_flow(g, cap, rescap, rev, pred, color, dist, idx, 0, 3);
std::cout << "Flow: " << flow << "\n";
}
Live On Coliru ,评论更少。