page_rank.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2004-5 The Trustees of Indiana University.
  2. // Copyright 2002 Brad King and Douglas Gregor
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Authors: Douglas Gregor
  7. // Andrew Lumsdaine
  8. #ifndef BOOST_GRAPH_PAGE_RANK_HPP
  9. #define BOOST_GRAPH_PAGE_RANK_HPP
  10. #include <boost/property_map/property_map.hpp>
  11. #include <boost/graph/graph_traits.hpp>
  12. #include <boost/graph/properties.hpp>
  13. #include <boost/graph/iteration_macros.hpp>
  14. #include <boost/graph/overloading.hpp>
  15. #include <boost/graph/detail/mpi_include.hpp>
  16. #include <vector>
  17. namespace boost
  18. {
  19. namespace graph
  20. {
  21. struct n_iterations
  22. {
  23. explicit n_iterations(std::size_t n) : n(n) {}
  24. template < typename RankMap, typename Graph >
  25. bool operator()(const RankMap&, const Graph&)
  26. {
  27. return n-- == 0;
  28. }
  29. private:
  30. std::size_t n;
  31. };
  32. namespace detail
  33. {
  34. template < typename Graph, typename RankMap, typename RankMap2 >
  35. void page_rank_step(const Graph& g, RankMap from_rank, RankMap2 to_rank,
  36. typename property_traits< RankMap >::value_type damping,
  37. incidence_graph_tag)
  38. {
  39. typedef typename property_traits< RankMap >::value_type rank_type;
  40. // Set new rank maps
  41. BGL_FORALL_VERTICES_T(v, g, Graph)
  42. put(to_rank, v, rank_type(1 - damping));
  43. BGL_FORALL_VERTICES_T(u, g, Graph)
  44. {
  45. rank_type u_rank_out
  46. = damping * get(from_rank, u) / out_degree(u, g);
  47. BGL_FORALL_ADJ_T(u, v, g, Graph)
  48. put(to_rank, v, get(to_rank, v) + u_rank_out);
  49. }
  50. }
  51. template < typename Graph, typename RankMap, typename RankMap2 >
  52. void page_rank_step(const Graph& g, RankMap from_rank, RankMap2 to_rank,
  53. typename property_traits< RankMap >::value_type damping,
  54. bidirectional_graph_tag)
  55. {
  56. typedef
  57. typename property_traits< RankMap >::value_type damping_type;
  58. BGL_FORALL_VERTICES_T(v, g, Graph)
  59. {
  60. typename property_traits< RankMap >::value_type rank(0);
  61. BGL_FORALL_INEDGES_T(v, e, g, Graph)
  62. rank += get(from_rank, source(e, g))
  63. / out_degree(source(e, g), g);
  64. put(to_rank, v, (damping_type(1) - damping) + damping * rank);
  65. }
  66. }
  67. } // end namespace detail
  68. template < typename Graph, typename RankMap, typename Done,
  69. typename RankMap2 >
  70. void page_rank(const Graph& g, RankMap rank_map, Done done,
  71. typename property_traits< RankMap >::value_type damping,
  72. typename graph_traits< Graph >::vertices_size_type n,
  73. RankMap2 rank_map2 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
  74. Graph, vertex_list_graph_tag))
  75. {
  76. typedef typename property_traits< RankMap >::value_type rank_type;
  77. rank_type initial_rank = rank_type(rank_type(1) / n);
  78. BGL_FORALL_VERTICES_T(v, g, Graph) put(rank_map, v, initial_rank);
  79. bool to_map_2 = true;
  80. while ((to_map_2 && !done(rank_map, g))
  81. || (!to_map_2 && !done(rank_map2, g)))
  82. {
  83. typedef typename graph_traits< Graph >::traversal_category category;
  84. if (to_map_2)
  85. {
  86. detail::page_rank_step(
  87. g, rank_map, rank_map2, damping, category());
  88. }
  89. else
  90. {
  91. detail::page_rank_step(
  92. g, rank_map2, rank_map, damping, category());
  93. }
  94. to_map_2 = !to_map_2;
  95. }
  96. if (!to_map_2)
  97. {
  98. BGL_FORALL_VERTICES_T(v, g, Graph)
  99. put(rank_map, v, get(rank_map2, v));
  100. }
  101. }
  102. template < typename Graph, typename RankMap, typename Done >
  103. void page_rank(const Graph& g, RankMap rank_map, Done done,
  104. typename property_traits< RankMap >::value_type damping,
  105. typename graph_traits< Graph >::vertices_size_type n)
  106. {
  107. typedef typename property_traits< RankMap >::value_type rank_type;
  108. std::vector< rank_type > ranks2(num_vertices(g));
  109. page_rank(g, rank_map, done, damping, n,
  110. make_iterator_property_map(ranks2.begin(), get(vertex_index, g)));
  111. }
  112. template < typename Graph, typename RankMap, typename Done >
  113. inline void page_rank(const Graph& g, RankMap rank_map, Done done,
  114. typename property_traits< RankMap >::value_type damping = 0.85)
  115. {
  116. page_rank(g, rank_map, done, damping, num_vertices(g));
  117. }
  118. template < typename Graph, typename RankMap >
  119. inline void page_rank(const Graph& g, RankMap rank_map)
  120. {
  121. page_rank(g, rank_map, n_iterations(20));
  122. }
  123. // TBD: this could be _much_ more efficient, using a queue to store
  124. // the vertices that should be reprocessed and keeping track of which
  125. // vertices are in the queue with a property map. Baah, this only
  126. // applies when we have a bidirectional graph.
  127. template < typename MutableGraph >
  128. void remove_dangling_links(
  129. MutableGraph& g BOOST_GRAPH_ENABLE_IF_MODELS_PARM(
  130. MutableGraph, vertex_list_graph_tag))
  131. {
  132. typename graph_traits< MutableGraph >::vertices_size_type old_n;
  133. do
  134. {
  135. old_n = num_vertices(g);
  136. typename graph_traits< MutableGraph >::vertex_iterator vi, vi_end;
  137. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end;
  138. /* in loop */)
  139. {
  140. typename graph_traits< MutableGraph >::vertex_descriptor v
  141. = *vi++;
  142. if (out_degree(v, g) == 0)
  143. {
  144. clear_vertex(v, g);
  145. remove_vertex(v, g);
  146. }
  147. }
  148. } while (num_vertices(g) < old_n);
  149. }
  150. }
  151. } // end namespace boost::graph
  152. #include BOOST_GRAPH_MPI_INCLUDE(< boost / graph / distributed / page_rank.hpp >)
  153. #endif // BOOST_GRAPH_PAGE_RANK_HPP