1

以下代码未编译。

#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 需要确定性访问吗?

谢谢,

4

1 回答 1

3

“限制”被记录为对顶点索引的需要。您可以添加一个(请注意,您也应该替换intGraph::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);
}
于 2015-08-04T21:26:14.943 回答