在boost中查找有向图的所有循环

问题描述 投票:0回答:2

有人可以告诉我如何使用 boost 图库查找有向图的所有循环吗?

boost graph-theory
2个回答
1
投票

Google 出现了 - 否则未记录 - tiernan_all_cycles。不过有一个例子

该示例预设了不太理想的图模型。根据问题#182,您应该确实能够满足邻接列表缺失的概念要求(前提是它具有正确的顶点索引):

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::/*un*/directedS>;

// see https://github.com/boostorg/graph/issues/182
namespace boost { void renumber_vertex_indices(Graph const&) {} }

这是一个现代化的示例:

实时编译器资源管理器

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/tiernan_all_cycles.hpp>
#include <iostream>

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::/*un*/directedS>;

// see https://github.com/boostorg/graph/issues/182
namespace boost { void renumber_vertex_indices(Graph const&) {} }

struct Vis {
    void cycle(auto const& path, Graph const& g) const {
        auto indices = get(boost::vertex_index, g);
        for (auto v : path)
            std::cout << "ABCDEFGHIJKL"[get(indices, v)] << " ";
        std::cout << "\n";
    };
};

int main()
{
    enum { A, B, C, D, E, F, G, H, I, J, K, L, NUM };
    Graph g(NUM);
    // Graph from https://commons.wikimedia.org/wiki/File:Graph_with_Chordless_and_Chorded_Cycles.svg
    for (auto [s, t] : { std::pair //
            {A, B}, {B, C}, {C, D}, {D, E}, {E, F}, {F, A},
            {G, H}, {H, I}, {I, J}, {J, K}, {K, L}, {L, G},
            {C, L}, {D, K}, {B, G}, {C, G}, {I, K}
         }) //
        add_edge(s, t, g);

    tiernan_all_cycles(g, Vis{});
}

打印

A B C D E F 
G H I J K L 
G H I K L 

0
投票

感谢这个解决方案。有没有一种巧妙的方法将循环存储在向量中,而不是仅仅打印它们?

© www.soinside.com 2019 - 2024. All rights reserved.