private void setGraphParameters ( ) { // Diameter is the maximum of all shortest path lengths // Average shortest path length is (as the name says) the average of the shortest path length between all node pairs double maxPathLength = 0.0 ; double shortestPathLengthSum = 0.0 ; double degreeSum = 0.0 ; double clusterCoefficientSum = 0.0 ; // iterate over all node pairs Set < Integer > nodes = undirectedGraph . vertexSet ( ) ; // a hashset of the nodes which have been the start node of the computation process // for such nodes all path lengths have beeen already computed Set < Integer > wasSource = new HashSet < Integer > ( ) ; int progress = 0 ; for ( int node : nodes ) { progress ++ ; ApiUtilities . printProgressInfo ( progress , nodes . size ( ) , 100 , ApiUtilities . ProgressInfoMode . TEXT , "Getting graph parameters" ) ; int nodeDegree = undirectedGraph . degreeOf ( node ) ; degreeSum += nodeDegree ; updateDegreeDistribution ( nodeDegree ) ; // cluster coefficient of a node is C_v is the fraction of the connections that exist between the neighbor nodes (k_v) of a this node and all allowable connections between the neighbors (k_v(k_v -1)/2) // for degrees 0 or 1 there is no cluster coefficient, as there can be no connections between neighbors if ( undirectedGraph . degreeOf ( node ) > 1 ) { double numberOfNeighborConnections = getNumberOfNeighborConnections ( node ) ; clusterCoefficientSum += ( numberOfNeighborConnections / ( nodeDegree * ( nodeDegree - 1 ) ) ) ; } // Returns the new shortestPathLengthSum and the new maxPathLength. // They are returned as an double array for performance reasons. // I do not want to create an object, as this function is called *very* often double [ ] returnValues = computeShortestPathLenghts ( node , shortestPathLengthSum , maxPathLength , wasSource ) ; shortestPathLengthSum = returnValues [ 0 ] ; maxPathLength = returnValues [ 1 ] ; // save the info that the node was already used as the source of path computation wasSource . add ( node ) ; } if ( nodes . size ( ) > 1 ) { this . averageShortestPathLength = shortestPathLengthSum / ( nodes . size ( ) * ( nodes . size ( ) - 1 ) / 2 ) ; // sum of path lengths / (number of node pairs) } else { this . averageShortestPathLength = 0 ; // there is only one node } this . diameter = maxPathLength ; this . averageDegree = degreeSum / nodes . size ( ) ; this . clusterCoefficient = clusterCoefficientSum / nodes . size ( ) ; }
Computes and sets the diameter the average degree and the average shortest path length of the graph . Do not call this in the constructor . May run a while . It is called in the getters if parameters are not yet initialized when retrieved .