【问题标题】:struggling to implement simple boost::graph traversal努力实现简单的 boost::graph 遍历
【发布时间】:2013-09-01 23:22:35
【问题描述】:

我正在努力实现一个简单的 boost::graph。我已经尝试阅读我能找到的所有文档和示例等......

这是我的代码(复制自 StackOverflow 上的其他示例)

顶点:

class customvertex
{
public:
    double some_member;
};

来访者:

class MyVisitor : public boost::default_dfs_visitor
{
public:
    void discover_vertex(MyGraphVertex v, const MyGraph& g) const
    {
        std::cout << v << std::endl;
        return;
    }
};

typedef 声明:

typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::undirectedS, customvertex> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor MyGraphVertex;

导致问题的代码:

MyGraph theGraph;
customvertex a;
MyGraphVertex vert_a = boost::add_vertex(a, theGraph);

customvertex b;
MyGraphVertex vert_b = boost::add_vertex(b, theGraph);

boost::add_edge(vert_a , vert_b, theGraph);

MyVisitor vis;
boost::depth_first_search(theGraph, boost::visitor(MyVisitor()));

对 depth_first_search 的最终调用导致编译器抛出 112 行级联错误。
这些概念似乎是 IncidenceGraphConcept 和 MultiPassInputIterator

关键部分似乎是:

1>        T:\boost\boost_1_47_0\boost/concept/detail/msvc.hpp(23) : while compiling class template member function 'void boost::concepts::check<Model>::failed(Model *)'
1>        with
1>        [
1>            Model=boost::SignedInteger<int>
1>        ]

1>        T:\boost\boost_1_47_0\boost/graph/depth_first_search.hpp(83) : see reference to function template instantiation 'void boost::function_requires<boost::concepts::IncidenceGraphConcept<G>>(Model *)' being compiled
1>        with
1>        [
1>            G=MyGraph,
1>            Model=boost::concepts::IncidenceGraphConcept<MyGraph>
1>        ]
1>        T:\boost\boost_1_47_0\boost/graph/depth_first_search.hpp(202) : see reference to function template instantiation 'void boost::detail::depth_first_visit_impl<VertexListGraph,DFSVisitor,ColorMap,boost::detail::nontruth2>(const IncidenceGraph &,unsigned int,DFSVisitor &,ColorMap,TerminatorFunc)' being compiled
1>        with
1>        [
1>            VertexListGraph=MyGraph,
1>            DFSVisitor=const MyVisitor,
1>            ColorMap=boost::shared_array_property_map<boost::default_color_type,boost::vec_adj_list_vertex_id_map<boost::property<boost::vertex_bundle_t,customvertex>,unsigned int>>,
1>            IncidenceGraph=MyGraph,
1>            TerminatorFunc=boost::detail::nontruth2
1>        ]

非常感谢任何和所有帮助。 我确定我遗漏了一些简单的东西,我通常可以从示例中弄清楚。我认为 boost::graph 看起来很棒,非常适合我的需要,但需要更多文档......

我在模板编程方面相当有经验,但我现在在这方面花了太多时间,是时候寻求帮助了!

【问题讨论】:

  • 你不想boost::visitor(vis) 吗?
  • 根据文档,depth_first_search 仅适用于有向图,但在 g++ 4.8.1 上使用 boost 1.54,使用您发布的片段的示例似乎有效。当您的图是无向的时,还有另一种算法应该起作用:undirected_dfs。所以也许你应该尝试看看它是否适用于你的 Visual Studio 版本。
  • @Guy Sirton:是的,我知道,这只是我在代码中乱七八糟的一个片段
  • @cv_and_he:是的,我是这么想的,所以我尝试了很多有向图和无向图和搜索的组合。上面几乎是从另一个例子中提取的。那么上面的片段对你有用吗?嗯....
  • @KenMoynihan 你能尝试为我们构建一个更小的独立样本来显示问题吗?

标签: c++ boost boost-graph


【解决方案1】:

来自this GitHub,我复制了一个类似的工作(和测试)示例:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/graph/depth_first_search.hpp>

struct Node
{
  Node(const int t = 0) : m_t{t} {}
  int m_t;
};

/// A phylogeny is a graph of Node of different generations
/// connected by ancestry
using phylogeny = boost::adjacency_list<
  boost::vecS,
  boost::vecS,
  boost::undirectedS,
  Node
>;

using phylogeny_vd = boost::graph_traits<phylogeny>::vertex_descriptor;

class my_visitor : public boost::default_dfs_visitor
{
public:
  void discover_vertex(phylogeny_vd vd, const phylogeny& g) const
  {
    std::cout << g[vd].m_t << std::endl;
  }
};

//Create a phylogeny
//
//        b---c---d
//        |
//    a---+
//        |
//        e---f---g
//
// ---+---+---+---+---
//    0   1   2   3  t (generation)
phylogeny create_phylogeny()
{
  phylogeny p;
  const auto a = boost::add_vertex(Node(0), p);
  const auto b = boost::add_vertex(Node(1), p);
  const auto c = boost::add_vertex(Node(2), p);
  const auto d = boost::add_vertex(Node(3), p);
  const auto e = boost::add_vertex(Node(1), p);
  const auto f = boost::add_vertex(Node(2), p);
  const auto g = boost::add_vertex(Node(3), p);
  boost::add_edge(a, b, p);
  boost::add_edge(b, c, p);
  boost::add_edge(c, d, p);
  boost::add_edge(a, e, p);
  boost::add_edge(f, g, p);
  return p;
}

int main()
{
  const phylogeny p = create_phylogeny();
  my_visitor v;
  boost::depth_first_search(
    p,
    boost::visitor(v)
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多