remove_edge_if_bidir.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <boost/graph/adjacency_list.hpp>
  12. #include <boost/graph/graph_utility.hpp>
  13. /*
  14. Sample output:
  15. original graph:
  16. 0 --> 3 2 3
  17. 1 --> 3
  18. 2 --> 0
  19. 3 --> 2
  20. 1(0,3) 2(0,2) 3(0,3) 4(1,3) 5(2,0) 6(3,2)
  21. removing edges connecting 0 to 3
  22. 0 --> 2
  23. 1 --> 3
  24. 2 --> 0
  25. 3 --> 2
  26. 2(0,2) 4(1,3) 5(2,0) 6(3,2)
  27. removing edges with weight greater than 3
  28. 0 --> 2
  29. 1 -->
  30. 2 -->
  31. 3 -->
  32. 2(0,2)
  33. */
  34. using namespace boost;
  35. typedef adjacency_list<vecS, vecS, bidirectionalS,
  36. no_property, property<edge_weight_t, int> > Graph;
  37. struct has_weight_greater_than {
  38. has_weight_greater_than(int w_, Graph& g_) : w(w_), g(g_) { }
  39. bool operator()(graph_traits<Graph>::edge_descriptor e) {
  40. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  41. property_map<Graph, edge_weight_t>::type weight = get(edge_weight, g);
  42. return get(weight, e) > w;
  43. #else
  44. // This version of get() breaks VC++
  45. return get(edge_weight, g, e) > w;
  46. #endif
  47. }
  48. int w;
  49. Graph& g;
  50. };
  51. int
  52. main()
  53. {
  54. typedef std::pair<std::size_t,std::size_t> Edge;
  55. Edge edge_array[6] = { Edge(0,3), Edge(0,2), Edge(0, 3),
  56. Edge(1,3),
  57. Edge(2, 0),
  58. Edge(3, 2) };
  59. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  60. Graph g(4);
  61. for (std::size_t j = 0; j < 6; ++j)
  62. add_edge(edge_array[j].first, edge_array[j].second, g);
  63. #else
  64. Graph g(edge_array, edge_array + 6, 4);
  65. #endif
  66. property_map<Graph, edge_weight_t>::type
  67. weight = get(edge_weight, g);
  68. int w = 0;
  69. graph_traits<Graph>::edge_iterator ei, ei_end;
  70. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  71. weight[*ei] = ++w;
  72. property_map<Graph, vertex_index_t>::type indexmap = get(vertex_index, g);
  73. std::cout << "original graph:" << std::endl;
  74. print_graph(g, indexmap);
  75. print_edges2(g, indexmap, weight);
  76. std::cout << std::endl;
  77. std::cout << "removing edges connecting 0 to 3" << std::endl;
  78. remove_out_edge_if(vertex(0,g), incident_to(vertex(3,g), g), g);
  79. print_graph(g, indexmap);
  80. print_edges2(g, indexmap, weight);
  81. std::cout << "removing edges with weight greater than 3" << std::endl;
  82. remove_edge_if(has_weight_greater_than(3, g), g);
  83. print_graph(g, indexmap);
  84. print_edges2(g, indexmap, weight);
  85. return 0;
  86. }