Boost logo

Boost Users :

Subject: [Boost-users] [graph] template beginner question: How to create a template parameter option
From: Christoph (c_p_at_[hidden])
Date: 2011-10-07 14:27:36


Hi,

i would like to create a generic function, that returns
a random bipartite graph like this:

enum graph_type {bipartite, none_bipartite};

template <typename Graph>
Graph generate_random_graph (unsigned int num_vertices, graph_type t,
double density)
{
    if (density > 1) {
        std::cout << "value of parameter density in Function
generate_random_graph is out of range (0.0 - 1): " << density <<
std::endl;
        exit(-1);
    }
    if (t == bipartite)
    {
        Graph g(2*num_vertices);
        for (int i = 0; i < num_vertices; ++i)
            for (int j = 0; j < num_vertices; ++j)
                if (std::rand()%100 <= 100*density)
                    add_edge (i, j+num_vertices, g);
        return g;
    }
    else return Graph(99); // dummy part
}
@

Then i realized that the option parameter 'graph_type' in
'generate_random_graph' should be a template parameter, too. Like the
way we use a template parameter 'boost::undirectedS' in

typedef boost::adjacency_list < boost::listS,
                        boost::vecS,
                        boost::undirectedS>
my_graph;

I don't know where to start from here. I tried to find out the way the
'boost::undirectedS' thing works but i was lost in code jungle.

Any assistance is welcome. Maybe you know a nice reference for me to
start. If you have any other criticism, please do not hide, i am eager
to learn.

bye
Christoph


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net