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

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.