简体   繁体   English

使用Boost库生成图表,允许用户选择顶点数

[英]Generating a Graph using Boost library by allowing user to select the number of vertex

I would like to generate a graph using boost library which would allow user to input the number of edges and vertex. 我想使用boost库生成一个图形,允许用户输入边数和顶点数。 What I basically want to do is, 我基本上想做的是,

  1. I would want the user to input the number of vertices and number each vertex. 我希望用户输入顶点的数量并为每个顶点编号。
  2. I would give the user a privilege to select a vertex as a master vertex using the numeral as a reference. 我将授予用户使用数字作为参考选择顶点作为主顶点的权限。
  3. I would like the user to specify in the console, number of edges from each vertex and the edges can be random. 我希望用户在控制台中指定每个顶点的边数和边可以是随机的。

Is it possible to somehow implement this using BGL? 是否有可能以某种方式使用BGL实现这一点? If so, an example would be a great thing to start with. 如果是这样,一个例子将是一个伟大的事情开始。

Thanks a ton in advance, 非常感谢,提前,

Cheers!! 干杯!!

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: 假设你知道a)基本的C ++,和b)基本的BGL,这里有一个简单的算法来构建一个给定顶点效价的随机无向图:

  1. Read the number of desired vertices; 读取所需顶点的数量; call it N . 叫它N We have the vertex set [0, N) . 我们有顶点集[0, N)

  2. 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> ). 对于[0, N)每个i ,读取所需的化合价v[i] (例如存储在诸如std::vector<int>的容器中)。

  3. 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. 这里有一些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. 

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!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM