edge_property.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // Sample output:
  11. //
  12. // 0 --(8, 10)--> 1
  13. //
  14. // 1 --(12, 20)--> 4 --(12, 40)--> 3
  15. // <--(8,10)-- 0 <--(16,20)-- 2
  16. // 2 --(16, 20)--> 1
  17. // <--(16,20)-- 5
  18. // 3 --(12, 40)--> 6
  19. // <--(12,40)-- 1
  20. // 4 --(12, 20)--> 7
  21. // <--(12,20)-- 1
  22. // 5 --(16, 20)--> 2
  23. // <--(16,20)-- 6
  24. // 6 --(16, 20)--> 5 --(8, 10)--> 8
  25. // <--(12,20)-- 7 <--(12,40)-- 3
  26. // 7 --(12, 20)--> 6
  27. // <--(12,20)-- 4
  28. // 8
  29. // <--(8,10)-- 6
  30. //
  31. //
  32. // 0 --(8, 1)--> 1
  33. //
  34. // 1 --(12, 2)--> 4 --(12, 3)--> 3
  35. // <--(8,1)-- 0 <--(16,4)-- 2
  36. // 2 --(16, 4)--> 1
  37. // <--(16,7)-- 5
  38. // 3 --(12, 5)--> 6
  39. // <--(12,3)-- 1
  40. // 4 --(12, 6)--> 7
  41. // <--(12,2)-- 1
  42. // 5 --(16, 7)--> 2
  43. // <--(16,8)-- 6
  44. // 6 --(16, 8)--> 5
  45. // <--(12,10)-- 7 <--(12,5)-- 3
  46. // 7 --(12, 10)--> 6
  47. // <--(12,6)-- 4
  48. // 8
  49. //
  50. #include <boost/config.hpp>
  51. #include <iostream>
  52. #include <boost/utility.hpp>
  53. #include <boost/property_map/property_map.hpp>
  54. #include <boost/graph/adjacency_list.hpp>
  55. using namespace boost;
  56. using namespace std;
  57. enum edge_myflow_t { edge_myflow };
  58. enum edge_mycapacity_t { edge_mycapacity };
  59. namespace boost {
  60. BOOST_INSTALL_PROPERTY(edge, myflow);
  61. BOOST_INSTALL_PROPERTY(edge, mycapacity);
  62. }
  63. template <class Graph>
  64. void print_network(const Graph& G)
  65. {
  66. typedef typename boost::graph_traits<Graph>::vertex_iterator Viter;
  67. typedef typename boost::graph_traits<Graph>::out_edge_iterator OutEdgeIter;
  68. typedef typename boost::graph_traits<Graph>::in_edge_iterator InEdgeIter;
  69. typename property_map<Graph, edge_mycapacity_t>::const_type
  70. capacity = get(edge_mycapacity, G);
  71. typename property_map<Graph, edge_myflow_t>::const_type
  72. flow = get(edge_myflow, G);
  73. Viter ui, uiend;
  74. boost::tie(ui, uiend) = vertices(G);
  75. for (; ui != uiend; ++ui) {
  76. OutEdgeIter out, out_end;
  77. cout << *ui << "\t";
  78. boost::tie(out, out_end) = out_edges(*ui, G);
  79. for(; out != out_end; ++out)
  80. cout << "--(" << capacity[*out] << ", " << flow[*out] << ")--> "
  81. << target(*out,G) << "\t";
  82. InEdgeIter in, in_end;
  83. cout << endl << "\t";
  84. boost::tie(in, in_end) = in_edges(*ui, G);
  85. for(; in != in_end; ++in)
  86. cout << "<--(" << capacity[*in] << "," << flow[*in] << ")-- "
  87. << source(*in,G) << "\t";
  88. cout << endl;
  89. }
  90. }
  91. int main(int , char* [])
  92. {
  93. typedef property<edge_mycapacity_t, int> Cap;
  94. typedef property<edge_myflow_t, int, Cap> Flow;
  95. typedef adjacency_list<vecS, vecS, bidirectionalS,
  96. no_property, Flow> Graph;
  97. const int num_vertices = 9;
  98. Graph G(num_vertices);
  99. /* 2<----5
  100. / ^
  101. / \
  102. V \
  103. 0 ---->1---->3----->6--->8
  104. \ ^
  105. \ /
  106. V /
  107. 4----->7
  108. */
  109. add_edge(0, 1, Flow(10, Cap(8)), G);
  110. add_edge(1, 4, Flow(20, Cap(12)), G);
  111. add_edge(4, 7, Flow(20, Cap(12)), G);
  112. add_edge(7, 6, Flow(20, Cap(12)), G);
  113. add_edge(1, 3, Flow(40, Cap(12)), G);
  114. add_edge(3, 6, Flow(40, Cap(12)), G);
  115. add_edge(6, 5, Flow(20, Cap(16)), G);
  116. add_edge(5, 2, Flow(20, Cap(16)), G);
  117. add_edge(2, 1, Flow(20, Cap(16)), G);
  118. add_edge(6, 8, Flow(10, Cap(8)), G);
  119. print_network(G);
  120. property_map<Graph, edge_myflow_t>::type
  121. flow = get(edge_myflow, G);
  122. boost::graph_traits<Graph>::vertex_iterator v, v_end;
  123. boost::graph_traits<Graph>::out_edge_iterator e, e_end;
  124. int f = 0;
  125. for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v)
  126. for (boost::tie(e, e_end) = out_edges(*v, G); e != e_end; ++e)
  127. flow[*e] = ++f;
  128. cout << endl << endl;
  129. remove_edge(6, 8, G);
  130. print_network(G);
  131. return 0;
  132. }