Centrality

Overview

Centrality is a core concept in graph theory and network science for quantifying how “important” a node is within a network. Centrality measures convert network position into numeric scores so analysts can rank nodes by influence, connectivity, brokerage, or visibility. In data analysis and engineering, these metrics help identify key accounts in social graphs, fragile points in infrastructure, and high-impact entities in fraud, logistics, and recommendation systems.

Core Concepts: Centrality measures differ in what they treat as importance. Local structure emphasizes immediate neighbors, geodesic distance emphasizes reachability across shortest paths, and spectral/random-walk structure emphasizes influence that propagates through the full network. In practice, these perspectives are complementary: degree-like measures find hubs, shortest-path measures find brokers, and eigenvector/PageRank-style measures find globally influential nodes. A useful mental model is that no single centrality is universally “best”; the right metric depends on whether the problem is about access, control, or influence flow.

Implementation: This category is powered by NetworkX, a widely used Python library for graph modeling and network algorithms. The underlying implementations come from NetworkX’s centrality algorithms, which provide robust methods for weighted and directed graphs as well as large sparse networks. The library is designed for clarity and reproducibility, making it suitable for exploratory analysis, production pipelines, and research workflows.

Shortest-Path and Distance Centrality: BETWEENNESS_CENT and CLOSENESS_CENT quantify importance through shortest-path geometry. BETWEENNESS_CENT measures how often a node lies on shortest routes between other pairs, so it highlights bridge nodes and potential bottlenecks in communication or transport networks. CLOSENESS_CENT measures how near a node is to all others in path-length terms, favoring nodes that can quickly reach the rest of the graph. Together, these tools are practical for routing design, contagion analysis, organizational mapping, and resilience planning.

Neighborhood and Spectral Influence: DEGREE_CENTRALITY captures immediate connectedness, while EIGENVECTOR_CENT and PAGERANK capture recursive influence from important neighbors. Degree centrality is the fastest high-level screen for hubs, especially when analysts care about direct exposure or local activity. Eigenvector centrality solves a dominant-eigenvector problem, \mathbf{A}x = \lambda x, so a node scores highly when it connects to other high-scoring nodes. PAGERANK extends this idea with a damping/teleport mechanism for directed graphs, \mathbf{p} = \alpha \mathbf{P}^\top \mathbf{p} + (1-\alpha)\mathbf{v}, which improves stability and interpretability in web, citation, and interaction networks.

BETWEENNESS_CENT

Betweenness centrality measures the importance of a node based on the number of shortest paths between all pairs of nodes that pass through it. Nodes with high betweenness serve as bridges or brokers between different parts of a network.

This is useful for identifying bottlenecks in communication networks or key influencers who connect diverse groups in social networks.

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

Excel Usage

=BETWEENNESS_CENT(edges, weighted, directed)
  • edges (list[list], required): 2D array of edges [source, target, weight?].
  • weighted (bool, optional, default: false): If True, use edge weights for shortest path calculations.
  • 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: Diamond graph betweenness

Inputs:

edges
A B
A C
B D
C D

Excel formula:

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

Expected output:

Result
A 0.166667
B 0.166667
C 0.166667
D 0.166667
Example 2: Bridge node betweenness

Inputs:

edges
1 2
2 3
3 4
4 5
3 6
6 7

Excel formula:

=BETWEENNESS_CENT({1,2;2,3;3,4;4,5;3,6;6,7})

Expected output:

Result
3 0.8
2 0.333333
4 0.333333
6 0.333333
1 0
5 0
7 0
Example 3: Weighted betweenness

Inputs:

edges weighted
A B 10 true
A C 1
C B 1

Excel formula:

=BETWEENNESS_CENT({"A","B",10;"A","C",1;"C","B",1}, TRUE)

Expected output:

Result
C 1
A 0
B 0
Example 4: Directed betweenness

Inputs:

edges directed
A B true
B C

Excel formula:

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

Expected output:

Result
B 0.5
A 0
C 0

Python Code

Show Code
import networkx as nx

