C++ Boost不相交集:如何检索集?

C++ Boost不相交集:如何检索集?,c++,boost,disjoint-sets,C++,Boost,Disjoint Sets,我试图使用Boost中的不相交集,但在整天浏览StackOverflow和文档之后,我无法解决我的问题 主要问题是:给定一些将作为秩和父元素的int元素类型的映射(在将来,该元素类型需要是指向实际对象的指针),并在执行一些union\u sets后,获得向量向量:每个外部向量是连接的组件编号,每个内部向量是点列表(或,指针)组成连接的组件 例如:v[1]->[0,30234,…] 我已经看过了,还有一些其他的问题,在谷歌头版的搜索结果中 我已经用user@janoma的代码创建了一个小例子。然而

我试图使用Boost中的不相交集,但在整天浏览StackOverflow和文档之后,我无法解决我的问题

主要问题是:给定一些将作为秩和父元素的int元素类型的映射(在将来,该元素类型需要是指向实际对象的指针),并在执行一些
union\u set
s后,获得向量向量:每个外部向量是连接的组件编号,每个内部向量是点列表(或,指针)组成连接的组件

例如:
v[1]->[0,30234,…]

我已经看过了,还有一些其他的问题,在谷歌头版的搜索结果中

我已经用user@janoma的代码创建了一个小例子。然而,他的答案虽然非常好,但“过于定制”了,无法满足他的需求,在修补了一段时间后,我看不出如何使他的代码适应
std::maps
的使用

/*!
 * Adapted from
 *   http://janoma.cl/post/using-disjoint-sets-with-a-vector/?i=1
 *   https://github.com/janoma/study/blob/master/disjoint_sets/main.cpp
 *
 */

#include <algorithm>
#include <map>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <random>
#include <vector>

#include <boost/pending/disjoint_sets.hpp>
#include <boost/pending/property.hpp>

typedef int element_t;

void
printElements(std::vector<int>& elements, boost::disjoint_sets<boost::associative_property_map<std::map<int,int>>, boost::associative_property_map<std::map<int,int>>> sets)
{
    std::cout << "Elements:            ";
    for (size_t i = 0; i < elements.size(); ++i)
    {
        std::cout << std::setw(4) << elements[i];
    }
    std::cout << std::endl;
    std::cout << "Set representatives: ";
    for (size_t i = 0; i < elements.size(); ++i)
    {
        std::cout << std::setw(4) << sets.find_set(elements[i]);
    }
    std::cout << std::endl;
}

int main()
{
    // initialization
    std::vector<element_t> elements;
    elements.reserve(30);
    for (size_t i = 0; i < elements.capacity(); ++i)
    {
        elements.push_back(element_t(rand() % 90));
    }

    // disjoint sets
    std::map<element_t,int> rank;
    std::map<element_t,element_t> parent;

    boost::disjoint_sets<
        boost::associative_property_map<std::map<element_t,int>>,
        boost::associative_property_map<std::map<element_t,element_t>> > sets(
            boost::make_assoc_property_map(rank),
            boost::make_assoc_property_map(parent));

    // initialize disjoint sets
    for (size_t i = 0; i < elements.size(); ++i)
    {
        sets.make_set(elements.at(i));
    }

    // unions
    for (size_t i = 0; i < elements.size()/2; ++i)
    {
        // Union between this element and one randomly chosen from the rest
        size_t j = rand() % elements.size();
        sets.union_set(elements[i], elements[j]);
    }

    std::cout << "Found " << sets.count_sets(elements.begin(), elements.end()) << " sets:" << std::endl;
    printElements(elements,sets);

    // compression
    sets.compress_sets(elements.begin(), elements.end());

    // QUICK & DIRTY
    std::vector<element_t> representatives;
    representatives.reserve(30);
    for (size_t i = 0; i < elements.capacity(); ++i)
        representatives.push_back(sets.find_set(elements[i]));
    // ---

    std::cout << std::endl << "After path compression:" << std::endl;
    printElements(elements,sets);

    std::sort(elements.begin(),elements.end(), [representatives](auto lhs, auto rhs){ return representatives[lhs] < representatives[rhs]; });

    std::cout << std::endl << "After path compression and sorting:" << std::endl;
    printElements(elements,sets);
}
实际结果是,我没有把它分成单独的列表,但是:

After path compression and sorting:
Elements:              76  55  37  62  80  62  69  87  71  46  52  36  60  73  79  50  67  32  69  46  23   1   8  12  23  27  13  16  25  25
Set representatives:   76  55  37  62  80  62  50  87  71  55  52  36  60  55  55  50  52  87  50  55  55  87  50  60  55  50  52  50  52  52
这是无序的


此时,我没有资源继续寻找/学习如何正确使用boost不相交集。

我对所讨论的代码正试图做什么感到困惑,但我可以回答一般性问题“如何从boost的不相交集实现中检索集?”基本上,您使用由数据结构构建的父映射

不相交集合数据结构将每个集合表示为“子集合”对于集合的任意成员,将这些任意成员称为集合代表。由Boost库构建的父属性映射将每个元素与其所属集合的代表关联。要构建实际集合,我们需要基本上反转父映射。我们迭代父映射,从代表构建映射表示元素,这是一对多关系,因此此映射具有元素向量作为值。此映射的值将是我们要查找的集合

下面的代码。以下代码在中查找连接的组件

我使用字符串作为元素只是为了得到一个不使用整数项的StackOverflow示例,这是完整的。请注意,
get\u unique\u vertices
函数只是此代码设计的产物,它仅使用边作为输入来构建图林。如果您已经知道v或者通过使用不相交的_集数据结构本身来跟踪它们。我这样做是为了使不相交的_集的实际使用尽可能简洁:

