]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/graph/example/topo_sort.cpp
1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
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>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/graph/topological_sort.hpp>
18 typedef std::pair
< std::size_t, std::size_t > Pair
;
21 Topological sort example
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.
29 A topological ordering: 2 5 0 1 4 3
33 int main(int, char*[])
36 using namespace boost
;
38 /* Topological sort will need to color the graph. Here we use an
39 internal decorator, so we "property" the color to the graph.
41 typedef adjacency_list
< vecS
, vecS
, directedS
,
42 property
< vertex_color_t
, default_color_type
> >
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
51 for (std::size_t j
= 0; j
< 6; ++j
)
52 add_edge(edges
[j
].first
, edges
[j
].second
, G
);
54 Graph
G(edges
, edges
+ 6, 6);
57 boost::property_map
< Graph
, vertex_index_t
>::type id
58 = get(vertex_index
, G
);
60 typedef std::vector
< Vertex
> container
;
62 topological_sort(G
, std::back_inserter(c
));
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
;