def betweenness_cent(edges, weighted=False, directed=False):
    """
    Calculate the shortest-path betweenness centrality for nodes.

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

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

    Args:
        edges (list[list]): 2D array of edges [source, target, weight?].
        weighted (bool, optional): If True, use edge weights for shortest path calculations. Default is False.
        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()

        if weighted:
            G.add_weighted_edges_from([(str(row[0]), str(row[1]), float(row[2]) if len(row) >= 3 else 1.0) for row in edges if len(row) >= 2])
            weight_arg = 'weight'
        else:
            G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])
            weight_arg = None

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

        scores = nx.betweenness_centrality(G, weight=weight_arg)

        # 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, weight?].
If True, use edge weights for shortest path calculations.
If True, treat the graph as directed.

CLOSENESS_CENT

Closeness centrality measures the importance of a node based on how close it is to all other nodes in the network. A node is central if it can quickly reach all other nodes.

It is calculated as the reciprocal of the average shortest path distance to all other reachable nodes.

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

Excel Usage

=CLOSENESS_CENT(edges, weighted, directed)
  • edges (list[list], required): 2D array of edges [source, target, distance?].
  • weighted (bool, optional, default: false): If True, use edge distances for shortest path calculations.
  • 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: Path graph closeness

Inputs:

edges
A B
B C

Excel formula:

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

Expected output:

Result
B 1
A 0.666667
C 0.666667
Example 2: Star graph closeness

Inputs:

edges
Center A
Center B
Center C

Excel formula:

=CLOSENESS_CENT({"Center","A";"Center","B";"Center","C"})

Expected output:

Result
Center 1
A 0.6
B 0.6
C 0.6
Example 3: Weighted closeness

Inputs:

edges weighted
A B 10 true
A C 1
C B 1

Excel formula:

=CLOSENESS_CENT({"A","B",10;"A","C",1;"C","B",1}, TRUE)

Expected output:

Result
C 1
A 0.666667
B 0.666667
Example 4: Directed cycle closeness

Inputs:

edges directed
A B true
B C
C A

Excel formula:

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

Expected output:

Result
A 0.666667
B 0.666667
C 0.666667

Python Code

Show Code
import networkx as nx

def closeness_cent(edges, weighted=False, directed=False):
    """
    Calculate the closeness centrality for nodes.

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

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

    Args:
        edges (list[list]): 2D array of edges [source, target, distance?].
        weighted (bool, optional): If True, use edge distances for shortest path calculations. Default is False.
        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()

        if weighted:
            G.add_weighted_edges_from([(str(row[0]), str(row[1]), float(row[2]) if len(row) >= 3 else 1.0) for row in edges if len(row) >= 2])
            distance_arg = 'weight'
        else:
            G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])
            distance_arg = None

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

        scores = nx.closeness_centrality(G, distance=distance_arg)

        # 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, distance?].
If True, use edge distances for shortest path calculations.
If True, treat the graph as directed.

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

Show 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.

EIGENVECTOR_CENT

Eigenvector centrality measures the importance of a node by accounting for both its number of connections and the quality of those connections. A node is central if it is connected to nodes that are themselves central.

This measure is common in social network analysis and bibliometrics to identify prestigious or influential entities.

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

Excel Usage

=EIGENVECTOR_CENT(edges, weighted, max_iter)
  • edges (list[list], required): 2D array of edges [source, target, weight?].
  • weighted (bool, optional, default: false): If True, use edge weights for calculations.
  • max_iter (int, optional, default: 100): Maximum number of power iterations.

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

Example 1: Path graph eigenvector

Inputs:

edges
A B
B C
C D

Excel formula:

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

Expected output:

Result
C 0.601501
B 0.601501
A 0.371748
D 0.371748
Example 2: Cycle graph eigenvector

Inputs:

edges
A B
B C
C A

Excel formula:

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

Expected output:

Result
A 0.57735
B 0.57735
C 0.57735
Example 3: Star graph eigenvector

Inputs:

edges
Center A
Center B
Center C

Excel formula:

=EIGENVECTOR_CENT({"Center","A";"Center","B";"Center","C"})

Expected output:

