quick_tour.cpp 5.1 KB

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