[英]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
從該集合中采樣的一種好方法是使用儲層采樣 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.