Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] in-depth parsing and modifications of the graph
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2013-07-12 14:54:43


On Fri, 12 Jul 2013, Orianne Siret wrote:

> I mean traverse when i said parse. So the problem is i can't traverse in depth-first order my graph and modify  node (or edge) properties. I wrote a
> basic case which doesn't work.
>
> -----------------------------------------
> // Begin main.cpp
>
> #include <boost/graph/adjacency_list.hpp>
> #include <boost/graph/depth_first_search.hpp>
> #include <vector>
>
> // Node properties
> struct MyNode
> {
> int node_id;
> MyNode() : node_id() {}
> MyNode(int id) : node_id(id) {}
> };
>
> //Edge Properties
> struct MyEdge
> {
> int edge_id;
> MyEdge() : edge_id() {}
> MyEdge(int id) : edge_id(id) {}
> };
>
> // Graph definition
> typedef boost::adjacency_list<
> boost::vecS, boost::vecS, boost::directedS,
> MyNode, MyEdge
> > MyGraph;
>
> typedef boost::graph_traits<MyGraph>::vertex_descriptor MyGraphVD;
>
> //My idiot visitor
> class my_dfs_visitor : public boost::default_dfs_visitor
> {
> public:
> template < typename Vertex, typename Graph > void discover_vertex(Vertex u, Graph & g)
> {
>                 // here i cant assign 0 to m node_id. however g is not cont.. I didn't find a code with a depth-first traversal and assign
> ment.
> g[u].node_id = 0;
> }
> };
>
> int main()
> {
> MyGraph g;
> MyGraphVD vd0 = boost::add_vertex(MyNode(0), g);
> MyGraphVD vd1 = boost::add_vertex(MyNode(1), g);
>
> boost::add_edge(vd0, vd1, MyEdge(), g);
> boost::add_edge(vd1, vd0, MyEdge(), g);
>
> my_dfs_visitor visitor;
> boost::depth_first_search(g, boost::visitor(visitor));
> return 0;
> }
>
> // end
>
> ------------------------------------------
>
> This code doesn't work. I have the following error : ../main.cpp:35:3: erreur: assignment of member ¡MyNode::node_id¢ in read-only object. 
> (I do: "g++ -O3 -Wall -c -o main.o main.cpp " for compilation, i run on linux ubuntu 12.04 with gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3)
> I didn't find any example on the web.. 
>
> Thanks for spending time on my problem. It is very nice of you.

I think the easiest solution for you is to put a non-const reference to
your graph into the visitor as a data member, then have your visitor's
member functions refer to that reference rather than using the graph
passed in by the algorithm.

-- Jeremiah Willcock


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net