0

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,

  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? If so, an example would be a great thing to start with.

Thanks a ton in advance,

Cheers!!

Spaniard89
  • 2,359
  • 4
  • 34
  • 55
  • You don't even need BGL for that, it would seem. – Kerrek SB Sep 15 '12 at 18:33
  • Is the only goal here to construct a random graph with specified vertex valencies? – Kerrek SB Sep 15 '12 at 18:36
  • @KerrekSB No, generating the graph is just an initial step, would perform various search algorithms on it staring from the master node. IS there an example or something online that matches my criteria? – Spaniard89 Sep 16 '12 at 08:09
  • The algorithm is not too hard to come up with. Do you just want a high-level description, or do you need help with the basic C++ as well? – Kerrek SB Sep 16 '12 at 12:03
  • @KerrekSB I want to generate the graph with described requirements, it is basically to generate the network topology. I am new to C++ and BGL both. I just need an example for the initial state to start with and I want to use BGL to work on the generated topology graph. Thanks a lot for your reply and help. – Spaniard89 Sep 16 '12 at 19:17

1 Answers1

2

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:

  1. Read the number of desired vertices; call it N. We have the vertex set [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>).

  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.

    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!
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • It would be really helpful if you could expand the example of yours above, so that I can run it. I am too new in programming and I once got the code, I would try to figure out things in it and it would really help me. Thanks a lot in advance. – Spaniard89 Sep 17 '12 at 06:58
  • I got this code from the examples provided in the boost library: Once i enter the number of vertices, how would i assign numbers or alphabet to each vertex? something like this does it in BGL:`enum { A, B, C, D, E, N }; const int num_vertices = N; const char* name = "ABCDE";` But in this the vertex are predefined as well the edges. How would achieve it in an random way? – Spaniard89 Sep 17 '12 at 13:08
  • @Kishorepandey: Wait, I'll be making an example. Vertex labels are an irrelevant detail. Just consider each vertex to be a number in `[0, N)`. – Kerrek SB Sep 17 '12 at 13:46
  • Ok mate. I will wait. Thanks for helping and replying. – Spaniard89 Sep 17 '12 at 13:55
  • @Kishorepandey: OK, I've added an example framework that reads in the number of vertices (`N`) and the valencies and names for each vertex. Now over to you! – Kerrek SB Sep 17 '12 at 15:05
  • Thanks a lot mate. But I am getting an error: error C2039: 'to_string' : is not a member of 'std'. How can I solve this? – Spaniard89 Sep 17 '12 at 20:10
  • 1
    @Kishorepandey: `to_string` is new in C++11, so you need a modern compiler (e.g. VC10 or VC11). Alternatively, grab Boost and use `boost::lexical_cast`. Anyway, that's just for the message, not very important. – Kerrek SB Sep 17 '12 at 20:31
  • How can i View the graph that is generated? and I are the edges added in the above code? Do I have to add them? I would really appreciate if you could help me! – Spaniard89 Sep 18 '12 at 10:03
  • How would I pick up a random J from [i+1, N)? which c++ method can i possibly use? and How would i add edges? Which function in C++ does that? or do I have to use BOOST in this case? – Spaniard89 Sep 18 '12 at 13:45
  • @Kishorepandey: To make the graph, add edges to your own adjacency matrix or list; and to view it, just print out that matrix or list in any way you like. Random number generation is standard; search the internet for one minute to find out how to do it. – Kerrek SB Sep 18 '12 at 14:47
  • I have a small doubt, what if I don't give my valencies as input. How would the above algorithm look like? And I still don't want to have more than four edges from each vertex. I tried your algorithm, by generating a random number and adding the edges, but wouldn't it create different graph with different run? What if I want to create a graph and run it multiple times with the same output. What should i modify in the above algorithm of yours? Thanks a ton in advance. – Spaniard89 Sep 19 '12 at 14:38
  • @Kishorepandey: I don't think you're being clear about what you want. Do you want a random graph, or do you *expect* the user input to determine a unique graph? And why "at most 4", when your question says "the number of edges can be random"? – Kerrek SB Sep 19 '12 at 14:50
  • I was not very clear in my question that I posted before, and hence I will do one thing, I will post another question with exactly the requirements of mine. And I would explain it there why precisely i want only 4 edges. AS in the comment, i am limited to few hundreds of charters, I cannot make myself clear over here :) – Spaniard89 Sep 19 '12 at 18:40
  • I have posted another question over here http://stackoverflow.com/questions/12501403/creating-a-graph-using-boost-and-saving-it-in-grpah-viz-format-while-allowing-u – Spaniard89 Sep 19 '12 at 19:19
  • @Kishorepandey: Good, a separate question is definitely the way to go. See you there! :-) – Kerrek SB Sep 19 '12 at 20:09