1 // Copyright 2004 The Trustees of Indiana University.
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)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_GRAPH_PARALLEL_BRANDES_BETWEENNESS_CENTRALITY_HPP
10 #define BOOST_GRAPH_PARALLEL_BRANDES_BETWEENNESS_CENTRALITY_HPP
12 #ifndef BOOST_GRAPH_USE_MPI
13 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
16 // #define COMPUTE_PATH_COUNTS_INLINE
18 #include <boost/graph/betweenness_centrality.hpp>
19 #include <boost/graph/overloading.hpp>
20 #include <boost/graph/distributed/concepts.hpp>
21 #include <boost/graph/graph_traits.hpp>
22 #include <boost/config.hpp>
23 #include <boost/assert.hpp>
25 // For additive_reducer
26 #include <boost/graph/distributed/distributed_graph_utility.hpp>
27 #include <boost/type_traits/is_convertible.hpp>
28 #include <boost/type_traits/is_same.hpp>
29 #include <boost/property_map/property_map.hpp>
30 #include <boost/graph/named_function_params.hpp>
32 #include <boost/property_map/parallel/distributed_property_map.hpp>
33 #include <boost/graph/distributed/detail/dijkstra_shortest_paths.hpp>
34 #include <boost/tuple/tuple.hpp>
36 // NGE - Needed for minstd_rand at L807, should pass vertex list
37 // or generator instead
38 #include <boost/random/linear_congruential.hpp>
46 struct append_reducer {
47 BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
50 T operator()(const K&) const { return T(); }
53 T operator()(const K&, const T& x, const T& y) const
55 T z(x.begin(), x.end());
56 for (typename T::const_iterator iter = y.begin(); iter != y.end(); ++iter)
57 if (std::find(z.begin(), z.end(), *iter) == z.end())
66 namespace serialization {
68 // TODO(nge): Write generalized serialization for tuples
69 template<typename Archive, typename T1, typename T2, typename T3,
71 void serialize(Archive & ar,
72 boost::tuple<T1,T2,T3, T4>& t,
75 ar & boost::tuples::get<0>(t);
76 ar & boost::tuples::get<1>(t);
77 ar & boost::tuples::get<2>(t);
78 ar & boost::tuples::get<3>(t);
83 template <typename OwnerMap, typename Tuple>
84 class get_owner_of_first_tuple_element {
87 typedef typename property_traits<OwnerMap>::value_type owner_type;
89 get_owner_of_first_tuple_element(OwnerMap owner) : owner(owner) { }
91 owner_type get_owner(Tuple t) { return get(owner, boost::tuples::get<0>(t)); }
97 template <typename OwnerMap, typename Tuple>
98 typename get_owner_of_first_tuple_element<OwnerMap, Tuple>::owner_type
99 get(get_owner_of_first_tuple_element<OwnerMap, Tuple> o, Tuple t)
100 { return o.get_owner(t); }
102 template <typename OwnerMap>
103 class get_owner_of_first_pair_element {
106 typedef typename property_traits<OwnerMap>::value_type owner_type;
108 get_owner_of_first_pair_element(OwnerMap owner) : owner(owner) { }
110 template <typename Vertex, typename T>
111 owner_type get_owner(std::pair<Vertex, T> p) { return get(owner, p.first); }
117 template <typename OwnerMap, typename Vertex, typename T>
118 typename get_owner_of_first_pair_element<OwnerMap>::owner_type
119 get(get_owner_of_first_pair_element<OwnerMap> o, std::pair<Vertex, T> p)
120 { return o.get_owner(p); }
122 namespace graph { namespace parallel { namespace detail {
124 template<typename DistanceMap, typename IncomingMap>
125 class betweenness_centrality_msg_value
127 typedef typename property_traits<DistanceMap>::value_type distance_type;
128 typedef typename property_traits<IncomingMap>::value_type incoming_type;
129 typedef typename incoming_type::value_type incoming_value_type;
132 typedef std::pair<distance_type, incoming_value_type> type;
134 static type create(distance_type dist, incoming_value_type source)
135 { return std::make_pair(dist, source); }
139 /************************************************************************/
140 /* Delta-stepping Betweenness Centrality */
141 /************************************************************************/
143 template<typename Graph, typename DistanceMap, typename IncomingMap,
144 typename EdgeWeightMap, typename PathCountMap
145 #ifdef COMPUTE_PATH_COUNTS_INLINE
146 , typename IsSettledMap, typename VertexIndexMap
149 class betweenness_centrality_delta_stepping_impl {
150 // Could inherit from delta_stepping_impl to get run() method
151 // but for the time being it's just reproduced here
153 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
154 typedef typename graph_traits<Graph>::degree_size_type Degree;
155 typedef typename property_traits<EdgeWeightMap>::value_type Dist;
156 typedef typename property_traits<IncomingMap>::value_type IncomingType;
157 typedef typename boost::graph::parallel::process_group_type<Graph>::type
160 typedef std::list<Vertex> Bucket;
161 typedef typename Bucket::iterator BucketIterator;
162 typedef typename std::vector<Bucket*>::size_type BucketIndex;
164 typedef betweenness_centrality_msg_value<DistanceMap, IncomingMap>
168 // Relax a remote vertex. The message contains a pair<Vertex,
169 // MessageValue>, the first part of which is the vertex whose
170 // tentative distance is being relaxed and the second part
171 // contains either the new distance (if there is no predecessor
172 // map) or a pair with the distance and predecessor.
178 // Must supply delta, ctor that guesses delta removed
179 betweenness_centrality_delta_stepping_impl(const Graph& g,
180 DistanceMap distance,
181 IncomingMap incoming,
182 EdgeWeightMap weight,
183 PathCountMap path_count,
184 #ifdef COMPUTE_PATH_COUNTS_INLINE
185 IsSettledMap is_settled,
186 VertexIndexMap vertex_index,
193 // Relax the edge (u, v), creating a new best path of distance x.
194 void relax(Vertex u, Vertex v, Dist x);
196 // Synchronize all of the processes, by receiving all messages that
197 // have not yet been received.
200 using boost::parallel::synchronize;
204 // Setup triggers for msg_relax messages
205 void setup_triggers()
207 using boost::parallel::simple_trigger;
208 simple_trigger(pg, msg_relax, this,
209 &betweenness_centrality_delta_stepping_impl::handle_msg_relax);
212 void handle_msg_relax(int /*source*/, int /*tag*/,
213 const std::pair<Vertex, typename MessageValue::type>& data,
214 boost::parallel::trigger_receive_context)
215 { relax(data.second.second, data.first, data.second.first); }
218 IncomingMap incoming;
219 DistanceMap distance;
220 EdgeWeightMap weight;
221 PathCountMap path_count;
222 #ifdef COMPUTE_PATH_COUNTS_INLINE
223 IsSettledMap is_settled;
224 VertexIndexMap vertex_index;
228 typename property_map<Graph, vertex_owner_t>::const_type owner;
229 typename property_map<Graph, vertex_local_t>::const_type local;
231 // A "property map" that contains the position of each vertex in
232 // whatever bucket it resides in.
233 std::vector<BucketIterator> position_in_bucket;
235 // Bucket data structure. The ith bucket contains all local vertices
236 // with (tentative) distance in the range [i*delta,
238 std::vector<Bucket*> buckets;
240 // This "dummy" list is used only so that we can initialize the
241 // position_in_bucket property map with non-singular iterators. This
242 // won't matter for most implementations of the C++ Standard
243 // Library, but it avoids undefined behavior and allows us to run
244 // with library "debug modes".
245 std::list<Vertex> dummy_list;
247 // A "property map" that states which vertices have been deleted
248 // from the bucket in this iteration.
249 std::vector<bool> vertex_was_deleted;
252 template<typename Graph, typename DistanceMap, typename IncomingMap,
253 typename EdgeWeightMap, typename PathCountMap
254 #ifdef COMPUTE_PATH_COUNTS_INLINE
255 , typename IsSettledMap, typename VertexIndexMap
258 betweenness_centrality_delta_stepping_impl<
259 Graph, DistanceMap, IncomingMap, EdgeWeightMap, PathCountMap
260 #ifdef COMPUTE_PATH_COUNTS_INLINE
261 , IsSettledMap, VertexIndexMap
264 betweenness_centrality_delta_stepping_impl(const Graph& g,
265 DistanceMap distance,
266 IncomingMap incoming,
267 EdgeWeightMap weight,
268 PathCountMap path_count,
269 #ifdef COMPUTE_PATH_COUNTS_INLINE
270 IsSettledMap is_settled,
271 VertexIndexMap vertex_index,
278 path_count(path_count),
279 #ifdef COMPUTE_PATH_COUNTS_INLINE
280 is_settled(is_settled),
281 vertex_index(vertex_index),
284 pg(boost::graph::parallel::process_group_adl(g), boost::parallel::attach_distributed_object()),
285 owner(get(vertex_owner, g)),
286 local(get(vertex_local, g))
288 { setup_triggers(); }
290 template<typename Graph, typename DistanceMap, typename IncomingMap,
291 typename EdgeWeightMap, typename PathCountMap
292 #ifdef COMPUTE_PATH_COUNTS_INLINE
293 , typename IsSettledMap, typename VertexIndexMap
297 betweenness_centrality_delta_stepping_impl<
298 Graph, DistanceMap, IncomingMap, EdgeWeightMap, PathCountMap
299 #ifdef COMPUTE_PATH_COUNTS_INLINE
300 , IsSettledMap, VertexIndexMap
305 typedef typename boost::graph::parallel::process_group_type<Graph>::type
307 typename process_group_type::process_id_type id = process_id(pg);
309 Dist inf = (std::numeric_limits<Dist>::max)();
311 // None of the vertices are stored in the bucket.
312 position_in_bucket.clear();
313 position_in_bucket.resize(num_vertices(g), dummy_list.end());
315 // None of the vertices have been deleted
316 vertex_was_deleted.clear();
317 vertex_was_deleted.resize(num_vertices(g), false);
319 // No path from s to any other vertex, yet
320 BGL_FORALL_VERTICES_T(v, g, Graph)
321 put(distance, v, inf);
323 // The distance to the starting node is zero
324 if (get(owner, s) == id)
325 // Put "s" into its bucket (bucket 0)
328 // Note that we know the distance to s is zero
329 cache(distance, s, 0);
331 #ifdef COMPUTE_PATH_COUNTS_INLINE
332 // Synchronize here to deliver initial relaxation since we don't
333 // synchronize at the beginning of the inner loop any more
336 // Incoming edge count map is an implementation detail and should
337 // be freed as soon as possible so build it here
338 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
340 std::vector<edges_size_type> incoming_edge_countS(num_vertices(g));
341 iterator_property_map<typename std::vector<edges_size_type>::iterator, VertexIndexMap>
342 incoming_edge_count(incoming_edge_countS.begin(), vertex_index);
345 BucketIndex max_bucket = (std::numeric_limits<BucketIndex>::max)();
346 BucketIndex current_bucket = 0;
348 #ifdef COMPUTE_PATH_COUNTS_INLINE
349 // We need to clear the outgoing map after every bucket so just build it here
350 std::vector<IncomingType> outgoingS(num_vertices(g));
351 IncomingMap outgoing(outgoingS.begin(), vertex_index);
353 outgoing.set_reduce(append_reducer<IncomingType>());
355 // Synchronize with all of the other processes.
359 // Find the next bucket that has something in it.
360 while (current_bucket < buckets.size()
361 && (!buckets[current_bucket] || buckets[current_bucket]->empty()))
363 if (current_bucket >= buckets.size())
364 current_bucket = max_bucket;
366 // Find the smallest bucket (over all processes) that has vertices
367 // that need to be processed.
368 using boost::parallel::all_reduce;
369 using boost::parallel::minimum;
370 current_bucket = all_reduce(pg, current_bucket, minimum<BucketIndex>());
372 if (current_bucket == max_bucket)
373 // There are no non-empty buckets in any process; exit.
376 // Contains the set of vertices that have been deleted in the
377 // relaxation of "light" edges. Note that we keep track of which
378 // vertices were deleted with the property map
379 // "vertex_was_deleted".
380 std::vector<Vertex> deleted_vertices;
382 // Repeatedly relax light edges
383 bool nonempty_bucket;
385 // Someone has work to do in this bucket.
387 if (current_bucket < buckets.size() && buckets[current_bucket]) {
388 Bucket& bucket = *buckets[current_bucket];
389 // For each element in the bucket
390 while (!bucket.empty()) {
391 Vertex u = bucket.front();
393 // Remove u from the front of the bucket
396 // Insert u into the set of deleted vertices, if it hasn't
397 // been done already.
398 if (!vertex_was_deleted[get(local, u)]) {
399 vertex_was_deleted[get(local, u)] = true;
400 deleted_vertices.push_back(u);
403 // Relax each light edge.
404 Dist u_dist = get(distance, u);
405 BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
406 if (get(weight, e) <= delta) // light edge
407 relax(u, target(e, g), u_dist + get(weight, e));
411 // Synchronize with all of the other processes.
414 // Is the bucket empty now?
415 nonempty_bucket = (current_bucket < buckets.size()
416 && buckets[current_bucket]
417 && !buckets[current_bucket]->empty());
418 } while (all_reduce(pg, nonempty_bucket, std::logical_or<bool>()));
420 // Relax heavy edges for each of the vertices that we previously
422 for (typename std::vector<Vertex>::iterator iter = deleted_vertices.begin();
423 iter != deleted_vertices.end(); ++iter) {
424 // Relax each heavy edge.
426 Dist u_dist = get(distance, u);
427 BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
428 if (get(weight, e) > delta) // heavy edge
429 relax(u, target(e, g), u_dist + get(weight, e));
431 #ifdef COMPUTE_PATH_COUNTS_INLINE
432 // Set outgoing paths
433 IncomingType in = get(incoming, u);
434 for (typename IncomingType::iterator pred = in.begin(); pred != in.end(); ++pred)
435 if (get(owner, *pred) == id) {
436 IncomingType x = get(outgoing, *pred);
437 if (std::find(x.begin(), x.end(), u) == x.end())
439 put(outgoing, *pred, x);
443 put(outgoing, *pred, in);
446 // Set incoming edge counts
447 put(incoming_edge_count, u, in.size());
451 #ifdef COMPUTE_PATH_COUNTS_INLINE
452 synchronize(); // Deliver heavy edge relaxations and outgoing paths
455 typedef typename property_traits<PathCountMap>::value_type PathCountType;
456 typedef std::pair<Vertex, PathCountType> queue_value_type;
457 typedef typename property_map<Graph, vertex_owner_t>::const_type OwnerMap;
458 typedef typename get_owner_of_first_pair_element<OwnerMap> IndirectOwnerMap;
460 typedef boost::queue<queue_value_type> local_queue_type;
461 typedef boost::graph::distributed::distributed_queue<process_group_type,
463 local_queue_type> dist_queue_type;
465 IndirectOwnerMap indirect_owner(owner);
466 dist_queue_type Q(pg, indirect_owner);
468 // Find sources to initialize queue
469 BGL_FORALL_VERTICES_T(v, g, Graph) {
470 if (get(is_settled, v) && !(get(outgoing, v).empty())) {
471 put(incoming_edge_count, v, 1);
472 Q.push(std::make_pair(v, 0)); // Push this vertex with no additional path count
476 // Set path counts for vertices in this bucket
478 queue_value_type t = Q.top(); Q.pop();
480 PathCountType p = t.second;
482 put(path_count, v, get(path_count, v) + p);
483 put(incoming_edge_count, v, get(incoming_edge_count, v) - 1);
485 if (get(incoming_edge_count, v) == 0) {
486 IncomingType out = get(outgoing, v);
487 for (typename IncomingType::iterator iter = out.begin(); iter != out.end(); ++iter)
488 Q.push(std::make_pair(*iter, get(path_count, v)));
492 // Mark the vertices in this bucket settled
493 for (typename std::vector<Vertex>::iterator iter = deleted_vertices.begin();
494 iter != deleted_vertices.end(); ++iter)
495 put(is_settled, *iter, true);
497 // No need to clear path count map as it is never read/written remotely
498 // No need to clear outgoing map as it is re-alloced every bucket
501 // Go to the next bucket: the current bucket must already be empty.
505 // Delete all of the buckets.
506 for (typename std::vector<Bucket*>::iterator iter = buckets.begin();
507 iter != buckets.end(); ++iter) {
515 template<typename Graph, typename DistanceMap, typename IncomingMap,
516 typename EdgeWeightMap, typename PathCountMap
517 #ifdef COMPUTE_PATH_COUNTS_INLINE
518 , typename IsSettledMap, typename VertexIndexMap
522 betweenness_centrality_delta_stepping_impl<
523 Graph, DistanceMap, IncomingMap, EdgeWeightMap, PathCountMap
524 #ifdef COMPUTE_PATH_COUNTS_INLINE
525 , IsSettledMap, VertexIndexMap
528 relax(Vertex u, Vertex v, Dist x)
531 if (x <= get(distance, v)) {
533 // We're relaxing the edge to vertex v.
534 if (get(owner, v) == process_id(pg)) {
535 if (x < get(distance, v)) {
536 // Compute the new bucket index for v
537 BucketIndex new_index = static_cast<BucketIndex>(x / delta);
539 // Make sure there is enough room in the buckets data structure.
540 if (new_index >= buckets.size()) buckets.resize(new_index + 1, 0);
542 // Make sure that we have allocated the bucket itself.
543 if (!buckets[new_index]) buckets[new_index] = new Bucket;
545 if (get(distance, v) != (std::numeric_limits<Dist>::max)()
546 && !vertex_was_deleted[get(local, v)]) {
547 // We're moving v from an old bucket into a new one. Compute
548 // the old index, then splice it in.
549 BucketIndex old_index
550 = static_cast<BucketIndex>(get(distance, v) / delta);
551 buckets[new_index]->splice(buckets[new_index]->end(),
553 position_in_bucket[get(local, v)]);
555 // We're inserting v into a bucket for the first time. Put it
557 buckets[new_index]->push_back(v);
560 // v is now at the last position in the new bucket
561 position_in_bucket[get(local, v)] = buckets[new_index]->end();
562 --position_in_bucket[get(local, v)];
564 // Update tentative distance information and incoming, path_count
565 if (u != v) put(incoming, v, IncomingType(1, u));
567 } // u != v covers initial source relaxation and self-loops
568 else if (x == get(distance, v) && u != v) {
570 // Add incoming edge if it's not already in the list
571 IncomingType in = get(incoming, v);
572 if (std::find(in.begin(), in.end(), u) == in.end()) {
574 put(incoming, v, in);
578 // The vertex is remote: send a request to the vertex's owner
579 send(pg, get(owner, v), msg_relax,
580 std::make_pair(v, MessageValue::create(x, u)));
582 // Cache tentative distance information
583 cache(distance, v, x);
588 /************************************************************************/
589 /* Shortest Paths function object for betweenness centrality */
590 /************************************************************************/
592 template<typename WeightMap>
593 struct brandes_shortest_paths {
594 typedef typename property_traits<WeightMap>::value_type weight_type;
596 brandes_shortest_paths()
597 : weight(1), delta(0) { }
598 brandes_shortest_paths(weight_type delta)
599 : weight(1), delta(delta) { }
600 brandes_shortest_paths(WeightMap w)
601 : weight(w), delta(0) { }
602 brandes_shortest_paths(WeightMap w, weight_type delta)
603 : weight(w), delta(delta) { }
605 template<typename Graph, typename IncomingMap, typename DistanceMap,
606 typename PathCountMap
607 #ifdef COMPUTE_PATH_COUNTS_INLINE
608 , typename IsSettledMap, typename VertexIndexMap
614 typename graph_traits<Graph>::vertex_descriptor s,
615 IncomingMap incoming,
616 DistanceMap distance,
617 PathCountMap path_count
618 #ifdef COMPUTE_PATH_COUNTS_INLINE
619 , IsSettledMap is_settled,
620 VertexIndexMap vertex_index
624 // The "distance" map needs to act like one, retrieving the default
625 // value of infinity.
626 set_property_map_role(vertex_distance, distance);
628 // Only calculate delta the first time operator() is called
629 // This presumes g is the same every time, but so does the fact
630 // that we're reusing the weight map
634 // TODO (NGE): Restructure the code so we don't have to construct
636 betweenness_centrality_delta_stepping_impl<
637 Graph, DistanceMap, IncomingMap, WeightMap, PathCountMap
638 #ifdef COMPUTE_PATH_COUNTS_INLINE
639 , IsSettledMap, VertexIndexMap
642 impl(g, distance, incoming, weight, path_count,
643 #ifdef COMPUTE_PATH_COUNTS_INLINE
644 is_settled, vertex_index,
653 template <typename Graph>
655 set_delta(const Graph& g)
657 using boost::parallel::all_reduce;
658 using boost::parallel::maximum;
661 typedef typename graph_traits<Graph>::degree_size_type Degree;
662 typedef weight_type Dist;
664 // Compute the maximum edge weight and degree
665 Dist max_edge_weight = 0;
666 Degree max_degree = 0;
667 BGL_FORALL_VERTICES_T(u, g, Graph) {
668 max_degree = max BOOST_PREVENT_MACRO_SUBSTITUTION (max_degree, out_degree(u, g));
669 BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
670 max_edge_weight = max BOOST_PREVENT_MACRO_SUBSTITUTION (max_edge_weight, get(weight, e));
673 max_edge_weight = all_reduce(process_group(g), max_edge_weight, maximum<Dist>());
674 max_degree = all_reduce(process_group(g), max_degree, maximum<Degree>());
676 // Take a guess at delta, based on what works well for random
678 delta = max_edge_weight / max_degree;
687 // Perform a single SSSP from the specified vertex and update the centrality map(s)
688 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
689 typename IncomingMap, typename DistanceMap, typename DependencyMap,
690 typename PathCountMap,
691 #ifdef COMPUTE_PATH_COUNTS_INLINE
692 typename IsSettledMap,
694 typename VertexIndexMap, typename ShortestPaths>
696 do_brandes_sssp(const Graph& g,
697 CentralityMap centrality,
698 EdgeCentralityMap edge_centrality_map,
699 IncomingMap incoming,
700 DistanceMap distance,
701 DependencyMap dependency,
702 PathCountMap path_count,
703 #ifdef COMPUTE_PATH_COUNTS_INLINE
704 IsSettledMap is_settled,
706 VertexIndexMap vertex_index,
707 ShortestPaths shortest_paths,
708 typename graph_traits<Graph>::vertex_descriptor s)
710 using boost::detail::graph::update_centrality;
711 using boost::graph::parallel::process_group;
713 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
714 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
716 typedef typename property_traits<IncomingMap>::value_type incoming_type;
717 typedef typename property_traits<DistanceMap>::value_type distance_type;
718 typedef typename property_traits<DependencyMap>::value_type dependency_type;
719 typedef typename property_traits<PathCountMap>::value_type path_count_type;
721 typedef typename incoming_type::iterator incoming_iterator;
723 typedef typename property_map<Graph, vertex_owner_t>::const_type OwnerMap;
724 OwnerMap owner = get(vertex_owner, g);
726 typedef typename boost::graph::parallel::process_group_type<Graph>::type
728 process_group_type pg = process_group(g);
729 typename process_group_type::process_id_type id = process_id(pg);
731 // TODO: Is it faster not to clear some of these maps?
732 // Initialize for this iteration
737 BGL_FORALL_VERTICES_T(v, g, Graph) {
738 put(path_count, v, 0);
739 put(dependency, v, 0);
742 if (get(owner, s) == id) {
743 put(incoming, s, incoming_type());
744 #ifdef COMPUTE_PATH_COUNTS_INLINE
745 put(path_count, s, 1);
746 put(is_settled, s, true);
750 // Execute the shortest paths algorithm. This will be either
751 // a weighted or unweighted customized breadth-first search,
752 shortest_paths(g, s, incoming, distance, path_count
753 #ifdef COMPUTE_PATH_COUNTS_INLINE
754 , is_settled, vertex_index
758 #ifndef COMPUTE_PATH_COUNTS_INLINE
761 // TODO: Optimize case where source has no out-edges
764 // Count of incoming edges to tell when all incoming edges have been relaxed in
765 // the induced shortest paths DAG
766 std::vector<edges_size_type> incoming_edge_countS(num_vertices(g));
767 iterator_property_map<typename std::vector<edges_size_type>::iterator, VertexIndexMap>
768 incoming_edge_count(incoming_edge_countS.begin(), vertex_index);
770 BGL_FORALL_VERTICES_T(v, g, Graph) {
771 put(incoming_edge_count, v, get(incoming, v).size());
774 if (get(owner, s) == id) {
775 put(incoming_edge_count, s, 1);
776 put(incoming, s, incoming_type());
779 std::vector<incoming_type> outgoingS(num_vertices(g));
780 iterator_property_map<typename std::vector<incoming_type>::iterator, VertexIndexMap>
781 outgoing(outgoingS.begin(), vertex_index);
783 outgoing.set_reduce(append_reducer<incoming_type>());
785 // Mark forward adjacencies in DAG of shortest paths
787 // TODO: It's possible to do this using edge flags but it's not currently done this way
788 // because during traversal of the DAG we would have to examine all out edges
789 // which would lead to more memory accesses and a larger cache footprint.
791 // In the bidirectional graph case edge flags would be an excellent way of marking
792 // edges in the DAG of shortest paths
793 BGL_FORALL_VERTICES_T(v, g, Graph) {
794 incoming_type i = get(incoming, v);
795 for (typename incoming_type::iterator iter = i.begin(); iter != i.end(); ++iter) {
796 if (get(owner, *iter) == id) {
797 incoming_type x = get(outgoing, *iter);
798 if (std::find(x.begin(), x.end(), v) == x.end())
800 put(outgoing, *iter, x);
804 put(outgoing, *iter, in);
811 // Traverse DAG induced by forward edges in dependency order and compute path counts
813 typedef std::pair<vertex_descriptor, path_count_type> queue_value_type;
814 typedef get_owner_of_first_pair_element<OwnerMap> IndirectOwnerMap;
816 typedef boost::queue<queue_value_type> local_queue_type;
817 typedef boost::graph::distributed::distributed_queue<process_group_type,
819 local_queue_type> dist_queue_type;
821 IndirectOwnerMap indirect_owner(owner);
822 dist_queue_type Q(pg, indirect_owner);
824 if (get(owner, s) == id)
825 Q.push(std::make_pair(s, 1));
828 queue_value_type t = Q.top(); Q.pop();
829 vertex_descriptor v = t.first;
830 path_count_type p = t.second;
832 put(path_count, v, get(path_count, v) + p);
833 put(incoming_edge_count, v, get(incoming_edge_count, v) - 1);
835 if (get(incoming_edge_count, v) == 0) {
836 incoming_type out = get(outgoing, v);
837 for (typename incoming_type::iterator iter = out.begin(); iter != out.end(); ++iter)
838 Q.push(std::make_pair(*iter, get(path_count, v)));
843 #endif // COMPUTE_PATH_COUNTS_INLINE
846 // Compute dependencies
850 // Build the distributed_queue
851 // Value type consists of 1) target of update 2) source of update
852 // 3) dependency of source 4) path count of source
853 typedef boost::tuple<vertex_descriptor, vertex_descriptor, dependency_type, path_count_type>
855 typedef get_owner_of_first_tuple_element<OwnerMap, queue_value_type> IndirectOwnerMap;
857 typedef boost::queue<queue_value_type> local_queue_type;
858 typedef boost::graph::distributed::distributed_queue<process_group_type,
860 local_queue_type> dist_queue_type;
862 IndirectOwnerMap indirect_owner(owner);
863 dist_queue_type Q(pg, indirect_owner);
865 // Calculate number of vertices each vertex depends on, when a vertex has been pushed
866 // that number of times then we will update it
867 // AND Request path counts of sources of incoming edges
868 std::vector<dependency_type> dependency_countS(num_vertices(g), 0);
869 iterator_property_map<typename std::vector<dependency_type>::iterator, VertexIndexMap>
870 dependency_count(dependency_countS.begin(), vertex_index);
872 dependency_count.set_reduce(boost::graph::distributed::additive_reducer<dependency_type>());
874 path_count.set_max_ghost_cells(0);
876 BGL_FORALL_VERTICES_T(v, g, Graph) {
877 if (get(distance, v) < (std::numeric_limits<distance_type>::max)()) {
878 incoming_type el = get(incoming, v);
879 for (incoming_iterator vw = el.begin(); vw != el.end(); ++vw) {
880 if (get(owner, *vw) == id)
881 put(dependency_count, *vw, get(dependency_count, *vw) + 1);
883 put(dependency_count, *vw, 1);
885 // Request path counts
886 get(path_count, *vw);
889 // request() doesn't work here, perhaps because we don't have a copy of this
890 // ghost cell already?
897 // Push vertices with non-zero distance/path count and zero dependency count
898 BGL_FORALL_VERTICES_T(v, g, Graph) {
899 if (get(distance, v) < (std::numeric_limits<distance_type>::max)()
900 && get(dependency_count, v) == 0)
901 Q.push(boost::make_tuple(v, v, get(dependency, v), get(path_count, v)));
904 dependency.set_max_ghost_cells(0);
907 queue_value_type x = Q.top(); Q.pop();
908 vertex_descriptor w = boost::tuples::get<0>(x);
909 vertex_descriptor source = boost::tuples::get<1>(x);
910 dependency_type dep = boost::tuples::get<2>(x);
911 path_count_type pc = boost::tuples::get<3>(x);
913 cache(dependency, source, dep);
914 cache(path_count, source, pc);
916 if (get(dependency_count, w) != 0)
917 put(dependency_count, w, get(dependency_count, w) - 1);
919 if (get(dependency_count, w) == 0) {
921 // Update dependency and centrality of sources of incoming edges
922 incoming_type el = get(incoming, w);
923 for (incoming_iterator vw = el.begin(); vw != el.end(); ++vw) {
924 vertex_descriptor v = *vw;
926 BOOST_ASSERT(get(path_count, w) != 0);
928 dependency_type factor = dependency_type(get(path_count, v))
929 / dependency_type(get(path_count, w));
930 factor *= (dependency_type(1) + get(dependency, w));
932 if (get(owner, v) == id)
933 put(dependency, v, get(dependency, v) + factor);
935 put(dependency, v, factor);
937 update_centrality(edge_centrality_map, v, factor);
941 update_centrality(centrality, w, get(dependency, w));
943 // Push sources of edges in incoming edge list
944 for (incoming_iterator vw = el.begin(); vw != el.end(); ++vw)
945 Q.push(boost::make_tuple(*vw, w, get(dependency, w), get(path_count, w)));
950 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
951 typename IncomingMap, typename DistanceMap, typename DependencyMap,
952 typename PathCountMap, typename VertexIndexMap, typename ShortestPaths,
955 brandes_betweenness_centrality_impl(const Graph& g,
956 CentralityMap centrality,
957 EdgeCentralityMap edge_centrality_map,
958 IncomingMap incoming,
959 DistanceMap distance,
960 DependencyMap dependency,
961 PathCountMap path_count,
962 VertexIndexMap vertex_index,
963 ShortestPaths shortest_paths,
966 using boost::detail::graph::init_centrality_map;
967 using boost::detail::graph::divide_centrality_by_two;
968 using boost::graph::parallel::process_group;
970 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
972 typedef typename property_traits<DistanceMap>::value_type distance_type;
973 typedef typename property_traits<DependencyMap>::value_type dependency_type;
975 // Initialize centrality
976 init_centrality_map(vertices(g), centrality);
977 init_centrality_map(edges(g), edge_centrality_map);
979 // Set the reduction operation on the dependency map to be addition
980 dependency.set_reduce(boost::graph::distributed::additive_reducer<dependency_type>());
981 distance.set_reduce(boost::graph::distributed::choose_min_reducer<distance_type>());
983 // Don't allow remote procs to write incoming or path_count maps
984 // updating them is handled inside the betweenness_centrality_queue
985 incoming.set_consistency_model(0);
986 path_count.set_consistency_model(0);
988 typedef typename boost::graph::parallel::process_group_type<Graph>::type
990 process_group_type pg = process_group(g);
992 #ifdef COMPUTE_PATH_COUNTS_INLINE
993 // Build is_settled maps
994 std::vector<bool> is_settledS(num_vertices(g));
995 typedef iterator_property_map<std::vector<bool>::iterator, VertexIndexMap>
998 IsSettledMap is_settled(is_settledS.begin(), vertex_index);
1001 if (!sources.empty()) {
1003 while (!sources.empty()) {
1004 do_brandes_sssp(g, centrality, edge_centrality_map, incoming, distance,
1005 dependency, path_count,
1006 #ifdef COMPUTE_PATH_COUNTS_INLINE
1009 vertex_index, shortest_paths, sources.top());
1012 } else { // Exact Betweenness Centrality
1013 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
1014 vertices_size_type n = num_vertices(g);
1015 n = boost::parallel::all_reduce(pg, n, std::plus<vertices_size_type>());
1017 for (vertices_size_type i = 0; i < n; ++i) {
1018 vertex_descriptor v = vertex(i, g);
1020 do_brandes_sssp(g, centrality, edge_centrality_map, incoming, distance,
1021 dependency, path_count,
1022 #ifdef COMPUTE_PATH_COUNTS_INLINE
1025 vertex_index, shortest_paths, v);
1029 typedef typename graph_traits<Graph>::directed_category directed_category;
1030 const bool is_undirected =
1031 is_convertible<directed_category*, undirected_tag*>::value;
1032 if (is_undirected) {
1033 divide_centrality_by_two(vertices(g), centrality);
1034 divide_centrality_by_two(edges(g), edge_centrality_map);
1038 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1039 typename IncomingMap, typename DistanceMap, typename DependencyMap,
1040 typename PathCountMap, typename VertexIndexMap, typename ShortestPaths,
1043 do_sequential_brandes_sssp(const Graph& g,
1044 CentralityMap centrality,
1045 EdgeCentralityMap edge_centrality_map,
1046 IncomingMap incoming,
1047 DistanceMap distance,
1048 DependencyMap dependency,
1049 PathCountMap path_count,
1050 VertexIndexMap vertex_index,
1051 ShortestPaths shortest_paths,
1052 Stack& ordered_vertices,
1053 typename graph_traits<Graph>::vertex_descriptor v)
1055 using boost::detail::graph::update_centrality;
1057 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
1059 // Initialize for this iteration
1060 BGL_FORALL_VERTICES_T(w, g, Graph) {
1061 // put(path_count, w, 0);
1062 incoming[w].clear();
1063 put(dependency, w, 0);
1066 put(path_count, v, 1);
1067 incoming[v].clear();
1069 // Execute the shortest paths algorithm. This will be either
1070 // Dijkstra's algorithm or a customized breadth-first search,
1071 // depending on whether the graph is weighted or unweighted.
1072 shortest_paths(g, v, ordered_vertices, incoming, distance,
1073 path_count, vertex_index);
1075 while (!ordered_vertices.empty()) {
1076 vertex_descriptor w = ordered_vertices.top();
1077 ordered_vertices.pop();
1079 typedef typename property_traits<IncomingMap>::value_type
1081 typedef typename incoming_type::iterator incoming_iterator;
1082 typedef typename property_traits<DependencyMap>::value_type
1085 for (incoming_iterator vw = incoming[w].begin();
1086 vw != incoming[w].end(); ++vw) {
1087 vertex_descriptor v = source(*vw, g);
1088 dependency_type factor = dependency_type(get(path_count, v))
1089 / dependency_type(get(path_count, w));
1090 factor *= (dependency_type(1) + get(dependency, w));
1091 put(dependency, v, get(dependency, v) + factor);
1092 update_centrality(edge_centrality_map, *vw, factor);
1096 update_centrality(centrality, w, get(dependency, w));
1101 // Betweenness Centrality variant that duplicates graph across processors
1102 // and parallizes SSSPs
1103 // This function expects a non-distributed graph and property-maps
1104 template<typename ProcessGroup, typename Graph,
1105 typename CentralityMap, typename EdgeCentralityMap,
1106 typename IncomingMap, typename DistanceMap,
1107 typename DependencyMap, typename PathCountMap,
1108 typename VertexIndexMap, typename ShortestPaths,
1111 non_distributed_brandes_betweenness_centrality_impl(const ProcessGroup& pg,
1113 CentralityMap centrality,
1114 EdgeCentralityMap edge_centrality_map,
1115 IncomingMap incoming, // P
1116 DistanceMap distance, // d
1117 DependencyMap dependency, // delta
1118 PathCountMap path_count, // sigma
1119 VertexIndexMap vertex_index,
1120 ShortestPaths shortest_paths,
1123 using boost::detail::graph::init_centrality_map;
1124 using boost::detail::graph::divide_centrality_by_two;
1125 using boost::graph::parallel::process_group;
1127 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
1129 typedef ProcessGroup process_group_type;
1131 typename process_group_type::process_id_type id = process_id(pg);
1132 typename process_group_type::process_size_type p = num_processes(pg);
1134 // Initialize centrality
1135 init_centrality_map(vertices(g), centrality);
1136 init_centrality_map(edges(g), edge_centrality_map);
1138 std::stack<vertex_descriptor> ordered_vertices;
1140 if (!sources.empty()) {
1141 std::vector<vertex_descriptor> local_sources;
1143 for (int i = 0; i < id; ++i) if (!sources.empty()) sources.pop();
1144 while (!sources.empty()) {
1145 local_sources.push_back(sources.top());
1147 for (int i = 0; i < p; ++i) if (!sources.empty()) sources.pop();
1151 for(size_t i = 0; i < local_sources.size(); ++i)
1152 do_sequential_brandes_sssp(g, centrality, edge_centrality_map, incoming,
1153 distance, dependency, path_count, vertex_index,
1154 shortest_paths, ordered_vertices, local_sources[i]);
1156 } else { // Exact Betweenness Centrality
1157 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
1158 vertices_size_type n = num_vertices(g);
1160 for (vertices_size_type i = id; i < n; i += p) {
1161 vertex_descriptor v = vertex(i, g);
1163 do_sequential_brandes_sssp(g, centrality, edge_centrality_map, incoming,
1164 distance, dependency, path_count, vertex_index,
1165 shortest_paths, ordered_vertices, v);
1169 typedef typename graph_traits<Graph>::directed_category directed_category;
1170 const bool is_undirected =
1171 is_convertible<directed_category*, undirected_tag*>::value;
1172 if (is_undirected) {
1173 divide_centrality_by_two(vertices(g), centrality);
1174 divide_centrality_by_two(edges(g), edge_centrality_map);
1177 // Merge the centrality maps by summing the values at each vertex)
1178 // TODO(nge): this copy-out, reduce, copy-in is lame
1179 typedef typename property_traits<CentralityMap>::value_type centrality_type;
1180 typedef typename property_traits<EdgeCentralityMap>::value_type edge_centrality_type;
1182 std::vector<centrality_type> centrality_v(num_vertices(g));
1183 std::vector<edge_centrality_type> edge_centrality_v;
1184 edge_centrality_v.reserve(num_edges(g));
1186 BGL_FORALL_VERTICES_T(v, g, Graph) {
1187 centrality_v[get(vertex_index, v)] = get(centrality, v);
1190 // Skip when EdgeCentralityMap is a dummy_property_map
1191 if (!is_same<EdgeCentralityMap, dummy_property_map>::value) {
1192 BGL_FORALL_EDGES_T(e, g, Graph) {
1193 edge_centrality_v.push_back(get(edge_centrality_map, e));
1195 // NGE: If we trust that the order of elements in the vector isn't changed in the
1196 // all_reduce below then this method avoids the need for an edge index map
1199 using boost::parallel::all_reduce;
1201 all_reduce(pg, ¢rality_v[0], ¢rality_v[centrality_v.size()],
1202 ¢rality_v[0], std::plus<centrality_type>());
1204 if (edge_centrality_v.size())
1205 all_reduce(pg, &edge_centrality_v[0], &edge_centrality_v[edge_centrality_v.size()],
1206 &edge_centrality_v[0], std::plus<edge_centrality_type>());
1208 BGL_FORALL_VERTICES_T(v, g, Graph) {
1209 put(centrality, v, centrality_v[get(vertex_index, v)]);
1212 // Skip when EdgeCentralityMap is a dummy_property_map
1213 if (!is_same<EdgeCentralityMap, dummy_property_map>::value) {
1215 BGL_FORALL_EDGES_T(e, g, Graph) {
1216 put(edge_centrality_map, e, edge_centrality_v[i]);
1222 } } } // end namespace graph::parallel::detail
1224 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1225 typename IncomingMap, typename DistanceMap, typename DependencyMap,
1226 typename PathCountMap, typename VertexIndexMap, typename Buffer>
1228 brandes_betweenness_centrality(const Graph& g,
1229 CentralityMap centrality,
1230 EdgeCentralityMap edge_centrality_map,
1231 IncomingMap incoming,
1232 DistanceMap distance,
1233 DependencyMap dependency,
1234 PathCountMap path_count,
1235 VertexIndexMap vertex_index,
1237 typename property_traits<DistanceMap>::value_type delta
1238 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,distributed_graph_tag))
1240 typedef typename property_traits<DistanceMap>::value_type distance_type;
1241 typedef static_property_map<distance_type> WeightMap;
1243 graph::parallel::detail::brandes_shortest_paths<WeightMap>
1244 shortest_paths(delta);
1246 graph::parallel::detail::brandes_betweenness_centrality_impl(g, centrality,
1247 edge_centrality_map,
1249 dependency, path_count,
1255 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1256 typename IncomingMap, typename DistanceMap, typename DependencyMap,
1257 typename PathCountMap, typename VertexIndexMap, typename WeightMap,
1260 brandes_betweenness_centrality(const Graph& g,
1261 CentralityMap centrality,
1262 EdgeCentralityMap edge_centrality_map,
1263 IncomingMap incoming,
1264 DistanceMap distance,
1265 DependencyMap dependency,
1266 PathCountMap path_count,
1267 VertexIndexMap vertex_index,
1269 typename property_traits<WeightMap>::value_type delta,
1270 WeightMap weight_map
1271 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,distributed_graph_tag))
1273 graph::parallel::detail::brandes_shortest_paths<WeightMap> shortest_paths(weight_map, delta);
1275 graph::parallel::detail::brandes_betweenness_centrality_impl(g, centrality,
1276 edge_centrality_map,
1278 dependency, path_count,
1284 namespace graph { namespace parallel { namespace detail {
1285 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1286 typename WeightMap, typename VertexIndexMap, typename Buffer>
1288 brandes_betweenness_centrality_dispatch2(const Graph& g,
1289 CentralityMap centrality,
1290 EdgeCentralityMap edge_centrality_map,
1291 WeightMap weight_map,
1292 VertexIndexMap vertex_index,
1294 typename property_traits<WeightMap>::value_type delta)
1296 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
1297 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
1298 typedef typename mpl::if_c<(is_same<CentralityMap,
1299 dummy_property_map>::value),
1301 CentralityMap>::type a_centrality_map;
1302 typedef typename property_traits<a_centrality_map>::value_type
1305 typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
1307 std::vector<std::vector<vertex_descriptor> > incoming(V);
1308 std::vector<centrality_type> distance(V);
1309 std::vector<centrality_type> dependency(V);
1310 std::vector<degree_size_type> path_count(V);
1312 brandes_betweenness_centrality(
1313 g, centrality, edge_centrality_map,
1314 make_iterator_property_map(incoming.begin(), vertex_index),
1315 make_iterator_property_map(distance.begin(), vertex_index),
1316 make_iterator_property_map(dependency.begin(), vertex_index),
1317 make_iterator_property_map(path_count.begin(), vertex_index),
1318 vertex_index, unwrap_ref(sources), delta,
1322 // TODO: Should the type of the distance and dependency map depend on the
1323 // value type of the centrality map?
1324 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1325 typename VertexIndexMap, typename Buffer>
1327 brandes_betweenness_centrality_dispatch2(const Graph& g,
1328 CentralityMap centrality,
1329 EdgeCentralityMap edge_centrality_map,
1330 VertexIndexMap vertex_index,
1332 typename graph_traits<Graph>::edges_size_type delta)
1334 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
1335 typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
1336 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
1338 typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
1340 std::vector<std::vector<vertex_descriptor> > incoming(V);
1341 std::vector<edges_size_type> distance(V);
1342 std::vector<edges_size_type> dependency(V);
1343 std::vector<degree_size_type> path_count(V);
1345 brandes_betweenness_centrality(
1346 g, centrality, edge_centrality_map,
1347 make_iterator_property_map(incoming.begin(), vertex_index),
1348 make_iterator_property_map(distance.begin(), vertex_index),
1349 make_iterator_property_map(dependency.begin(), vertex_index),
1350 make_iterator_property_map(path_count.begin(), vertex_index),
1351 vertex_index, unwrap_ref(sources), delta);
1354 template<typename WeightMap>
1355 struct brandes_betweenness_centrality_dispatch1
1357 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1358 typename VertexIndexMap, typename Buffer>
1360 run(const Graph& g, CentralityMap centrality, EdgeCentralityMap edge_centrality_map,
1361 VertexIndexMap vertex_index, Buffer sources,
1362 typename property_traits<WeightMap>::value_type delta, WeightMap weight_map)
1364 boost::graph::parallel::detail::brandes_betweenness_centrality_dispatch2(
1365 g, centrality, edge_centrality_map, weight_map, vertex_index, sources, delta);
1370 struct brandes_betweenness_centrality_dispatch1<boost::param_not_found>
1372 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap,
1373 typename VertexIndexMap, typename Buffer>
1375 run(const Graph& g, CentralityMap centrality, EdgeCentralityMap edge_centrality_map,
1376 VertexIndexMap vertex_index, Buffer sources,
1377 typename graph_traits<Graph>::edges_size_type delta,
1378 boost::param_not_found)
1380 boost::graph::parallel::detail::brandes_betweenness_centrality_dispatch2(
1381 g, centrality, edge_centrality_map, vertex_index, sources, delta);
1385 } } } // end namespace graph::parallel::detail
1387 template<typename Graph, typename Param, typename Tag, typename Rest>
1389 brandes_betweenness_centrality(const Graph& g,
1390 const bgl_named_params<Param,Tag,Rest>& params
1391 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,distributed_graph_tag))
1393 typedef bgl_named_params<Param,Tag,Rest> named_params;
1395 typedef queue<typename graph_traits<Graph>::vertex_descriptor> queue_t;
1398 typedef typename get_param_type<edge_weight_t, named_params>::type ew_param;
1399 typedef typename detail::choose_impl_result<mpl::true_, Graph, ew_param, edge_weight_t>::type ew;
1400 graph::parallel::detail::brandes_betweenness_centrality_dispatch1<ew>::run(
1402 choose_param(get_param(params, vertex_centrality),
1403 dummy_property_map()),
1404 choose_param(get_param(params, edge_centrality),
1405 dummy_property_map()),
1406 choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
1407 choose_param(get_param(params, buffer_param_t()), boost::ref(q)),
1408 choose_param(get_param(params, lookahead_t()), 0),
1409 choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
1412 template<typename Graph, typename CentralityMap>
1414 brandes_betweenness_centrality(const Graph& g, CentralityMap centrality
1415 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,distributed_graph_tag))
1417 typedef queue<typename graph_traits<Graph>::vertex_descriptor> queue_t;
1420 boost::graph::parallel::detail::brandes_betweenness_centrality_dispatch2(
1421 g, centrality, dummy_property_map(), get(vertex_index, g), boost::ref(q), 0);
1424 template<typename Graph, typename CentralityMap, typename EdgeCentralityMap>
1426 brandes_betweenness_centrality(const Graph& g, CentralityMap centrality,
1427 EdgeCentralityMap edge_centrality_map
1428 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,distributed_graph_tag))
1430 typedef queue<int> queue_t;
1433 boost::graph::parallel::detail::brandes_betweenness_centrality_dispatch2(
1434 g, centrality, edge_centrality_map, get(vertex_index, g), boost::ref(q), 0);
1437 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1438 typename EdgeCentralityMap, typename IncomingMap, typename DistanceMap,
1439 typename DependencyMap, typename PathCountMap, typename VertexIndexMap,
1442 non_distributed_brandes_betweenness_centrality(const ProcessGroup& pg,
1444 CentralityMap centrality,
1445 EdgeCentralityMap edge_centrality_map,
1446 IncomingMap incoming,
1447 DistanceMap distance,
1448 DependencyMap dependency,
1449 PathCountMap path_count,
1450 VertexIndexMap vertex_index,
1453 detail::graph::brandes_unweighted_shortest_paths shortest_paths;
1455 graph::parallel::detail::non_distributed_brandes_betweenness_centrality_impl(pg, g, centrality,
1456 edge_centrality_map,
1458 dependency, path_count,
1464 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1465 typename EdgeCentralityMap, typename IncomingMap, typename DistanceMap,
1466 typename DependencyMap, typename PathCountMap, typename VertexIndexMap,
1467 typename WeightMap, typename Buffer>
1469 non_distributed_brandes_betweenness_centrality(const ProcessGroup& pg,
1471 CentralityMap centrality,
1472 EdgeCentralityMap edge_centrality_map,
1473 IncomingMap incoming,
1474 DistanceMap distance,
1475 DependencyMap dependency,
1476 PathCountMap path_count,
1477 VertexIndexMap vertex_index,
1478 WeightMap weight_map,
1481 detail::graph::brandes_dijkstra_shortest_paths<WeightMap> shortest_paths(weight_map);
1483 graph::parallel::detail::non_distributed_brandes_betweenness_centrality_impl(pg, g, centrality,
1484 edge_centrality_map,
1486 dependency, path_count,
1492 namespace detail { namespace graph {
1493 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1494 typename EdgeCentralityMap, typename WeightMap, typename VertexIndexMap,
1497 non_distributed_brandes_betweenness_centrality_dispatch2(const ProcessGroup& pg,
1499 CentralityMap centrality,
1500 EdgeCentralityMap edge_centrality_map,
1501 WeightMap weight_map,
1502 VertexIndexMap vertex_index,
1505 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
1506 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
1507 typedef typename mpl::if_c<(is_same<CentralityMap,
1508 dummy_property_map>::value),
1510 CentralityMap>::type a_centrality_map;
1511 typedef typename property_traits<a_centrality_map>::value_type
1514 typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
1516 std::vector<std::vector<edge_descriptor> > incoming(V);
1517 std::vector<centrality_type> distance(V);
1518 std::vector<centrality_type> dependency(V);
1519 std::vector<degree_size_type> path_count(V);
1521 non_distributed_brandes_betweenness_centrality(
1522 pg, g, centrality, edge_centrality_map,
1523 make_iterator_property_map(incoming.begin(), vertex_index),
1524 make_iterator_property_map(distance.begin(), vertex_index),
1525 make_iterator_property_map(dependency.begin(), vertex_index),
1526 make_iterator_property_map(path_count.begin(), vertex_index),
1527 vertex_index, weight_map, unwrap_ref(sources));
1531 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1532 typename EdgeCentralityMap, typename VertexIndexMap, typename Buffer>
1534 non_distributed_brandes_betweenness_centrality_dispatch2(const ProcessGroup& pg,
1536 CentralityMap centrality,
1537 EdgeCentralityMap edge_centrality_map,
1538 VertexIndexMap vertex_index,
1541 typedef typename graph_traits<Graph>::degree_size_type degree_size_type;
1542 typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
1543 typedef typename mpl::if_c<(is_same<CentralityMap,
1544 dummy_property_map>::value),
1546 CentralityMap>::type a_centrality_map;
1547 typedef typename property_traits<a_centrality_map>::value_type
1550 typename graph_traits<Graph>::vertices_size_type V = num_vertices(g);
1552 std::vector<std::vector<edge_descriptor> > incoming(V);
1553 std::vector<centrality_type> distance(V);
1554 std::vector<centrality_type> dependency(V);
1555 std::vector<degree_size_type> path_count(V);
1557 non_distributed_brandes_betweenness_centrality(
1558 pg, g, centrality, edge_centrality_map,
1559 make_iterator_property_map(incoming.begin(), vertex_index),
1560 make_iterator_property_map(distance.begin(), vertex_index),
1561 make_iterator_property_map(dependency.begin(), vertex_index),
1562 make_iterator_property_map(path_count.begin(), vertex_index),
1563 vertex_index, unwrap_ref(sources));
1566 template<typename WeightMap>
1567 struct non_distributed_brandes_betweenness_centrality_dispatch1
1569 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1570 typename EdgeCentralityMap, typename VertexIndexMap, typename Buffer>
1572 run(const ProcessGroup& pg, const Graph& g, CentralityMap centrality,
1573 EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index,
1574 Buffer sources, WeightMap weight_map)
1576 non_distributed_brandes_betweenness_centrality_dispatch2(pg, g, centrality, edge_centrality_map,
1577 weight_map, vertex_index, sources);
1582 struct non_distributed_brandes_betweenness_centrality_dispatch1<param_not_found>
1584 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1585 typename EdgeCentralityMap, typename VertexIndexMap, typename Buffer>
1587 run(const ProcessGroup& pg, const Graph& g, CentralityMap centrality,
1588 EdgeCentralityMap edge_centrality_map, VertexIndexMap vertex_index,
1589 Buffer sources, param_not_found)
1591 non_distributed_brandes_betweenness_centrality_dispatch2(pg, g, centrality, edge_centrality_map,
1592 vertex_index, sources);
1596 } } // end namespace detail::graph
1598 template<typename ProcessGroup, typename Graph, typename Param, typename Tag, typename Rest>
1600 non_distributed_brandes_betweenness_centrality(const ProcessGroup& pg, const Graph& g,
1601 const bgl_named_params<Param,Tag,Rest>& params)
1603 typedef bgl_named_params<Param,Tag,Rest> named_params;
1605 typedef queue<int> queue_t;
1608 typedef typename get_param_type<edge_weight_t, named_params>::type ew_param;
1609 typedef typename detail::choose_impl_result<mpl::true_, Graph, ew_param, edge_weight_t>::type ew;
1610 detail::graph::non_distributed_brandes_betweenness_centrality_dispatch1<ew>::run(
1612 choose_param(get_param(params, vertex_centrality),
1613 dummy_property_map()),
1614 choose_param(get_param(params, edge_centrality),
1615 dummy_property_map()),
1616 choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
1617 choose_param(get_param(params, buffer_param_t()), boost::ref(q)),
1618 choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
1621 template<typename ProcessGroup, typename Graph, typename CentralityMap>
1623 non_distributed_brandes_betweenness_centrality(const ProcessGroup& pg, const Graph& g,
1624 CentralityMap centrality)
1626 typedef queue<int> queue_t;
1629 detail::graph::non_distributed_brandes_betweenness_centrality_dispatch2(
1630 pg, g, centrality, dummy_property_map(), get(vertex_index, g), boost::ref(q));
1633 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1636 non_distributed_brandes_betweenness_centrality(const ProcessGroup& pg, const Graph& g,
1637 CentralityMap centrality, Buffer sources)
1639 detail::graph::non_distributed_brandes_betweenness_centrality_dispatch2(
1640 pg, g, centrality, dummy_property_map(), get(vertex_index, g), sources);
1643 template<typename ProcessGroup, typename Graph, typename CentralityMap,
1644 typename EdgeCentralityMap, typename Buffer>
1646 non_distributed_brandes_betweenness_centrality(const ProcessGroup& pg, const Graph& g,
1647 CentralityMap centrality,
1648 EdgeCentralityMap edge_centrality_map,
1651 detail::graph::non_distributed_brandes_betweenness_centrality_dispatch2(
1652 pg, g, centrality, edge_centrality_map, get(vertex_index, g), sources);
1655 // Compute the central point dominance of a graph.
1656 // TODO: Make sure central point dominance works in parallel case
1657 template<typename Graph, typename CentralityMap>
1658 typename property_traits<CentralityMap>::value_type
1659 central_point_dominance(const Graph& g, CentralityMap centrality
1660 BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,distributed_graph_tag))
1664 typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
1665 typedef typename property_traits<CentralityMap>::value_type centrality_type;
1666 typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
1668 typedef typename boost::graph::parallel::process_group_type<Graph>::type
1670 process_group_type pg = boost::graph::parallel::process_group(g);
1672 vertices_size_type n = num_vertices(g);
1674 using boost::parallel::all_reduce;
1675 n = all_reduce(pg, n, std::plus<vertices_size_type>());
1677 // Find max centrality
1678 centrality_type max_centrality(0);
1679 vertex_iterator v, v_end;
1680 for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
1681 max_centrality = (max)(max_centrality, get(centrality, *v));
1684 // All reduce to get global max centrality
1685 max_centrality = all_reduce(pg, max_centrality, boost::parallel::maximum<centrality_type>());
1687 // Compute central point dominance
1688 centrality_type sum(0);
1689 for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
1690 sum += (max_centrality - get(centrality, *v));
1693 sum = all_reduce(pg, sum, std::plus<centrality_type>());
1698 } // end namespace boost
1700 #endif // BOOST_GRAPH_PARALLEL_BRANDES_BETWEENNESS_CENTRALITY_HPP