Monday, April 25, 2011

Boost adjacency_list help needed

I'm trying to use Boost's adjacency_list type and I'm having trouble understanding the documentation.

Say I define a class named State and I instantiate one instance for each state in the USA:

class State { ... };
State california, oregon, nevada, arizona, hawaii, ...

I want to enter these into a boost::adjacency_list the vertices are states and the edges are borders. For the states I listed above, I think the graph would have this data:

california : oregon, nevada, arizona
hawaii :
oregon : california, nevada
nevada : oregon, california, arizona
arizona : california, nevada

I understand how to put ints into the graph and I considered just making an array of states and inserting their array index into the graph, but it seems like I should be able to just say:

add_edge(california, oregon, graph);

but of course that doesn't work. Please help!

Edit:
Here's an example of almost exactly what I need.

From stackoverflow
  • Reading up on boost::adjacency_list, it appears you are supposed to use properties for the vertices rather than something like a class:

    struct VertexProperties {
        std::string stateName;
    };
    
    typedef adjacency_list<listS, listS, bidirectionalS, VertexProperties> Graph;
    Graph adjacentStates(50);
    
    property_map<Graph, std::string VertexProperties::*>::type
        stateName = get(&VertexProperties::stateName, adjacentStates);
    
    add_edge(vertex("california", adjacentStates), vertex("oregon", adjacentStates), adjacentStates);
    

    (Poorly) adapted from an example in boost.

    criddell : Oy! It's worse than I thought. I think I'll find a different way to do this. I'd don't want to have to maintain code that... clever. Thanks for the answer.
    BenoƮt : Please add VertexProperties as Graph's 4th template parameter. That means you'll have to add bidirectionalS as 3rd.
    sixlettervariables : Thanks, i've made those changes.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.