gerdemann.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // -*- c++ -*-
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. #include <boost/config.hpp>
  11. #include <iostream>
  12. #include <boost/graph/adjacency_list.hpp>
  13. /*
  14. Thanks to Dale Gerdemann for this example, which inspired some
  15. changes to adjacency_list to make this work properly.
  16. */
  17. /*
  18. Sample output:
  19. 0 --c--> 1 --j--> 1 --c--> 2 --x--> 2
  20. 1 --c--> 2 --d--> 3
  21. 2 --t--> 4
  22. 3 --h--> 4
  23. 4
  24. merging vertex 1 into vertex 0
  25. 0 --c--> 0 --j--> 0 --c--> 1 --x--> 1 --d--> 2
  26. 1 --t--> 3
  27. 2 --h--> 3
  28. 3
  29. */
  30. // merge_vertex(u,v,g):
  31. // incoming/outgoing edges for v become incoming/outgoing edges for u
  32. // v is deleted
  33. template <class Graph, class GetEdgeProperties>
  34. void merge_vertex
  35. (typename boost::graph_traits<Graph>::vertex_descriptor u,
  36. typename boost::graph_traits<Graph>::vertex_descriptor v,
  37. Graph& g, GetEdgeProperties getp)
  38. {
  39. typedef boost::graph_traits<Graph> Traits;
  40. typename Traits::edge_descriptor e;
  41. typename Traits::out_edge_iterator out_i, out_end;
  42. for (boost::tie(out_i, out_end) = out_edges(v, g); out_i != out_end; ++out_i) {
  43. e = *out_i;
  44. typename Traits::vertex_descriptor targ = target(e, g);
  45. add_edge(u, targ, getp(e), g);
  46. }
  47. typename Traits::in_edge_iterator in_i, in_end;
  48. for (boost::tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i) {
  49. e = *in_i;
  50. typename Traits::vertex_descriptor src = source(e, g);
  51. add_edge(src, u, getp(e), g);
  52. }
  53. clear_vertex(v, g);
  54. remove_vertex(v, g);
  55. }
  56. template <class StoredEdge>
  57. struct order_by_name
  58. {
  59. typedef StoredEdge first_argument_type;
  60. typedef StoredEdge second_argument_type;
  61. typedef bool result_type;
  62. bool operator()(const StoredEdge& e1, const StoredEdge& e2) const {
  63. // Using std::pair operator< as an easy way to get lexicographical
  64. // compare over tuples.
  65. return std::make_pair(e1.get_target(), boost::get(boost::edge_name, e1))
  66. < std::make_pair(e2.get_target(), boost::get(boost::edge_name, e2));
  67. }
  68. };
  69. struct ordered_set_by_nameS { };
  70. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  71. namespace boost {
  72. template <class ValueType>
  73. struct container_gen<ordered_set_by_nameS, ValueType> {
  74. typedef std::set<ValueType, order_by_name<ValueType> > type;
  75. };
  76. template <>
  77. struct parallel_edge_traits<ordered_set_by_nameS> {
  78. typedef allow_parallel_edge_tag type;
  79. };
  80. }
  81. #endif
  82. template <class Graph>
  83. struct get_edge_name {
  84. get_edge_name(const Graph& g_) : g(g_) { }
  85. template <class Edge>
  86. boost::property<boost::edge_name_t, char> operator()(Edge e) const {
  87. return boost::property<boost::edge_name_t, char>(boost::get(boost::edge_name, g, e));
  88. }
  89. const Graph& g;
  90. };
  91. int
  92. main()
  93. {
  94. #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  95. std::cout << "This program requires partial specialization." << std::endl;
  96. #else
  97. using namespace boost;
  98. typedef property<edge_name_t, char> EdgeProperty;
  99. typedef adjacency_list<ordered_set_by_nameS, vecS, bidirectionalS,
  100. no_property, EdgeProperty> graph_type;
  101. graph_type g;
  102. add_edge(0, 1, EdgeProperty('j'), g);
  103. add_edge(0, 2, EdgeProperty('c'), g);
  104. add_edge(0, 2, EdgeProperty('x'), g);
  105. add_edge(1, 3, EdgeProperty('d'), g);
  106. add_edge(1, 2, EdgeProperty('c'), g);
  107. add_edge(1, 3, EdgeProperty('d'), g);
  108. add_edge(2, 4, EdgeProperty('t'), g);
  109. add_edge(3, 4, EdgeProperty('h'), g);
  110. add_edge(0, 1, EdgeProperty('c'), g);
  111. property_map<graph_type, vertex_index_t>::type id = get(vertex_index, g);
  112. property_map<graph_type, edge_name_t>::type name = get(edge_name, g);
  113. graph_traits<graph_type>::vertex_iterator i, end;
  114. graph_traits<graph_type>::out_edge_iterator ei, edge_end;
  115. for (boost::tie(i, end) = vertices(g); i != end; ++i) {
  116. std::cout << id[*i] << " ";
  117. for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
  118. std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
  119. std::cout << std::endl;
  120. }
  121. std::cout << std::endl;
  122. std::cout << "merging vertex 1 into vertex 0" << std::endl << std::endl;
  123. merge_vertex(0, 1, g, get_edge_name<graph_type>(g));
  124. for (boost::tie(i, end) = vertices(g); i != end; ++i) {
  125. std::cout << id[*i] << " ";
  126. for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
  127. std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
  128. std::cout << std::endl;
  129. }
  130. std::cout << std::endl;
  131. #endif
  132. return 0;
  133. }