开发者

Boost Graph Library: Potential Bug

开发者 https://www.devze.com 2023-03-28 00:56 出处:网络
BGL\'s depth_first_search algorithm sometimes calls back_edge() on visitors even if there are no cycles in the graph. By definition of back edge, and according to Boost\'s DFS Visitor Documentation, t

BGL's depth_first_search algorithm sometimes calls back_edge() on visitors even if there are no cycles in the graph. By definition of back edge, and according to Boost's DFS Visitor Documentation, this shouldn't happen. Note that this is reproducible only when listS is used as the representation for both vertices and edges.

The code example below (should compile as is) constructs a graph with two nodes and a single edge. It incorrectly prints "back edge." Am I doing anything wrong here? Or is this a bug?

#include <iostream>
using namespace std;

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/visitors.hpp>
using namespace boost;

typedef boost::property<boost::vertex_index_t,std::size_t> VertexProperties;
typedef boost::adjacency_list<boost::listS,
                              boost::listS,
                              boost::bidirectionalS,
                              VertexProperties> Graph;  // Graph object type

typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef boost::graph_traits<Graph>::edge_descriptor Edge;

class VisitorClass : public dfs_visitor<> {
 public:
  VisitorClass() {}

  template <typename Edge, typename Graph>
  void back_edge(Edge, const Graph&) const {
    cout << "back edge" << endl;
  }
};

int
main() {
  Graph g;
  Vertex v = add_vertex(g);
  Vertex u = add_vertex(g);

  bool inserted;
  tie(tuples::ignore, inserted) = add_edge(v, u, g);
  assert(inserted);

  VisitorClass vst;
  depth_f开发者_Python百科irst_search(g, visitor(vst));
  // Should not print "back edge", but does.

  return 0;
}

Tested with Boost 1.46.1 using i686-apple-darwin11-llvm-g++-4.2 on Mac OS 10.7;

Tested with Boost 1.42.0 using gcc 4.4.5 on Debian 2.6.32-34squeeze1.


You filed a bug, but you didn't follow up there.

Not long afterwards, you got an answer:

You are not initializing the vertex_index property of your graph. Try adding some code such as:

graph_traits::vertices_size_type i = 0;

BGL_FORALL_VERTICES(v, graph, Graph) put(vertex_index, g, v, i++);

I tried this (fixing the typo) and it works fine:

#include <boost/graph/iteration_macros.hpp>

int main() {
  Graph g;   
  Vertex v = add_vertex(g);   
  Vertex u = add_vertex(g);

  graph_traits<Graph>::vertices_size_type i = 0;  
  BGL_FORALL_VERTICES(v, g, Graph) put(vertex_index, g, v, i++);

  bool inserted;   
  tie(tuples::ignore, inserted) = add_edge(v, u, g); 
  assert(inserted);

  VisitorClass vst;   
  depth_first_search(g, visitor(vst));
// Should not print "back edge", but does.

  return 0;
  }

(at least, it no longer prints "back edge")

0

精彩评论

暂无评论...
验证码 换一张
取 消