edge_basics.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. Edge Basics
  17. This example demonstrates the GGCL Edge interface
  18. There is not much to the Edge interface. Basically just two
  19. functions to access the source and target vertex:
  20. source(e)
  21. target(e)
  22. and one associated type for the vertex type:
  23. edge_traits<Edge>::vertex_type
  24. Sample output:
  25. (0,1) (0,2) (0,3) (0,4) (2,0) (2,4) (3,0) (3,1)
  26. */
  27. template <class Graph>
  28. struct exercise_edge {
  29. exercise_edge(Graph& g) : G(g) {}
  30. typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
  31. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  32. void operator()(Edge e) const
  33. {
  34. //begin
  35. // Get the associated vertex type out of the edge using the
  36. // edge_traits class
  37. // Use the source() and target() functions to access the vertices
  38. // that belong to Edge e
  39. Vertex src = source(e, G);
  40. Vertex targ = target(e, G);
  41. // print out the vertex id's just because
  42. cout << "(" << src << "," << targ << ") ";
  43. //end
  44. }
  45. Graph& G;
  46. };
  47. int
  48. main()
  49. {
  50. typedef adjacency_list<> MyGraph;
  51. typedef pair<int,int> Pair;
  52. Pair edge_array[8] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4),
  53. Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1) };
  54. // Construct a graph using the edge_array (passing in pointers
  55. // (iterators) to the beginning and end of the array), and
  56. // specifying the number of vertices as 5
  57. MyGraph G(5);
  58. for (int i=0; i<8; ++i)
  59. add_edge(edge_array[i].first, edge_array[i].second, G);
  60. // Use the STL for_each algorithm to "exercise" all of the edges in
  61. // the graph
  62. for_each(edges(G).first, edges(G).second, exercise_edge<MyGraph>(G));
  63. cout << endl;
  64. return 0;
  65. }