boost DFS不能与set垂直列表一起工作
boost DFS does not work with setS vertice lists
不编译以下代码。
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/function_output_iterator.hpp>
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::no_property, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelist
> Graph;
bool hasCycle(const Graph& aG)
{
try {
boost::topological_sort(
aG,
boost::make_function_output_iterator([](int){}));
} catch(boost::not_a_dag const&)
{
return true;
}
return false;
}
它在将顶点列表更改为vecS后工作http://coliru.stacked-crooked.com/a/abeb9e3f96e92af0
这个限制是因为DFS需要确定性访问吗?
谢谢你,
"限制"被记录为需要一个顶点索引。您可以添加一个(注意,您还应该将int
替换为Graph::vertex_descriptor
),或者调整图形类型以包含一个:
添加外部索引属性映射
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/function_output_iterator.hpp>
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::no_property, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelist
> Graph;
bool hasCycle(const Graph& aG)
{
try {
std::map<Graph::vertex_descriptor, size_t> index;
auto pmap = boost::make_assoc_property_map(index);
for (auto vd : boost::make_iterator_range(boost::vertices(aG)))
index[vd] = index.size();
boost::topological_sort(
aG,
boost::make_function_output_iterator([](Graph::vertex_descriptor){}),
boost::vertex_index_map(pmap));
}
catch (boost::not_a_dag const&)
{
return true;
}
return false;
}
int main() {
}
添加内部索引属性映射
这相当于将负担转移给调用者。根据应用程序的性能要求,这可能有(很多)意义。
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/function_output_iterator.hpp>
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::property<boost::vertex_index_t, int>, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelist
> Graph;
bool hasCycle(const Graph& aG)
{
try {
boost::topological_sort(
aG,
boost::make_function_output_iterator([](Graph::vertex_descriptor){})
);
}
catch (boost::not_a_dag const&)
{
return true;
}
return false;
}
#include <boost/graph/random.hpp>
#include <random>
int main() {
std::mt19937 prng{ std::random_device{}() };
Graph g;
boost::generate_random_graph(g, 100, 200, prng);
auto index = boost::get(boost::vertex_index, g);
int gen_id = 0;
for (auto vd : boost::make_iterator_range(boost::vertices(g))) {
boost::put(index, vd, gen_id++);
std::cout << "Vertex " << boost::get(index, vd) << "n";
}
bool check = hasCycle(g);
}
相关文章:
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 与引用一起使用的列表,在用作成员时更改行为
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 将[]运算符与链接列表一起使用
- 当将 getline 与 int 一起使用时,如何修复"没有重载函数 'getline' 的实例与参数列表匹配"
- 如何将重新绑定与自定义分配器和自定义列表一起使用
- 如何将 2 个链表合并或合并在一起以创建新列表
- 常量变量初始化仅与成员初始化列表一起工作
- 将initalizer列表与从空基类继承的结构一起使用
- 具有与专用列表一起使用的参数列表的超类方法<superclass>
- 为什么 auto 关键字不能与指向函数的指针的初始化列表一起使用?
- 如何将列表语法与默认精神一起使用
- 使初始化列表与 C++ 中的继承一起使用
- 将 clang 3.1 与初始值设定项列表一起使用
- 将BOOST_FOREACH与恒定的侵入性列表一起使用
- 如何将 auto 与初始值设定项列表一起使用push_back
- 如何使用与成员初始化器列表一起使用的static_assert
- boost DFS不能与set垂直列表一起工作
- STL算法可以与循环列表一起使用吗?
- 将字符串的填充构造函数与初始值设定项列表一起使用