views:

61

answers:

2

I'm rusty on C++ templates and I'm using the boost graph library (a fatal combination). I've searched the web and can't find any direct instructions on how to take a custom graph structure and fit enough of it to BGL (boost graph library) that I can use boosts graph traversing algorithms. Anyone familiar enough with the library to help me out?

EDIT: So, the main problem I've been having is where to find a source where the total requirements to map an arbitrary graph to a BGL graph. I'm really new to templates so it's hard for me to read BGL's specification/examples. Maybe I should look for a general source on templates?

+1  A: 

The approach, as I understand it, is to specialize the boost::graph_traits structure for your graph type. This configures BGL with various important properties it needs to know about your graph. You then specialize global template functions for your graph's specialized type of graph_traits to implement whatever boost graph interfaces might apply to your specific kind of graph.

An example is right there in the BGL documentation:

http://www.boost.org/doc/libs/1_43_0/libs/graph/doc/leda_conversion.html

There are links for several of the different interfaces there, which indicate which global template functions you'll need to specialize for your graph if you want to support that interface. The full list of interfaces is here:

http://www.boost.org/doc/libs/1_43_0/libs/graph/doc/graph_concepts.html

Owen S.
I have read the majority of BGL documentation that the site has with regards to getting the templating to work. However, if you're unfamiliar with LEDA the example you show is not trivial and is not well explained. If you look at their code it is almost completely devoid of comments. Every piece of code I've found on the boost graph site is almost completely devoid of comments, and for an object this generic that's quite discouraging.
Michael
It will help, then, if you indicate specific things that you're not clear on or specifics relating to your graph structure that are making the adaptation difficult.
Owen S.
fair enough, will put up an edit later today
Michael
+1  A: 

My suggestion would be to abandon use of BGL entirely unless you already have a significant amount of code written on top of it. I was testing it out recently for future use on a large graph analysis project, and I found it to be almost unusable due to an overly complex and poorly designed API.

There are no simple tasks in BGL, only complex ones, and I was constantly fighting the compiler due to the excessively complicated template hierarchy that BGL has. Little to no useful documentation (at least not where it's really needed) and not enough examples only aggravate matters. That's no way to write code.

I'd recommend switching to LEMON. It's stable, written in C++, easy-to-understand and code in, offers several specialized forms of graphs to support different usage needs, and it supports both BFS and DFS search/visitor functions. It also has its own equivalent of property maps for nodes/edges, so you should be able to fit your own graph structure and other data onto it.

Try LEMON; it tastes a lot better and will cause fewer ulcers. ;-)

Joel Hoff
I just finished testing LEMON on a graph with 1 million nodes and 100 million edges; scaled nicely with no performance issues, etc.
Joel Hoff
Thanks for the idea! Unfortunately I'm working with a LARGE codebase and I don't think my bosses want another dependency :S
Michael
Oh, and good to hear I'm not the only one that thinks BGL is utterly complex and too general and that the examples aren't particularly revealing!
Michael