views:

12

answers:

0

I have a class that contains a BGL graph. I'd like to traverse the graph in a constant context. For example, I might want an accessor function to report if the graph is cyclic. As soon as I make my dfs function const the code won't compile.

Here's a minimal example:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/undirected_dfs.hpp>

using namespace boost;
using namespace std;

class Graph {
  public:
    Graph() {
      add_edge(0, 1, g);
      add_edge(0, 2, g);
      add_edge(1, 2, g);
      add_edge(4, 5, g);
    }

    // QUESTION HERE: Why can't this method be const?
    void dfs() {
      vector<default_color_type> colors(num_vertices(g), white_color);
      undirected_depth_first_visit(g, vertex_t(0), default_dfs_visitor(), &colors[0], get(&Graph::EdgeContainer::color, g));
    }

  private:
    struct EdgeContainer {
      default_color_type color;
    };
    adjacency_list< vecS, vecS, undirectedS, no_property, EdgeContainer> g;
};

int main(int, char*[]) {
  Graph g;
  g.dfs();
  return 0;
}

From the header of undirected_depth_first_visit, undirected_dfs, and the dfs_visitor class, all of the parameters are either passed by value, or constant. What is causing this error, and how can I make my function const?

Edit The problem must be that the edge colors are being changed. So, another way to phrase my question would be: How do I make an external ColorMap for edges? The method I have used for vertex colors (the vector) does not work for edges.