DEGREE_CENTRALITY

Degree centrality measures the importance of a node based on the number of edges connected to it. In a social network, this corresponds to the number of direct friends or followers.

The values are normalized by dividing by the maximum possible degree in a simple graph (n-1).

The function returns a ranked list of nodes and their centrality scores, sorted from most to least central.

Excel Usage

=DEGREE_CENTRALITY(edges, directed)
  • edges (list[list], required): 2D array of edges [source, target].
  • directed (bool, optional, default: false): If True, treat the graph as directed.

Returns (list[list]): 2D array of [node, score] pairs, sorted by score descending.

Example 1: Star graph centrality

Inputs:

edges
A B
A C
A D

Excel formula:

=DEGREE_CENTRALITY({"A","B";"A","C";"A","D"})

Expected output:

Result
A 1
B 0.333333
C 0.333333
D 0.333333
Example 2: Path graph centrality

Inputs:

edges
1 2
2 3
3 4

Excel formula:

=DEGREE_CENTRALITY({1,2;2,3;3,4})

Expected output:

Result
2 0.666667
3 0.666667
1 0.333333
4 0.333333
Example 3: Directed clique centrality

Inputs:

edges directed
A B true
B C
C A

Excel formula:

=DEGREE_CENTRALITY({"A","B";"B","C";"C","A"}, TRUE)

Expected output:

Result
A 1
B 1
C 1
Example 4: Isolated nodes in edges

Inputs:

edges
A B
C C

Excel formula:

=DEGREE_CENTRALITY({"A","B";"C","C"})

Expected output:

Result
C 1
A 0.5
B 0.5

Python Code

import networkx as nx

def degree_centrality(edges, directed=False):
    """
    Calculate the degree centrality for nodes in a graph.

    See: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.centrality.degree_centrality.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        edges (list[list]): 2D array of edges [source, target].
        directed (bool, optional): If True, treat the graph as directed. Default is False.

    Returns:
        list[list]: 2D array of [node, score] pairs, sorted by score descending.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        edges = to2d(edges)

        if not isinstance(edges, list) or not edges:
            return "Error: edges must be a non-empty 2D list"

        G = nx.DiGraph() if directed else nx.Graph()
        G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])

        if not G.nodes:
            return "Error: No valid nodes or edges found in input"

        scores = nx.degree_centrality(G)

        # Sort by score descending, then by node name
        sorted_scores = sorted(scores.items(), key=lambda x: (-x[1], x[0]))

        return [[node, float(score)] for node, score in sorted_scores]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D array of edges [source, target].
If True, treat the graph as directed.