Created
November 16, 2013 10:41
-
-
Save sturmer/7498715 to your computer and use it in GitHub Desktop.
Basic example with Boost Graph Library, a slight variation on the book's introductory example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <utility> | |
#include <boost/graph/graph_traits.hpp> | |
#include <boost/graph/adjacency_list.hpp> | |
#include <boost/graph/dijkstra_shortest_paths.hpp> | |
using namespace boost; | |
int main(int,char*[]) | |
{ | |
// create a typedef for the Graph type | |
typedef adjacency_list<vecS, vecS, bidirectionalS> Graph; | |
// Make convenient labels for the vertices | |
enum { _1, _2, _3, _4, _5, _6, N }; | |
const int num_vertices = N; | |
const char* name = "CLRS_22.4"; | |
// writing out the edges in the graph | |
typedef std::pair<int, int> Edge; | |
Edge edge_array[] = { | |
Edge(_1,_2), Edge(_1,_4), Edge(_2,_5), Edge(_4,_2), | |
Edge(_5,_4), Edge(_3,_5), Edge(_3,_6), Edge(_6,_6) | |
}; | |
const int num_edges = sizeof(edge_array)/sizeof(Edge); | |
Graph g(edge_array, edge_array + num_edges, num_vertices); | |
// get the property map for vertex indices | |
typedef property_map<Graph, vertex_index_t>::type IndexMap; | |
IndexMap index = get(vertex_index, g); | |
std::cout << "vertices(g) = "; | |
typedef graph_traits<Graph>::vertex_iterator vertex_iter; | |
std::pair<vertex_iter, vertex_iter> vp; | |
for (vp = vertices(g); vp.first != vp.second; ++vp.first) | |
std::cout << index[*vp.first] << " "; | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The original example is at the url:
http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/quick_tour.html