Degree Centrality :: Boost.Graph Documentation

# Degree Centrality

Measures vertex importance by the number of connections. Out-degree (influence) counts outgoing edges; in-degree (prestige) counts incoming edges.

Complexity: O(1) per vertex
Defined in: `<boost/graph/degree_centrality.hpp>`

## Example

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/degree_centrality.hpp>
    #include <iostream>

    // Bundled vertex property
    struct VertexProps {
        std::string name;
    };

    int main() {
        // Bidirectional graph with bundled vertex properties
        // (bidirectionalS is needed so that in-degree / prestige can be computed)
        using Graph = boost::adjacency_list<
            boost::vecS, boost::vecS, boost::bidirectionalS, VertexProps>;
        using Vertex = boost::graph_traits<Graph>::vertex_descriptor;

        Graph g(5);
        g[0].name = "Alice";
        g[1].name = "Bob";
        g[2].name = "Carol";
        g[3].name = "Dave";
        g[4].name = "Eve";

        // Build a small directed network:
        //   Alice -> Bob, Alice -> Carol, Alice -> Dave
        //   Bob -> Carol
        //   Carol -> Dave, Carol -> Eve
        //   Dave -> Eve
        add_edge(0, 1, g);
        add_edge(0, 2, g);
        add_edge(0, 3, g);
        add_edge(1, 2, g);
        add_edge(2, 3, g);
        add_edge(2, 4, g);
        add_edge(3, 4, g);

        std::cout << "Degree centrality (influence = out-degree):\n";
        for (Vertex v = 0; v < boost::num_vertices(g); ++v) {
            auto c = boost::degree_centrality(g, v, boost::measure_influence(g));
            std::cout << "  " << g[v].name << ": " << c << "\n";
        }

        std::cout << "\nDegree centrality (prestige = in-degree):\n";
        for (Vertex v = 0; v < boost::num_vertices(g); ++v) {
            auto c = boost::degree_centrality(g, v, boost::measure_prestige(g));
            std::cout << "  " << g[v].name << ": " << c << "\n";
        }
    }

    Degree centrality (influence = out-degree):
      Alice: 3
      Bob: 1
      Carol: 2
      Dave: 1
      Eve: 0

    Degree centrality (prestige = in-degree):
      Alice: 0
      Bob: 1
      Carol: 2
      Dave: 2
      Eve: 2

### (1) degree_centrality with explicit measure

    template <typename Graph, typename Vertex, typename Measure>
    typename Measure::degree_type
    degree_centrality(const Graph& g, Vertex v, Measure measure);

### (2) degree_centrality (default: total degree)

    template <typename Graph, typename Vertex>
    typename graph_traits<Graph>::degree_size_type
    degree_centrality(const Graph& g, Vertex v);

Uses `measure_degree(g)` as the measure (returns `degree(v, g)` — equivalent to `out_degree` for undirected graphs).

### (3) influence

    template <typename Graph, typename Vertex>
    typename graph_traits<Graph>::degree_size_type
    influence(const Graph& g, Vertex v);

Returns `out_degree(v, g)`. Convenience wrapper over `degree_centrality(g, v, measure_influence(g))`.

### (4) prestige

    template <typename Graph, typename Vertex>
    typename graph_traits<Graph>::degree_size_type
    prestige(const Graph& g, Vertex v);

Returns `in_degree(v, g)`. Requires the graph to model Bidirectional Graph. Convenience wrapper over `degree_centrality(g, v, measure_prestige(g))`.

### (5) all_degree_centralities with explicit measure

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

### (6) all_degree_centralities (default: total degree)

    template <typename Graph, typename CentralityMap>
    void all_degree_centralities(const Graph& g, CentralityMap cent);

### (7) all_influence_values

    template <typename Graph, typename CentralityMap>
    void all_influence_values(const Graph& g, CentralityMap cent);

Writes `out_degree` for every vertex into `cent`.

### (8) all_prestige_values

    template <typename Graph, typename CentralityMap>
    void all_prestige_values(const Graph& g, CentralityMap cent);

Writes `in_degree` for every vertex into `cent`. Requires Bidirectional Graph.

### (9) Measure helpers

    template <typename Graph>
    degree_centrality_measure<Graph> measure_degree(const Graph&);

    template <typename Graph>
    influence_measure<Graph>       measure_influence(const Graph&);

    template <typename Graph>
    prestige_measure<Graph>        measure_prestige(const Graph&);

Construct the measure functors used by overloads (1) and (5). `degree_centrality_measure`, `influence_measure`, `prestige_measure` are the corresponding struct types.

## Parameters

Direction | Parameter | Description
--- | --- | ---
IN | `const Graph& g` | Must model Incidence Graph. Bidirectional Graph is additionally required for `prestige` / `all_prestige_values` / `measure_prestige`.
IN | `Vertex v` | The vertex to compute centrality for (single-vertex overloads).
IN | `Measure measure` | Model of `DistanceMeasureConcept`; see overload (9) for the built-in choices.
OUT | `CentralityMap cent` | WritablePropertyMap from vertex to `degree_size_type`.