Result
Center 0.707107
A 0.408248
B 0.408248
C 0.408248
Example 4: Weighted eigenvector

Inputs:

edges weighted
A B 10 true
A C 1

Excel formula:

=EIGENVECTOR_CENT({"A","B",10;"A","C",1}, TRUE)

Expected output:

Result
A 0.707107
B 0.703597
C 0.0703597

Python Code

Show Code
import networkx as nx

def eigenvector_cent(edges, weighted=False, max_iter=100):
    """
    Calculate the eigenvector centrality for nodes.

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

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

    Args:
        edges (list[list]): 2D array of edges [source, target, weight?].
        weighted (bool, optional): If True, use edge weights for calculations. Default is False.
        max_iter (int, optional): Maximum number of power iterations. Default is 100.

    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.Graph() # eigenvector_centrality works on directed but usually undirected in this context

        if weighted:
            G.add_weighted_edges_from([(str(row[0]), str(row[1]), float(row[2]) if len(row) >= 3 else 1.0) for row in edges if len(row) >= 2])
            weight_arg = 'weight'
        else:
            G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])
            weight_arg = None

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

        try:
            scores = nx.eigenvector_centrality(G, weight=weight_arg, max_iter=int(max_iter))
        except nx.PowerIterationFailedConvergence:
            return "Error: Power iteration failed to converge within max_iter"

        # 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, weight?].
If True, use edge weights for calculations.
Maximum number of power iterations.

PAGERANK

PageRank computes a ranking of the nodes based on the structure of the incoming links. Originally designed to rank web pages, it can be applied to any graph to measure “prestige” or “influence”.

It works by counting the number and quality of links to a node to determine a rough estimate of how important the node is. The underlying assumption is that more important nodes are likely to receive more links from other important nodes.

The function returns a ranked list of nodes and their PageRank scores, sorted from highest to lowest.

Excel Usage

=PAGERANK(edges, alpha, weighted)
  • edges (list[list], required): 2D array of edges [source, target, weight?].
  • alpha (float, optional, default: 0.85): Damping parameter (typical value 0.85).
  • weighted (bool, optional, default: false): If True, use edge weights for calculations.

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

Example 1: Simple path PageRank

Inputs:

edges
A B
B C
C D

Excel formula:

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

Expected output:

Result
D 0.370146
C 0.298811
B 0.214888
A 0.116156
Example 2: Cycle PageRank

Inputs:

edges
A B
B C
C A

Excel formula:

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

Expected output:

Result
A 0.333333
B 0.333333
C 0.333333
Example 3: Custom damping PageRank

Inputs:

edges alpha
A B 0.5
A C

Excel formula:

=PAGERANK({"A","B";"A","C"}, 0.5)

Expected output:

Result
B 0.357143
C 0.357143
A 0.285714
Example 4: Weighted PageRank

Inputs:

edges weighted
A B 10 true
A C 1

Excel formula:

=PAGERANK({"A","B",10;"A","C",1}, TRUE)

Expected output:

Result
B 0.460449
C 0.279811
A 0.25974

Python Code

Show Code
import networkx as nx
import scipy

def pagerank(edges, alpha=0.85, weighted=False):
    """
    Calculate the PageRank of the nodes in the graph.

    See: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.link_analysis.pagerank_alg.pagerank.html

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

    Args:
        edges (list[list]): 2D array of edges [source, target, weight?].
        alpha (float, optional): Damping parameter (typical value 0.85). Default is 0.85.
        weighted (bool, optional): If True, use edge weights for calculations. 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 weighted:
            G.add_weighted_edges_from([(str(row[0]), str(row[1]), float(row[2]) if len(row) >= 3 else 1.0) for row in edges if len(row) >= 2])
            weight_arg = 'weight'
        else:
            G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])
            weight_arg = None

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

        try:
            scores = nx.pagerank(G, alpha=float(alpha), weight=weight_arg)
        except nx.PowerIterationFailedConvergence:
            return "Error: Power iteration failed to converge"

        # 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, weight?].
Damping parameter (typical value 0.85).
If True, use edge weights for calculations.