#include "boost/pending/disjoint_sets.hpp"
#include "boost/property_map/property_map.hpp"
#include <iostream>
#include <tuple>
#include <unordered_set>
#include <unordered_map>

template<typename T>
using assoc_map = boost::associative_property_map<T>;
using rank_map = std::unordered_map<std::string, int>;
using parent_map = std::unordered_map<std::string, std::string>;
using disjoint_sets = boost::disjoint_sets<assoc_map<rank_map>, assoc_map<parent_map>>;

std::vector<std::string> get_unique_vertices(const std::vector<std::tuple<std::string, std::string>>& edges)
{
    std::unordered_set<std::string> vertex_set;
    std::vector<std::string> vertices;
    vertices.reserve(2 * edges.size());
    for (const auto [u, v] : edges) {
        if (vertex_set.find(u) == vertex_set.end()) {
            vertex_set.insert(u);
            vertices.push_back(u);
        }
        if (vertex_set.find(v) == vertex_set.end()) {
            vertex_set.insert(v);
            vertices.push_back(v);
        }
    }
    return vertices;
}

std::vector<std::vector<std::string>> find_connected_components(const std::vector<std::tuple<std::string, std::string>>& edges)
{
    rank_map rank;
    parent_map parent;
    disjoint_sets ds( boost::make_assoc_property_map(rank),  boost::make_assoc_property_map(parent));

    // insert all the vertices as single sets
    auto vertices = get_unique_vertices(edges);
    for (const auto& v : vertices) {
        ds.make_set(v);
    }

    // add each graph edge to the data structure
    for (const auto [u, v] : edges) {
        ds.link(u, v);
    }

    // build a map mapping representatives to set elements...
    std::unordered_map<std::string, std::vector<std::string>> sets;
    for (const auto& v : vertices) {
        auto parent = ds.find_set(v);
        sets[parent].push_back(v);
    }

    // return just the values from the above
    std::vector<std::vector<std::string>> output(sets.size());
    std::transform(sets.begin(), sets.end(), output.begin(),
        [](const auto& key_val) {
            return key_val.second;
        }
    );

    return output;
}

int main()
{
    std::vector<std::tuple<std::string, std::string>> edges = {
       {"A" , "B"},
       {"D" , "E"},
       {"H" , "I"},
       {"K" , "J"},
       {"E" , "F"},
       {"B" , "C"},
       {"H" , "K"},
       {"E" , "G"},
       {"I" , "J"}
    };

    auto connected_components = find_connected_components(edges);
    for (const auto& cc : connected_components) {
        for (const auto& vertex : cc)
            std::cout << vertex;
        std::cout << "\n";
    }
}

可悲的是,这已经过去了一年半,我不再编写代码,也无法访问开发环境来测试您的答案,但我会接受这句话,作为对所做努力的“感谢”。IIRC,我试图实现一种群集算法,该算法必须返回节点列表(图中连接的组件).不相交集似乎是最干净/最有效的处理方法。
#include "boost/pending/disjoint_sets.hpp"
#include "boost/property_map/property_map.hpp"
#include <iostream>
#include <tuple>
#include <unordered_set>
#include <unordered_map>

template<typename T>
using assoc_map = boost::associative_property_map<T>;
using rank_map = std::unordered_map<std::string, int>;
using parent_map = std::unordered_map<std::string, std::string>;
using disjoint_sets = boost::disjoint_sets<assoc_map<rank_map>, assoc_map<parent_map>>;

std::vector<std::string> get_unique_vertices(const std::vector<std::tuple<std::string, std::string>>& edges)
{
    std::unordered_set<std::string> vertex_set;
    std::vector<std::string> vertices;
    vertices.reserve(2 * edges.size());
    for (const auto [u, v] : edges) {
        if (vertex_set.find(u) == vertex_set.end()) {
            vertex_set.insert(u);
            vertices.push_back(u);
        }
        if (vertex_set.find(v) == vertex_set.end()) {
            vertex_set.insert(v);
            vertices.push_back(v);
        }
    }
    return vertices;
}

std::vector<std::vector<std::string>> find_connected_components(const std::vector<std::tuple<std::string, std::string>>& edges)
{
    rank_map rank;
    parent_map parent;
    disjoint_sets ds( boost::make_assoc_property_map(rank),  boost::make_assoc_property_map(parent));

    // insert all the vertices as single sets
    auto vertices = get_unique_vertices(edges);
    for (const auto& v : vertices) {
        ds.make_set(v);
    }

    // add each graph edge to the data structure
    for (const auto [u, v] : edges) {
        ds.link(u, v);
    }

    // build a map mapping representatives to set elements...
    std::unordered_map<std::string, std::vector<std::string>> sets;
    for (const auto& v : vertices) {
        auto parent = ds.find_set(v);
        sets[parent].push_back(v);
    }

    // return just the values from the above
    std::vector<std::vector<std::string>> output(sets.size());
    std::transform(sets.begin(), sets.end(), output.begin(),
        [](const auto& key_val) {
            return key_val.second;
        }
    );

    return output;
}

int main()
{
    std::vector<std::tuple<std::string, std::string>> edges = {
       {"A" , "B"},
       {"D" , "E"},
       {"H" , "I"},
       {"K" , "J"},
       {"E" , "F"},
       {"B" , "C"},
       {"H" , "K"},
       {"E" , "G"},
       {"I" , "J"}
    };

    auto connected_components = find_connected_components(edges);
    for (const auto& cc : connected_components) {
        for (const auto& vertex : cc)
            std::cout << vertex;
        std::cout << "\n";
    }
}
HIKJ
ABC
DEFG