The Generic Graph Component Library by Jeremy G. Siek, Lie-Quan Lee, and Andrew Lumsdaine Listing One depth_first_search(Graph G, Color color) { for each vertex u in vertices(G) { color[u] = white; visit(u, color); } } visit(u, Color color) { color[u] = gray; for each vertex v in adj(u) { if (color[v] = white) visit(v); } color[u] = black; } Listing Two dijkstra_shortest_paths(Graph G, Vertex s, Distance d, Weight w, Parent p) { PriorityQueue Q; push(s, Q); while (Q is not empty) { vertex u = pop(Q); for each edge e=(u,v) in out-edges(u) if (d[v] > d[u] + w(e)) p[v] = u; } } Listing Three bellman_ford_shortest_paths(Graph G, Weight w, Distance d, Parent p) { for k = 0...num_vertices(G) for each edge e=(u,v) in edges(G) if (relax(e, g, w, d)) p[v] = u; for each each e=(u,v) in edges(g) if (d[u] + w[e] < d[v]) return false; } Listing Four extend_shortest_paths(AdjacencyMatrix G, Distance d, Weight w) { for (i=1...N) for (j=1...N) for (k=1..N) { edge e_ij = G(i,j), e_ik = G(i,k), e_kj = G(k,j); d[e_ij] = min( d[e_ij], d[e_ik] + w[e_kj]; } } Listing Five // WRONG! Overspecified requirements template void copy(RandomAccessIter1 first, RandomAccessIter1 last, RandomAccessIter2 result) { while (first != last) *first++ = *result++; } // Just right template void copy(InputIterator first, InputIterator last, OutputIterator result) { while (first != last) *first++ = *result++; } Listing Six template void graph_search(IncidenceGraph& g, Vertex s, Bag& bag, Visitor visitor) { Vertex u; typename graph_traits::incidence_iterator i, end; visitor.start(s); bag.push(s); while (! bag.empty()) { u = bag.top(); if (visitor.is_undiscovered(u)) { visitor.discover(u, bag); for (tie(i,end) = out_edges(u,g); i != end; ++i) visitor.process(*i, g, bag); } else { visitor.finish(u); bag.pop(); } } } Listing Seven template void breadth_first_search(IncidenceGraph& g, Vertex s, Color color, Visitor visitor) { boost::queue q; graph_search(g, s, q, visit_color(color, visitor)); } template void depth_first_search(VertexListGraph& g, Color color, Visitor visitor) { std::stack std; typename graph_traits::vertex_iterator i, end; for (tie(i,end) = vertices(g); i != end; ++i) graph_search(g, *i, stk, visit_color(color, visitor)); } Listing Eight template < class Color, class Super = null_visitor> class coloring_visitor : public Super { // constructors and typedefs ... template void initialize(Vertex u) { set(color, u, color_tr::white()); Super::initialize(u); } template void discover(Vertex u, Bag& bag) { set(color, u, color_tr::gray()); Super::discover(u, bag); } template void finish(Vertex u) { if (get(color, u) != color_tr::black()) { set(color, u, color_tr::black()); Super::finish(u); } } template bool process(Edge e, Graph& g, Bag& bag) { typedef typename graph_traits::vertex_descriptor Vertex; Vertex v = target(e, g); if ( is_undiscovered(v) ) { bag.push(v); Super::process(e, g, bag); return true; } else if ( FocusOnEdge ) Super::process(e, g, bag); return false; } template bool is_undiscovered(Vertex u) { return (get(color,u) == color_tr::white()); } protected: Color color; }; // Helper class for creating color visitors template coloring_visitor visit_color(Color c, Super b) { return coloring_visitor(c, b); } Listing Nine template void topological_sort( VertexListGraph& G, OutputIter result, Color color, Visitor visitor) { topo_sort_visitor topo_visit(c, visitor); depth_first_search(G, topo_visit, color); } template struct topo_sort_visitor : public Super { //constructors ... template void finish(Vertex u) { *result = u; ++result; Super::finish(u); } OutputIterator result; }; Listing Ten #include // ... typedef std::vector< std::list > Graph; Graph g(N); // fill the graph... std::vector color(N, WHITE), discover(N), finish(N); depth_first_search(g, color.begin(), visit_time(discover.begin(), finish.begin())); 4