Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] template beginner question: How to create a template parameter option
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2011-10-07 17:50:00


On Fri, 7 Oct 2011, Christoph wrote:

> 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.

Do you want something like a selector (like boost::undirectedS is)? In
that case, look at
<URL:http://www.boost.org/doc/libs/1_47_0/libs/graph/doc/using_adjacency_list.html#sec:custom-storage>
for how BGL does it.

-- Jeremiah Willcock


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