]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/graph/example/vertex_basics.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 //=======================================================================
10 #include <boost/config.hpp>
13 #include <boost/graph/adjacency_list.hpp>
16 using namespace boost
;
22 This example demonstrates the GGCL Vertex interface.
26 vertices(g) = 0 1 2 3 4
28 out-edges: (0,1) (0,2) (0,3) (0,4)
29 in-edges: (2,0) (3,0) (4,0)
30 adjacent vertices: 1 2 3 4
34 in-edges: (0,1) (3,1) (4,1)
38 out-edges: (2,0) (2,4)
40 adjacent vertices: 0 4
43 out-edges: (3,0) (3,1) (3,4)
45 adjacent vertices: 0 1 4
48 out-edges: (4,0) (4,1)
49 in-edges: (0,4) (2,4) (3,4)
50 adjacent vertices: 0 1
56 /* some helper functors for output */
58 template <class Graph
>
60 print_edge(Graph
& g
) : G(g
) { }
62 typedef typename
boost::graph_traits
<Graph
>::edge_descriptor Edge
;
63 typedef typename
boost::graph_traits
<Graph
>::vertex_descriptor Vertex
;
64 void operator()(Edge e
) const
66 typename
boost::property_map
<Graph
, vertex_index_t
>::type
67 id
= get(vertex_index
, G
);
69 Vertex src
= source(e
, G
);
70 Vertex targ
= target(e
, G
);
72 cout
<< "(" << id
[src
] << "," << id
[targ
] << ") ";
78 template <class Graph
>
80 print_index(Graph
& g
) : G(g
){ }
82 typedef typename
boost::graph_traits
<Graph
>::vertex_descriptor Vertex
;
83 void operator()(Vertex c
) const
85 typename
boost::property_map
<Graph
,vertex_index_t
>::type
86 id
= get(vertex_index
, G
);
94 template <class Graph
>
95 struct exercise_vertex
{
96 typedef typename
boost::graph_traits
<Graph
>::vertex_descriptor Vertex
;
98 exercise_vertex(Graph
& _g
) : g(_g
) { }
100 void operator()(Vertex v
) const
102 typename
boost::property_map
<Graph
, vertex_index_t
>::type
103 id
= get(vertex_index
, g
);
105 cout
<< "vertex id: " << id
[v
] << endl
;
107 cout
<< "out-edges: ";
108 for_each(out_edges(v
, g
).first
, out_edges(v
,g
).second
,
109 print_edge
<Graph
>(g
));
113 cout
<< "in-edges: ";
114 for_each(in_edges(v
, g
).first
, in_edges(v
,g
).second
,
115 print_edge
<Graph
>(g
));
119 cout
<< "adjacent vertices: ";
120 for_each(adjacent_vertices(v
,g
).first
,
121 adjacent_vertices(v
,g
).second
, print_index
<Graph
>(g
));
122 cout
<< endl
<< endl
;
132 typedef adjacency_list
<vecS
,vecS
,bidirectionalS
> MyGraphType
;
134 typedef pair
<int,int> Pair
;
135 Pair edge_array
[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4),
136 Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1),
137 Pair(3,4), Pair(4,0), Pair(4,1) };
139 /* Construct a graph using the edge_array*/
141 for (int i
=0; i
<11; ++i
)
142 add_edge(edge_array
[i
].first
, edge_array
[i
].second
, g
);
144 boost::property_map
<MyGraphType
, vertex_index_t
>::type
145 id
= get(vertex_index
, g
);
147 cout
<< "vertices(g) = ";
148 boost::graph_traits
<MyGraphType
>::vertex_iterator vi
;
149 for (vi
= vertices(g
).first
; vi
!= vertices(g
).second
; ++vi
)
150 std::cout
<< id
[*vi
] << " ";
151 std::cout
<< std::endl
;
153 /* Use the STL for_each algorithm to "exercise" all
154 of the vertices in the graph */
155 for_each(vertices(g
).first
, vertices(g
).second
,
156 exercise_vertex
<MyGraphType
>(g
));