page_rank.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <vector>
  16. namespace boost { namespace graph {
  17. struct n_iterations
  18. {
  19. explicit n_iterations(std::size_t n) : n(n) { }
  20. template<typename RankMap, typename Graph>
  21. bool
  22. operator()(const RankMap&, const Graph&)
  23. {
  24. return n-- == 0;
  25. }
  26. private:
  27. std::size_t n;
  28. };
  29. namespace detail {
  30. template<typename Graph, typename RankMap, typename RankMap2>
  31. void page_rank_step(const Graph& g, RankMap from_rank, RankMap2 to_rank,
  32. typename property_traits<RankMap>::value_type damping,
  33. incidence_graph_tag)
  34. {
  35. typedef typename property_traits<RankMap>::value_type rank_type;
  36. // Set new rank maps
  37. BGL_FORALL_VERTICES_T(v, g, Graph) put(to_rank, v, rank_type(1 - damping));
  38. BGL_FORALL_VERTICES_T(u, g, Graph) {
  39. rank_type u_rank_out = damping * get(from_rank, u) / out_degree(u, g);
  40. BGL_FORALL_ADJ_T(u, v, g, Graph)
  41. put(to_rank, v, get(to_rank, v) + u_rank_out);
  42. }
  43. }
  44. template<typename Graph, typename RankMap, typename RankMap2>
  45. void page_rank_step(const Graph& g, RankMap from_rank, RankMap2 to_rank,
  46. typename property_traits<RankMap>::value_type damping,
  47. bidirectional_graph_tag)
  48. {
  49. typedef typename property_traits<RankMap>::value_type damping_type;
  50. BGL_FORALL_VERTICES_T(v, g, Graph) {
  51. typename property_traits<RankMap>::value_type rank(0);
  52. BGL_FORALL_INEDGES_T(v, e, g, Graph)
  53. rank += get(from_rank, source(e, g)) / out_degree(source(e, g), g);
  54. put(to_rank, v, (damping_type(1) - damping) + damping * rank);
  55. }
  56. }
  57. } // end namespace detail
  58. template<typename Graph, typename RankMap, typename Done, typename RankMap2>
  59. void
  60. page_rank(const Graph& g, RankMap rank_map, Done done,
  61. typename property_traits<RankMap>::value_type damping,
  62. typename graph_traits<Graph>::vertices_size_type n,
  63. RankMap2 rank_map2
  64. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag))
  65. {
  66. typedef typename property_traits<RankMap>::value_type rank_type;
  67. rank_type initial_rank = rank_type(rank_type(1) / n);
  68. BGL_FORALL_VERTICES_T(v, g, Graph) put(rank_map, v, initial_rank);
  69. bool to_map_2 = true;
  70. while ((to_map_2 && !done(rank_map, g)) ||
  71. (!to_map_2 && !done(rank_map2, g))) {
  72. typedef typename graph_traits<Graph>::traversal_category category;
  73. if (to_map_2) {
  74. detail::page_rank_step(g, rank_map, rank_map2, damping, category());
  75. } else {
  76. detail::page_rank_step(g, rank_map2, rank_map, damping, category());
  77. }
  78. to_map_2 = !to_map_2;
  79. }
  80. if (!to_map_2) {
  81. BGL_FORALL_VERTICES_T(v, g, Graph) put(rank_map, v, get(rank_map2, v));
  82. }
  83. }
  84. template<typename Graph, typename RankMap, typename Done>
  85. void
  86. page_rank(const Graph& g, RankMap rank_map, Done done,
  87. typename property_traits<RankMap>::value_type damping,
  88. typename graph_traits<Graph>::vertices_size_type n)
  89. {
  90. typedef typename property_traits<RankMap>::value_type rank_type;
  91. std::vector<rank_type> ranks2(num_vertices(g));
  92. page_rank(g, rank_map, done, damping, n,
  93. make_iterator_property_map(ranks2.begin(), get(vertex_index, g)));
  94. }
  95. template<typename Graph, typename RankMap, typename Done>
  96. inline void
  97. page_rank(const Graph& g, RankMap rank_map, Done done,
  98. typename property_traits<RankMap>::value_type damping = 0.85)
  99. {
  100. page_rank(g, rank_map, done, damping, num_vertices(g));
  101. }
  102. template<typename Graph, typename RankMap>
  103. inline void
  104. page_rank(const Graph& g, RankMap rank_map)
  105. {
  106. page_rank(g, rank_map, n_iterations(20));
  107. }
  108. // TBD: this could be _much_ more efficient, using a queue to store
  109. // the vertices that should be reprocessed and keeping track of which
  110. // vertices are in the queue with a property map. Baah, this only
  111. // applies when we have a bidirectional graph.
  112. template<typename MutableGraph>
  113. void
  114. remove_dangling_links(MutableGraph& g
  115. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(MutableGraph,
  116. vertex_list_graph_tag))
  117. {
  118. typename graph_traits<MutableGraph>::vertices_size_type old_n;
  119. do {
  120. old_n = num_vertices(g);
  121. typename graph_traits<MutableGraph>::vertex_iterator vi, vi_end;
  122. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; /* in loop */) {
  123. typename graph_traits<MutableGraph>::vertex_descriptor v = *vi++;
  124. if (out_degree(v, g) == 0) {
  125. clear_vertex(v, g);
  126. remove_vertex(v, g);
  127. }
  128. }
  129. } while (num_vertices(g) < old_n);
  130. }
  131. } } // end namespace boost::graph
  132. #ifdef BOOST_GRAPH_USE_MPI
  133. # include <boost/graph/distributed/page_rank.hpp>
  134. #endif
  135. #endif // BOOST_GRAPH_PAGE_RANK_HPP