UNPKG

2.43 kBJavaScriptView Raw
1import * as is from '../../is';
2import * as util from '../../util';
3
4const defaults = util.defaults({
5 harmonic: true,
6 weight: () => 1,
7 directed: false,
8 root: null
9});
10
11const elesfn = ({
12
13 closenessCentralityNormalized: function( options ){
14 let { harmonic, weight, directed } = defaults(options);
15
16 let cy = this.cy();
17 let closenesses = {};
18 let maxCloseness = 0;
19 let nodes = this.nodes();
20 let fw = this.floydWarshall({ weight, directed });
21
22 // Compute closeness for every node and find the maximum closeness
23 for( let i = 0; i < nodes.length; i++ ){
24 let currCloseness = 0;
25 let node_i = nodes[i];
26
27 for( let j = 0; j < nodes.length; j++ ){
28 if( i !== j ){
29 let d = fw.distance( node_i, nodes[j] );
30
31 if( harmonic ){
32 currCloseness += 1 / d;
33 } else {
34 currCloseness += d;
35 }
36 }
37 }
38
39 if( !harmonic ){
40 currCloseness = 1 / currCloseness;
41 }
42
43 if( maxCloseness < currCloseness ){
44 maxCloseness = currCloseness;
45 }
46
47 closenesses[ node_i.id() ] = currCloseness;
48 }
49
50 return {
51 closeness: function( node ){
52 if( maxCloseness == 0 ){ return 0; }
53
54 if( is.string( node ) ){
55 // from is a selector string
56 node = (cy.filter( node )[0]).id();
57 } else {
58 // from is a node
59 node = node.id();
60 }
61
62 return closenesses[ node ] / maxCloseness;
63 }
64 };
65 },
66
67 // Implemented from pseudocode from wikipedia
68 closenessCentrality: function( options ){
69 let { root, weight, directed, harmonic } = defaults(options);
70
71 root = this.filter(root)[0];
72
73 // we need distance from this node to every other node
74 let dijkstra = this.dijkstra({ root, weight, directed });
75 let totalDistance = 0;
76 let nodes = this.nodes();
77
78 for( let i = 0; i < nodes.length; i++ ){
79 let n = nodes[i];
80
81 if( !n.same(root) ){
82 let d = dijkstra.distanceTo(n);
83
84 if( harmonic ){
85 totalDistance += 1 / d;
86 } else {
87 totalDistance += d;
88 }
89 }
90 }
91
92 return harmonic ? totalDistance : 1 / totalDistance;
93 } // closenessCentrality
94
95}); // elesfn
96
97// nice, short mathematical alias
98elesfn.cc = elesfn.closenessCentrality;
99elesfn.ccn = elesfn.closenessCentralityNormalised = elesfn.closenessCentralityNormalized;
100
101export default elesfn;