Skip to content

Instantly share code, notes, and snippets.

@sturmer sturmer/main.cpp
Created Nov 16, 2013

What would you like to do?
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;

This comment has been minimized.

Copy link
Owner Author

sturmer commented Nov 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.