ngraph.centrality
Library computes centrality for entire graph and returns object, where keys are
nodes' identifiers and values are centrality values:
{
node_1: centrality_value_for_node_1,
node_2: centrality_value_for_node_2
}
usage
var centrality = require('ngraph.centrality');
var g = require('ngraph.graph')();
g.addLink('fortran', 'c');
g.addLink('c', 'c++');
g.addLink('c++', 'perl');
g.addLink('c', 'javascript');
var degreeCentrality = centrality.degree(g);
var inCentrality = centrality.degree(g, 'in');
var outCentrality = centrality.degree(g, 'out');
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
var centrality = require('ngraph.centrality');
var g = require('ngraph.graph')();
g.addLink('fortran', 'c');
g.addLink('c', 'c++');
g.addLink('c++', 'perl');
g.addLink('c', 'javascript');
var betweenness = centrality.betweenness(g);
var directedBetweenness = centrality.betweenness(g, true);
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
and further discussed in On Variants of Shortest-Path Betweenness
Centrality and their Generic Computation.
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.
var centrality = require('ngraph.centrality');
var g = createGraph();
g.addLink(1, 2);
g.addLink(2, 3);
var closeness = centrality.closeness(g);
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.
var centrality = require('ngraph.centrality');
var g = createGraph();
g.addLink(1, 2);
g.addLink(2, 3);
var eccentricity = centrality.eccentricity(g);
Since the graph's diameter equals maximum eccentricity, we can easily calculate this using the returned object:
var eccentricityValues = Object.keys(eccentricity).map(function(key) {return eccentricity[key]});
var diameter = Math.max.apply(null, eccentricityValues);
install
With npm do:
npm install ngraph.centrality
license
MIT
todo
It would be nice to have asynchronous version for each centrality calculator.