This documentation is being rewritten. If something looks off, please cross-check with the Boost 1.91.0 Boost.Graph docs and open an issue.

Closeness Centrality

Measures how close a vertex is to all other vertices. Computed as the inverse of the sum of shortest distances from that vertex to every other vertex. A high closeness means the vertex can reach all others quickly.

Complexity: O(V * (V + E) log V) (Dijkstra per vertex)
Defined in: <boost/graph/closeness_centrality.hpp>

Example

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/closeness_centrality.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>

// Bundled edge property holding the weight
struct EdgeProps {
    int weight;
};

int main() {
    // Undirected graph with bundled edge properties
    using Graph = boost::adjacency_list<
        boost::vecS, boost::vecS, boost::undirectedS,
        boost::no_property, EdgeProps>;
    using Vertex = boost::graph_traits<Graph>::vertex_descriptor;

    Graph g{5};

    // Build a small weighted undirected graph:
    //   0 --1-- 1 --2-- 2
    //   |               |
    //   3               1
    //   |               |
    //   3 ------2------ 4
    add_edge(0, 1, EdgeProps{1}, g);
    add_edge(1, 2, EdgeProps{2}, g);
    add_edge(0, 3, EdgeProps{3}, g);
    add_edge(2, 4, EdgeProps{1}, g);
    add_edge(3, 4, EdgeProps{2}, g);

    auto weight_map = boost::get(&EdgeProps::weight, g);
    auto index_map = boost::get(boost::vertex_index, g);

    std::cout << std::fixed << std::setprecision(4);
    std::cout << "Closeness centrality (1 / sum of shortest distances):\n";

    for (Vertex v = 0; v < boost::num_vertices(g); ++v) {
        // Run Dijkstra from vertex v to get shortest distances
        auto n = boost::num_vertices(g);
        std::vector<int> distances(n);
        std::vector<Vertex> predecessors(n);
        auto dist_map = boost::make_iterator_property_map(
            distances.begin(), index_map);
        auto pred_map = boost::make_iterator_property_map(
            predecessors.begin(), index_map);

        boost::dijkstra_shortest_paths(g, v,
            pred_map, dist_map, weight_map, index_map,
            std::less<int>{}, std::plus<int>{},
            (std::numeric_limits<int>::max)(), int{0},
            boost::default_dijkstra_visitor{});

        // Compute closeness centrality from the distance map
        double cc = boost::closeness_centrality(g, dist_map);
        std::cout << "  vertex " << v << ": " << cc << "\n";
    }
}
Closeness centrality (1 / sum of shortest distances):
  vertex 0: 0.0909
  vertex 1: 0.1000
  vertex 2: 0.1111
  vertex 3: 0.0833
  vertex 4: 0.1000

(1) closeness_centrality with explicit combinator

template <typename Graph, typename DistanceMap,
          typename Measure, typename Combinator>
typename Measure::result_type
closeness_centrality(const Graph& g, DistanceMap dist,
                     Measure measure, Combinator combine);

Combines distances via combine (e.g. std::plus) before applying the measure.


(2) closeness_centrality with measure

template <typename Graph, typename DistanceMap, typename Measure>
typename Measure::result_type
closeness_centrality(const Graph& g, DistanceMap dist, Measure measure);

Equivalent to (1) with combine = std::plus<distance_type>().


(3) closeness_centrality with default measure (double)

template <typename Graph, typename DistanceMap>
double closeness_centrality(const Graph& g, DistanceMap dist);

Uses measure_closeness(g, dist) as the measure (reciprocal of sum of distances).


(4) closeness_centrality with explicit result type

template <typename T, typename Graph, typename DistanceMap>
T closeness_centrality(const Graph& g, DistanceMap dist);

Same as (3) but the result type is parametrized as T.


(5) all_closeness_centralities with measure

template <typename Graph, typename DistanceMatrixMap,
          typename CentralityMap, typename Measure>
void all_closeness_centralities(const Graph& g, DistanceMatrixMap dist,
                                CentralityMap cent, Measure measure);

For each vertex, runs overload (2) against its row of dist and writes the result into cent.


(6) all_closeness_centralities with default measure

template <typename Graph, typename DistanceMatrixMap,
          typename CentralityMap>
void all_closeness_centralities(const Graph& g, DistanceMatrixMap dist,
                                CentralityMap cent);

Equivalent to (5) with measure_closeness<Result>(g, DistanceMap()) where Result is the value type of CentralityMap.


(7) Helper: measure_closeness

template <typename Graph, typename DistanceMap>
closeness_measure</*...*/, double, reciprocal<double>>
measure_closeness(const Graph&, DistanceMap);

template <typename T, typename Graph, typename DistanceMap>
closeness_measure</*...*/, T, reciprocal<T>>
measure_closeness(const Graph&, DistanceMap);

template <typename T, typename Graph, typename DistanceMap, typename Reciprocal>
closeness_measure</*...*/, T, Reciprocal>
measure_closeness(const Graph&, DistanceMap);

Constructs the default closeness_measure functor. Three overloads let you choose the result type (double or T) and the reciprocal functor.


(8) closeness_measure (struct)

template <typename Graph, typename DistanceType, typename ResultType,
          typename Reciprocal = detail::reciprocal<ResultType>>
struct closeness_measure
    : public geodesic_measure<Graph, DistanceType, ResultType>;

The default closeness functor: given a summed distance d, returns 0 if d is the infinite distance marker, otherwise Reciprocal()(d).

Parameters

Direction Parameter Description

IN

const Graph& g

Must model Vertex List Graph for overloads that iterate vertices; the 2/3/4-arg overloads only require Graph.

IN

DistanceMap dist

ReadablePropertyMap from vertex descriptor to distance. Must be populated by a shortest-paths algorithm (e.g. Dijkstra or BFS) before calling.

IN

DistanceMatrixMap dist (all_*)

ReadablePropertyMap from vertex to another property map (the row for that vertex).

IN

Measure measure

A DistanceMeasureConcept functor mapping summed distance to result. Default constructed via measure_closeness.

IN

Combinator combine

Binary operation used to reduce distances to a single scalar. Default in (2) is std::plus<distance_type>.

OUT

CentralityMap cent

(all_* only) WritablePropertyMap receiving the per-vertex closeness.

The single-vertex overloads take a pre-computed distance map, not the graph alone. Run a shortest-paths algorithm first to fill the distance map.