Boost graph library - adjacent_vertices function not found - c++

I am trying to write an algorithm to (greedily) find the chromatic number of a graph. For this I need to be able to query the adjacent vertices of a given vertex.
My function is the following:
int Network::greedy_colouring() {
// create an undirected graph with the vertices and edges of the first one
UndirectedGraph g;
copy_graph(network, g);
int vertices_amount = num_vertices(g);
// Assign the first color to first vertex
std::map<std::string, int> vertex_colouring;
vertex_pair_iterators vp = vertices(g);
vertex_colouring[g[*vp.first].name] = 0;
++vp.first; // start from second vertex
for (; vp.first != vp.second; ++vp.first)
vertex_colouring[g[*vp.first].name] = -1;
// A temporary array to store the available colors. True
// value of available[cr] would mean that the color cr is
// assigned to one of its adjacent vertices
bool available[vertices_amount];
for (int cr = 0; cr < vertices_amount; cr++)
available[cr] = false;
// Assign colors to remaining V-1 vertices
vp = vertices(g); // reset to beginning
++vp.first; // start from second vertex
for (; vp.first != vp.second; ++vp.first) {
// Process all adjacent vertices and flag their colors
// as unavailable
for (std::pair<adjacency_it, adjacency_it> neighbours = boost::adjacent_vertices(g[*vp.first], g);
neighbours.first != neighbours.second; ++neighbours.first)
if (vertex_colouring[g[*neighbours.first].name] != -1)
available[vertex_colouring[g[*neighbours.first].name]] = true;
// Find the first available color
int cr;
for (cr = 0; cr < vertices_amount; cr++)
if (available[cr] == false)
break;
vertex_colouring[g[*vp.first].name] = cr; // Assign the found color
// Reset the values back to false for the next iteration
neighbours = boost::adjacent_vertices(g[*vp.first], g); // reset to beginning
for (; neighbours.first != neighbours.second; ++neighbours.first)
if (vertex_colouring[g[*neighbours.first].name] != -1)
available[vertex_colouring[g[*neighbours.first].name]] = false;
}
// print the result and find colour number
unsigned colour_number = 0;
for (std::map<std::string, int>::iterator it = vertex_colouring.begin(); it != vertex_colouring.end(); ++it) {
std::cout << "Vertex " << it->first << " ---> Color " << it->second << std::endl;
if (it->second > colour_number)
colour_number = it->second;
}
return colour_number;
}
The error I get is related to the call to:
std::pair<adjacency_it, adjacency_it> neighbours = boost::adjacent_vertices(g[*vp.first],g)
Which gives the following compile error: "error: no matching function for call to ‘boost::adjacency_iterator ... " (partial copy).
Commenting out the code related to the function adjacency lets it compile, so I am sure that this is the problem code.
Some typedefs that are being used in the function:
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, Vertex, Edge > Graph;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Vertex, Edge > UndirectedGraph;
typedef std::pair<Vertex ,Vertex > vert_p;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
typedef boost::graph_traits<Graph>::in_edge_iterator in_edge_it;
typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter;
typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
typedef std::pair<vertex_iter, vertex_iter> vertex_pair_iterators;
typedef std::pair<in_edge_it, in_edge_it> edge_pair_iterators;
typedef boost::graph_traits<Graph>::adjacency_iterator adjacency_it;
Can anyone give me a clue what I am doing wrong?

