gpt4 book ai didi

c++ - boost 不相交集:如何检索集?

转载 作者:行者123 更新时间:2023-12-01 14:58:56 25 4
gpt4 key购买 nike

我正在尝试使用Boost中的不相交集,但是,在整日浏览了StackOverflow和文档之后,我无法解决问题。

主要问题是:给定一些将用作Rank和Parent的映射,其类型为int元素类型(尽管将来该元素类型需要作为指向实际对象的指针),并在执行了union_set之后,获得一个 vector vector :每个外部 vector 是一个连接的组件编号,每个内部 vector 都是组成该连接的组件的点(或指针)列表。

例如:v[1] -> [0, 30, 234, ...]

我在SO中查看了thisthisthis +其他几个问题,并在Google的首页上找到了每个结果。

我使用用户@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);
}

如果执行 janoma's code,则预期结果将是您获得的最后一部分,即:
Alternative, using iterators:
Sorted set: 1 8 12 16 23 27 32 37 46 46 50 55 60 62 69 73 76 79 87
Sorted set: 23 36
Sorted set: 62
Sorted set: 13 25 25 52 67 69 71 80

实际结果是,我并没有将其分成单独的列表,但是:
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的disjoint_sets实现中检索这些集合?”基本上,您使用由数据结构构造的父映射。
disjoints_sets数据结构将每个集合表示为集合中任意成员的“子代”,称这些任意成员为集合的代表。 Boost库构建的父属性映射将每个元素与其所属的集合的代表相关联。为了构建实际的集合,我们需要本质上反转父图。我们遍历父映射,以构建从代表到元素的映射,这是一对多的关系,因此此映射将元素的 vector 作为值。该 map 的值将是我们正在寻找的集合。
下面的代码。以下内容可在以下位置找到连接的组件
enter image description here
我使用字符串作为元素只是为了在StackOverflow上获得一个示例,该示例不使用整数项,并且操作很完整。注意get_unique_vertices函数只是该代码设计的产物,该代码仅使用边作为输入来构建图林。如果您已经知道顶点或通过使用disjoint_sets数据结构本身来跟踪它们,则无需执行此步骤即可执行以下操作。我这样做是为了使disjoint_sets的实际用法尽可能简洁:

#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

关于c++ - boost 不相交集:如何检索集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54519613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com