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 | |
---|
10 | #include <boost/config.hpp> |
---|
11 | #include <iostream> |
---|
12 | #include <algorithm> |
---|
13 | #include <boost/graph/adjacency_list.hpp> |
---|
14 | |
---|
15 | using namespace std; |
---|
16 | using namespace boost; |
---|
17 | |
---|
18 | |
---|
19 | /* |
---|
20 | Vertex Basics |
---|
21 | |
---|
22 | This example demonstrates the GGCL Vertex interface. |
---|
23 | |
---|
24 | Sample output: |
---|
25 | |
---|
26 | vertices(g) = 0 1 2 3 4 |
---|
27 | vertex id: 0 |
---|
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 |
---|
31 | |
---|
32 | vertex id: 1 |
---|
33 | out-edges: |
---|
34 | in-edges: (0,1) (3,1) (4,1) |
---|
35 | adjacent vertices: |
---|
36 | |
---|
37 | vertex id: 2 |
---|
38 | out-edges: (2,0) (2,4) |
---|
39 | in-edges: (0,2) |
---|
40 | adjacent vertices: 0 4 |
---|
41 | |
---|
42 | vertex id: 3 |
---|
43 | out-edges: (3,0) (3,1) (3,4) |
---|
44 | in-edges: (0,3) |
---|
45 | adjacent vertices: 0 1 4 |
---|
46 | |
---|
47 | vertex id: 4 |
---|
48 | out-edges: (4,0) (4,1) |
---|
49 | in-edges: (0,4) (2,4) (3,4) |
---|
50 | adjacent vertices: 0 1 |
---|
51 | |
---|
52 | |
---|
53 | */ |
---|
54 | |
---|
55 | |
---|
56 | /* some helper functors for output */ |
---|
57 | |
---|
58 | template <class Graph> |
---|
59 | struct print_edge { |
---|
60 | print_edge(Graph& g) : G(g) { } |
---|
61 | |
---|
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 |
---|
65 | { |
---|
66 | typename boost::property_map<Graph, vertex_index_t>::type |
---|
67 | id = get(vertex_index, G); |
---|
68 | |
---|
69 | Vertex src = source(e, G); |
---|
70 | Vertex targ = target(e, G); |
---|
71 | |
---|
72 | cout << "(" << id[src] << "," << id[targ] << ") "; |
---|
73 | } |
---|
74 | |
---|
75 | Graph& G; |
---|
76 | }; |
---|
77 | |
---|
78 | template <class Graph> |
---|
79 | struct print_index { |
---|
80 | print_index(Graph& g) : G(g){ } |
---|
81 | |
---|
82 | typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; |
---|
83 | void operator()(Vertex c) const |
---|
84 | { |
---|
85 | typename boost::property_map<Graph,vertex_index_t>::type |
---|
86 | id = get(vertex_index, G); |
---|
87 | cout << id[c] << " "; |
---|
88 | } |
---|
89 | |
---|
90 | Graph& G; |
---|
91 | }; |
---|
92 | |
---|
93 | |
---|
94 | template <class Graph> |
---|
95 | struct exercise_vertex { |
---|
96 | typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; |
---|
97 | |
---|
98 | exercise_vertex(Graph& _g) : g(_g) { } |
---|
99 | |
---|
100 | void operator()(Vertex v) const |
---|
101 | { |
---|
102 | typename boost::property_map<Graph, vertex_index_t>::type |
---|
103 | id = get(vertex_index, g); |
---|
104 | |
---|
105 | cout << "vertex id: " << id[v] << endl; |
---|
106 | |
---|
107 | cout << "out-edges: "; |
---|
108 | for_each(out_edges(v, g).first, out_edges(v,g).second, |
---|
109 | print_edge<Graph>(g)); |
---|
110 | |
---|
111 | cout << endl; |
---|
112 | |
---|
113 | cout << "in-edges: "; |
---|
114 | for_each(in_edges(v, g).first, in_edges(v,g).second, |
---|
115 | print_edge<Graph>(g)); |
---|
116 | |
---|
117 | cout << endl; |
---|
118 | |
---|
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; |
---|
123 | } |
---|
124 | |
---|
125 | Graph& g; |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | int |
---|
130 | main() |
---|
131 | { |
---|
132 | typedef adjacency_list<vecS,vecS,bidirectionalS> MyGraphType; |
---|
133 | |
---|
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) }; |
---|
138 | |
---|
139 | /* Construct a graph using the edge_array*/ |
---|
140 | MyGraphType g(5); |
---|
141 | for (int i=0; i<11; ++i) |
---|
142 | add_edge(edge_array[i].first, edge_array[i].second, g); |
---|
143 | |
---|
144 | boost::property_map<MyGraphType, vertex_index_t>::type |
---|
145 | id = get(vertex_index, g); |
---|
146 | |
---|
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; |
---|
152 | |
---|
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)); |
---|
157 | |
---|
158 | return 0; |
---|
159 | } |
---|