vertex_basics.cpp 3.8 KB

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