some java code to get average shortest path and diameter of a network 03-07-2014d1702

2014-08-29

some java code to get average shortest path and diameter of a network 03-07-2014d1702



public double calculateAverageShortestPathLength(Graph graph)
   

calculateAverageShortestPathLengthAndDiameter(graph);
return average_path_length;



public double calculateDiameter(Graph graph)

calculateAverageShortestPathLengthAndDiameter(graph);
return diameter;


public void calculateAverageShortestPathLengthAndDiameter(Graph graph)

if(average_path_length_and_diameter_calculated==false)
{
FloydWarshallShortestPaths shortest_paths = new FloydWarshallShortestPaths(graph);
Collection c = shortest_paths.getShortestPaths();
Object[] o = c.toArray();
int sum = 0;
int count = 0;
for(int i=0; i<o.length; i++)
{
Object path = o[i];
List list = ((GraphPath) path).getEdgeList();
int size = list.size();
sum+=size;
count++;

average_path_length = Double.valueOf(sum).doubleValue()/Double.valueOf(count).doubleValue();
diameter = shortest_paths.getDiameter();
average_path_length_and_diameter_calculated = true;
}

}