Boost Disjoint 集:如何检索集?
Boost Disjoint sets: How to retrieve sets?
我正在尝试使用 Boost 中的不相交集,但是在浏览了一整天的 Whosebug 和文档之后,我无法解决我的问题。
主要问题是:给定一些将充当 Rank 和 Parent 的 int 元素类型的映射(尽管将来该元素类型需要是指向实际对象的指针),并且在做了一些 union_set
s,得到一个向量的向量:每个外部向量是一个连通分量数,每个内部向量都是组成该连通分量的点(或指针)列表。
例如:v[1] -> [0, 30, 234, ...]
.
我查看了 this, this and this + SO 中的其他几个问题,每个结果都出现在 google 的首页。
我使用用户@janoma 的代码创建了一个小示例。然而,他的回答虽然非常好,但 "too customized" 满足了他的需求,在修改了一段时间后,我看不出如何调整他的代码以适应 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 不相交集。
我对所讨论的代码究竟试图做什么感到困惑,但我可以回答一般性问题“您如何从 boost 的 disjoint_sets 实现中检索集合?”基本上,您使用由数据结构构建的 parent 映射。
disjoints_sets数据结构将每个集合表示为集合中任意成员的“children”,称这些任意成员为集合代表。 Boost 库构建的 parent 属性 映射将每个元素与其所属集合的代表相关联。然后要构建实际的集合,我们需要从根本上反转 parent 映射。我们迭代 parent 映射构建从代表到元素的映射,这是一个 one-to-many 关系,因此该映射具有元素向量作为值。该地图的值将是我们正在寻找的集合。
我使用字符串作为元素只是为了在 Whosebug 上获得一个不使用整数项的示例,并且是完整的。请注意,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
我正在尝试使用 Boost 中的不相交集,但是在浏览了一整天的 Whosebug 和文档之后,我无法解决我的问题。
主要问题是:给定一些将充当 Rank 和 Parent 的 int 元素类型的映射(尽管将来该元素类型需要是指向实际对象的指针),并且在做了一些 union_set
s,得到一个向量的向量:每个外部向量是一个连通分量数,每个内部向量都是组成该连通分量的点(或指针)列表。
例如:v[1] -> [0, 30, 234, ...]
.
我查看了 this, this and this + SO 中的其他几个问题,每个结果都出现在 google 的首页。
我使用用户@janoma 的代码创建了一个小示例。然而,他的回答虽然非常好,但 "too customized" 满足了他的需求,在修改了一段时间后,我看不出如何调整他的代码以适应 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 不相交集。
我对所讨论的代码究竟试图做什么感到困惑,但我可以回答一般性问题“您如何从 boost 的 disjoint_sets 实现中检索集合?”基本上,您使用由数据结构构建的 parent 映射。
disjoints_sets数据结构将每个集合表示为集合中任意成员的“children”,称这些任意成员为集合代表。 Boost 库构建的 parent 属性 映射将每个元素与其所属集合的代表相关联。然后要构建实际的集合,我们需要从根本上反转 parent 映射。我们迭代 parent 映射构建从代表到元素的映射,这是一个 one-to-many 关系,因此该映射具有元素向量作为值。该地图的值将是我们正在寻找的集合。
我使用字符串作为元素只是为了在 Whosebug 上获得一个不使用整数项的示例,并且是完整的。请注意,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