filtered_graph.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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<vecS, 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, E, 0, g);
  47. add_edge(D, B, 3, g);
  48. add_edge(E, C, 0, g);
  49. positive_edge_weight<EdgeWeightMap> filter(get(edge_weight, g));
  50. filtered_graph<Graph, positive_edge_weight<EdgeWeightMap> >
  51. fg(g, filter);
  52. std::cout << "filtered edge set: ";
  53. print_edges(fg, name);
  54. std::cout << "filtered out-edges:" << std::endl;
  55. print_graph(fg, name);
  56. return 0;
  57. }