如何使用boost :: vf2_subgraph_iso获得边缘映射

时间:2019-04-25 11:39:44

标签: c++ boost graph boost-graph isomorphism

boost::vf2_subgraph_iso应该在给定的大图内找到给定的小图的诱导子图。传递给它的回调将获得一个映射作为输入。

template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1 g) const{
    // boost::get(f, u) maps u in small to v in large
}

但是,文档仅提到了顶点映射。我了解可以通过映射每个边缘的源和目标来理解边缘映射。但是我需要映射边缘的捆绑属性。

boost::get似乎不适用于映射和边缘描述符。 boost::get(f, e)产生以下错误消息。

error: no match for ‘operator[]’ (operand types are ‘const boost::iterator_property_map<__gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int, std::allocator<long unsigned int> > >, boost::vec_adj_list_vertex_id_map<bya::util::isomorphism::vertex_data, long unsigned int>, long unsigned int, long unsigned int&>’ and ‘const boost::detail::edge_desc_impl<boost::bidirectional_tag, long unsigned int>’)

1 个答案:

答案 0 :(得分:0)

是的,该映射不适用于边缘描述符,因为它们不是顶点描述符。

我建议您在“其他”图中查找边缘。因此,例如

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/vf2_sub_graph_iso.hpp>

using namespace boost;

struct VBundle { std::string name; };
struct EBundle { int data = 0; };

using G = adjacency_list<vecS, vecS, bidirectionalS, VBundle, EBundle>;
using Edge = G::edge_descriptor;

int main() {
    G small(4), large(5);
    small[0].name = "zero";
    small[1].name = "one";
    small[2].name = "two";
    small[3].name = "three";
    add_edge(0, 1, {33}, small);
    add_edge(1, 3, {44}, small);
    add_edge(2, 3, {55}, small);

    add_edge(4, 2, large); // 0->4, 1->2, 3->0, 2->3
    add_edge(2, 0, large);
    add_edge(3, 0, large);

    auto cb = [&](auto&& f, auto&&) {
        for (auto small_vd: make_iterator_range(vertices(small))) {
            auto large_vd = get(f, small_vd);

            std::cout << '(' << small_vd << ", " << large_vd << ") ";
            large[large_vd] = small[small_vd];

            for (Edge small_ed: make_iterator_range(out_edges(small_vd, small))) {
                auto large_src = get(f, source(small_ed, small));
                auto large_tgt = get(f, target(small_ed, small));

                auto [large_ed, found] = edge(large_src, large_tgt, large);
                assert(found);

                // e.g. copy edge bundle to the smaller graph
                large[large_ed] = small[small_ed];
            }
        }

        std::cout << std::endl;
        return true;
    };

    bool ok = vf2_subgraph_iso(small, large, cb);

    std::cout << std::boolalpha << ok << "\n";
}

此打印

(0, 4) (1, 2) (2, 3) (3, 0) 
true

但还将已将相应的顶点和边束从小图复制到大图。