Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/quick_tour.cpp @ 30

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

updated boost from 1_33_1 to 1_34_1

File size: 5.0 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>                      // for std::cout
12#include <utility>                       // for std::pair
13#include <algorithm>                     // for std::for_each
14#include <boost/utility.hpp>             // for boost::tie
15#include <boost/graph/graph_traits.hpp>  // for boost::graph_traits
16#include <boost/graph/adjacency_list.hpp>
17#include <boost/graph/graphviz.hpp>
18
19using namespace boost;
20
21template <class Graph> struct exercise_vertex {
22  exercise_vertex(Graph& g_) : g(g_) { }
23  typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
24  void operator()(const Vertex& v) const
25  {
26    using namespace boost;
27    typename property_map<Graph, vertex_index_t>::type
28      vertex_id = get(vertex_index, g);
29    std::cout << "vertex: " << get(vertex_id, v) << std::endl;
30
31    // Write out the outgoing edges
32    std::cout << "\tout-edges: ";
33    typename graph_traits<Graph>::out_edge_iterator out_i, out_end;
34    typename graph_traits<Graph>::edge_descriptor e;
35    for (tie(out_i, out_end) = out_edges(v, g); 
36         out_i != out_end; ++out_i)
37    {
38      e = *out_i;
39      Vertex src = source(e, g), targ = target(e, g);
40      std::cout << "(" << get(vertex_id, src)
41                << "," << get(vertex_id, targ) << ") ";
42    }
43    std::cout << std::endl;
44
45    // Write out the incoming edges   
46    std::cout << "\tin-edges: ";
47    typename graph_traits<Graph>::in_edge_iterator in_i, in_end;
48    for (tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i)
49    {
50      e = *in_i;
51      Vertex src = source(e, g), targ = target(e, g);
52      std::cout << "(" << get(vertex_id, src)
53                << "," << get(vertex_id, targ) << ") ";
54    }
55    std::cout << std::endl;
56
57    // Write out all adjacent vertices   
58    std::cout << "\tadjacent vertices: ";
59    typename graph_traits<Graph>::adjacency_iterator ai, ai_end;
60    for (tie(ai,ai_end) = adjacent_vertices(v, g);  ai != ai_end; ++ai)
61      std::cout << get(vertex_id, *ai) <<  " ";
62    std::cout << std::endl;
63  }
64  Graph& g;
65};
66
67
68int main(int,char*[])
69{
70  // create a typedef for the Graph type
71  typedef adjacency_list<vecS, vecS, bidirectionalS,
72     no_property, property<edge_weight_t, float> > Graph;
73
74  // Make convenient labels for the vertices
75  enum { A, B, C, D, E, N };
76  const int num_vertices = N;
77  const char* name = "ABCDE";
78
79  // writing out the edges in the graph
80  typedef std::pair<int,int> Edge;
81  Edge edge_array[] = 
82  { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
83    Edge(C,E), Edge(B,D), Edge(D,E), };
84  const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
85
86  // average transmission delay (in milliseconds) for each connection
87  float transmission_delay[] = { 1.2, 4.5, 2.6, 0.4, 5.2, 1.8, 3.3, 9.1 };
88
89  // declare a graph object, adding the edges and edge properties
90#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
91  // VC++ can't handle the iterator constructor
92  Graph g(num_vertices);
93  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
94  for (std::size_t j = 0; j < num_edges; ++j) {
95    graph_traits<Graph>::edge_descriptor e; bool inserted;
96    tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
97    weightmap[e] = transmission_delay[j];
98  }
99#else
100  Graph g(edge_array, edge_array + num_edges,
101          transmission_delay, num_vertices);
102#endif
103
104  boost::property_map<Graph, vertex_index_t>::type
105    vertex_id = get(vertex_index, g);
106  boost::property_map<Graph, edge_weight_t>::type
107    trans_delay = get(edge_weight, g);
108
109  std::cout << "vertices(g) = ";
110  typedef graph_traits<Graph>::vertex_iterator vertex_iter;
111  std::pair<vertex_iter, vertex_iter> vp;
112  for (vp = vertices(g); vp.first != vp.second; ++vp.first)
113    std::cout << name[get(vertex_id, *vp.first)] <<  " ";
114  std::cout << std::endl;
115 
116  std::cout << "edges(g) = ";
117  graph_traits<Graph>::edge_iterator ei, ei_end;
118  for (tie(ei,ei_end) = edges(g); ei != ei_end; ++ei)
119    std::cout << "(" << name[get(vertex_id, source(*ei, g))]
120              << "," << name[get(vertex_id, target(*ei, g))] << ") ";
121  std::cout << std::endl;
122 
123  std::for_each(vertices(g).first, vertices(g).second,
124                exercise_vertex<Graph>(g));
125 
126  std::map<std::string,std::string> graph_attr, vertex_attr, edge_attr;
127  graph_attr["size"] = "3,3";
128  graph_attr["rankdir"] = "LR";
129  graph_attr["ratio"] = "fill";
130  vertex_attr["shape"] = "circle";
131
132  boost::write_graphviz(std::cout, g, 
133                        make_label_writer(name),
134                        make_label_writer(trans_delay),
135                        make_graph_attributes_writer(graph_attr, vertex_attr, 
136                                                     edge_attr));
137 
138  return 0;
139}
140
141
Note: See TracBrowser for help on using the repository browser.