在使用 BOOST 图形库生成的图形中添加随机边
Adding random edges in a graph generated using BOOST graphical library
我想在我的图中添加随机边缘,如下所示:
#include <iostream>
#include <utility> // for std::pair
#include <algorithm>
#include <boost/graph/adjacency_list.hpp>
#include "boost/graph/topological_sort.hpp"
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/graphviz.hpp>
int main()
{
using namespace std;
using namespace boost;
typedef adjacency_list< listS, vecS, undirectedS > undigraph;
int const N = read_int_from_user("Number of vertices: ");
undigraph g(N);
// add some edges, // #1
for (int i = 0; i != N; ++i)
{
for (int j = 0; j != N; ++j)
{
add_edge(i, j, g);
}
}
write_graphviz(cout, g);
}
#1
后面的行就是这样做的。
8 条边,但我希望最多只有 4 条,并希望以随机方式连接所有顶点,最重要的是每个顶点只能有 4 条价。我怎样才能做到这一点?
编辑:当我的意思是"无序对"时,我说"有序对"! 希望改写现在更清楚。
您需要做的是从 N <的所有>无序非负整数对的集合中进行采样,而无需替换。 由于计算机表示有序对比表示无序对容易得多,因此生成此集合的常用方法是生成第一个元素小于第二个元素的所有有序对:的所有>
vector<pair<int, int> > pairs;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
pairs.push_back(make_pair(i, j));
}
}
因此,例如,如果N = 4,则要考虑的可能边集为:
0, 1
0, 2
0, 3
1, 2
1, 3
2, 3
一旦有了这个集合,一个很好的方法是使用水库采样。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- 通过相邻顶点列表添加和减去图形中的边
- 制作完成后是否可以将节点添加到图形中
- CMAKE:在多图形/二进制C 项目中添加通用标头的IDE可见性
- 巨大的内存占用,同时向图形添加边缘
- 在 Boost.Graph 中向图形添加边
- 在使用 BOOST 图形库生成的图形中添加随机边
- 添加可绘制对象层以将逻辑与图形分离
- 如何向C++添加图形
- 使用邻接列表将顶点添加到图形
- 动态添加到图形数据结构中
- 图形在borland c++中添加
- 添加纹理到QT OpenGL场景图形