Reputation: 1127
The issue
The code in the bottom of the post prints out:
Vertices in g = [ 0 1 2 3 4 ]
Vertices in g' = [ 0 1 ]
I was expected the output to be:
Vertices in g = [ 0 1 2 3 4 ]
Vertices in g' = [ 3 4 ]
Is this a bug in boost::subgraph or in my understanding of the library?
The code in question
#include <sstream>
#include <iostream>
#include <boost/graph/subgraph.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;
// Underlying graph representation and implementation
typedef adjacency_list_traits<vecS, vecS, directedS> Traits;
// Graph representation
typedef subgraph< adjacency_list<vecS, vecS, directedS,
property<vertex_color_t, int>, property<edge_index_t, int> > > Graph;
// Iterating over vertices and edges
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
int main(void)
{
Graph g;
add_edge(0,1, g);
add_edge(1,2, g);
add_edge(3,4, g);
Graph sub = g.create_subgraph();
add_vertex(3, sub);
add_vertex(4, sub);
pair<vertex_iter, vertex_iter> vip;
cout << "Vertices in g = [ ";
vip = vertices(g);
for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
cout << *vi << " ";
}
cout << "]" << endl;
cout << "Vertices in g' = [ ";
vip = vertices(sub);
for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
cout << *vi << " ";
}
cout << "]" << endl;
return 0;
}
Upvotes: 3
Views: 611
Reputation: 1127
I was too lazy to read the documentation. boost::subgraph distinguished between "local" and "global" descriptors.
The add_vertex function uses global descriptors when adding vertices to the subgraph. The vertices() function returns local descriptors.
What I needed to do, was to use the method local_to_global() on the subgraph to "resolve" the local descriptor into the global descriptor that I was expecting.
The output:
Vertices in g = [ 0 1 2 3 4 ]
Vertices (local) in g' = [ 0 1 ]
Vertices (global) in g' = [ 3 4 ]
Comes from the code:
#include <sstream>
#include <iostream>
#include <boost/graph/subgraph.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;
// Underlying graph representation and implementation
typedef adjacency_list_traits<vecS, vecS, directedS> Traits;
// Graph representation
typedef subgraph< adjacency_list<vecS, vecS, directedS,
property<vertex_color_t, int>, property<edge_index_t, int> > > Graph;
// Iterating over vertices and edges
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
int main(void)
{
Graph g;
add_edge(0,1, g);
add_edge(1,2, g);
add_edge(3,4, g);
Graph sub = g.create_subgraph();
add_vertex(3, sub);
add_vertex(4, sub);
pair<vertex_iter, vertex_iter> vip;
cout << "Vertices in g = [ ";
vip = vertices(g);
for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
cout << *vi << " ";
}
cout << "]" << endl;
cout << "Vertices (local) in g' = [ ";
vip = vertices(sub);
for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
cout << *vi << " ";
}
cout << "]" << endl;
cout << "Vertices (global) in g' = [ ";
vip = vertices(sub);
for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
cout << sub.local_to_global(*vi) << " ";
}
cout << "]" << endl;
return 0;
}
Upvotes: 5