[graph] template beginner question: How to create a template parameter option

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

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... for how BGL does it. -- Jeremiah Willcock

[...]
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... for how BGL does it.
-- Jeremiah Willcock
This is exactly what i was looking for. Thank you very much for this one! -- Christoph
participants (2)
-
Christoph
-
Jeremiah Willcock