Two issues:
the first argument needs to be a vertex descriptor, not the property bundle. Change
boost::adjacent_vertices(g[*vp.first], g)
into
boost::adjacent_vertices(*vp.first, g)
the return type is std::pair<adjacency_iterator, adjacency_iterator>. However, you defined adjacency_iterator as
typedef boost::graph_traits<Graph>::adjacency_iterator adjacency_it;
when it needs to be
typedef boost::graph_traits<UndirectedGraph>::adjacency_iterator adjacency_it;
Further notes:
It's easier to work with separate iterators instead of vp.first and vp.second (use boost::tie to assign both at once)
You have a "poisonous" unsigned value in your comparison, write it explicitly as
if(it->second > static_cast<int>(colour_number))
Or review the logic with possible -1 values in the map.
it's likely very inefficient to keep the colour map indexed by Vertex::name (which is a string). You should consider indexing by vertex_descriptor.
Now, since your vertex model uses vecS for the VertexContainer, you could actually use the fact that this descriptor is an integral index in the range [0, num_vertices(g)).
Therefore you can replace the map<> (which has bad memory locality) with a vector<int> (where the vertex descriptor is the vector index).
If you want to support other graph models, you can let the caller pass in an IndexMap that maps vertex-descriptor to similar consecutive indices. Lots of algorithms in the BGL use this approach.
Obviously, bool[] could (should) be std::bitset or even std::vector<bool>. Boost has the dynamic_bitset which is probably best here.
(I'd need to understand your algorithm a lot better. Perhaps a set of "taken" colour would be even better. And implemented as an unsorted contiguous collection for speed, unless you anticipate the number of colour to get big enough that an ordered/hash lookup would be faster (?!).
Always make your code selfcontained:
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <iostream>
struct Vertex {
std::string name;
};
struct Edge {
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, Vertex, Edge > Graph;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Vertex, Edge > UndirectedGraph;
Graph network;
int greedy_colouring() {
using namespace boost;
typedef boost::graph_traits<UndirectedGraph>::vertex_descriptor vertex_descriptor;
static_assert(is_integral<vertex_descriptor>::value, "IndexMap not provided yet TODO");
typedef boost::graph_traits<UndirectedGraph>::vertex_iterator vertex_iter;
typedef boost::graph_traits<UndirectedGraph>::adjacency_iterator adjacency_it;
// create an undirected graph with the vertices and edges of the first one
UndirectedGraph g;
copy_graph(network, g);
vertex_iter vit, vend;
tie(vit, vend) = vertices(g);
size_t const vertices_amount = num_vertices(g);
std::vector<int> vertex_colouring(vertices_amount, -1);
vertex_colouring[*vit] = 0; // Assign the first color to first vertex
// A temporary array to store the available colors.
// - available[cr]: assigned to one of its adjacent vertices
std::vector<bool> available(vertices_amount, false);
for (++vit; vit!=vend; ++vit)
{
// Process all adjacent vertices and flag their colors as unavailable
adjacency_it neighbour, neighbour_end;
for (tie(neighbour, neighbour_end) = adjacent_vertices(*vit, g); neighbour != neighbour_end; ++neighbour)
if (vertex_colouring[*neighbour] != -1)
available[vertex_colouring[*neighbour]] = true;
// Find the first available color
vertex_colouring[*vit] = distance(available.begin(), std::find(available.begin(), available.end(), false));
// Reset the values back to false for the next iteration
for (tie(neighbour, neighbour_end) = adjacent_vertices(*vit, g); neighbour != neighbour_end; ++neighbour)
if (vertex_colouring[*neighbour] != -1)
available[vertex_colouring[*neighbour]] = false;
}
// print the result and find colour number
for (vertex_descriptor v = 0; v < vertices_amount; ++v)
std::cout << "Vertex " << v << " ---> Color " << vertex_colouring[v] << std::endl;
return *std::max_element(vertex_colouring.begin(), vertex_colouring.end());
}
int main() { }

Related

Boost - Find maximum matching edges in a graph

I am a Boost (and C++) newbie, going through the graph library tutorial. I can create a graph and give it vertices and edges. I would like to use maximum cardinality matching in Boost to return a set of edges that form the maximum matching in the graph.
I have looked through max_cardinality_matching.hpp, but am not quite sure how to use it, or which functions to use, to return the maximum matching set of edges.
Here is my code so far:
#include <iostream>
#include <boost/graph/max_cardinality_matching.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
using namespace boost;
int main(int argc, const char * argv[]) {
// create a typedef for the Graph type
typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;
// Make convenient labels for the vertices
enum { A, B, C, D, E, F, N };
const int num_vertices = N;
// const char* name = "ABCDE";
// writing out the edges in the graph
typedef std::pair<int, int> Edge;
Edge edge_array[] =
{ Edge(A,B), Edge(B,C), Edge(C,D), Edge(D,E), Edge(E,F) };
// const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
// declare a graph object and add the edges
Graph g(edge_array, edge_array + sizeof(edge_array) / sizeof(Edge), num_vertices);
// get the property map for vertex indices
// property_map<Graph, property type>
typedef property_map<Graph, vertex_index_t>::type IndexMap;
IndexMap index = get(vertex_index, g);
// Create an iterator for vertices
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
std::cout << "vertices(g) = ";
// Vertices returns a pair of vertex iterators
// The first iter points to the beginning of the vertices
// The second points past the end
std::pair<vertex_iter, vertex_iter> vp;
// vertices() returns the vertices in graph g
for (vp = vertices(g); vp.first != vp.second; ++vp.first)
std::cout << index[*vp.first] << " ";
std::cout << std::endl;
graph_traits<Graph>::edge_iterator ei, ei_end;
std::cout << "edges(g) = ";
// For each tuple of vertices (an edge), till the end of the edge list ...
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
// ... print out the source and target vertices in the edge
std::cout << "(" << index[source(*ei, g)] << "," << index[target(*ei, g)] << ") ";
std::cout << std::endl;
// Return the set of edges that form a maximum matching in graph g
return 0;
}
All you need to do is create read-write property map and pass it as second argument to
template <typename Graph, typename MateMap>
bool checked_edmonds_maximum_cardinality_matching(const Graph& g, MateMap mate);
You can create ordinary std::map where key and value are vertex descriptors and adapts it by associative_property_map in order to use it with checked_edmonds_maximum_cardinality_matching. From this map you can read all edges which create maximum cardinality of your graph.
// Return the set of edges that form a maximum matching in graph g
typedef graph_traits<Graph>::vertex_descriptor VD;
std::map<VD, VD> match;
boost::associative_property_map< std::map<VD,VD> > mapAdapter(match);
bool rc = checked_edmonds_maximum_cardinality_matching(g,mapAdapter);
if (rc)
{
std::set<graph_traits<Graph>::edge_descriptor> edges;
for (auto& i : match)
{
std::pair<Graph::edge_descriptor,bool> e = boost::edge(i.first,i.second,g);
if (e.second)
edges.insert(e.first);
std::cout << i.first << " is matched to " << i.second << std::endl;
}
// print edges
for (auto& e : edges)
std::cout << "edge: " << e << std::endl;
}
As output you can see:
0 is matched to 1
1 is matched to 0
2 is matched to 3
3 is matched to 2
4 is matched to 5
5 is matched to 4
edge: (0,1)
edge: (2,3)
edge: (4,5)

Random order iteration over BGL vertices

Here's some example code to create a graph with bgl and iterate over the vertices. I would like to do this iteration in random order - in other words: the loop should manipulate every vertex, but the order of the vertices should be random for every call of the main function. How can I achieve this?
I experimented unsuccessfully with std::random_shuffle. I think there are different kinds of iterator concepts, but I don't understand the differences yet.
#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
// vertex struct to store some properties in vertices
struct Vertex {
std::string name;
};
int main(int,char*[]) {
// create a typedef for the graph type
typedef adjacency_list<vecS, vecS, undirectedS, Vertex> Graph;
// declare a graph object
Graph g(3);
// prepare iteration
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
std::pair<vertex_iter, vertex_iter> vp;
// add some property data to the vertices
vp = vertices(g);
g[*vp.first].name = "A";
g[*(++vp.first)].name = "B";
g[*(++vp.first)].name = "C";
// iterate over the vertices
for (vp = vertices(g); vp.first != vp.second; ++vp.first)
std::cout << g[*vp.first].name << " ";
std::cout << std::endl;
return 0;
}
Edit: Here's the solution I came up with thanks to the answer of #Jay.
#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <algorithm> // std::random_shuffle
#include <vector> // std::vector
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand
using namespace boost;
// vertex struct to store some properties in vertices
struct Vertex {
std::string name;
};
// random number generator function
int myrandom (int i) {
return std::rand()%i;
}
int main(int,char*[]) {
// create a typedef for the graph type
typedef adjacency_list<vecS, vecS, undirectedS, Vertex> Graph;
// declare a graph object
Graph g(3);
// prepare iteration
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
std::pair<vertex_iter, vertex_iter> vp;
// add some property data to the vertices
vp = vertices(g);
g[*vp.first].name = "A";
g[*(++vp.first)].name = "B";
g[*(++vp.first)].name = "C";
// initialize pseudo random number generator
std::srand(unsigned (std::time(0)));
// create offset vector
std::vector<int> myvector;
for (int i=0; i<3; ++i) {
myvector.push_back(i);
}
// using myrandom to shuffle offset vector
std::random_shuffle(myvector.begin(), myvector.end(), myrandom);
// keep vp.first at the start
vp = vertices(g);
// iterate over the vertices effectively shuffled by the offset
vertex_iter dummy_iter;
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) {
dummy_iter = vp.first + *it;
std::cout << g[*dummy_iter].name << " ";
}
std::cout << std::endl;
return 0;
}
I think the simplest thing to do is set up a random vector of indices, as outlined here. Then you can iterate the shuffled list and use it as an offset for your vertex iterator.
For example
vp = vertices(g); // Keep vp.first at the start
vertex_iter dummy_iter;
// Looping on a shuffled vector, values should be 0..N-1
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
{
dummy_iter = vp.first + *it;
Vertex* v = *dummy_iter;
...
To create a random number within a given range use the code below.
#include ctime and #include stdlib.h
int getNumberRange(int min, int max)
{
srand(static_cast<unsigned int>(time(0)));
// always call rand(); after srand() on visual vasic;
rand();
static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<int>(rand() * fraction * (max - min + 1) + min);
}
getNumberRange(1, 100); //picks number between 1 and 100
Every time you need a new number modify the range values (1, 100) and call the function again.

Boost.Graph - algo.is_optimal() assertion

I am trying to implement a program that detects arbitrage trading opportunities using a minimum cost flow algorithm. This algorithm is implemented in Boost.Graph in the form of boost::push_relabel_max_flow() followed by a call to boost::cycle_canceling().
The following is the code I have already, leaving out the boost::cycle_canceling-part because my program dies before reaching the function.
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/push_relabel_max_flow.hpp>
#include <boost/graph/cycle_canceling.hpp>
#include <boost/graph/directed_graph.hpp>
#include <boost/config.hpp>
#include <iostream>
#include <string>
typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS> Traits;
struct st_order {
double price;
double amount;
std::string type;
};
struct VertexProps {
unsigned int id;
};
struct EdgeProps {
double capacity;
double residual_capacity;
double weight;
Traits::edge_descriptor reverse;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProps, EdgeProps > DirectedGraph;
int main() {
DirectedGraph g;
// ETH / BTC
std::vector<st_order> trades{
st_order{0.0101,10.0,"SELL"},
st_order{0.01,3.0,"BUY"},
st_order{0.0102,5.0,"SELL"},
st_order{0.2,42.0,"BUY"},
};
std::vector<boost::graph_traits<DirectedGraph>::vertex_descriptor> vertices;
for(unsigned int vertex_index = 0; vertex_index < trades.size(); vertex_index++)
{
vertices.push_back(boost::add_vertex({vertex_index}, g));
}
std::map<DirectedGraph::vertex_descriptor, std::map<DirectedGraph::vertex_descriptor, Traits::edge_descriptor>> edges;
int ifirst = 0;
for(auto& first : vertices)
{
int isecond = 0;
for(auto& second : vertices)
{
if(first == second || trades[ifirst].type.compare(trades[isecond].type) == 0)
{
isecond++;
continue;
}
double amount = trades[isecond].amount;
if(trades[isecond].type.compare("SELL") == 0)
amount = amount * trades[isecond].price;
edges[first][second] = boost::add_edge(first, second, {amount, amount, (trades[isecond].price / trades[ifirst].price)} , g).first;
std::cout << "Add Edge: From " << first << " to " << second << std::endl;
isecond++;
}
ifirst++;
}
for(auto& i : vertices)
{
for(auto& j : vertices)
{
if(i == j)
continue;
if(edges.find(i) != edges.end() && edges[i].find(j) != edges[i].end())
{
if(edges.find(j) == edges.end() || edges[j].find(i) == edges[j].end())
{
throw std::runtime_error("No return edge found: "+std::to_string(i)+" "+std::to_string(j));
}
auto edge = boost::edge(i,j,g).first;
g[edge].reverse = edges[i][j];
}
}
}
double flow = boost::push_relabel_max_flow(g, vertices[0], vertices[1],
boost::get(&EdgeProps::capacity, g),
boost::get(&EdgeProps::residual_capacity, g),
boost::get(&EdgeProps::reverse, g),
boost::get(boost::vertex_index, g)
);
// Now boost::cycle_canceling() would follow
std::cout << "END << std::endl;
return 0;
};
The "normal" output of my program is:
Add Edge: From 0 to 1
Add Edge: From 0 to 3
Add Edge: From 1 to 0
Add Edge: From 1 to 2
Add Edge: From 2 to 1
Add Edge: From 2 to 3
Add Edge: From 3 to 0
Add Edge: From 3 to 2
As flowchart:
My program asserts in the push_relabel_max_flow-function. The following is the complete error code (which is printed at runtime):
/usr/local/include/boost/graph/push_relabel_max_flow.hpp:707: typename
boost::property_traits<IndexMap>::value_type
boost::push_relabel_max_flow(Graph&, typename
boost::graph_traits<Graph>::vertex_descriptor, typename
boost::graph_traits<Graph>::vertex_descriptor, CapacityEdgeMap,
ResidualCapacityEdgeMap, ReverseEdgeMap, VertexIndexMap) [with Graph =
boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
VertexProps, EdgeProps>; CapacityEdgeMap =
boost::adj_list_edge_property_map<boost::directed_tag, double, double&, long
unsigned int, EdgeProps, double EdgeProps::*>; ResidualCapacityEdgeMap =
boost::adj_list_edge_property_map<boost::directed_tag, double, double&, long
unsigned int, EdgeProps, double EdgeProps::*>; ReverseEdgeMap =
boost::adj_list_edge_property_map<boost::directed_tag,
boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>,
boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>&, long
unsigned int, EdgeProps, boost::detail::edge_desc_impl<boost::directed_tag,
long unsigned int> EdgeProps::*>; VertexIndexMap =
boost::vec_adj_list_vertex_id_map<VertexProps, long unsigned int>; typename
boost::property_traits<IndexMap>::value_type = double; typename
boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]: Assertion
`algo.is_optimal()' failed.
At the very end of the message you can see Assertion: algo.is_optimal() failed. I have absolutly no idea what this means.
In the source file (boost/graph/push_relabel_max_flow.hpp) it is defined as:
bool is_optimal() {
// check if mincut is saturated...
global_distance_update();
return get(distance, src) >= n;
}
I have googled it and didn't find anything. Did I pass my parameters the wrong way? Is it because I use double as capacity (although, if I recall correctly, the "documentation" described it as possible to use a double for the capacity)?
Also, I have discovered this sentence in the documentation:
The CapacityEdgeMap argument cap must map each edge in E to a positive
number, and each edge in E^T to 0.
What does the bold part mean? Does it mean that I have to set the capacity of all outgoing edges from the sink-vertex to 0?
You need to set the reverse edges' capacities to 0.
So you need:
auto edge = boost::edge(i,j,g).first;
g[edge].reverse = edges[i][j];
g[edges[i][j]].capacity = 0;
I'm not sure why this is though. Looking into read_dimacs.hpp I noticed that they create their reverse edges and give them 0 capacity. About 3/4 of the way down the page:
capacity[e1] = cap;
capacity[e2] = 0;
reverse_edge[e1] = e2;
reverse_edge[e2] = e1;
Likely without this constraint, the algorithm will try to treat these as normal edges. The portion of the documentation that you quote describes this, but it's not completely obvious.
There are several special requirements on the input graph and property
map parameters for this algorithm. First, the directed graph G=(V,E)
that represents the network must be augmented to include the reverse
edge for every edge in E. That is, the input graph should be Gin =
(V,{E U E^T}). The ReverseEdgeMap argument rev must map each edge in
the original graph to its reverse edge, that is (u,v) -> (v,u) for all
(u,v) in E. The CapacityEdgeMap argument cap must map each edge in E
to a positive number, and each edge in E^T to 0.
I think here E^T means transpose not target. You have to know that the transpose of a directed adjacency matrix is actually the reverse of the all the edges. That is why they say that the input graph is G = {V, E U E^T}. The edges plus the reverse ones that need to be added.
Side note: changing long to double in the push-relable example worked perfectly fine.

Boost Graph Getting Adjacent Vertices

I am working with a graphing using Boost library.
The graph is defined as follows.
typedef boost::adjacency_list<boost::setS,boost::setS,boost::undirectedS, uint32_t, float> AdjacencyList;
After creating the graph using appropriate data, in a separate function I want to print the adjacent_vertices of each vertex with their appropriate edge weight as computed in the beginning.
The creating part works well but when I want to extract adjacent vertices I dont get the values.
typedef boost::graph_traits<AdjacencyList>::adjacency_iterator AdjacencyIterator;
AdjacencyList::vertex_iterator i, end;
for (boost::tie(i, end) = boost::vertices(adjacency_list); i != end; i++) {
AdjacencyIterator ai, a_end;
boost::tie(ai, a_end) = boost::adjacent_vertices( *i, adjacency_list);
for (; ai != a_end; ai++) {
std::cout << *ai << "\t";
}
}
The Output I get are memory address in Hexademial number.
How can I get vertex indices and the edge weight?
You should access the property bundles, either using the graph's operator[] with the vertex/edge descriptor, or using the property map:
Using the operator[]
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
typedef boost::adjacency_list<boost::setS,boost::setS,boost::undirectedS, uint32_t, float> AdjacencyList;
typedef boost::graph_traits<AdjacencyList>::adjacency_iterator AdjacencyIterator;
int main() {
AdjacencyList adjacency_list;;
boost::add_edge(
boost::add_vertex(10, adjacency_list),
boost::add_vertex(20, adjacency_list),
1.5f,
adjacency_list
);
boost::add_edge(
boost::add_vertex(30, adjacency_list),
boost::add_vertex(40, adjacency_list),
2.5f,
adjacency_list
);
AdjacencyList::vertex_iterator i, end;
for (boost::tie(i, end) = boost::vertices(adjacency_list); i != end; i++) {
AdjacencyIterator ai, a_end;
boost::tie(ai, a_end) = boost::adjacent_vertices(*i, adjacency_list);
for (; ai != a_end; ai++) {
std::cout << adjacency_list[*ai] << "\t";
}
}
}
Output:
10 20 30 40
Using the property map:
boost::property_map<AdjacencyList, boost::vertex_bundle_t>::type pmap = boost::get(boost::vertex_bundle, adjacency_list);
Now you can use boost::get(pmap, vertex_descriptor1) to access the vertex property bundle

BGL: adjacency list returns wrong edge property for descriptor

I have a graph stored as an adjacency list. The vertex property holds an int ID, and the edge property holds a 4x4 matrix and a weight;
In a test case, the graph is a 3-vertex graph with an edge connecting each pair of vertices (a complete graph).
I have a vector of edges descriptors PathType, representing a path, and I am iterating through it and accessing each edge and its property, RelationshipEdge, as follows.
for(PathType::iterator pathIterator = path.begin(); pathIterator != path.end(); ++pathIterator){
edge_t edge = *pathIterator;
RelationshipEdge rEdge = m_graph[edge];
int sourceID = m_graph[boost::source(edge, m_graph)].id;
int destID = m_graph[boost::target(edge, m_graph)].id;
However sometimes when this is performed, the RelationshipEdge returned contains the data for the wrong edge.
As an example, inspecting edge shows an m_source of 1 and m_target of 2. If I inspect the graph and find the edge with source 1 and target 2, the weight is 3 and the matrix is as entered. However rEdge has a weight of 1, and a different matrix. Those values actually correspond with the edge with source 0 and target 1.
Am I accessing the edge properties correctly?
The definition of my graph type is:
typedef boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS, MarkerVertex, RelationshipEdge>
CorrelationAdjacencyList;
I believe your error is coming from somewhere else in the code base.
I put together this simple code to test out edge access and order on a similar graph and everything works as expected.
Culprits could be manually maintained edge_descriptors or vertex_descriptors as sehe mentioned. Or perhaps your path vector initialization or construction.
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
using namespace std;
enum edge_t {A,B};
struct MarkerVertex{
std::string id;
};
struct RelationshipEdge{
std::string id;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
MarkerVertex, RelationshipEdge> CorrelationAdjacencyList;
typedef boost::graph_traits<CorrelationAdjacencyList>::edge_descriptor Edge;
typedef boost::graph_traits<CorrelationAdjacencyList>::vertex_descriptor Vertex;
typedef vector<Edge> PathType;
void printPath(PathType &p, CorrelationAdjacencyList &g){
cout << "print path" << endl;
for(PathType::iterator pi = p.begin(); pi != p.end(); ++pi){
Edge e = *pi;
Vertex s = boost::source(e,g);
Vertex t = boost::target(e,g);
cout << g[s].id << "\t"
<< g[e].id << "\t"
<< g[t].id << endl;
bool isFound;
Edge eForward;
boost::tie(eForward,isFound) = boost::edge(s,t,g);
cout << "forward " << g[eForward].id << "\t" << isFound << endl;
Edge eBackward;
boost::tie(eBackward,isFound) = boost::edge(t,s,g);
cout << "backward " << g[eBackward].id << "\t" << isFound << endl;
}
}
int main(int argc, char* argv[]){
CorrelationAdjacencyList graph;
Vertex a = boost::add_vertex(graph); graph[a].id = "a";
Vertex b = boost::add_vertex(graph); graph[b].id = "b";
Vertex c = boost::add_vertex(graph); graph[c].id = "c";
Edge e1 = boost::add_edge(a,b,graph).first; graph[e1].id = "e1";
Edge e2 = boost::add_edge(b,c,graph).first; graph[e2].id = "e2";
Edge e3 = boost::add_edge(c,a,graph).first; graph[e3].id = "e3";
PathType path1,path2,path3;
path1.push_back(e1);
path1.push_back(e2);
path1.push_back(e3);
path2.push_back(e2);
path2.push_back(e3);
path2.push_back(e1);
path3.push_back(e3);
path3.push_back(e2);
path3.push_back(e1);
printPath(path1,graph);
cout << endl;
printPath(path2,graph);
cout << endl;
printPath(path3,graph);
cin.get();
}
Without further information, I can make the educated guess that you use vecS containers and the iterators/references have become invalidated.
This would happen on insertion/deletion of edges/vertices.
Here's the solution I found, though I'm not convinced I fully understand why the original method does not.
I have replaced the above code with
for(PathType::iterator pathIterator = path.begin(); pathIterator != path.end(); ++pathIterator){
edge_t edge = *pathIterator;
int sourceID = m_graph[boost::source(edge, m_graph)].id;
int destID = m_graph[boost::target(edge, m_graph)].id;
int lowerID, higherID;
if (sourceID < destID){
lowerID = sourceID;
higherID = destID;
} else {
lowerID = destID;
higherID = sourceID;
}
edge_t edge2 = m_edgeDescriptorsByEndpoints.at(make_pair(lowerID, higherID));
RelationshipEdge rEdge = m_graph[edge2];
m_edgeDescriptorsByEndpoints is a map of pairs of vertex IDs to edge descriptors.
So I'm taking the edge descriptor, getting the IDs of the source and target vertices, finding the corresponding edge descriptor from the map and then getting the properties of that edge.
Not exactly a satisfying solution but it works, as far as I've been able to test so far.
EDIT
I substituted the call to my map with a call to boost::edge(u,v,g) as suggested by #sehe, so the code is now as follows:
for(PathType::iterator pathIterator = path.begin(); pathIterator != path.end(); ++pathIterator){
edge_t edge = *pathIterator;
vert_t sourceV = boost::source(edge, m_graph);
vert_t targetV = boost::target(edge, m_graph);
pair<edge_t, bool> edgePair = boost::edge(sourceV, targetV, m_graph);
if (!edgePair.second){
cerr << "Edge does not exist" << endl;
}
RelationshipEdge rEdge = m_graph[edgePair.first];
This code still results in rEdge containing the correct properties, which seems odd, since I'd think that querying the graph for the vertices of an edge, and then querying the graph for the edge that connects those vertices would always give you that exact same edge back.