Search code examples
javascriptgraph-theorycytoscape.jscytoscapenetwork-analysis

Perform Centrality Functions on all Nodes using Cytoscape.js


I need to calculate degree, closeness and betweenness centrality for every node on a graph. I'm currently using the functions built into Cytoscape.js on each node after the cy.ready() event. However, as the graphs are quite large (250+ Nodes, 650+ Connections) it's taking too long to compute. Can anyone suggest a more efficient method?

var calculateSNA = function() {
  // Don't run if already set...
  if(data.sna) return false
  console.log('Running SNA')

  _.map(nodes, function(node) {
    var target = cy.nodes('#' + node.data.id)
    node.data.sna  = {
      degreeCentrality: cy.$().dc({ root: target }).degree,
      closenessCentrality: cy.$().cc({ root: target }),
      betweennessCentrality: cy.$().bc().betweenness(target)
    }
    return node
  })

  // Add SNA data to local storage
  Data.add({
    sna: true
  })
  Node.set(nodes)

  console.log('SNA complete')
}


cy.ready(function(event) {
  console.log('cy.ready()')
  calculateSNA()
})

Solution

  • Run the algorithm once, rather than running it N times. Then just query the result:

    let ccn = cy.elements().closenessCentralityNormalized({ /* my options */ });
    
    cy.nodes().forEach( n => {
      n.data({
        ccn: ccn.closeness( n )
      });
    } );
    

    Use normalised versions of centrality algorithms unless you have a good reason to do otherwise. Only the normalised versions really mean anything if you compare results in one graph to results in another graph, for example.