remove_edge_if_undir.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. using namespace boost;
  14. /*
  15. Sample output:
  16. original graph:
  17. 0 <--> 3 3 2
  18. 1 <--> 3
  19. 2 <--> 0 3
  20. 3 <--> 0 0 1 2
  21. 1(0,3) 2(0,3) 3(1,3) 4(2,0) 5(3,2)
  22. removing edges connecting 0 and 3
  23. 0 <--> 2
  24. 1 <--> 3
  25. 2 <--> 0 3
  26. 3 <--> 1 2
  27. 3(1,3) 4(2,0) 5(3,2)
  28. removing edges with weight greater than 3
  29. 0 <-->
  30. 1 <--> 3
  31. 2 <-->
  32. 3 <--> 1
  33. 3(1,3)
  34. */
  35. typedef adjacency_list<vecS, vecS, undirectedS,
  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[5] = { Edge(0, 3), 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 < 5; ++j)
  62. add_edge(edge_array[j].first, edge_array[j].second, g);
  63. #else
  64. Graph g(edge_array, edge_array + 5, 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. std::cout << "original graph:" << std::endl;
  73. print_graph(g, get(vertex_index, g));
  74. print_edges2(g, get(vertex_index, g), get(edge_weight, g));
  75. std::cout << std::endl;
  76. std::cout << "removing edges connecting 0 and 3" << std::endl;
  77. remove_out_edge_if(vertex(0, g), incident_on(vertex(3, g), g), g);
  78. print_graph(g, get(vertex_index, g));
  79. print_edges2(g, get(vertex_index, g), get(edge_weight, g));
  80. std::cout << "removing edges with weight greater than 3" << std::endl;
  81. remove_edge_if(has_weight_greater_than(3, g), g);
  82. print_graph(g, get(vertex_index, g));
  83. print_edges2(g, get(vertex_index, g), get(edge_weight, g));
  84. return 0;
  85. }