Skip to content

@graphty/algorithms / index / closenessCentrality

Function: closenessCentrality() ​

closenessCentrality(graph, options): Record<string, number>

Defined in: algorithms/centrality/closeness.ts:114

Calculate closeness centrality for all nodes in the graph

Closeness centrality measures how close a node is to all other nodes in the graph. It is the reciprocal of the sum of the shortest path distances to all other reachable nodes.

Parameters ​

graph ​

Graph

The input graph

options ​

ClosenessCentralityOptions = {}

Algorithm options

Returns ​

Record<string, number>

Centrality scores for each node

Example ​

typescript
const graph = new Graph();
graph.addEdge("A", "B");
graph.addEdge("B", "C");

const centrality = closenessCentrality(graph);
// { A: 0.5, B: 1.0, C: 0.5 }

Time Complexity: O(V * (V + E)) for unweighted graphs Space Complexity: O(V)