c++ - 哪些 VertexList 类型对 depth_first_search 有效

标签 c++ c++14 depth-first-search boost-graph

当在 adjacency_list boost::depth_first_search(Graph, Visitor) 中为 VertexList 使用 boost::vecS 时,编译和工作正常。将 VertexList 类型切换为 boost::listS 时,我收到编译器错误:

boost_1_65_0\boost\graph\detail\adjacency_list.hpp(2545): error C2182: 'const_reference': 非法使用类型 'void

从这个错误中,我认为 boost::listS 不是有效类型,但是 BOOST CONCEPT 检查通过了。

如果 boost::listS 不是 depth_first_search 的有效类型,为什么?

下面是演示该问题的示例代码。从 vecS 切换到 listS 会产生上述错误。我正在使用 Visual Studio 2017 和 boost 1.65.0

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_concepts.hpp>


//typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS> MyGraph;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::bidirectionalS> MyGraph;
using VertexType = boost::graph_traits<MyGraph>::vertex_descriptor;

BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<MyGraph>));
BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<MyGraph>));
class dfs_visitor : public boost::default_dfs_visitor
{

public:

  void discover_vertex(VertexType u,  const MyGraph& g) const
  {
  }

  void finish_vertex(VertexType u,  const MyGraph& g) const
  {
  }

};

BOOST_CONCEPT_ASSERT((boost::DFSVisitorConcept<dfs_visitor, MyGraph>));

int main()
{
  MyGraph g;
  dfs_visitor vis;
  boost::depth_first_search(g, boost::visitor(vis));
  return 0;
}

最佳答案

首先,我很高兴您包含了概念检查。今天学到的一个技巧。

真正的答案很简单:概念限制了图形类型。但是,其他参数增加了更多限制:

https://www.boost.org/doc/libs/1_69_0/libs/graph/doc/depth_first_search.html (大胆的矿)

IN: vertex_index_map(VertexIndexMap i_map)

This maps each vertex to an integer in the range [0, num_vertices(g)). This parameter is only necessary when the default color property map is used. The type VertexIndexMap must be a model of Readable Property Map. The value type of the map must be an integer type. The vertex descriptor type of the graph needs to be usable as the key type of the map.

Default: get(vertex_index, g). Note: if you use this default, make sure your graph has an internal vertex_index property. For example, adjacency_list with VertexList=listS does not have an internal vertex_index property.

换句话说,你的图表很好。但是您必须提供顶点索引。

Live On Wandbox

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_concepts.hpp>
#include <numeric>

typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS> MyGraph;
using VertexType = boost::graph_traits<MyGraph>::vertex_descriptor;

BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<MyGraph>));
BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<MyGraph>));

struct dfs_visitor : boost::default_dfs_visitor {
    void discover_vertex(VertexType, const MyGraph &) const {}
    void finish_vertex(VertexType, const MyGraph &) const {}
};

BOOST_CONCEPT_ASSERT((boost::DFSVisitorConcept<dfs_visitor, MyGraph>));

int main() {
    MyGraph g;
    dfs_visitor vis;
    std::map<MyGraph::vertex_descriptor, int> index;

    // fill index
    for (auto vd : boost::make_iterator_range(vertices(g))) {
        index.emplace(vd, index.size());
    }

    auto index_map = boost::make_assoc_property_map(index);
    boost::depth_first_search(g, boost::visitor(vis).vertex_index_map(index_map));
}

如果仔细阅读,通过自定义颜色图也可以满足。这有一个微小的优势,即不需要将所有元素初始化为默认初始化值:

Live On Wandbox

int main() {
    MyGraph g;
    dfs_visitor vis;
    std::map<MyGraph::vertex_descriptor, boost::default_color_type> colors;

    auto color_map = boost::make_assoc_property_map(colors);
    boost::depth_first_search(g, boost::visitor(vis).color_map(color_map));
}

关于c++ - 哪些 VertexList 类型对 depth_first_search 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55367106/

相关文章:

c++ - 在邻接矩阵上应用广度和深度优先搜索?

java - 调试递归方法来查找单词是否存在于滑板中

c++ - 用于渲染不同对象的 VAO 和 VBO

C++:使用回调机制调用函数

c++ - 模板类中的变量模板 - 意外错误(可能的错误?)

c++ - 函数模板重载解析在 Visual C++ 2017 中失败

c++ - 尝试初始化 __m128 类成员变量时出现 EXC_BAD_ACCESS 信号

c++ - Botan::SecureVector - 在构造函数中调用析构函数?

c++ - 具有前向声明类型的函数模板特化

java - 查找流程图中的所有方法?