Skip to content

Instantly share code, notes, and snippets.

@sturmer
Created November 16, 2013 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturmer/7498715 to your computer and use it in GitHub Desktop.
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.
#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;
}
@sturmer
Copy link
Author

sturmer commented Nov 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment