I am endeavouring to remove all of the nodes from my graph (using the pattern defined here) which have no connecting edges. My (MWE) code thus far is as follows:
//g++ -O3 question.cpp -o question.exe
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
typedef long long node_id_t;
typedef boost::adjacency_list<
boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
boost::no_property, // vertex properties
boost::no_property // edge properties
> AdjGraph;
typedef boost::labeled_graph<
AdjGraph,
node_id_t // Node ID
> LabeledGraph;
int main(){
LabeledGraph g;
add_vertex( 10, g );
add_vertex( 20, g );
add_vertex( 30, g );
add_vertex( 40, g );
add_vertex( 50, g );
boost::graph_traits<LabeledGraph>::vertex_iterator vi, vi_end, next;
boost::tie(vi, vi_end) = boost::vertices(g);
for (next = vi; vi != vi_end; vi = next) {
++next;
if(boost::degree(*vi)==0)
boost::remove_vertex(*vi, g);
}
}
Unfortunately, the code errors out in compilation with the complaint:
question.cpp:36:25: error: no matching function for call to ‘degree(void*&)’
if(boost::degree(*vi)==0)
My expectation is that vi
is a vertex_iterator
and dereferencing it should give me a valid descriptor. I am not sure why this is not happening.
How can I achieve this?
You need to pass the extra argument
g
todegree()
.Further more as the LabeledGraph adaptor doesn't model a
MutableGraph
theremove_vertex
call can't work.Luckily you can get the underlying graph and mutate that. I'd have to read up on
LabeledGraph
to see whether there are adverse effects:Live On Coliru
Maybe you can do without the LabeledGraph:
Live On Coliru
Prints: