I mostly work with my own network flow algorithms. However, I have just begun using boost recently, but am struggling to define graphs. More specifically, vertices in my own code are numbered 0 through n-1. Edges are numbered 0 through m-1. I am trying to build a very simple network with 4 edges.
The capacity of all four edges is 4 units. I am looking for boost to find me a maximum flow from s = 0 to t = 3. (The answer is 8.)
To get this to run, I have the following code, but although it compiles and builds without errors, the code is not doing what I expect. Please see comments in the code for my specific questions. There are two questions (Q1) and (Q2).
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/boykov_kolmogorov_max_flow.hpp>
#include <boost/graph/push_relabel_max_flow.hpp>
#include <boost/graph/edmonds_karp_max_flow.hpp>
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, vertex_index_t>::type v = get(vertex_index, 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(v[vd] == i);//(Q1)Here, v[vd] = i; produces an error. Is there any other way to assign integer indices to vertices?
}
Graph::vertex_iterator vertexIt, vertexEnd;
tie(vertexIt, vertexEnd) = vertices(g);
//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(*(vertexIt+0), *(vertexIt + 1), g)).first;
edb = (add_edge(*(vertexIt + 1), *(vertexIt + 0), g)).first;
e[edf] = 0;
e[edb] = 1;
cap[edf] = 4;
cap[edb] = 4;
edf = (add_edge(*(vertexIt + 0), *(vertexIt + 2), g)).first;
edb = (add_edge(*(vertexIt + 2), *(vertexIt + 0), g)).first;
e[edf] = 2;
e[edb] = 3;
cap[edf] = 4;
cap[edb] = 4;
edf = (add_edge(*(vertexIt + 1), *(vertexIt + 3), g)).first;
edb = (add_edge(*(vertexIt + 3), *(vertexIt + 1), g)).first;
e[edf] = 4;
e[edb] = 5;
cap[edf] = 4;
cap[edb] = 4;
edf = (add_edge(*(vertexIt + 2), *(vertexIt + 3), g)).first;
edb = (add_edge(*(vertexIt + 3), *(vertexIt + 2), g)).first;
e[edf] = 6;
e[edb] = 7;
cap[edf] = 4;
cap[edb] = 4;
double flow = boykov_kolmogorov_max_flow(g, *(vertexIt + 0), *(vertexIt + 3));
return 0;
}
Regarding Q1) I looked up the solution provided On C++ Boost Graph Creation and the vertex_index Property.. However, it is unclear to me why v[vd] = i;
causes a compile time error, but e[edf] = 0;
later on does not cause a compile time error.
Regarding Q2) What I am really looking for is a way to access vertices to pass to the add_edge()
function. More generally, is there a way to access the 2nd edge (counting from 0) via some mechanism such as vertex[2]
or to access the 3rd edge (counting from 0) via some mechanism such as edge[3]
, etc.?