通过允许用户选择顶点数量,使用Boost库生成图形
Generating a Graph using Boost library by allowing user to select the number of vertex
我想使用boost库生成一个图,该库允许用户输入边和顶点的数量。我基本上想做的是,
- 我希望用户输入顶点的数量和每个顶点的数量
- 我会给用户一个特权,使用数字作为参考,选择一个顶点作为主顶点
- 我希望用户在控制台中指定每个顶点的边数,并且这些边可以是随机的
是否可以使用BGL以某种方式实现这一点?如果是这样的话,一个例子将是一个很好的开始。
提前感谢
干杯!!
假设你知道a)基本C++和b)基本BGL,这里有一个简单的算法来构建一个具有给定顶点价的随机无向图:
-
读取所需顶点的数量;称之为CCD_ 1。我们得到了顶点集CCD_ 2。
-
对于
[0, N)
中的每个i
,读取所需的价态v[i]
(例如存储在诸如std::vector<int>
的容器中)。 -
现在有趣的部分是:在每个顶点上迭代,并尽可能长地添加一条随机边。这里有一些类似C++的伪代码,有一些空白需要您填写
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.
如果你想扩展其中的任何部分,请留下评论。
更新:下面是一个示例实现。会有缺口;这只是一个大纲。
#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::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 使用 boost 进行标记化会给出相同的输出
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- 执行时使用 boost::asio::d eadline_timer 时出错
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 避免使用 boost::进程间::消息队列创建文件
- 使用Boost文件系统C++将具有特定扩展名的文件的名称保存在特定文件夹中
- 程序崩溃使用boost::asio
- 如何使用 boost beast websocket 客户端收听 websocket feed?
- 在 Julia 中使用 boost 库和 Windows 上的 Cxx.jl
- 如何使用 boost 将流放入缓冲区
- 使用boost::multiprecision cpp_int左移时出现超时错误
- 使用 boost::p rogram_options 指定多个标志
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- yum :在 CentOS 上使用 Boost 1.69 而不是默认 (1.53) 版本
- 使用 boost::p roperty_tree::p tree 如何获取特定键的值
- 如何在使用 Boost MSM 编写的状态机中直接访问任何状态