[graph] Modifying properties in a visitor?

Can a visitor modify properties in a graph? I would like to use the following visitor with the breadth_first_search algorithm: struct ExecPriorityVisitor : public boost::default_dfs_visitor { ExecPriorityVisitor(); void start_vertex( Vertex v, Graph& g ){ g[v].priority = 0; } void examine_edge( Edge e, Graph& g ){ const Vertex src = boost::source(e,g); Vertex dest = boost::target(e,g); int& priority = g[dest].priority; priority = std::max( priority, g[src].priority + 1 ); } }; However, it fails to compile because apparently the graph argument must be const. That implies that I cannot modify graph properties from within the visitor? Why not? Is the suggested approach to record the modifications in yet another object and later impose that on the graph, or is there a way to accomplish this through a visitor? James

On Thu, Jan 15, 2009 at 4:52 PM, James C. Sutherland < James.Sutherland@utah.edu> wrote:
Can a visitor modify properties in a graph? I would like to use the following visitor with the breadth_first_search algorithm:
struct ExecPriorityVisitor : public boost::default_dfs_visitor { ExecPriorityVisitor(); void start_vertex( Vertex v, Graph& g ){ g[v].priority = 0; } void examine_edge( Edge e, Graph& g ){ const Vertex src = boost::source(e,g); Vertex dest = boost::target(e,g); int& priority = g[dest].priority; priority = std::max( priority, g[src].priority + 1 ); } };
However, it fails to compile because apparently the graph argument must be const. That implies that I cannot modify graph properties from within the visitor? Why not?
You can, and there are two ways to go about doing it: the dirty way, and the kind of weird way. 1. You could pass the graph by const&, and then const_cast it and modify the property. That's the dirty way. 2. The weird (but right) way is to define the visitor to hold a read/write property map to the graph property in question. The visitor method still takes the graph by const&, but you have an object that has a non-const& that allows you to modify properties. Be careful that the property you're modifying isn't being used by the algorithm. You might get weird results. Andrew Sutton andrew.n.sutton@gmail.com
participants (2)
-
Andrew Sutton
-
James C. Sutherland