# ngraph.centrality

> Module to calculate graph centrality metrics

Latest version **2.2.0** (published 2026-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install ngraph.centrality
pnpm add ngraph.centrality
yarn add ngraph.centrality
bun add ngraph.centrality
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2026-03-08 |
| First published | 2015-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Author | Andrei Kashcha |
| Maintainers | anvaka |
| Keywords | graph, centrality, betweenness, degree |

## Links

- npm: https://www.npmjs.com/package/ngraph.centrality
- Repository: https://github.com/anvaka/ngraph.centrality
- Homepage: https://github.com/anvaka/ngraph.centrality#readme
- Issues: https://github.com/anvaka/ngraph.centrality/issues
- npm.io page: https://npm.io/package/ngraph.centrality

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 2.2.0 (latest) — 2026-03-08
- 2.1.0 — 2022-01-08
- 2.0.1 — 2020-04-24
- 2.0.0 — 2019-05-22
- 0.3.0 — 2017-08-17
- 0.2.0 — 2017-07-22
- 0.1.6 — 2017-07-01
- 0.1.5 — 2015-09-26
- 0.1.4 — 2015-09-20
- 0.1.3 — 2015-04-09
- 0.1.2 — 2015-03-25
- 0.1.1 — 2015-01-19
- 0.1.0 — 2015-01-19

## README

# ngraph.centrality [![build status](https://github.com/anvaka/ngraph.centrality/actions/workflows/tests.yaml/badge.svg)](https://github.com/anvaka/ngraph.centrality/actions/workflows/tests.yaml)

Library computes centrality for entire graph and returns object, where keys are
nodes' identifiers and values are centrality values:

``` javascript
{
  node_1: centrality_value_for_node_1,
  node_2: centrality_value_for_node_2
  // ...
}
```

# usage

## [Degree centrality](https://en.wikipedia.org/wiki/Centrality#Degree_centrality)

``` javascript
var centrality = require('ngraph.centrality');
var g = require('ngraph.graph')();

// Let's build a simple graph:
g.addLink('fortran', 'c');
g.addLink('c', 'c++');
g.addLink('c++', 'perl');
g.addLink('c', 'javascript');

// this will consider graph as undirected:
var degreeCentrality = centrality.degree(g);

/*
degreeCentrality is:
{
  "fortran": 1,
  "c": 3,
  "c++": 2,
  "perl": 1,
  "javascript": 1
}
*/

// This will compute in-centrality:
var inCentrality = centrality.degree(g, 'in');
/* inCentrality is 
{
  "fortran": 0,
  "c": 1,
  "c++": 1,
  "perl": 1,
  "javascript": 1
}
*/

// out-centrality:
var outCentrality = centrality.degree(g, 'out');
/* outCentrality is
{
  "fortran": 1,
  "c": 2,
  "c++": 1,
  "perl": 0,
  "javascript": 0
}
*/

// You can also pass 'inout' or 'both' to get same results
// as `degreeCentrality`
var sameAsDegreeCentrality = centrality.degree(g, 'inout');
```

Performance of degree centrality calculation is:

* **inout**: `O(n)`, where `n` is number of nodes
* **in** or **out**: `O(n * a)`, where `a` is the average number of edges per
node


## [Betweenness centrality](https://en.wikipedia.org/wiki/Betweenness_centrality)

``` javascript
var centrality = require('ngraph.centrality');
var g = require('ngraph.graph')();
// Let's use the same graph as before:
g.addLink('fortran', 'c');
g.addLink('c', 'c++');
g.addLink('c++', 'perl');
g.addLink('c', 'javascript');

// this will consider graph as undirected:
var betweenness = centrality.betweenness(g);
/* betweenness centrality is:

{
  "fortran": 0,
  "c": 5,
  "c++": 3,
  "perl": 0,
  "javascript": 0
}
*/

// this will consider graph as directed:
var directedBetweenness = centrality.betweenness(g, true);
/* directedBetweenness is:
{
  "fortran": 0,
  "c": 3,
  "c++": 2,
  "perl": 0,
  "javascript": 0
}
*/
```

Performance of betweenness calculation is `O(n * e)` time, and `O(n + e)` space
where `n` is number of nodes and `e` is number of edges.

This library implements Brandes's algorithm published in [A Faster Algorithm for Betweenness Centrality](http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf)
and further discussed in [On Variants of Shortest-Path Betweenness
Centrality and their Generic Computation](http://www.inf.uni-konstanz.de/algo/publications/b-vspbc-08.pdf).

## [Closeness centrality](https://en.wikipedia.org/wiki/Closeness_centrality)

 In a connected graph, the normalized closeness centrality of a node is the average
 length of the shortest path between the node and all other nodes in the
 graph. Thus the more central a node is, the closer it is to all other nodes.

 ``` js
var centrality = require('ngraph.centrality');
var g = createGraph();
g.addLink(1, 2);
g.addLink(2, 3);

var closeness = centrality.closeness(g);

// closeness is: 
// { 
//   '1': 0.6666666666666666,
//   '2': 1,
//   '3': 0.6666666666666666
// }
 ```
 
 ## [Eccentricity centrality](https://en.wikipedia.org/wiki/Distance_(graph_theory))

 The eccentricity centrality of a node is the greatest distance between that node and
 any other node in the network. It can be thought of as how far a node is from the 
 node most distant from it in the graph.

 ``` js
var centrality = require('ngraph.centrality');
var g = createGraph();
g.addLink(1, 2);
g.addLink(2, 3);

var eccentricity = centrality.eccentricity(g);

// eccentricity is: 
// { 
//   '1': 2,
//   '2': 1,
//   '3': 2
// }
 ```
 
 Since the graph's diameter equals maximum eccentricity, we can easily calculate this using the returned object:
 
 ```js
 var eccentricityValues = Object.keys(eccentricity).map(function(key) {return eccentricity[key]});
 var diameter = Math.max.apply(null, eccentricityValues);
 // Returns 2
 ```

# install

With [npm](https://npmjs.org) do:

```
npm install ngraph.centrality
```

# license

MIT

# todo

It would be nice to have asynchronous version for each centrality calculator.

---
_Source: https://npm.io/package/ngraph.centrality · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
