exterior_properties.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // This example is similar to the one in edge_property.cpp.
  11. // The only difference is that this example uses exterior
  12. // property storage instead of interior (properties).
  13. //
  14. // Sample output:
  15. //
  16. // 0 --(10, 8)--> 1
  17. //
  18. // 1 --(20, 12)--> 4 --(40, 12)--> 3
  19. // <--(10,8)-- 0 <--(20,16)-- 2
  20. // 2 --(20, 16)--> 1
  21. // <--(20,16)-- 5
  22. // 3 --(40, 12)--> 6
  23. // <--(40,12)-- 1
  24. // 4 --(20, 12)--> 7
  25. // <--(20,12)-- 1
  26. // 5 --(20, 16)--> 2
  27. // <--(20,16)-- 6
  28. // 6 --(20, 16)--> 5 --(10, 8)--> 8
  29. // <--(20,12)-- 7 <--(40,12)-- 3
  30. // 7 --(20, 12)--> 6
  31. // <--(20,12)-- 4
  32. // 8
  33. // <--(10,8)-- 6
  34. #include <boost/config.hpp>
  35. #include <iostream>
  36. #include <boost/graph/adjacency_list.hpp>
  37. #include <boost/property_map/property_map.hpp>
  38. template <class Graph, class Capacity, class Flow>
  39. void print_network(Graph& G, Capacity capacity, Flow flow)
  40. {
  41. typedef typename boost::graph_traits<Graph>::vertex_iterator Viter;
  42. typedef typename boost::graph_traits<Graph>::out_edge_iterator OutEdgeIter;
  43. typedef typename boost::graph_traits<Graph>::in_edge_iterator InEdgeIter;
  44. Viter ui, uiend;
  45. for (boost::tie(ui, uiend) = boost::vertices(G); ui != uiend; ++ui) {
  46. OutEdgeIter out, out_end;
  47. std::cout << *ui << "\t";
  48. for(boost::tie(out, out_end) = boost::out_edges(*ui, G); out != out_end; ++out)
  49. std::cout << "--(" << boost::get(capacity, *out) << ", "
  50. << boost::get(flow, *out) << ")--> " << boost::target(*out,G) << "\t";
  51. std::cout << std::endl << "\t";
  52. InEdgeIter in, in_end;
  53. for(boost::tie(in, in_end) = boost::in_edges(*ui, G); in != in_end; ++in)
  54. std::cout << "<--(" << boost::get(capacity, *in) << "," << boost::get(flow, *in) << ")-- "
  55. << boost::source(*in, G) << "\t";
  56. std::cout << std::endl;
  57. }
  58. }
  59. int main(int , char* []) {
  60. typedef boost::adjacency_list<boost::vecS, boost::vecS,
  61. boost::bidirectionalS, boost::no_property,
  62. boost::property<boost::edge_index_t, std::size_t> > Graph;
  63. const int num_vertices = 9;
  64. Graph G(num_vertices);
  65. /* 2<----5
  66. / ^
  67. / \
  68. V \
  69. 0 ---->1---->3----->6--->8
  70. \ ^
  71. \ /
  72. V /
  73. 4----->7
  74. */
  75. int capacity[] = { 10, 20, 20, 20, 40, 40, 20, 20, 20, 10 };
  76. int flow[] = { 8, 12, 12, 12, 12, 12, 16, 16, 16, 8 };
  77. // insert edges into the graph, and assign each edge an ID number
  78. // to index into the property arrays
  79. boost::add_edge(0, 1, 0, G);
  80. boost::add_edge(1, 4, 1, G);
  81. boost::add_edge(4, 7, 2, G);
  82. boost::add_edge(7, 6, 3, G);
  83. boost::add_edge(1, 3, 4, G);
  84. boost::add_edge(3, 6, 5, G);
  85. boost::add_edge(6, 5, 6, G);
  86. boost::add_edge(5, 2, 7, G);
  87. boost::add_edge(2, 1, 8, G);
  88. boost::add_edge(6, 8, 9, G);
  89. typedef boost::property_map<Graph, boost::edge_index_t>::type EdgeIndexMap;
  90. EdgeIndexMap edge_id = boost::get(boost::edge_index, G);
  91. typedef boost::iterator_property_map<int*, EdgeIndexMap, int, int&> IterMap;
  92. print_network(G, IterMap(capacity, edge_id), IterMap(flow, edge_id));
  93. return 0;
  94. }