Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/vertex_basics.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.8 KB
Line 
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
15using namespace std;
16using 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
58template <class Graph>
59struct 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
78template <class Graph>
79struct 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
94template <class Graph>
95struct 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
129int
130main()
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}
Note: See TracBrowser for help on using the repository browser.