- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用Boost中的不相交集,但是,在整日浏览了StackOverflow和文档之后,我无法解决问题。
主要问题是:给定一些将用作Rank和Parent的映射,其类型为int元素类型(尽管将来该元素类型需要作为指向实际对象的指针),并在执行了union_set
之后,获得一个 vector vector :每个外部 vector 是一个连接的组件编号,每个内部 vector 都是组成该连接的组件的点(或指针)列表。
例如:v[1] -> [0, 30, 234, ...]
。
我在SO中查看了this,this和this +其他几个问题,并在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);
}
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 的值将是我们正在寻找的集合。
下面的代码。以下内容可在以下位置找到连接的组件
我使用字符串作为元素只是为了在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/
我正在尝试使用boost.spirit的qi库解析某些内容,而我遇到了一个问题。根据spirit docs,a >> b应该产生类型为tuple的东西。但这是boost::tuple(又名 fusio
似乎有/正在努力做到这一点,但到目前为止我看到的大多数资源要么已经过时(带有死链接),要么几乎没有信息来实际构建一个小的工作样本(例如,依赖于boost program_options 以构建可执行文
我对 Boost.Log 的状态有点困惑。这是 Boost 的官方部分,还是尚未被接受?当我用谷歌搜索时,我看到一些帖子谈论它在 2010 年是如何被接受的,等等,但是当我查看最后一个 Boost 库
Boost 提供了两种不同的实现 string_view ,这将成为 C++17 的一部分: boost::string_ref在 utility/string_ref.hpp boost::stri
最近,我被一家GIS公司雇用来重写他们的旧地理信息库。所以我目前正在寻找一个好的计算几何库。我看过CGAL,这真是了不起,但是我的老板想要免费的东西。 所以我现在正在检查Boost.Geometry。
假设我有一个无向图 G。假设我添加以下内容 add_edge(1,2,G); add_edge(1,3,G); add_edge(0,2,G); 现在我再说一遍: add_edge(0,2,G); 我
我使用 CMake 来查找 Boost。找到了 Boost,但 CMake 出错了 Imported targets not available for Boost version 请参阅下面的完整错
我是 boost::fusion 和 boost::mpl 库的新手。谁能告诉我这两个库之间的主要区别? 到目前为止,我只使用 fusion::vector 和其他一些简单的东西。现在我想使用 fus
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What are the benefits of using Boost.Phoenix? 所以我开始阅读 boos
我正在尝试获得一个使用 Boost.Timer 的简单示例,用于一些秒表性能测量,但我不明白为什么我无法成功地将 Boost.Timer 链接到 Boost.Chrono。我使用以下简单脚本从源代码构
我有这样的东西: enum EFood{ eMeat, eFruit }; class Food{ }; class Meat: public Food{ void someM
有人可以告诉我,我如何获得boost::Variant处理无序地图? typedef boost::variant lut_value;unordered_map table; 我认为有一个用于boo
我对 Boost.Geometry 中的环和多边形感到困惑。 在文档中,没有图形显示什么是环,什么是多边形。 谁能画图解释两个概念的区别? 最佳答案 在 Boost.Geometry 中,多边形被定义
我正在使用 boost.pool,但我不知道何时使用 boost::pool<>::malloc和 boost::pool<>::ordered_malloc ? 所以, boost::pool<>:
我正在尝试通过 *boost::fast_pool_allocator* 使用 *boost::container::flat_set*。但是,我收到编译错误。非常感谢您的意见和建议。为了突出这个问题
sau_timer::sau_timer(int secs, timerparam f) : strnd(io), t(io, boost::posix_time::seconds(secs)
我无法理解此功能的文档,我已多次看到以下内容 tie (ei,ei_end) = out_edges(*(vi+a),g); **g**::out_edge_iterator ei, ei_end;
我想在 C++ 中序列化分层数据结构。我正在处理的项目使用 boost,所以我使用 boost::property_tree::ptree 作为我的数据节点结构。 我们有像 Person 这样的高级结
我需要一些帮助来解决这个异常,我正在实现一个 NPAPI 插件,以便能够使用来自浏览器扩展的本地套接字,为此我正在使用 Firebreath 框架。 对于套接字和连接,我使用带有异步调用的 Boost
我尝试将 boost::bind 与 boost::factory 结合使用但没有成功 我有这个类 Zambas 有 4 个参数(2 个字符串和 2 个整数)和 class Zambas { publ
我是一名优秀的程序员,十分优秀!