annotate DEPENDENCIES/generic/include/boost/graph/page_rank.hpp @ 133:4acb5d8d80b6 tip

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