我想在我的图表中添加随机边,如下所示:
#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个值 . 我怎样才能做到这一点?
1 回答
EDIT: 我说"ordered pair"当我的意思"unordered pair"!希望现在的改述更加清晰 .
您需要做的是从<N的所有无序非整数对的集合中替换样本 . 由于计算机更容易表示有序对而不是无序对,因此生成此集合的常用方法是生成第一个元素小于第二个元素的所有有序对:
所以例如如果N = 4,要考虑的可能边集合为:
一旦你拥有它,从这个集合中采样的一个好方法是使用reservoir sampling .