Graphology metrics
Miscellaneous metrics to be used with graphology
.
Installation
npm install graphology-metrics
Usage
Graph metrics
Node metrics
Edge metrics
Centrality
Layout quality metrics
Graph metrics
Density
Computes the density of the given graph. Note that multi variants can exceed 0
, as it is also the case when considering self loops.
import {density} from 'graphology-metrics/graph/density';
const d = density(graph);
const d = density(order, size);
import {
mixedDensity,
directedDensity,
undirectedDensity,
multiMixedDensity,
multiDirectedDensity,
multiUndirectedDensity
} from 'graphology-metric/graph/density';
const d = undirectedDensity(mixedGraph);
import {abstractDensity} from 'graphology-metric/graph/density';
abstractDensity('directed', true, 10, 24);
Arguments
Either:
- graph Graph: target graph.
Or:
- order number: number of nodes in the graph.
- size number: number of edges in the graph.
Abstract version arguments
Either:
- type string: type of density to compute (
directed
, undirected
or mixed
). - multi boolean: whether to compute density for the multi of simple case.
- graph Graph: target graph.
Or:
- type string: type of density to compute (
directed
, undirected
or mixed
). - multi boolean: whether to compute density for the multi of simple case.
- order number: number of nodes in the graph.
- size number: number of edges in the graph.
Diameter
Computes the diameter, i.e the maximum eccentricity of any node of the given graph.
import diameter from 'graphology-metrics/graph/diameter';
const graph = new Graph();
graph.addNode('1');
graph.addNode('2');
graph.addNode('3');
graph.addUndirectedEdge(1, 2);
graph.addUndirectedEdge(2, 3);
diameter(graph);
>>> 2
Arguments
- graph Graph: target graph.
Extent
Computes the extent - min, max - of a node or edge's attribute.
import {nodeExtent, edgeExtent} from 'graphology-metrics/graph';
import {nodeExtent, edgeExtent} from 'graphology-metrics/graph/extent';
nodeExtent(graph, 'size');
>>> [1, 34]
nodeExtent(graph, ['x', 'y']);
>>> {x: [-4, 3], y: [-34, 56]}
edgeExtent(graph, 'weight');
>>> [0, 5.7]
Arguments
- graph Graph: target graph.
- attributes string|array: single attribute names or array of attribute names.
Modularity
Computes the modularity, given the graph and a node partition. It works on both directed & undirected networks and will return the relevant modularity.
import modularity from 'graphology-metrics/graph/modularity';
const Q = modularity(graph);
const Q = modularity(graph, {
getNodeCommunity(node, attr) {
return attr.customPartition;
}
});
Arguments
- graph Graph: target graph.
- options ?object: options:
- getNodeCommunity ?string|function [
community
]: name of the node community attribute or getter function. - getEdgeWeight ?string|function [
weight
]: name of the edges' weight attribute or getter function. - resolution ?number: resolution parameter (
γ
).
Simple size
Computes the simple size of a given graph, i.e. its number of edges if we consider the graph simple, even if it has multiple edges between pairs of nodes.
import {simpleSize} from 'graphology-metrics';
import simpleSize from 'graphology-metrics/graph/simple-size';
const graph = new MultiGraph();
graph.mergeEdge(1, 2);
graph.mergeEdge(1, 2);
graph.mergeEdge(4, 3);
graph.mergeUndirectedEdge(5, 6);
simpleSize(graph);
>>> 3
Weighted size
Computes the weighted size, i.e. the sum of the graph's edges' weight, of the given graph.
import weightedSize from 'graphology-metrics/graph/weighted-size';
const graph = new Graph();
graph.mergeEdge(1, 2, {weight: 3});
graph.mergeEdge(1, 2, {weight: 1});
weightedSize(graph);
>>> 4
weightedSize(graph, 'myWeightAttribute');
>>> 4
weightedSize(graph, (_, attr) => attr.importance);
Arguments
- graph Graph: target graph.
- getEdgeWeight ?string|function [
weight
]: name of the weight attribute or getter function.
Node metrics
Weighted degree
Computes the weighted degree of nodes. The weighted degree of a node is the sum of its edges' weights.
import {
weightedDegree,
weightedInDegree,
weightedOutDegree,
weightedInboundDegree,
weightedOutboundDegree,
weightedUndirectedDegree,
weightedDirectedDegree
} from 'graphology-metrics/node/weighted-degree';
weightedDegree(graph, 'A');
weightedDegree(graph, 'A', function (_, attr) {
return attr.importance;
});
Arguments
- graph Graph: target graph.
- node any: desired node.
- getEdgeWeight ?string|function: name of the edge weight attribute or getter function.
Eccentricity
Computes the eccentricity which is the maximum of the shortest paths between the given node and any other node.
import eccentricity from 'graphology-metrics/node/eccentricity';
graph.addNode('1');
graph.addNode('2');
graph.addNode('3');
graph.addNode('4');
graph.addUndirectedEdge(1, 2);
graph.addUndirectedEdge(2, 3);
graph.addUndirectedEdge(3, 1);
graph.addUndirectedEdge(3, 4);
eccentricity(graph, 3) >> 1;
Arguments
- graph Graph: target graph.
- node any: desired node.
Edge metrics
Simmelian strength
Function returning the simmelian strength, i.e. the number of triangles an edge is part of, of all the edges in the given graph.
import simmelianStrength from 'graphology-metrics/edge/simmelian-strength';
const strengths = simmelianStrength(graph);
simmelianStrength.assign(graph);
Centrality
Betweenness centrality
Computes the betweenness centrality for every node.
import betweennessCentrality from 'graphology-metrics/centrality/betweenness';
const centralities = betweennessCentrality(graph);
betweennessCentrality.assign(graph);
betweennessCentrality.assign(graph, {nodeCentralityAttribute: 'myCentrality'});
const centralities = betweennessCentrality(graph, {getEdgeWeight: null});
const centralities = betweennessCentrality(graph, {
getEdgeWeight: (_, attr) => attr.importance
});
Arguments
- graph Graph: target graph.
- options ?object: options:
- nodeCentralityAttribute ?string [
betweennessCentrality
]: Name of the centrality attribute to assign. - getEdgeWeight ?string|function [
weight
]: Name of the edge weight attribute or getter function. - normalized ?boolean [
true
]: should the result be normalized?
Closeness centrality
Computes the closeness centrality of a graph's nodes.
import closenessCentrality from 'graphology-metrics/centrality/closeness';
const scores = closenessCentrality(graph);
closenessCentrality.assign(graph);
const p = closenessCentrality(graph, {wassermanFaust: true});
Arguments
- graph Graph: target graph.
- options ?object: options:
- nodeCentralityAttribute ?string [
closenessCentrality
]: name of the node attribute that will be assigned the closeness centrality. - wassermanFaust ?boolean [
false
]: whether to use Wasserman & Faust's normalization scheme.
Degree centrality
Computes the degree centrality for every node.
import {
degreeCentrality,
inDegreeCentrality,
outDegreeCentrality
} from 'graphology-metrics/centrality/degree';
const centralities = degreeCentrality(graph);
degreeCentrality.assign(graph);
degreeCentrality.assign(graph, {nodeCentralityAttribute: 'myCentrality'});
Arguments
- graph Graph: target graph.
- options ?object: options:
- nodeCentralityAttribute ?string [
degreeCentrality
]: name of the centrality attribute to assign.
Eigenvector centrality
Computes the eigenvector centrality of a graph's nodes.
import eigenvectorCentrality from 'graphology-metrics/centrality/eigenvector';
const scores = eigenvectorCentrality(graph);
eigenvectorCentrality.assign(graph);
const p = eigenvectorCentrality(graph, {tolerance: 1e-3});
eigenvectorCentrality.assign(graph, {getEdgeWeight: null});
Arguments
- graph Graph: target graph.
- options ?object: options:
- nodeCentralityAttribute ?string [
eigenvectorCentrality
]: name of the node attribute that will be assigned the eigenvector centrality. - getEdgeWeight ?string|function [
weight
]: name of the edges' weight attribute or getter function. - maxIterations ?number [
100
]: maximum number of iterations to perform. - tolerance ?number [
1.e-6
]: convergence error tolerance.
HITS
Computes the hub/authority metrics for each node using the HITS algorithm.
import hits from 'graphology-metrics/centrality/hits';
const {hubs, authorities} = hits(graph);
hits.assign(graph);
const {hubs, authorities} = hits(graph, {normalize: false});
Arguments
- graph Graph: target graph.
- options ?object: options:
- getEdgeWeight ?string|function [
weight
]: name of the edges' weight attribute or getter function. - nodeHubAttribute ?string [
hub
]: name of the node attribute holding hub information. - nodeAuthorityAttribute ?string [
authority
]: name of the node attribute holding authority information. - maxIterations ?number [
100
]: maximum number of iterations to perform. - normalize ?boolean [
true
]: should the result be normalized by the sum of values. - tolerance ?number [
1.e-8
]: convergence error tolerance.
Computes the pagerank metrics for each node.
import pagerank from 'graphology-metrics/centrality/pagerank';
const scores = pagerank(graph);
pagerank.assign(graph);
const p = pagerank(graph, {alpha: 0.9});
pagerank.assign(graph, {getEdgeWeight: null});
Arguments
- graph Graph: target graph.
- options ?object: options:
- nodePagerankAttribute ?string [
pagerank
]: name of the node attribute that will be assigned the pagerank score. - getEdgeWeight ?string|function [
weight
]: name of the edges' weight attribute or getter function. - alpha ?number [
0.85
]: damping parameter of the algorithm. - maxIterations ?number [
100
]: maximum number of iterations to perform. - tolerance ?number [
1.e-6
]: convergence error tolerance.
Layout quality metrics
Edge Uniformity
Computes the edge uniformity layout quality metric from the given graph having x
and y
positions attached to its nodes. Edge uniformity is the normalized standard deviation of edge length of the graph. Lower values should be synonym of better layout according to this particular metric.
Runs in O(E)
.
import edgeUniformity from 'graphology-metrics/layout-quality/edge-uniformity';
edgeUniformity(graph);
>>> ~1.132
Neighborhood preservation
Computes the "neighborhood preservation" layout quality metric from the given graph having x
and y
positions attached to its nodes. Neighborhood preservation is the average proportion of node neighborhood being the same both in the graph's topology and its 2d layout space. The metric is therefore comprised between 0
and 1
, 1
being the best, meaning that every node keeps its neighborhood perfectly intact within the layout space.
Runs in approximately O(N * log(N))
.
import neighborhoodPreservation from 'graphology-metrics/layout-quality/neighborhood-preservation';
neighborhoodPreservation(graph);
Stress
Computes the "stress" layout quality metric from the given graph having x
and y
positions attached to its nodes. Stress is the sum of normalized delta between node topology distances and their layout space distances. Lower values should be synonym of better layout according to this particular metric.
Note that this metric does not work very well when the graph has multiple connected components.
Note also that this metric traverses any given graph as an undirected one.
Runs in O(N^2)
.
import stress from 'graphology-metrics/layout-quality/stress';
stress(graph);