• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9 #include <boost/config.hpp>
10 #include <iostream>
11 #include <list>
12 #include <algorithm>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/graph/topological_sort.hpp>
15 #include <iterator>
16 #include <utility>
17 
18 typedef std::pair< std::size_t, std::size_t > Pair;
19 
20 /*
21   Topological sort example
22 
23   The topological sort algorithm creates a linear ordering
24   of the vertices such that if edge (u,v) appears in the graph,
25   then u comes before v in the ordering.
26 
27   Sample output:
28 
29   A topological ordering: 2 5 0 1 4 3
30 
31 */
32 
main(int,char * [])33 int main(int, char*[])
34 {
35     // begin
36     using namespace boost;
37 
38     /* Topological sort will need to color the graph.  Here we use an
39        internal decorator, so we "property" the color to the graph.
40        */
41     typedef adjacency_list< vecS, vecS, directedS,
42         property< vertex_color_t, default_color_type > >
43         Graph;
44 
45     typedef boost::graph_traits< Graph >::vertex_descriptor Vertex;
46     Pair edges[6] = { Pair(0, 1), Pair(2, 4), Pair(2, 5), Pair(0, 3),
47         Pair(1, 4), Pair(4, 3) };
48 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
49     // VC++ can't handle the iterator constructor
50     Graph G(6);
51     for (std::size_t j = 0; j < 6; ++j)
52         add_edge(edges[j].first, edges[j].second, G);
53 #else
54     Graph G(edges, edges + 6, 6);
55 #endif
56 
57     boost::property_map< Graph, vertex_index_t >::type id
58         = get(vertex_index, G);
59 
60     typedef std::vector< Vertex > container;
61     container c;
62     topological_sort(G, std::back_inserter(c));
63 
64     std::cout << "A topological ordering: ";
65     for (container::reverse_iterator ii = c.rbegin(); ii != c.rend(); ++ii)
66         std::cout << id[*ii] << " ";
67     std::cout << std::endl;
68 
69     return 0;
70 }
71