exterior_property_map.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <string>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. using namespace std;
  16. using namespace boost;
  17. /*
  18. Exterior Decorator Basics
  19. An exterior decorator is a way of associating properties with the
  20. vertices or edges of a graph. The "exterior" part means that the
  21. properties are not stored inside the graph object (see
  22. internal_decorator_basics.cc). Instead they are stored
  23. separately, and passed as an extra argument to any
  24. algorithm they are needed in. There are several standard
  25. decorator types such a color and weight that are used
  26. in the GGCL algorithms.
  27. The main responsibility of a decorator is to provide an operator[]
  28. that maps a vertex (or vertex ID) to the property value for that
  29. vertex. It just so happens that a normal array provides this. In
  30. addition, a decorator must provide access to the property type
  31. through the decorator_traits class. For convenience, GGCL
  32. already defines a decorator_triats class for pointer and
  33. array types.
  34. Sample Output
  35. Jeremy owes Rich some money
  36. Jeremy owes Andrew some money
  37. Jeremy owes Jeff some money
  38. Jeremy owes Kinis some money
  39. Andrew owes Jeremy some money
  40. Andrew owes Kinis some money
  41. Jeff owes Jeremy some money
  42. Jeff owes Rich some money
  43. Jeff owes Kinis some money
  44. Kinis owes Jeremy some money
  45. Kinis owes Rich some money
  46. */
  47. template <class EdgeIter, class Graph, class Name>
  48. void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G,
  49. Name name)
  50. {
  51. while (first != last) {
  52. cout << name[source(*first,G)] << " owes "
  53. << name[target(*first,G)] << " some money" << endl;
  54. ++first;
  55. }
  56. }
  57. int
  58. main(int, char*[])
  59. {
  60. /* The property will be "names" attached to the vertices */
  61. string* names = new string[5];
  62. names[0] = "Jeremy";
  63. names[1] = "Rich";
  64. names[2] = "Andrew";
  65. names[3] = "Jeff";
  66. names[4] = "Kinis";
  67. typedef adjacency_list<> MyGraphType;
  68. typedef pair<int,int> Pair;
  69. Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4),
  70. Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1),
  71. Pair(3,4), Pair(4,0), Pair(4,1) };
  72. MyGraphType G(5);
  73. for (int i=0; i<11; ++i)
  74. add_edge(edge_array[i].first, edge_array[i].second, G);
  75. who_owes_who(edges(G).first, edges(G).second, G, names);
  76. return 0;
  77. }