问题描述
我想生成的曲线图使用升压库这将允许用户能够输入边和顶点的数量。我基本上想要做的是,
I would like to generate a graph using boost library which would allow user to input the number of edges and vertex. What I basically want to do is,
- 我想用户输入的顶点和数量每个顶点的数量。
- 我想给用户一个特权选择一个顶点作为使用数字作为基准的主顶点。
- 我想用户在控制台指定,从每个顶点的边缘和边缘的数量可以是随机的。
是否有可能以某种方式实现这个使用BGL?如果是这样,一个例子是开始与一个伟大的事情。
Is it possible to somehow implement this using BGL? If so, an example would be a great thing to start with.
由于一吨提前,
干杯!
推荐答案
假设你知道一)基本的C ++,和b)基本BGL,这里有一个简单的算法来构建一个随机的无向图定顶点价:
Assuming you know a) basic C++, and b) basic BGL, here's a simple algorithm to build a random undirected graph with given vertex valencies:
-
读取所需的顶点数目;称之为
N
。我们有顶点集[0,N)
。
对于每个 I
在 [0,N)
,读取所需的化合价 v [I]
(将被存储在例如一个容器,如的std ::矢量< INT>
)。
For each i
in[0, N)
, read the desired valency v[i]
(to be stored for example in a container such as a std::vector<int>
).
现在最有趣的部分:遍历每个顶点只要你可以添加一个随机的边缘。下面是一些C ++ - 仿伪code,有间隙为您填写
Now the fun part: Iterate over each vertex and add a random edge as long as you can. Here's some C++-like pseudo code, with gaps for you to fill in.
for (i = 0; i != N; ++i)
{
if (i + 1 == N && v[i] > 0)
{
Error: The user input was impossible to satisfy
Abort program
}
while (v[i] > 0)
{
pick a random j in [i + 1, N)
if (v[j] > 0)
{
Add edge i <-> j
--v[i];
--v[j];
}
}
}
If we haven't aborted, we now have a random graph.
如果你想要的这一扩大任何部分发表评论。
Leave a comment if you want any part of this expanded.
更新:下面是一个例子实现。将有差距;它只是一个大纲。
Update: Here's an example implementation. There will be gaps; it's just an outline.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
int read_int(std::string const & initmsg, std::string const & repeatmsg)
{
std::cout << initmsg;
for (std::string line; std::getline(std::cin, line); )
{
std::istringstream iss(line);
int res;
if (iss >> res >> std::ws && iss.get() == EOF) { return res; }
std::cout << repeatmsg;
}
std::cerr << "Unexpected end of input! Aborting.\n";
std::exit(1);
}
std::string read_string(std::string const & msg)
{
std::string res;
std::cout << msg;
if (std::getline(std::cin, res)) { return res; }
std::cerr << "Unexpected end of input! Aborting.\n";
std::exit(1);
}
int main()
{
int const N = read_int("Number of vertices: ",
"I did not understand, try again. Number of vertices: ");
std::vector<unsigned int> valencies;
std::vector<std::string> vertex_names;
valencies.reserve(N);
vertex_names.reserve(N);
for (int i = 0; i != N; ++i)
{
std::string const msg1 = "Enter valency for vertex " + std::to_string(i) + ": ";
std::string const msg2 = "Enter description for vertex " + std::to_string(i) + ": ";
std::string const rep = "Sorry, say again: ";
valencies.push_back(read_int(msg1, rep));
vertex_names.push_back(read_string(msg2));
}
for (int i = 0; i != N; ++i)
{
std::cout << "Vertex " << i << " (\"" << vertex_names[i]
<< "\") has valency " << valencies[i] << std::endl;
}
// Now run the above algorithm!
}
这篇关于通过允许用户生成使用Boost库中的图形来选择顶点数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!