Skip to content

@graphrs/centrality

Centrality measures for ranking node importance in graphs. Each algorithm captures a different notion of what makes a node "important."

bash
npm install @graphrs/centrality

Functions

pagerank(graph, options?)

Google's PageRank algorithm. Measures node importance based on incoming link structure — a node is important if it is linked to by other important nodes.

typescript
import { Graph } from '@graphrs/core';
import { pagerank } from '@graphrs/centrality';

const graph = Graph.fromEdges([
  [0, 1], [1, 2], [2, 0], [2, 3],
]);

const result = await pagerank(graph, { damping: 0.85 });
console.log(result.scores); // importance score per node
OptionTypeDefaultDescription
dampingnumber0.85Probability of following a link vs random jump
iterationsnumberMaximum iterations
tolerancenumberConvergence threshold

betweenness(graph, options?)

Betweenness centrality — measures how often a node lies on shortest paths between other nodes. High betweenness nodes are "bridges" or "brokers" that control information flow.

typescript
import { betweenness } from '@graphrs/centrality';

const result = await betweenness(graph, { directed: false });
console.log(result.scores); // betweenness score per node
OptionTypeDefaultDescription
directedbooleangraph's valueTreat edges as directed
normalizedbooleanNormalize scores to [0, 1]

closeness(graph, options?)

Closeness centrality — measures how close a node is to all other nodes. Defined as the inverse of the average shortest-path distance. Nodes with high closeness can reach all others quickly.

typescript
import { closeness } from '@graphrs/centrality';

const result = await closeness(graph, { normalized: true });
console.log(result.scores); // closeness score per node
OptionTypeDefaultDescription
normalizedbooleantrueNormalize by (n-1)

eigenvector(graph, options?)

Eigenvector centrality — measures influence by accounting for the importance of neighbors. A node is important if it is connected to other important nodes (the self-referential definition that PageRank relaxes).

typescript
import { eigenvector } from '@graphrs/centrality';

const result = await eigenvector(graph);
console.log(result.scores);
OptionTypeDefaultDescription
scalebooleanScale the result

hits(graph, options?)

HITS (Hyperlink-Induced Topic Search) — computes two scores per node: hub (links to good authorities) and authority (linked to by good hubs). Designed for directed link analysis.

typescript
import { hits } from '@graphrs/centrality';

const result = await hits(graph);
console.log(result.hubs);        // hub scores
console.log(result.authorities); // authority scores
OptionTypeDefaultDescription
iterationsnumberMaximum iterations
tolerancenumberConvergence threshold

Returns: Promise<HitsResult> (different from other centrality functions)

typescript
interface HitsResult {
  hubs: number[];        // hub score per node
  authorities: number[]; // authority score per node
}

katz(graph, options?)

Katz centrality — measures influence by counting all paths from a node, with longer paths attenuated by a factor α. Generalizes degree centrality.

typescript
import { katz } from '@graphrs/centrality';

const result = await katz(graph, { alpha: 0.1 });
console.log(result.scores);
OptionTypeDefaultDescription
alphanumber0.1Attenuation factor (must be < 1/λ₁)
betanumberWeight of the exogenous factor

harmonic(graph, options?)

Harmonic centrality — variant of closeness that uses the harmonic mean of distances instead of the arithmetic mean. Handles disconnected graphs gracefully (unreachable nodes contribute 0 instead of making the score undefined).

typescript
import { harmonic } from '@graphrs/centrality';

const result = await harmonic(graph);
console.log(result.scores);
OptionTypeDefaultDescription
normalizedbooleanNormalize scores

Result Type

Most functions return Promise<CentralityResult>:

typescript
interface CentralityResult {
  scores: number[];  // centrality score per node (indexed by node order)
}

Exception: hits() returns HitsResult with separate hubs and authorities arrays.

Complete Example

Compare multiple centrality measures to understand node roles:

typescript
import { Graph } from '@graphrs/core';
import { pagerank, betweenness, closeness } from '@graphrs/centrality';

const graph = Graph.fromEdges([
  [0,1],[1,2],[2,0],   // cluster A
  [3,4],[4,5],[5,3],   // cluster B
  [2,3],               // bridge
]);

const [pr, bw, cl] = await Promise.all([
  pagerank(graph),
  betweenness(graph),
  closeness(graph),
]);

graph.nodes().forEach((id, i) => {
  console.log(
    `Node ${id}: PR=${pr.scores[i]!.toFixed(3)}, ` +
    `BW=${bw.scores[i]!.toFixed(3)}, CL=${cl.scores[i]!.toFixed(3)}`
  );
});
// Nodes 2 and 3 will have highest betweenness (bridge nodes)

API Summary

FunctionReturnsBest For
pagerankCentralityResultGlobal ranking by influence
betweennessCentralityResultFinding bridges and bottlenecks
closenessCentralityResultFinding nodes with shortest reach
eigenvectorCentralityResultNodes connected to important nodes
hitsHitsResultDirected link analysis (hubs & authorities)
katzCentralityResultLong-range influence with attenuation
harmonicCentralityResultCloseness for disconnected graphs

MIT License (TS code) · GPL-2.0 (WASM binary)