filtered_graph_edge_range.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //=======================================================================
  2. // Copyright 2001 University of Notre Dame.
  3. // Author: 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. filtered edge set: (A,B) (C,D) (D,B)
  12. filtered out-edges:
  13. A --> B
  14. B -->
  15. C --> D
  16. D --> B
  17. E -->
  18. */
  19. #include <boost/config.hpp>
  20. #include <iostream>
  21. #include <boost/graph/adjacency_list.hpp>
  22. #include <boost/graph/filtered_graph.hpp>
  23. #include <boost/graph/graph_utility.hpp>
  24. template <typename EdgeWeightMap>
  25. struct positive_edge_weight {
  26. positive_edge_weight() { }
  27. positive_edge_weight(EdgeWeightMap weight) : m_weight(weight) { }
  28. template <typename Edge>
  29. bool operator()(const Edge& e) const {
  30. return 0 < boost::get(m_weight, e);
  31. }
  32. EdgeWeightMap m_weight;
  33. };
  34. int main()
  35. {
  36. using namespace boost;
  37. typedef adjacency_list<multisetS, vecS, directedS,
  38. no_property, property<edge_weight_t, int> > Graph;
  39. typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap;
  40. enum { A, B, C, D, E, N };
  41. const char* name = "ABCDE";
  42. Graph g(N);
  43. add_edge(A, B, 2, g);
  44. add_edge(A, C, 0, g);
  45. add_edge(C, D, 1, g);
  46. add_edge(C, D, 0, g);
  47. add_edge(C, D, 3, g);
  48. add_edge(C, E, 0, g);
  49. add_edge(D, B, 3, g);
  50. add_edge(E, C, 0, g);
  51. EdgeWeightMap weight = get(edge_weight, g);
  52. std::cout << "unfiltered edge_range(C,D)\n";
  53. graph_traits<Graph>::out_edge_iterator f, l;
  54. for (boost::tie(f, l) = edge_range(C, D, g); f != l; ++f)
  55. std::cout << name[source(*f, g)] << " --" << weight[*f]
  56. << "-> " << name[target(*f, g)] << "\n";
  57. positive_edge_weight<EdgeWeightMap> filter(weight);
  58. typedef filtered_graph<Graph, positive_edge_weight<EdgeWeightMap> > FGraph;
  59. FGraph fg(g, filter);
  60. std::cout << "filtered edge_range(C,D)\n";
  61. graph_traits<FGraph>::out_edge_iterator first, last;
  62. for (boost::tie(first, last) = edge_range(C, D, fg); first != last; ++first)
  63. std::cout << name[source(*first, fg)] << " --" << weight[*first]
  64. << "-> " << name[target(*first, fg)] << "\n";
  65. return 0;
  66. }