Boost adjacency_list help needed
-
03-07-2019 - |
Question
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.
Solution
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.