I found http://www.boost.org/doc/libs/1_49_0/libs/graph/example/incremental_components.cpp and want to check if it will work for me. How to convert this example to cope with cartesian points with (x,y) or (x,y,z). I can't find such example in documentation of boost.
I see that i must redefine vertice in some way, so change in adjacency_list is needed. Tried to change vecS with Point definifion, but i think also some changes in add_edge functions are needed.
I made a couple minor changes to the example you pointed too. Specifically setting the 4th & fifth template parameters on the adjacency_list to be the a type containing any additional vertex and edge properties. See docs here: http://www.boost.org/doc/libs/1_48_0/libs/graph/doc/adjacency_list.html
struct point
{
int x;
int y;
int z;
};
typedef adjacency_list <vecS, vecS, undirectedS, point > Graph;
After nodes & vertices the additional point data can be set like this:
graph[0].x = 42;
And retrieved at the end after the components have been computed:
std::cout << child_index << " " << "x=" << graph[current_index].x << " ";
Full code:
//=======================================================================
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
// Copyright 2009 Trustees of Indiana University.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Michael Hansen
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/incremental_components.hpp>
#include <boost/pending/disjoint_sets.hpp>
/*
This example shows how to use the disjoint set data structure
to compute the connected components of an undirected, changing
graph.
Sample output:
An undirected graph:
0 <--> 1 4
1 <--> 0 4
2 <--> 5
3 <-->
4 <--> 1 0
5 <--> 2
representative[0] = 1
representative[1] = 1
representative[2] = 5
representative[3] = 3
representative[4] = 1
representative[5] = 5
component 0 contains: 4 1 0
component 1 contains: 3
component 2 contains: 5 2
*/
using namespace boost;
struct point
{
point() : x(0), y(0), z(0) {}
int x;
int y;
int z;
};
int main(int argc, char* argv[])
{
typedef adjacency_list <vecS, vecS, undirectedS, point > Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::vertices_size_type VertexIndex;
const int VERTEX_COUNT = 6;
Graph graph(VERTEX_COUNT);
std::vector<VertexIndex> rank(num_vertices(graph));
std::vector<Vertex> parent(num_vertices(graph));
typedef VertexIndex* Rank;
typedef Vertex* Parent;
disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]);
initialize_incremental_components(graph, ds);
incremental_components(graph, ds);
graph_traits<Graph>::edge_descriptor edge;
bool flag;
boost::tie(edge, flag) = add_edge(0, 1, graph);
ds.union_set(0,1);
boost::tie(edge, flag) = add_edge(1, 4, graph);
ds.union_set(1,4);
boost::tie(edge, flag) = add_edge(4, 0, graph);
ds.union_set(4,0);
boost::tie(edge, flag) = add_edge(2, 5, graph);
ds.union_set(2,5);
graph[0].x = 42;
std::cout << "An undirected graph:" << std::endl;
print_graph(graph, get(boost::vertex_index, graph));
std::cout << std::endl;
BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
std::cout << "representative[" << current_vertex << "] = " <<
ds.find_set(current_vertex) << std::endl;
}
std::cout << std::endl;
typedef component_index<VertexIndex> Components;
// NOTE: Because we're using vecS for the graph type, we're
// effectively using identity_property_map for a vertex index map.
// If we were to use listS instead, the index map would need to be
// explicitly passed to the component_index constructor.
Components components(parent.begin(), parent.end());
// Iterate through the component indices
BOOST_FOREACH(VertexIndex current_index, components) {
std::cout << "component " << current_index << " contains: ";
// Iterate through the child vertex indices for [current_index]
BOOST_FOREACH(VertexIndex child_index,
components[current_index])
{
std::cout << child_index
<< " {" << graph[child_index].x
<< "," << graph[child_index].y
<< "," << graph[child_index].z << "} ";
}
std::cout << std::endl;
}
return (0);
}
Related
After generating a graph with n nodes, and adding the edges at random, how would I go around getting all the neighbours of a specific node. Is there a function similar to NetworkX's G.neighbors(i)?
This is what I've got so far, creating adjacency list
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
using namespace boost;
using namespace std;
int main() {
int N = 10000;
struct status_t{
typedef vertex_property_tag kind;
};
typedef
property <status_t, string> status;
typedef
adjacency_list<vecS, vecS, undirectedS, status> MyGraph;
MyGraph g (N);
// add some random edges
add_edge(0, 1, g);
add_edge(100, 153, g);
add_edge(634, 12, g);
add_edge(94, 3, g);
property_map<MyGraph, status_t>::type status_map = get(status_t(), g);
for (int i = 0; i < 10; i++){
status_map[i] = "S";
}
return 0;
}
auto neighbours = boost::adjacent_vertices(94, g);
Print them like e.g.
for (auto vd : make_iterator_range(neighbours))
std::cout << "94 has adjacent vertex " << vd << "\n";
Prints
94 has adjacent vertex 93
94 has adjacent vertex 3
If you wanted outgoing edges only, that assumes directedS or bidirectionalS, in which case you can also do:
for (auto ed : make_iterator_range(boost::out_edges(94, g)))
std::cout << "outgoing: " << ed << "\n";
for (auto ed : make_iterator_range(boost::in_edges(94, g)))
std::cout << "incident: " << ed << "\n";
Live Demo
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <iostream>
using namespace boost;
using namespace std;
int main() {
int N = 10000;
struct status_t { typedef vertex_property_tag kind; };
typedef property<status_t, string> status;
typedef adjacency_list<vecS, vecS, bidirectionalS, status> MyGraph;
MyGraph g(N);
// add some random edges
add_edge(0, 1, g);
add_edge(100, 153, g);
add_edge(634, 12, g);
add_edge(93, 94, g);
add_edge(94, 3, g);
property_map<MyGraph, status_t>::type status_map = get(status_t(), g);
for (int i = 0; i < 10; i++) {
status_map[i] = "S";
}
{
auto neighbours = boost::adjacent_vertices(94, g);
for (auto vd : make_iterator_range(neighbours))
std::cout << "94 has adjacent vertex " << vd << "\n";
// prints
// for undirected:
// 94 has adjacent vertex 93
// 94 has adjacent vertex 3
// for directed/bidirectionalS:
// 94 has adjacent vertex 3
}
{ // for bidirectionalS:
for (auto ed : make_iterator_range(boost::out_edges(94, g)))
std::cout << "outgoing: " << ed << "\n";
for (auto ed : make_iterator_range(boost::in_edges(94, g)))
std::cout << "incident: " << ed << "\n";
}
}
Printing
94 has adjacent vertex 3
outgoing: (94,3)
incident: (93,94)
This is related to a question I had yesterday about accessing vertices using integer indices. That thread is here: Accessing specific vertices in boost::graph
The solution there indicated that using vecS as the type for vertices, it is indeed possible to access specific vertices using the integer index. I was wondering if there is a similar method provided by boost to access arbitrary edges efficiently using integer indices.
Attached is a code that depicts the former (valid access of vertices with integer indices) and accessing the edges based on the developer explicitly maintaining two arrays, from[] and to[], that store the source and the target, respectively of the edges.
The code creates the following graph:
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.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() {
int nonodes = 4;
const int maxnoedges = 4;//I want to avoid using this.
Graph g(nonodes);
property_map<Graph, edge_index_t>::type E = get(edge_index, g);
int from[maxnoedges], to[maxnoedges];//I want to avoid using this.
// Create edges
Traits::edge_descriptor ed;
int eindex = 0;
ed = (add_edge(0, 1, g)).first;
from[eindex] = 0; to[eindex] = 1;//I want to avoid using this.
E[ed] = eindex++;
ed = (add_edge(0, 2, g)).first;
from[eindex] = 0; to[eindex] = 2;//I want to avoid using this.
E[ed] = eindex++;
ed = (add_edge(1, 3, g)).first;
from[eindex] = 1; to[eindex] = 3;//I want to avoid using this.
E[ed] = eindex++;
ed = (add_edge(2, 3, g)).first;
from[eindex] = 2; to[eindex] = 3;//I want to avoid using this.
E[ed] = eindex++;
graph_traits < Graph >::out_edge_iterator ei, e_end;
for (int vindex = 0; vindex < num_vertices(g); vindex++) {
printf("Number of outedges for vertex %d is %d\n", vindex, out_degree(vindex, g));
for (tie(ei, e_end) = out_edges(vindex, g); ei != e_end; ++ei)
printf("From %d to %d\n", source(*ei, g), target(*ei, g));
}
printf("Number of edges is %d\n", num_edges(g));
//Is there any efficient method boost provides
//in lieu of having to explicitly maintain from and to arrays
//on part of the developer?
for (int eindex = 0; eindex < num_edges(g); eindex++)
printf("Edge %d is from %d to %d\n", eindex, from[eindex], to[eindex]);
}
The code builds and compiles without error. The for loop with vindex works fine with out_edges and out_degree working fine taking as parameters integer indices.
Is there a way to do likewise for the next for loop that prints the edges using boost::graph data structures directly?
I looked at the following thread dealing with a similar question:
Boost graph library: Get edge_descriptor or access edge by index of type int
The suggested answer there was to use an unordered_map. Is there any tradeoff in using this as opposed to having the from[] and to[] arrays? Are there any other computationally efficient methods of accessing edges?
You can only do this if you
use a different graph model
an external edge index
Concepts
You could be interested in the AdjacencyMatrix concept. It doesn't exactly sport integral edge ids, but AdjacencyMatrix has lookup of edge by source/target vertices as well.
To get truly integral edge descriptors, you'd probably need write your own graph model class (modeling a set of existing BGL concepts). You might also be interested in grid_graph<> (which has a fixed set of numbered edges per vertex, where the vertices are a grid).
How to access edge_descriptor with given vertex_descriptor in boost::grid_graph - you could devise a "global" numering scheme and thus get linear lookup time
Adjacency List
Here's a modification from the previous answer showing an external index. It's akin to your solution. I chose bimap so at least you get the reverse lookup "automagically".
// Create edges
boost::bimaps::bimap<int, Graph::edge_descriptor> edge_idx;
auto new_edge_pair = [&,edge_id=0](int from, int to) mutable {
auto single = [&](int from, int to) {
auto d = add_edge(from, to, EdgeProperty { edge_id, 4 }, g).first;
if (!edge_idx.insert({edge_id++, d}).second)
throw std::invalid_argument("duplicate key");
return d;
};
auto a = single(from, to), b = single(to, from);
rev[a] = b;
rev[b] = a;
};
new_edge_pair(0, 1);
new_edge_pair(0, 2);
new_edge_pair(1, 3);
new_edge_pair(2, 3);
Now you can do the loop by edge id:
auto& by_id = edge_idx.left;
for (auto const& e : by_id) {
std::cout << "Edge #" << e.first << " is (" << source(e.second, g) << " -> " << target(e.second, g) << ")\n";
}
You can directly lookup an edge by it's id:
auto ed = by_id.at(random);
std::cout << "Random edge #" << random << " is (" << source(ed, g) << " -> " << target(ed, g) << ")\n";
The reverse lookup is a bit redundant, because you can do the same using BGL quite easily:
std::cout << "Reverse lookup: " << by_desc.at(ed) << "\n"; // reverse, though not very spectacular
std::cout << "Classic property lookup: " << g[ed].id << "\n"; // because it can be done using boost easily
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>
#include <boost/bimap.hpp>
#include <random>
std::mt19937 prng { std::random_device{}() };
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 rev = make_vector_property_map<Graph::edge_descriptor>(get(&EdgeProperty::id, g));
// Create edges
boost::bimaps::bimap<int, Graph::edge_descriptor> edge_idx;
auto new_edge_pair = [&,edge_id=0](int from, int to) mutable {
auto single = [&](int from, int to) {
auto d = add_edge(from, to, EdgeProperty { edge_id, 4 }, g).first;
if (!edge_idx.insert({edge_id++, d}).second)
throw std::invalid_argument("duplicate key");
return d;
};
auto a = single(from, to), b = single(to, from);
rev[a] = b;
rev[b] = a;
};
new_edge_pair(0, 1);
new_edge_pair(0, 2);
new_edge_pair(1, 3);
new_edge_pair(2, 3);
// 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";
{
auto& by_id = edge_idx.left;
auto& by_desc = edge_idx.right;
for (auto const& e : edge_idx.left) {
std::cout << "Edge #" << e.first << " is (" << source(e.second, g) << " -> " << target(e.second, g) << ")\n";
}
int random = prng() % num_edges(g);
auto ed = by_id.at(random);
std::cout << "Random edge #" << random << " is (" << source(ed, g) << " -> " << target(ed, g) << ")\n";
std::cout << "Reverse lookup: " << by_desc.at(ed) << "\n"; // reverse, though not very spectacular
std::cout << "Classic property lookup: " << g[ed].id << "\n"; // because it can be done using boost easily
}
}
Printing
Flow: 8
Edge #0 is (0 -> 1)
Edge #1 is (1 -> 0)
Edge #2 is (0 -> 2)
Edge #3 is (2 -> 0)
Edge #4 is (1 -> 3)
Edge #5 is (3 -> 1)
Edge #6 is (2 -> 3)
Edge #7 is (3 -> 2)
Random edge #2 is (0 -> 2)
Reverse lookup: 2
Classic property lookup: 2
Adjacency Matrix
Keeps everything the same, except for changing the model:
#include <boost/graph/adjacency_matrix.hpp>
typedef adjacency_matrix<directedS, VertexProperty, EdgeProperty> Graph;
And now you get the added capability of lookup by vertices:
Live On Coliru
std::cout << "Finding (3, 1) results in Edge #" << by_desc.at(edge(3, 1, g).first) << "\n";
Prints
Finding (3, 1) results in Edge #5
Here is a complete snippet to copy a graph with bundled properties, but results in bunch of compiler errors. What is needed to fix the problems?
struct NodeInfo1 {};
struct EdgeInfo1 {};
typedef boost::labeled_graph< boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1>,
std::string> Graph1;
typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> Edge;
void TestCases::TestCopyGraph()
{
Graph1 grid, g1;
EdgeInfo1 ei;
Edge e = add_edge_by_label("A", "B", ei, grid);
copy_graph(grid, g1);
}
That's slightly misrepresenting the question. You're not actually copying the adjacency list, you're copying the labeled_graph adaptor, which happens to not satisfy the concepts required by copy_graph:
/** #name Labeled Mutable Graph
* The labeled mutable graph hides the add_ and remove_ vertex functions from
* the mutable graph concept. Note that the remove_vertex is hidden because
* removing the vertex without its key could leave a dangling reference in
* the map.
*/
Here's copying the adjacency_list: ¹
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;
void TestCopyGraph()
{
std::string names[3] = { "A", "B", "C" };
Graph grid(3, names);
EdgeInfo1 ei;
/*auto e =*/ add_edge_by_label("C", "B", ei, grid);
AList g1;
copy_graph(grid, g1);
}
Copying the Labeled adaptor
Is much easier. No copy_graph required, just copy-construct the object:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/graph_utility.hpp>
struct NodeInfo1 { int i; };
struct EdgeInfo1 { int j; };
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;
auto TestCopyGraph()
{
std::string names[3] = { "A", "B", "C" };
NodeInfo1 props[3] = { {11}, {22}, {33} };
Graph grid(3, names, props);
/*auto e =*/ add_edge_by_label("C", "B", EdgeInfo1{17}, grid);
Graph g1 = grid; // just copy-construct
return g1;
}
int main() {
auto copied = TestCopyGraph();
print_graph(copied);
// check that properties were copied: vertex B has NodeInfo1 22
{
auto pmap = boost::get(&NodeInfo1::i, copied);
std::cout << "Vertex B NodeInfo1.i after copy: " << pmap[copied.vertex("B")] << "\n";
}
// edge properties too:
for (auto e : boost::make_iterator_range(edges(copied)))
std::cout << "Edge has property EdgeInfo1 " << copied[e].j << "\n";
std::cout << "Removed A:\n";
copied.remove_vertex("A");
print_graph(copied);
}
Prints
0 <-->
1 <--> 2
2 <--> 1
Vertex B NodeInfo1.i after copy: 22
Edge has property EdgeInfo1 17
Removed A:
0 <--> 1
1 <--> 0
¹ Note that you need this patch because of bugs in labeled_graph: https://github.com/boostorg/graph/pull/58
I am using Boost C++ Library to build a adjacency list to represent an undirected graph. Each edge on the graph is associated with respective weights and I want to check if the weight is greater than some threshold than I merge the 2 vertices together.
How I merge:
For the vertex to merge, gather all the edges to and from this vertex and direct the edges to the new vertex
Clear the merging vertex
Remove the vertex
My Problem:
I use a simple program to first construct the algorithm before I use it for purpose. In this program I am using simple family tree method to perform the above steps. When I remove the vertex using the function remove_vertex(vertex, Graph) I get a segmentation fault.
I cannot figure out if once the vertex is removed, does the graph automatically updates its structure?
My C++ code is as follows:
#include <boost/graph/adjacency_list.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef boost::property<boost::vertex_index_t, int> vertex_property;
typedef boost::property<boost::edge_weight_t, float> edge_property;
typedef typename boost::adjacency_list <boost::vecS,
boost::vecS,
boost::undirectedS,
vertex_property,
edge_property> Graph;
void boostSampleGraph() {
enum family {
Jeanie, Debbie, Rick, John, Amanda, Margaret, Benjamin, N };
const char *name[] = { "Jeanie", "Debbie", "Rick", "John", "Amanda",
"Margaret", "Benjamin", "N"
};
/* actual graph structure */
Graph graph;
/* add vertices to the graph */
add_vertex(Jeanie, graph);
add_vertex(Debbie, graph);
add_vertex(Rick, graph);
add_vertex(John, graph);
add_vertex(Amanda, graph);
add_vertex(Margaret, graph);
add_vertex(Benjamin, graph);
// add_vertex(N, graph);
/* add edges to the vertices in the graph*/
add_edge(Jeanie, Debbie, edge_property(0.5f), graph);
add_edge(Jeanie, Rick, edge_property(0.2f), graph);
add_edge(Jeanie, John, edge_property(0.1f), graph);
add_edge(Debbie, Amanda, edge_property(0.3f), graph);
add_edge(Rick, Margaret, edge_property(0.4f), graph);
add_edge(John, Benjamin, edge_property(0.6f), graph);
// add_edge(Benjamin, Benjamin, edge_property(0.7f), graph);
/* vertex iterator */
boost::graph_traits<Graph>::vertex_iterator i, end;
typedef typename boost::graph_traits<
Graph>::adjacency_iterator AdjacencyIterator;
/* gets the graph vertex index */
typedef typename boost::property_map
<Graph, boost::vertex_index_t >::type IndexMap;
IndexMap index_map = get(boost::vertex_index, graph);
/* container to hold the edge descriptor info */
typedef typename boost::graph_traits<
Graph>::edge_descriptor EdgeDescriptor;
EdgeDescriptor e_descriptor;
typedef typename boost::property_map<Graph, boost::edge_weight_t
>::type EdgePropertyAccess;
EdgePropertyAccess edge_weights = get(boost::edge_weight, graph);
typedef typename boost::property_traits<boost::property_map<
Graph, boost::edge_weight_t>::const_type>::value_type EdgeValue;
float edge_size = num_vertices(graph);
std::cout << "# of Edges: " << edge_size << std::endl;
/* iterator throught the graph */
for (tie(i, end) = vertices(graph); i != end; ++i) {
std::cout << name[get(index_map, *i)];
AdjacencyIterator ai, a_end;
tie(ai, a_end) = adjacent_vertices(*i, graph);
if (ai == a_end) {
std::cout << " has no children";
} else {
std::cout << " is the parent of ";
}
for (; ai != a_end; ++ai) {
AdjacencyIterator tmp;
bool found;
tie(e_descriptor, found) = edge(*i, *ai, graph);
float weights_ = 0.0f;
if (found) {
EdgeValue edge_val = boost::get(
boost::edge_weight, graph, e_descriptor);
weights_ = edge_val;
if (weights_ > 0.3f) {
// - remove and merge
AdjacencyIterator aI, aEnd;
tie(aI, aEnd) = adjacent_vertices(*ai, graph);
for (; aI != aEnd; aI++) {
EdgeDescriptor ed;
bool located;
tie(ed, located) = edge(*aI, *ai, graph);
if (located && *aI != *i) {
add_edge(
get(index_map, *i), get(index_map, *aI), graph);
}
std::cout << "\n DEBUG: " << *i << " "
<< *ai << " "
<< *aI << " ";
}
std::cout << std::endl;
clear_vertex(*ai, graph);
remove_vertex(*ai, graph);
// std::cout << "\nGraph Size: " <<
// num_vertices(graph) << std::endl;
}
}
// ai = tmp;
std::cout << name[get(index_map, *ai)];
if (boost::next(ai) != a_end) {
std::cout << ", ";
}
}
std::cout << std::endl << std::endl;
}
std::cout << "\nGraph Size: " << num_vertices(graph) << std::endl;
}
int main(int argc, const char *argv[]) {
boostSampleGraph();
return 0;
}
Could I get some help and ideas on where did I got this wrong.
I don't know what you're actually trying to achieve with the algorithm shown in the OP.
Here's, however, one that simplifies the code considerably, so that at least it works safely:
uses Vertex bundled property type for vertex (id, name)
uses ranged for loops where possible (see mir, shorthand to create a boost::iterator_range from a std::pair of iterators)
the code is written in container-selection independent way (so it works just the same when you replace vecS by listS in the Graph type declaration)
it uses out_edges instead of adjacent_vertices to benefit more from the AdjacencyGraph concept, and avoid reverse-lookup of edge-descriptors by (source, target) vertices
most importantly, it uses a std::set<vertex_descriptor> of vertices that have been "removed". The actual removal happens later so we don't get undefined behaviour while iterating a changing container
runs cleanly under valgrind
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
struct Vertex {
int id;
const char* name;
Vertex(int i = -1, const char* name = "default") : id(i), name(name) {}
};
template <typename It> boost::iterator_range<It> mir(std::pair<It, It> const& p) {
return boost::make_iterator_range(p.first, p.second);
}
template <typename It> boost::iterator_range<It> mir(It b, It e) {
return boost::make_iterator_range(b, e);
}
typedef typename boost::adjacency_list<
boost::vecS, boost::vecS,
boost::undirectedS,
Vertex, // bundled properties (id, name)
boost::property<boost::edge_weight_t, float> // interior property
> Graph;
Graph make() {
Graph graph;
auto Jeanie = add_vertex(Vertex { 0, "Jeanie" }, graph);
auto Debbie = add_vertex(Vertex { 1, "Debbie" }, graph);
auto Rick = add_vertex(Vertex { 2, "Rick" }, graph);
auto John = add_vertex(Vertex { 3, "John" }, graph);
auto Amanda = add_vertex(Vertex { 4, "Amanda" }, graph);
auto Margaret = add_vertex(Vertex { 5, "Margaret" }, graph);
auto Benjamin = add_vertex(Vertex { 6, "Benjamin" }, graph);
add_edge(Jeanie, Debbie, 0.5f, graph);
add_edge(Jeanie, Rick, 0.2f, graph);
add_edge(Jeanie, John, 0.1f, graph);
add_edge(Debbie, Amanda, 0.3f, graph);
add_edge(Rick, Margaret, 0.4f, graph);
add_edge(John, Benjamin, 0.6f, graph);
return graph;
}
Graph reduce(Graph graph) {
/* vertex iterator */
using vertex_descriptor = boost::graph_traits<Graph>::vertex_descriptor;
std::cout << "# of vertices: " << num_vertices(graph) << "\n";
std::cout << "# of edges: " << num_edges(graph) << "\n";
std::set<vertex_descriptor> to_remove;
/* iterator throught the graph */
for (auto self : mir(vertices(graph)))
{
std::cout << graph[self].name << (boost::empty(mir(out_edges(self, graph)))? " has no children " : " is the parent of ");
for(auto edge : mir(out_edges(self, graph))) {
auto weight = boost::get(boost::edge_weight, graph, edge);
auto mid_point = target(edge, graph);
if (to_remove.count(mid_point)) // already elided
break;
if (weight > 0.3f) {
std::set<vertex_descriptor> traversed;
for (auto hop : mir(out_edges(mid_point, graph))) {
auto hop_target = target(hop, graph);
if (hop_target != self)
add_edge(self, hop_target, graph);
std::cout << "\n DEBUG: " << graph[self].name << " " << graph[mid_point].name << " " << graph[hop_target].name << " ";
}
std::cout << "\n";
clear_vertex(mid_point, graph);
to_remove.insert(mid_point);
}
std::cout << graph[mid_point].name;
}
std::cout << "\n\n";
}
for(auto vd : to_remove)
{
clear_vertex(vd, graph);
remove_vertex(vd, graph);
}
std::cout << "# of vertices: " << num_vertices(graph) << "\n";
std::cout << "# of edges: " << num_edges(graph) << "\n";
return graph;
}
void save(Graph const& g, const char* fname);
int main() {
auto const g = make();
auto const h = reduce(g);
save(g, "before.dot");
save(h, "after.dot");
}
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/property_map/function_property_map.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <boost/format.hpp>
#include <fstream>
void save(Graph const& g, const char* fname) {
std::ofstream ofs(fname);
using namespace boost;
write_graphviz(
ofs,
g,
make_label_writer(make_function_property_map<Graph::vertex_descriptor, std::string>([&] (Graph::vertex_descriptor v){ return g[v].name; })),
make_label_writer(make_transform_value_property_map([](float v){return boost::format("%1.1f") % v;}, boost::get(edge_weight, g)))
);
}
Prints
# of vertices: 7
# of edges: 6
Jeanie is the parent of
DEBUG: Jeanie Debbie Jeanie
DEBUG: Jeanie Debbie Amanda
DebbieJohnAmanda
Debbie has no children
Rick is the parent of Jeanie
DEBUG: Rick Margaret Rick
Margaret
John is the parent of Jeanie
DEBUG: John Benjamin John
Benjamin
Amanda is the parent of Jeanie
Margaret has no children
Benjamin has no children
# of vertices: 4
# of edges: 3
Graph before:
Graph after:
I am looking for a smart way to use the Boost Graph Library in conjunction with Boost uBLAS. More precisely, I need to update a given vertex property for each vertex by using the result of the scalar product between the graphs adjacency matrix and a vector containing some other vertex property for each vertex. Let me give you a (unfortunately lenghty) minimal example to illustrate the problem:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost;
namespace ublas = boost::numeric::ublas;
struct Vertex { //Using bundled vertex properties
double old_potential;
double new_potential;
};
typedef adjacency_list< listS, vecS, directedS, Vertex > Graph;
int main(){
//[Prepare a graph with two vertex property maps and initialize
Graph graph;
add_edge (0, 1, graph);
add_edge (0, 3, graph);
add_edge (1, 2, graph);
auto v_old_potential = boost::get( &Vertex::old_potential, graph );
auto v_new_potential = boost::get( &Vertex::new_potential, graph );
unsigned int source_strength = 0;
BGL_FORALL_VERTICES( v, graph, Graph ) {
v_old_potential[v] = source_strength++;
v_new_potential[v] = 0;
}
//]
//[ Extracting the adjacency matrix by iteration through all edges --> uBLAS matrix
ublas::zero_matrix<int> zero_matrix( num_vertices(graph) , num_vertices(graph) );
ublas::matrix<int> adjacency_matrix( zero_matrix ); //initialize properly
BGL_FORALL_EDGES( e, graph, Graph ) {
adjacency_matrix( source(e,graph), target(e,graph) ) = 1;
adjacency_matrix( target(e,graph), source(e,graph) ) = 1;
}
//]
//[ Extracting the old potentials by iteration through all vertices --> uBLAS vector
ublas::zero_vector<double> zero_vector( num_vertices(graph) );
ublas::vector<double> old_potential_vector( zero_vector ); //initialize properly
ublas::vector<double> new_potential_vector( zero_vector ); //initialize properly
BGL_FORALL_VERTICES(v, graph, Graph) {
old_potential_vector( vertex(v,graph) ) = v_old_potential[v];
}
//]
//[ Compute new potentials = A . old potentials !
new_potential_vector = ublas::prod ( adjacency_matrix, old_potential_vector ); // new = A.old
//]
//[ Updating the property map for the new potentials with the newly computed values from above
BGL_FORALL_VERTICES(v, graph, Graph) {
v_new_potential[v] = old_potential_vector( vertex(v,graph) );
}
//]
std::cout << adjacency_matrix << std::endl; //output = [4,4]((0,1,0,1),(1,0,1,0),(0,1,0,0),(1,0,0,0))
std::cout << old_potential_vector << std::endl; //output = [4](0,1,2,3)
std::cout << new_potential_vector << std::endl; //output = [4](4,2,1,0)
}
Now, while my suggestion is a possible solution, i am not quite satisfied with it. The problem is, (a) I copy the whole old_potential property map to the associated ublas::vector in order to compute the scalar product. And (b) I need to traverse the new_potential property map as well to get the newly computed values back into the graph.
I suspect that these operations will be repeated a lot in my application that is why I want to get this part done as clean as possible from the start.
Ideally, I'd like to be done with all this copying back and forth and instead use some sort of adapter to make a boost::property_map work as a ublas::vector in the call of prod(). It would be awesome to use something like this:
adapter(new_potential) = ublas::prod( adjacency_matrix, adapter(old_potential) );
If anyone has an idea how to achieve such functionality or how to improve on my solution I would be very grateful.
#include <iostream>
#include <memory>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost;
namespace ublas = boost::numeric::ublas;
enum vertex_old_potential_t { vertex_old_potential };
enum vertex_new_potential_t { vertex_new_potential };
namespace boost
{
BOOST_INSTALL_PROPERTY(vertex, new_potential);
BOOST_INSTALL_PROPERTY(vertex, old_potential);
}
typedef property<vertex_new_potential_t, double, property<vertex_old_potential_t,double> > Vertex;
typedef adjacency_list< listS, vecS, directedS, Vertex > Graph;
struct ublas_vector_map;
namespace boost {
template<>
struct property_map< Graph, vertex_new_potential_t > {
typedef ublas_vector_map type;
typedef ublas_vector_map const_type;
};
template<>
struct property_map< Graph, vertex_old_potential_t > {
typedef ublas_vector_map type;
typedef ublas_vector_map const_type;
};
}
struct ublas_vector_map : put_get_helper<double&,ublas_vector_map> {
typedef double value_type;
typedef value_type& reference;
typedef typename graph_traits<Graph>::vertex_descriptor key_type;
typedef boost::lvalue_property_map_tag category;
ublas_vector_map(Graph* g, vertex_old_potential_t&):vec(new ublas::vector<double>(num_vertices(*g),0.0)){}
ublas_vector_map(Graph* g, vertex_new_potential_t&):vec(new ublas::vector<double>(num_vertices(*g),0.0)){}
reference operator[](key_type v) const {
return (*vec)(v);
}
ublas::vector<double>& vector() { return *vec; }
private:
std::unique_ptr<ublas::vector<double> > vec;
};
int main(){
//[Prepare a graph with two vertex property maps and initialize
Graph graph;
add_edge (0, 1, graph);
add_edge (0, 3, graph);
add_edge (1, 2, graph);
auto v_old_potential = boost::get( vertex_old_potential, graph );
auto v_new_potential = boost::get( vertex_new_potential, graph );
unsigned int source_strength = 0;
BGL_FORALL_VERTICES( v, graph, Graph ) {
v_old_potential[v] = source_strength++;
}
//]
//[ Extracting the adjacency matrix by iteration through all edges --> uBLAS matrix
ublas::zero_matrix<int> zero_matrix( num_vertices(graph) , num_vertices(graph) );
ublas::matrix<int> adjacency_matrix( zero_matrix ); //initialize properly
BGL_FORALL_EDGES( e, graph, Graph ) {
adjacency_matrix( source(e,graph), target(e,graph) ) = 1;
adjacency_matrix( target(e,graph), source(e,graph) ) = 1;
}
//]
//[ Compute new potentials = A . old potentials !
v_new_potential.vector() = ublas::prod ( adjacency_matrix, v_old_potential.vector() ); // new = A.old
//]
std::cout << adjacency_matrix << std::endl; //output = [4,4]((0,1,0,1),(1,0,1,0),(0,1,0,0),(1,0,0,0))
std::cout << v_old_potential.vector() << std::endl; //output = [4](0,1,2,3)
std::cout << v_new_potential.vector() << std::endl; //output = [4](4,2,1,0)
//You must access the properties via v_new_potential and v_old_potential, if you use get... again it resets
std::cout << v_new_potential[0] << std::endl;
std::cout << get(vertex_new_potential, graph)[0] << std::endl;
}