/****************************************************************************/ /* Author: Mathias Golombek */ /* Title: Analyzing Tool for generated Graphs */ /* Date: 4/30/2002 */ /* 9/2/2007 added Joint Degree Distribution, assortativity */ /* coefficient & some more statistic output for degree */ /* distribution (Matthias Waehlisch) */ /****************************************************************************/ package Main; import Model.*; import Graph.*; import Export.*; import Topology.*; import Util.*; import Import.*; import java.io.*; import java.util.*; import java.lang.Math; /* * This class analyses an !!!undirected!!! graph in Brite's internal graph representation. * It provides computing : * -degree distribution (-> computeDegreeDistribution() creates .degrees ) * -cluster coefficient (-> computeClusterCoefficient() creates .cluster ) * -hop distribution (-> computeHopDistributionAndCharacteristicPathLength() creates .hops ) * -characteristic path length (-> computeHopDistributionAndCharacteristicPathLength() creates .charPath ) */ final public class Analyzer { private Graph graph; //the analyzed graph private String name; //the name of the outputfiles /** * @param g The graph that shall be analyzed * @param name This String representates the beginning of all files produced with this analyzer */ public Analyzer(Graph g, String name){ graph = g; this.name = name; System.out.println("Analyzing the generated Network"); } /** This method computes the degree distribution and writes this distribution into a file .degree */ public void computeDegreeDistribution(){ System.out.println("Computing the degree-distribution"); Node[] nodes = graph.getNodesArray(); double meanDegree = (graph.getEdgesArray().length/(double)nodes.length); double var = 0.0; //Variance double sd = 0.0; //Standard Deviation double sumdegree = 0.0; int maxnodedegree = computeMaxNodeDegree(); double distribution[] = new double[maxnodedegree+1]; int degree = 0; //temporary Variable for (int i=0; i 0){ var = var + ((distribution[i]) * (i - meanDegree) * (i - meanDegree)); sumdegree = sumdegree + distribution[i]; outFile.write(i+" "+distribution[i]+" "+(distribution[i]/(double)nodes.length)*100.0+" "+(distribution[i]/(double)nodes.length)+"\n"); } } var = (var / sumdegree); //sumdegree = nodes.length sd = Math.sqrt(var); outFile.write("#Nodes "+nodes.length+"\n"); outFile.write("#Edges "+(graph.getEdgesArray()).length+"\n"); outFile.write("MeanDegree "+meanDegree+"\n"); outFile.write("Variance "+var+"\n"); outFile.write("StdDeviation "+sd+"\n"); outFile.close(); } catch (Exception e){ System.out.println("Analyzer.computeDegreeDistribution: Error while writing!"); } } /* This method computes the cluster coefficient which means the average of the connectivity between the direct neighbors of all nodes * Attention: this method appends the cluster coefficient value to the file .cluster for statistical analyzing of more graphs */ public void computeClusterCoefficient(){ System.out.println("Computing the cluster coefficient"); Calendar startTime = new GregorianCalendar(); // for measuring the computing time of the method Node[] nodes = graph.getNodesArray(); // saves all nodes of the Graph final int NUMBER_OF_NODES = nodes.length; // the number of nodes in the Graph. It is saved to a constant because it is often used Hashtable allNeighbors = new Hashtable(); // this hashmap saves the neighbors for each node double counter = 0; // 1: save the neighbors of all nodes to save time later for (int i=0; i.charPath for statistical analyzing of more graphs * This method needs unfortunately much time because the problem finding all shortest paths between all pairs needs O(n^3) steps !!!! */ public void computeHopDistributionAndCharacteristicPathLength(){ System.out.println("Computing the hop distribution and the characteristic path length"); Calendar startTime = new GregorianCalendar(); // for measuring the computing time of the method Node[] nodes = graph.getNodesArray(); // saves all nodes of the Graph final int NUMBER_OF_NODES = nodes.length; // the number of nodes in the Graph. It is saved to a constant because it is often used int[] seenHosts = new int [NUMBER_OF_NODES]; // saves the number of seen hosts of all nodes int[] countsSeenHosts2 = new int[NUMBER_OF_NODES]; // saves the cumulative number of seen Hosts within k hops(sum of all nodes) Hashtable allNeighbors = new Hashtable(); // this hashmap saves the neighbors for each node boolean[] newHosts = new boolean[NUMBER_OF_NODES]; // this matrix saves for each node and each step which nodes are "new" seen (this matrix is initialized newly in each step) boolean[] copyOfRowI= new boolean[NUMBER_OF_NODES]; // this matrix saves one row out of the adjacence-matrix so no modification are made on the adj-matrix boolean[] visited = new boolean[NUMBER_OF_NODES]; // this matrix saves the already visited nodes for each node (this matrix is initialized newly for each node) // 1: search all neighbors and take them into one big adjacence-list for (int i=0; i so we can go to step } System.out.print("."); //indicating the method is still running !!! One point stands for computing one node //System.out.println("Ready with node "+ (i+1) +" of "+NUMBER_OF_NODES); } System.out.println(); //----------------Hop Distribution--------------------------------- // we have to modify the countseenHosts-array because before in each step (since step 2) were only considered the new seen Hosts, // but we need the cumulative sum int[] cumulative = new int[NUMBER_OF_NODES]; cumulative[0] = NUMBER_OF_NODES; for (int i=1; i.jdd */ public void computeJointDegreeDistribution(){ System.out.println("Computing the joint degree distribution"); Node[] nodes = graph.getNodesArray(); Edge[] edges = graph.getEdgesArray(); double r = 0; //assortativity coefficient double jdd = 0; double jddsum = 0; int degreek1 = 0; //temporary variable int degreek2 = 0; //temporary variable double meanDegree = (graph.getEdgesArray().length/(double)graph.getNodesArray().length); int maxnodedegree = computeMaxNodeDegree(); double m[][] = new double[maxnodedegree+1][maxnodedegree+1]; for(int i=0;i 0){ jdd = m[k1][k2] / (jddsum); outFile.write(k1+" "+k2+" "+jdd+"\n"); } else if(m[k1][k2] == 0){ outFile.write(k1+" "+k2+" "+"0"+"\n"); } } } r = computeAssortativityCoefficient(); outFile.write("AssortativityCoefficient "+r+"\n"); outFile.close(); } catch (Exception e){ System.out.println("Analyzer.computeDegreeDistribution: Error while writing!"); } } /* * This method computes the assortativity coefficient. Further details: * M. E. J. Newman "Assortative Mixing in Networks", 2002. Eq. (4) */ public double computeAssortativityCoefficient(){ System.out.println("Computing the assortativity coefficient ..."); Edge[] edges = graph.getEdgesArray(); int degreek1 = 0; int degreek2 = 0; double r = 0; //assortativity coefficient double sumk1k2mult = 0.0; //Sum{degreek1*degreek2} double sumk1k2add = 0.0; //Sum{degreek1+degreek2} double sumk1k2squareadd = 0.0; //Sum{degreek1^2+degreek2^2} double M = edges.length; for(int i=0;i maxnodedegree) maxnodedegree = nodes[i].getInDegree(); } return maxnodedegree; } }