socialNet.h File Reference

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <stack>
#include <string.h>
#include <math.h>

Functions

void print_int_array (int **array, int array_size)
void print_double_array (double **array, int array_size)
int ** initialise_int_array (int array_size)
int ** initialise_int_square_array (int x_size, int y_size)
double ** initialise_double_array (int array_size)
void initialise_adjacency_array_WS (int **adjacency, int n, int k, double p)
void initialise_adjacency_array_random (int **adjacency, int array_size)
void initialise_adjacency_array (int **adjacency, int array_size, int k, double p)
void initialise_weight_array_WS (double **weight, int array_size)
void initialise_weight_array_random (double **weight, int array_size)
void initialise_weight_array_ingroups (double **weight, int array_size, int numberOfGroups, double nRewiring, double threshold, double seed)
void rewire_WS (int **adjacency, double **weight, int n, int k, double p)
void rewire_WS2 (int **adjacency, double **weight, int n, int k, double p)
void initialise_weight_array (double **weight, int array_size)
void initialise_weight_array2 (double **weight, int array_size)
void rewire (int **adjacency, double **weight, int array_size, int k, double p)
void calculate_shortest_paths (double **shortest_path, double **shortest_path_ideal, int **adjacency, double **weight, int array_size)
double calculate_energy (double **shortest_path, int array_size)
double calculate_energy_local (int node, int **adjacency, double **weight, int array_size)
double calculate_E_glob (double **shortest_path, double **shortest_path_ideal, int array_size)
double calculate_E_loc (int **adjacency, double **weight, int array_size)
double gamma (double l)
double calculate_cost (int **adjacency, double **weight, int array_size)
int load_double_array (double **array, int array_size, const char *filename)
int load_double_array (double **array, const char *filename)
void generate_adjacency (double **weightMat, int **adjacencyMat, double threshold, int array_size)
void print_array_int (int **array, int array_size)
void print_array_double (double **array, int array_size)
void assign_distance_not_working (int current, int previous, double *distance, int d, int target, int *assigned, int **adjacency, int **pred, int array_size)
void assign_distance (double *distance, int d, int *leaf, int **adjacency, int **pred, int *predNum, int array_size, std::stack< int > &stoiva, int *sigma)
void calculate_edge_b (double **edge_bw, double *distance, int **pred, int **adjacency, int *predNum, int array_size, std::stack< int > &stoiva, int *sigma, double *delta, int current, int *leaf)
void calculate_node_b (double *betw, double *distance, int **pred, int *predNum, int array_size, std::stack< int > &stoiva, int *sigma, double *delta, int current)
void calculate_edge_betweenness (double **result_betw, int **adjacency, int array_size)
void calculate_node_betweenness (double *result_betw, int **adjacency, int array_size)
int assignToAGroup (int current, int currentGroup, int **adjacency, int **groups, int *numberOfMembers, int array_size, bool *assigned)
int getGroups (int **adjacency, int **groups, int *numberOfMembers, int array_size)
bool isInGroup (int node, int *group, int numberOfMembers)
void splitNetwork (int **adjacency, double *betw, int array_size)
double splitNetwork_Threshold (int **adjacency, double *betw, int array_size, double modThreshold)
double splitNetwork_edge_Threshold (int **adjacency, int **tempadjacency, double **betw, int array_size, double modThreshold)
bool areInTheSameGroup (int node1, int node2, int **groups, int numberOfGroups, int *numberOfMembers)
void printGroups (int numberOfGroups, int **groups, int *numberOfMembers, int array_size)
bool readAdjacency (string inputFile, double **weightMat, int **adjacencyMat, int numHosts, double threshold)

Function Documentation

bool areInTheSameGroup ( int  node1,
int  node2,
int **  groups,
int  numberOfGroups,
int *  numberOfMembers 
)

01429                                                                                                       {
01430         
01431         bool result=false;
01432         for (int k=0;k<numberOfGroups;k++) {
01433                 
01434                 if (isInGroup(node1,groups[k],numberOfMembers[k]))
01435                         if (isInGroup(node2,groups[k],numberOfMembers[k])) {
01436                                 result=true;
01437                                 //break;
01438                         }
01439                 }
01440         
01441         return result;
01442 }

void assign_distance ( double *  distance,
int  d,
int *  leaf,
int **  adjacency,
int **  pred,
int *  predNum,
int  array_size,
std::stack< int > &  stoiva,
int *  sigma 
)
void assign_distance_not_working ( int  current,
int  previous,
double *  distance,
int  d,
int  target,
int *  assigned,
int **  adjacency,
int **  pred,
int  array_size 
)

00606                                                                                                                                                            {
00607                         if (current!=target) {
00608                                 for (int k=0;k<array_size; k++)
00609                                 if ((k!=current)&&(k!=previous)) {                                                                      
00610                                         if (adjacency[current][k]==1) {                                 
00611                                                 if (assigned[k]==1) {
00612                                                         distance[k]=d+1;
00613                                                         assigned[k]=0;
00614                                                 }       
00615                                                 else if (distance[k]==d) {
00616                                                         //cout<<"Assign distance "<<d<<"\n";
00617                                                         //assign_distance(k,current,distance,d,target,assigned,adjacency,pred,array_size); 
00618                                                 }
00619                                         }
00620                                 }//if ((k!=current))
00621                 }
00622 }       

int assignToAGroup ( int  current,
int  currentGroup,
int **  adjacency,
int **  groups,
int *  numberOfMembers,
int  array_size,
bool *  assigned 
)

00914                                                                                                                                       {
00915         
00916         for (int k=0;k<array_size;k++) {                
00917                 if (adjacency[current][k]==1) {
00918                         if (current!=k) {
00919                                 if (assigned[k]==false) {
00920                                         numberOfMembers[currentGroup-1]=numberOfMembers[currentGroup-1]+1;
00921                                         groups[currentGroup-1][numberOfMembers[currentGroup-1]-1]=k+1;
00922                                         assigned[k]=true;
00923                                         assignToAGroup(k,currentGroup,adjacency, groups, numberOfMembers, array_size, assigned);        
00924                                 }
00925                         }
00926                 }
00927         }
00928 }

double calculate_cost ( int **  adjacency,
double **  weight,
int  array_size 
)

00499 {       double numerator = 0.0;
00500         double denominator = 0.0;
00501 
00502         for (int i = 0; i < array_size; i++)
00503           for (int j = 0; j < array_size; j++)
00504           {       if (i != j)
00505                         { denominator += gamma(weight[i][j]);
00506                           if (adjacency[i][j] != 0)
00507                         numerator += gamma(weight[i][j]);
00508                         }
00509           }
00510 
00511           return (numerator/denominator);
00512 }

double calculate_E_glob ( double **  shortest_path,
double **  shortest_path_ideal,
int  array_size 
)

00475 {
00476         return (calculate_energy(shortest_path, array_size)/calculate_energy(shortest_path_ideal, array_size));
00477 }       

double calculate_E_loc ( int **  adjacency,
double **  weight,
int  array_size 
)

00482 {       double sum = 0.0;
00483 
00484     for (int i = 0; i < array_size; i++)
00485                 sum += calculate_energy_local(i, adjacency, weight, array_size);
00486 
00487         return (sum/array_size);
00488 }       

void calculate_edge_b ( double **  edge_bw,
double *  distance,
int **  pred,
int **  adjacency,
int *  predNum,
int  array_size,
std::stack< int > &  stoiva,
int *  sigma,
double *  delta,
int  current,
int *  leaf 
)
void calculate_edge_betweenness ( double **  result_betw,
int **  adjacency,
int  array_size 
)

00740 {       
00741         stack<int> stoiva;
00742         int* b=new int[array_size];
00743         int* leaf= new int[array_size];
00744         int* predNum= new int[array_size];
00745         int* sigma= new int[array_size];
00746         double* distance= new double[array_size];
00747         double* delta=new double[array_size];
00748         double **edge_bw=initialise_double_array(array_size);
00749         
00750         //Initialize edge_bw
00751         for(int k=0;k<array_size; k++) {
00752                 for (int r=0;r<array_size;r++) {                
00753                         edge_bw[k][r]=0; 
00754                         result_betw[k][r]=0;
00755                 }
00756         }
00757         
00758         int d;
00759         for (int i=0; i<array_size;i++) { 
00760                 leaf[i]=1;
00761                 distance[i]=15;
00762                 //cout<<distance[i]<<"\n";
00763         
00764         }
00765                 
00766         double **dist= initialise_double_array(array_size);
00767         int **pred= initialise_int_array(array_size);
00768                                 
00769         for (int i=0; i<array_size; i++) {
00770         
00771                 //Initialize predecessor array
00772                 for(int l=0; l<array_size; l++)
00773                         for(int j=0; j<array_size; j++)
00774                                 pred[l][j]=-1;
00775                 
00776                 // Initialize other arrays      
00777                 for (int s=0; s<array_size;s++) { 
00778                         leaf[s]=1;
00779                         distance[s]=16;
00780                         predNum[s]=0;
00781                         sigma[s]=0;
00782                         delta[s]=0;
00783                 }
00784                 //for (int j=0; j<array_size; j++) {
00785                 leaf[i]=0;
00786                 distance[i]=0;
00787                 sigma[i]=1;
00788                 for (int d=0;d<15;d++) {
00789                         // For distances from 0 to 15 assign distances
00790                         assign_distance(distance,d,leaf,adjacency,pred,predNum,array_size,stoiva,sigma);        
00791                 }                               
00792         
00793                 calculate_edge_b (edge_bw, distance, pred, adjacency, predNum,array_size,stoiva,sigma,delta,i,leaf);
00794                 
00795                 //Add results to betweenness and reset temporal counter edge_bw
00796                 for (int z=0;z<array_size;z++) {
00797                         for (int p=0;p<array_size;p++) {
00798                                 result_betw[z][p]+=(edge_bw[z][p]/2);
00799                                 edge_bw[z][p]=0;
00800                         }
00801                 }       
00802                                 
00803         }//end for (int i<0; i<array_size; i++) 
00804 
00805         //delete data structures
00806         for (int i = 0; i < array_size; i++) {
00807                 delete edge_bw[i];
00808                 delete pred[i];
00809                 delete dist[i];
00810         }
00811         delete[] delta;
00812         delete[] edge_bw;
00813         delete[] pred;
00814         delete[] dist;
00815         delete[] b;
00816         delete[] distance;
00817         delete[] leaf;
00818         delete[] predNum;
00819 }

double calculate_energy ( double **  shortest_path,
int  array_size 
)

00399 {
00400   double sum = 0.0;
00401 
00402   for (int i = 0; i < array_size; i++)
00403           for (int j = 0; j < array_size; j++)
00404                   if (i != j && shortest_path[i][j] != infinity)
00405                     sum += 1.0/shortest_path[i][j];
00406 
00407   // Whilst, strictly, we need this to calculate the energy
00408   // because it is normalised, we can remove it.
00409   //
00410   // sum /= array_size * (array_size - 1);
00411   return(sum);
00412 }

double calculate_energy_local ( int  node,
int **  adjacency,
double **  weight,
int  array_size 
)

00423 {
00424         double res = 0.0;
00425         int        idx_array_size = 0;
00426 
00427         static int    *idx_array                        = new int[array_size];
00428         static int    **temp_adjacency                  = initialise_int_array(array_size);
00429         static double **temp_weight                     = initialise_double_array(array_size);
00430         static double **temp_shortest_path              = initialise_double_array(array_size);
00431         static double **temp_shortest_path_ideal        = initialise_double_array(array_size);
00432 
00433         // Calculate local energy for the graph formed by this node's immediate neighbours. Note that
00434         // this graph specifically excludes the node itself so as to measure fault tolerance -- how
00435         // efficient communication amongst the node's neighbours is if the node fails.
00436         //
00437 
00438         // work out the number of neighbours and their positions
00439         // the idx array can then be used to indirect through for further calcaulations
00440         //
00441         for (int i = 0; i < array_size; i++)
00442                 if (adjacency[node][i] != 0 && i != node)               // i is an immediate neighbour of node
00443                         idx_array[idx_array_size++] = i;
00444 
00445         // Fill in temporary arrays
00446         // Only need to fill in the entries we're interested in, which are in idx array.
00447         //
00448         for (int i = 0; i < idx_array_size; i++)
00449                 for (int j = 0; j < idx_array_size; j++)
00450                 {       int idx1 = idx_array[i];
00451                         int idx2 = idx_array[j];
00452 
00453                         temp_weight[i][j]        = weight[idx1][idx2];
00454                         temp_adjacency[i][j] = adjacency[idx1][idx2];
00455                 }
00456 
00457         // Using only these nodes calculate shortest paths
00458         //
00459         calculate_shortest_paths(temp_shortest_path, temp_shortest_path_ideal, temp_adjacency, temp_weight, idx_array_size);
00460 
00461         // And work out the normalised global energy for this restricted set
00462         //
00463         double energy           = calculate_energy(temp_shortest_path, idx_array_size) ;
00464         double energy_ideal = calculate_energy(temp_shortest_path_ideal, idx_array_size);
00465 
00466         if (energy_ideal != 0)
00467                 return (energy/energy_ideal);
00468         else
00469                 return (0.0);
00470 }

void calculate_node_b ( double *  betw,
double *  distance,
int **  pred,
int *  predNum,
int  array_size,
std::stack< int > &  stoiva,
int *  sigma,
double *  delta,
int  current 
)
void calculate_node_betweenness ( double *  result_betw,
int **  adjacency,
int  array_size 
)

00841                                                                                       {
00842 
00843         int* b=new int[array_size];
00844         int* leaf= new int[array_size];
00845         int* predNum= new int[array_size];
00846         double* distance= new double[array_size];
00847         double* betw= new double[array_size];
00848         double* current_betw=new double[array_size];
00849 
00850         stack<int> stoiva;
00851         int* sigma= new int[array_size];
00852         double* delta=new double[array_size];
00853         
00854         int d;
00855         for (int i=0; i<array_size;i++) { 
00856                 leaf[i]=1;
00857                 distance[i]=15;
00858                 betw[i]=0;
00859                 current_betw[i]=0;
00860         }
00861                 
00862         double **dist= initialise_double_array(array_size);
00863         int **pred= initialise_int_array(array_size);
00864                                 
00865         for (int i=0; i<array_size; i++) {
00866         
00867                         //Initialize predecessor array
00868                         for(int l=0; l<array_size; l++)
00869                                 for(int j=0; j<array_size; j++)
00870                                         pred[l][j]=-1;
00871                         
00872                         // Initialize other arrays      
00873                         for (int s=0; s<array_size;s++) { 
00874                                                 leaf[s]=1;
00875                                                 distance[s]=16;
00876                                                 predNum[s]=0;
00877                                                 sigma[s]=0;
00878                                                 delta[s]=0;
00879                                                 //betw[s]=0;
00880                         }
00881 
00882                         leaf[i]=0;
00883                         distance[i]=0;
00884                         sigma[i]=1;
00885                         for (int d=0;d<15;d++) {
00886                                 // For distances from 0 to 15 assign distances
00887                                 assign_distance(distance,d,leaf,adjacency,pred,predNum,array_size,stoiva,sigma);        
00888                         }                       
00889                 
00890                         calculate_node_b (betw,distance, pred, predNum,array_size,stoiva,sigma,delta,i);                                
00891          }
00892 
00893         for (int z=0;z<array_size;z++)          
00894                 result_betw[z]=betw[z]/2;
00895 
00896         //delete data structures
00897         for (int i = 0; i < array_size; i++) {
00898                 delete pred[i];
00899                 delete dist[i];
00900         }
00901         delete[] pred;
00902         delete[] dist;
00903         delete[] delta;
00904         delete[] b;
00905         delete[] current_betw;
00906         delete[] distance;
00907         delete[] leaf;
00908         delete[] predNum;
00909         delete[] betw;
00910 }

void calculate_shortest_paths ( double **  shortest_path,
double **  shortest_path_ideal,
int **  adjacency,
double **  weight,
int  array_size 
)

00358 {       int i, j, k;
00359 
00360         // ??? MIGHT BE BETTER TO USE DIJKSTRA HERE SINCE IT IS PROBABLE THAT THE NUMBER OF
00361         // EDGES << N^2
00362         
00363         // Use Floyd's algorithm
00364         //
00365         for (i = 0; i < array_size; i++)
00366                 for (j = 0; j < array_size; j++)
00367                 {       shortest_path[i][j]       = (adjacency[i][j] != 0 ? weight[i][j] : infinity);
00368                         shortest_path_ideal[i][j] = weight[i][j];
00369                 }       
00370                 
00371         for (k = 0; k < array_size; k++)
00372                 for (i = 0; i < array_size;i++)
00373                 {       if (i == k)
00374                                 continue;
00375 
00376                         for (j = 0; j < array_size; j++)
00377                         {   if (j == i || j == k)
00378                                   continue;
00379 
00380 
00381                           if (shortest_path[i][k] != infinity && shortest_path[k][j] != infinity)
00382                           { if (shortest_path[i][j] == infinity || (shortest_path[i][k] + shortest_path[k][j] < shortest_path[i][j]))
00383                                         shortest_path[i][j] = shortest_path[i][k] + shortest_path[k][j];
00384 
00385                                 if (shortest_path_ideal[i][j] == infinity || (shortest_path_ideal[i][k] + shortest_path_ideal[k][j] < shortest_path_ideal[i][j]))
00386                                         shortest_path_ideal[i][j] = shortest_path_ideal[i][k] + shortest_path_ideal[k][j];
00387                           }
00388 
00389                         }
00390 
00391                 }
00392 }

double gamma ( double  l  ) 

00494 { return l; }

void generate_adjacency ( double **  weightMat,
int **  adjacencyMat,
double  threshold,
int  array_size 
)

00572                                                                                                    {
00573         
00574         for (int i=0; i<array_size; i++)  
00575                 for (int j=0; j<array_size; j++) {
00576                         if (weightMat[i][j]>threshold)
00577                                 adjacencyMat[i][j]=1; 
00578                         else
00579                                 adjacencyMat[i][j]=0;
00580                 }
00581 }

int getGroups ( int **  adjacency,
int **  groups,
int *  numberOfMembers,
int  array_size 
)

00932                                                                                     {
00933         
00934         int numberOfGroups=0;
00935         bool assigned[array_size];
00936         
00937         for (int i=0; i<array_size; i++) {
00938                 assigned[i]=false;      
00939         } 
00940 
00941         for (int i=0; i<array_size; i++) {
00942                 
00943                 if (assigned[i]==false) {
00944                         numberOfGroups++;
00945                         numberOfMembers[numberOfGroups-1]=numberOfMembers[numberOfGroups-1]+1;
00946                         groups[numberOfGroups-1][numberOfMembers[numberOfGroups-1]-1]=i+1;
00947                         assigned[i]=true;
00948                         assignToAGroup(i,numberOfGroups,adjacency, groups, numberOfMembers, array_size, assigned);      
00949                 }                       
00950         }
00951         return numberOfGroups;
00952 }

void initialise_adjacency_array ( int **  adjacency,
int  array_size,
int  k,
double  p 
)

00165 {       
00166         // initialise_adjacency_array_random(adjacency, array_size);
00167         initialise_adjacency_array_WS(adjacency, array_size, k, p);
00168 
00169 }

void initialise_adjacency_array_random ( int **  adjacency,
int  array_size 
)

00154 {       for (int i = 0; i < array_size; i++)
00155                 for (int j = 0; j < array_size; j++)
00156                         if (i == j)
00157                                 adjacency[i][j] = 1;
00158                         else if (i < j)
00159                         { adjacency[i][j] = (((double) rand())/RAND_MAX < 0.5);
00160                           adjacency[j][i] = adjacency[i][j];
00161                         }
00162 }

void initialise_adjacency_array_WS ( int **  adjacency,
int  n,
int  k,
double  p 
)

00125 {       int i, j;
00126 
00127         for (i = 0; i < n; i++)
00128                 for (j = 0; j < n; j++)
00129                         if (i == j)
00130                                 adjacency[i][j] = 1;
00131                         else    
00132                                 adjacency[i][j] = 0;
00133 
00134         // Create the regular lattice -- note that this is a circular lattice
00135         // If we iterate over the whole of the arrayand put in the k/2 forward links, then,
00136         // because this is symmetric, we will end up with the nearest k links in.
00137         //
00138         for (i = 0; i < n; i++)
00139                 for (j = 1; j <= k/2; j++)
00140                 {   int idx = (i+j) % n;                // Make it circular
00141 
00142                         adjacency[i][idx] = 1;
00143                         adjacency[idx][i] = 1;
00144                 }
00145 
00146 }

double** initialise_double_array ( int  array_size  ) 

00104 {       double **result = new double * [array_size];
00105 
00106         for (int i = 0; i < array_size; i++) {
00107                 result[i] = new double [array_size];
00108                 for(int j = 0; j < array_size; j++)
00109                         result[i][j]=0;
00110         }
00111         
00112         return result;
00113 }

int** initialise_int_array ( int  array_size  ) 

00082                                            {
00083         
00084         int **result = new int * [array_size];
00085         
00086         for (int i = 0; i < array_size; i++)
00087                 result[i] = new int [array_size];
00088 
00089         return result;
00090 }

int** initialise_int_square_array ( int  x_size,
int  y_size 
)

00092                                                           {
00093 
00094         int **result = new int * [y_size];
00095 
00096         for (int i = 0; i < y_size; i++)
00097                 result[i] = new int [x_size];
00098 
00099         return result;
00100 }

void initialise_weight_array ( double **  weight,
int  array_size 
)

00328 { 
00329         // initialise_weight_array_random(weight, array_size);
00330         initialise_weight_array_WS(weight, array_size);
00331 }

void initialise_weight_array2 ( double **  weight,
int  array_size 
)

00334                                                                 {
00335         int seed = 5;
00336         srand(seed);
00337                 for (int i=0;i<array_size;i++)
00338                         for (int j=0;j<array_size;j++) {
00339                                 if (i==j) weight[i][j]=1;
00340                                 if (i<j){
00341                                         weight[i][j]=rand()/(RAND_MAX+1.0);
00342                                         weight[j][i]=weight[i][j];
00343                                 }
00344                         }
00345 }

void initialise_weight_array_ingroups ( double **  weight,
int  array_size,
int  numberOfGroups,
double  nRewiring,
double  threshold,
double  seed 
)

00207                                                                                                                                                 {
00208         
00209         srand((int)seed);
00210         int groupId=0;
00211 
00212         int **groups=initialise_int_array(array_size);
00213         int numberOfMembers[numberOfGroups];
00214         
00215         for (int i=0;i<numberOfGroups;i++) {
00216                 numberOfMembers[i]=0;
00217         }
00218         
00219         //Initial assignment of nodes to groups
00220         for (int i=0;i<array_size;i++) {
00221                 do {
00222                         groupId=rand()%numberOfGroups;
00223                 } while(numberOfMembers[groupId]!=floor(i/numberOfGroups));
00224                 //int groupId=i%numberOfGroups;
00225                 //cout<<"\n";
00226                 //cout<<"Group id of host "<<i<<" is "<<groupId<<".\n";
00227                 groups[groupId][numberOfMembers[groupId]]=i+1;
00228                 numberOfMembers[groupId]+=1;    
00229         }
00230         
00231         for (int i=0;i<array_size;i++)
00232                 for (int j=0;j<=i;j++) {
00233                          if (areInTheSameGroup (i+1,j+1,groups,numberOfGroups,numberOfMembers)==true) {
00234                                         if(i==j) {// if it's a diagonal element
00235                                                 weight[i][j]=1;
00236                                         }
00237                                         else if ((rand()/(RAND_MAX+1.0))<probRewiring) {        //rewiring
00238                                                 bool found=false;
00239                                                 for (int z=0;z<array_size;z++) {
00240                                                         if ((areInTheSameGroup (i+1,z+1,groups,numberOfGroups,numberOfMembers)==false)&&(weight[i][z]==0)&&(found==false)) {
00241                                                                 weight[i][z]=1.0-(rand()*threshold/(RAND_MAX+1.0));
00242                                                                 weight[z][i]=weight[i][z];
00243                                                                 found=true;
00244                                                         }
00245                                                 }
00246                                                 weight[i][j]=(rand()*threshold/(RAND_MAX+1.0));
00247                                                 weight[j][i]=weight[i][j];
00248                                         }
00249                                         else {//no rewiring 
00250                                                 weight[i][j]=1.0-(rand()*threshold/(RAND_MAX+1.0));
00251                                                 weight[j][i]=weight[i][j];
00252                                         }
00253                          } else if(weight[i][j]==0){//the hosts are not in the same cluster
00254                                 weight[i][j]=(rand()*threshold/(RAND_MAX+1.0));
00255                                 weight[j][i]=weight[i][j];
00256                          }                      
00257                 }
00258         
00259         //deletion of the variables
00260         for (int i=0;i<array_size;i++) 
00261                 delete[] groups[i];
00262         delete[] groups;
00263 }

void initialise_weight_array_random ( double **  weight,
int  array_size 
)

00188 {       for (int i = 0; i < array_size; i++)
00189                 for (int j = 0; j < array_size; j++)
00190                         if (i == j)
00191                                 weight[i][j] = 0;
00192                         else if (i < j)
00193                         { weight[i][j] = ((double) rand())/RAND_MAX;
00194 
00195                           // Impose arbitrary lower limit to ensure that reciprocals don't get
00196                           // unmanageably large
00197                           //
00198                           while (weight[i][j] < 0.01)
00199                                 weight[i][j] = ((double) rand())/RAND_MAX;
00200 
00201                           weight[j][i] = weight[i][j];
00202                         }
00203 }

void initialise_weight_array_WS ( double **  weight,
int  array_size 
)

00175 {
00176         for (int i = 0; i < array_size; i++)
00177                 for (int j = 0; j < array_size; j++)
00178                         if (i == j)
00179                                 weight[i][j] = 0;
00180                         else
00181                                 weight[i][j] = 1;
00182 }

bool isInGroup ( int  node,
int *  group,
int  numberOfMembers 
)

00957                                                           {
00958         
00959         bool result=false;
00960         
00961         for (int k=0;k<numberOfMembers;k++)     
00962                 if (group[k]==node) {
00963                         result=true;
00964                         //break;
00965                 }
00966         return result;
00967 }

int load_double_array ( double **  array,
const char *  filename 
)

00543                                                             {
00544         
00545         //no checks
00546         int i,j=0;
00547         int r;
00548         double value;
00549         //int * valueint;
00550         //FILE *fopen (const char *filename, const char *mode)
00551         FILE* file1=fopen (filename,"r");
00552         
00553         int array_size;
00554         //Segmentation fault
00555         r=fscanf(file1,"%d",&array_size);
00556         
00557         //<<"Loading matrix from file "<<filename<<"...\n";
00558         
00559         for (int i=0; i<array_size; i++) {
00560                 for (int j=0; j<array_size; j++) {
00561                         r=fscanf (file1, "%lf", &value);
00562                         array[i][j]=value;
00563                 }
00564         }
00565         
00566         fclose (file1);
00567         return array_size;
00568 }

int load_double_array ( double **  array,
int  array_size,
const char *  filename 
)

00517                                                                            {
00518         
00519         //no checks
00520         int i,j=0;
00521         int r;
00522         double value;
00523         //int * valueint;
00524         //FILE *fopen (const char *filename, const char *mode)
00525         FILE* file1=fopen (filename,"r");
00526         
00527         //cout<<"Loading matrix from file "<<filename<<"...\n";
00528         
00529         for (int i=0; i<array_size; i++) {
00530                 for (int j=0; j<array_size; j++) {
00531                         r=fscanf (file1, "%lf", &value);
00532                         array[i][j]=value;
00533                 }
00534         }
00535         
00536         fclose (file1);
00537 }

void print_array_double ( double **  array,
int  array_size 
)

00597                                                          {
00598         for (int i=0; i<array_size; i++) {
00599                 for (int j=0; j<array_size; j++) { 
00600                         printf ("%.2f  ",array[i][j]);
00601                 }
00602                 cout<<"\n";
00603         }
00604 }

void print_array_int ( int **  array,
int  array_size 
)

00585                                                    {
00586         
00587         for (int i=0; i<array_size; i++) {
00588                 for (int j=0; j<array_size; j++) { 
00589                         printf ("%i  ",array[i][j]);
00590                 }
00591                 cout<<"\n";
00592         }
00593 }

void print_double_array ( double **  array,
int  array_size 
)

00072 {       for (int i = 0; i < array_size; i++)
00073         { cout << array[i][0];
00074           for (int j = 1; j < array_size; j++)
00075                 cout << ", " << array[i][j];
00076           cout << "\n";
00077         }
00078         cout << "\n"; fout.flush();
00079 }

void print_int_array ( int **  array,
int  array_size 
)

00062 {       for (int i = 0; i < array_size; i++)
00063         { cout << array[i][0];
00064           for (int j = 1; j < array_size; j++)
00065                 cout << ", " << array[i][j];
00066           cout << "\n";
00067         }
00068         cout << "\n"; fout.flush();
00069 }

void printGroups ( int  numberOfGroups,
int **  groups,
int *  numberOfMembers,
int  array_size 
)

01446                                                                                        {
01447         
01448         for (int i=0;i<numberOfGroups;i++) {
01449                 cout<<"\n";
01450                 cout<<"The members of group "<<i+1<<" are: ";
01451                 for (int j=0;j<numberOfMembers[i];j++)
01452                         cout<<groups[i][j]<<" ";
01453                 cout<<"\n";
01454         }       
01455 }

bool readAdjacency ( string  inputFile,
double **  weightMat,
int **  adjacencyMat,
int  numHosts,
double  threshold 
)

01457                                                                                                              {
01458 
01459         int adj;
01460         int linecnt=0;
01461         int cnt=0;
01462         string line;
01463         
01464         //Input file
01465         ifstream infile(inputFile.c_str());
01466 
01467         //Opening file
01468         if (!infile) {
01469                 cout << "There was a problem opening file " << inputFile << " for reading." << endl;
01470                 return false;
01471         }
01472         
01473         //Count line number
01474         while (getline(infile,line)) {
01475                 linecnt++;
01476         }
01477         
01478         //Compare line number with number of hosts
01479         if(linecnt!=numHosts) {
01480                 cout << "Number of hosts in input file does not comply with given input" << endl;
01481                 return false;
01482         }
01483         
01484         //Move pointer to the beggining of file
01485         infile.clear();
01486         infile.seekg(0,ios::beg);
01487         
01488         //Read values
01489         for (int i=0; i<numHosts; i++) {
01490                 for (int j=0; j<numHosts; j++) {
01491                         if (infile >> adj) {
01492                                 if(adj >= 1) {
01493                                         adjacencyMat[i][j]=1;
01494                                         if(weightMat[i][j]==0) {
01495                                                 weightMat[i][j]=1.0-rand()*threshold/(RAND_MAX+1.0);
01496                                                 weightMat[j][i]=weightMat[i][j];
01497                                         }
01498                                 }
01499                                 else {
01500                                                 adjacencyMat[i][j]=0;
01501                                                 weightMat[i][j]=0;
01502                                 }
01503                         }
01504                         else {
01505                                 cout << "Problem when reading adjacency matrix" << endl;
01506                                 return false;
01507                         }
01508                 }
01509         }
01510 
01511         return true;
01512 }

void rewire ( int **  adjacency,
double **  weight,
int  array_size,
int  k,
double  p 
)

00349 {
00350         rewire_WS2(adjacency, weight, array_size, k, p);
00351 }

void rewire_WS ( int **  adjacency,
double **  weight,
int  n,
int  k,
double  p 
)

00267 {
00268         //
00269         // OK, now rewire.
00270         // Start with the edges closest to each node (in a clockwise direction) and rewire with
00271         // probability p to give a new endpoint, provided that (i) it is not wired to us and
00272         // (ii) there is not already a link between us and that node. Then move out to the edges
00273         // connecting us to the next furthest away nodes (again in a clockwise direction) and repeat
00274         //
00275         for (int i = 1; i <= k/2; i++)
00276                 for (int j = 0; j < n ; j++)
00277                         if (((double) rand())/RAND_MAX <= p)
00278                         {       int idx = (i+j) % n;    // Make it circular
00279 
00280                                 // Choose new endpoint
00281                                 //
00282                                 int new_end = rand() % n;
00283 
00284                                 while (new_end == idx || adjacency[j][new_end] != 0)
00285                                         new_end = rand() % n;
00286 
00287                                 adjacency[j][idx]         = 0;
00288                                 adjacency[idx][j]         = 0;
00289                                 adjacency[j][new_end] = 1;
00290                                 adjacency[new_end][j] = 1;
00291                         }
00292 }

void rewire_WS2 ( int **  adjacency,
double **  weight,
int  n,
int  k,
double  p 
)

00295 {
00296         //
00297         // OK, now rewire.
00298         // Start with the edges closest to each node (in a clockwise direction) and rewire with
00299         // probability p to give a new endpoint, provided that (i) it is not wired to us and
00300         // (ii) there is not already a link between us and that node. Then move out to the edges
00301         // connecting us to the next furthest away nodes (again in a clockwise direction) and repeat
00302         //
00303         for (int i = 1; i <= k/2; i++)
00304                 for (int j = 0; j < n ; j++)
00305                         if (((double) rand())/RAND_MAX <= p)
00306                         {       int idx = (i+j) % n;    // Make it circular
00307 
00308                                 // Choose new endpoint
00309                                 //
00310                                 int new_end = rand() % n;
00311 
00312                                 while (new_end == idx || adjacency[j][new_end] != 0)
00313                                         new_end = rand() % n;
00314 
00315                                 adjacency[j][idx]         = 0;
00316                                 adjacency[idx][j]         = 0;
00317                                 adjacency[j][new_end] = 1;
00318                                 adjacency[new_end][j] = 1;
00319 
00320                                 weight[j][new_end]        = 3;
00321                                 weight[new_end][j]    = 3;
00322                         }
00323 }

void splitNetwork ( int **  adjacency,
double *  betw,
int  array_size 
)

00972                                                                  { 
00973 
00974         int best=0;
00975         double bestBetw=0;
00976         
00977         double betw2[array_size];
00978         
00979         //calculate highest betweenness
00980         for (int i=0;i<array_size;i++) {
00981                 if (betw[i]>bestBetw) { 
00982                         bestBetw=betw[i];
00983                         best=i; 
00984                 }       
00985         }
00986         
00987         bool oneFound=false;
00988         double minBetw=bestBetw;
00989         int linkToDelete=0;
00990         for (int i=0;i<array_size;i++) {
00991                 if ((adjacency[best][i]==1)&&(best!=i)){
00992                                         adjacency[best][i]=0;
00993                                         adjacency[i][best]=0;   
00994                                         calculate_node_betweenness(betw2, adjacency, array_size);
00995                                         if (betw2[best]<minBetw) {
00996                                                 minBetw=betw2[best];
00997                                                 linkToDelete=i;
00998                                         }       
00999                                         adjacency[best][i]=1;
01000                                         adjacency[i][best]=1;
01001                                         
01002                 }
01003         }
01004 
01005         adjacency[best][linkToDelete]=0;
01006         adjacency[linkToDelete][best]=0;
01007         
01008         /*bool oneFound=false;
01009         for (int i=0;i<array_size;i++) {
01010                 if ((adjacency[best][i]==1)&&(best!=i)&&(oneFound==false)){
01011                                         adjacency[best][i]=0;
01012                                         adjacency[i][best]=0;   
01013                                         oneFound=true;
01014                 }
01015         }*/
01016 
01017         
01018         cout<<"The highest value of betweenness is "<<betw[best]<< " (node "<<best+1<<")\n";
01019         cout<<"Link to delete is between link "<<(best+1)<<" and "<<linkToDelete+1<<endl;
01020 }

double splitNetwork_edge_Threshold ( int **  adjacency,
int **  tempadjacency,
double **  betw,
int  array_size,
double  modThreshold 
)

01267                                                                                                                                {
01268 
01269         int numberOfGroups_before=0;
01270         int numberOfGroups_after=0;
01271         double numberOfLinks_before=0;
01272         double bestbw=0;
01273         int bestx=-1;
01274         int besty=-1;
01275 
01276         int **groups_before=initialise_int_array(array_size);
01277         int **groups_after=initialise_int_array(array_size);
01278         
01279         double **totalResults=initialise_double_array(array_size);
01280         int **adjacency_before=initialise_int_array(array_size);        
01281         
01282         //adjacency_before is the connectivity matrix before the splitting procedure
01283         for (int i=0;i<array_size;i++) {
01284                 for (int j=0;j<array_size;j++) {
01285                         adjacency_before[i][j]=adjacency[i][j];
01286                         totalResults[i][j]=0;
01287                 }
01288         }
01289         
01290         int numberOfMembers_before[array_size];
01291         int numberOfMembers_after[array_size];
01292 
01293         for (int i=0;i<array_size; i++) {
01294                 numberOfMembers_before[i]=0;
01295                 numberOfMembers_after[i]=0;
01296         }
01297          
01298         numberOfGroups_before=getGroups(adjacency, groups_before,numberOfMembers_before,array_size);
01299         
01300         //If there is a link to remove (the number of nodes is not the same as the groups)
01301         if(numberOfGroups_before<array_size) {
01302                 //Split network by removing the edge with the higher betweenness
01303                 //First finding edge with highest betweenness
01304                 for (int i=0;i<array_size;i++) {
01305                         for (int j=0;j<array_size;j++) {
01306                                 if(betw[i][j]>bestbw) {
01307                                         bestbw=betw[i][j];
01308                                         bestx=i;besty=j;
01309                                 }
01310                         }
01311                 }
01312         
01313                 //Removing edge
01314                 tempadjacency[bestx][besty]=0;
01315                 tempadjacency[besty][bestx]=0;
01316                 
01317         }
01318         //adjacency is not renamed but it must be considered adjacency_after
01319         
01320         numberOfGroups_after=getGroups(tempadjacency, groups_after ,numberOfMembers_after,array_size);
01321         
01322         double **modularity=initialise_double_array(array_size);
01323         double **modularity_num=initialise_double_array(array_size);
01324         
01325         for (int i=0;i<numberOfGroups_after;i++) 
01326                 for (int j=0;j<numberOfGroups_after;j++) {
01327                         modularity_num[i][j]=0;         
01328                 }
01329         
01330         //Measuring total nymber of links       
01331         for (int i=0;i<array_size;i++)
01332                 for (int j=0;j<array_size;j++) {
01333                          if(adjacency_before[i][j]==1 && i!=j)
01334                                 numberOfLinks_before=numberOfLinks_before+1;    
01335         }
01336 
01337         for (int i=0;i<array_size;i++) 
01338                 for (int j=0;j<array_size;j++) {
01339                         if (adjacency_before[i][j]==1 && i!=j)  {
01340                                 for (int g1=0;g1<numberOfGroups_after;g1++) {
01341                                         if (isInGroup(i+1,groups_after[g1],numberOfMembers_after[g1])) {
01342                                                 for (int g2=0;g2<numberOfGroups_after;g2++)
01343                                                         if (isInGroup(j+1,groups_after[g2],numberOfMembers_after[g2]))
01344                                                         {                                               
01345                                                                 modularity_num[g1][g2]=modularity_num[g1][g2]+1;
01346                                                         }
01347                                         }
01348                                 }
01349                         }
01350         }               
01351         
01352         for (int i=0;i<numberOfGroups_after;i++) 
01353                 for (int j=0;j<numberOfGroups_after;j++) {
01354                         modularity[i][j]=((double)modularity_num[i][j]/(double)(numberOfLinks_before));         
01355                 }       
01356                 
01357         //cout<<"The total number of links is:"<<numberOfLinks_before<<"\n";
01358         //cout<<"The e matrix is the following:\n";
01359         //print_array_double(modularity_num,numberOfGroups_after);
01360 
01361         //calculation of the modularity Q
01362         //Q= Tr e - ||e^2||
01363         //where ||x|| indicates the sum of all elements of x  
01364 
01365         //calculation of the trace of the matrix e
01366         double total1=0;   
01367         for (int i=0;i<numberOfGroups_after;i++)
01368                 total1=total1+modularity[i][i];
01369   
01370         for (int i=0;i<numberOfGroups_after;i++) {
01371                 for (int j=0;j<numberOfGroups_after;j++) {
01372                         double partial=0;
01373                         for (int t=0;t<numberOfGroups_after;t++)
01374                                 totalResults[i][j]+=partial+modularity[i][t]*modularity[t][j];
01375                 }
01376         }         
01377   
01378         //calculation of ||e^2|| 
01379         double total2=0;
01380         for (int i=0;i<numberOfGroups_after;i++)
01381                 for (int j=0;j<numberOfGroups_after;j++)
01382                         total2=total2+totalResults[i][j];
01383                 
01384         //sum of the two parts Tr e and ||e^2|| 
01385 
01386         double q=total1-total2;
01387 
01388         //cout<<"The scalar value of q is "<<q<<"\n";
01389 
01390         for (int i=0;i<array_size;i++) {
01391                 delete[] groups_before[i];
01392                 delete[] groups_after[i];
01393                 delete[] totalResults[i];
01394                 delete[] adjacency_before[i];
01395                 delete[] modularity[i];
01396                 delete[] modularity_num[i];
01397         }
01398 
01399         delete[] groups_before;
01400         delete[] groups_after;
01401         delete[] totalResults;
01402         delete[] adjacency_before;
01403         delete[] modularity;
01404         delete[] modularity_num;
01405         
01406         if (numberOfGroups_after==array_size) {
01407         //cout<<"It is not possible to split the networks, since the number of groups is equal to the number of hosts.\n";
01408         return -1;
01409     }
01410 
01411     if (2*q>=modThreshold || q==0) {
01412 
01413         //cout<<"Successful splitting!\n";
01414         return q;
01415 
01416     }
01417     else {
01418         for (int i=0;i<array_size;i++)
01419             for (int j=0;j<array_size;j++) {
01420                 tempadjacency[i][j]=adjacency_before[i][j];
01421         }
01422         return -1;
01423     }
01424 }

double splitNetwork_Threshold ( int **  adjacency,
double *  betw,
int  array_size,
double  modThreshold 
)

01024                                                                                                    {
01025 
01026         int numberOfGroups_before=0;
01027         int numberOfGroups_after=0;
01028         double numberOfLinks_before=0;
01029 
01030         int **groups_before=initialise_int_array(array_size);
01031         int **groups_after=initialise_int_array(array_size);
01032         
01033         double **totalResults=initialise_double_array(array_size);
01034         int **adjacency_before=initialise_int_array(array_size);        
01035         
01036         //adjacency_before is the connectivity matrix before the splitting procedure
01037         for (int i=0;i<array_size;i++)
01038                 for (int j=0;j<array_size;j++) {
01039                         adjacency_before[i][j]=adjacency[i][j];
01040                 }
01041         int numberOfMembers_before[array_size];
01042         int numberOfMembers_after[array_size];
01043 
01044         for (int i=0;i<array_size; i++) {
01045                 numberOfMembers_before[i]=0;
01046                 numberOfMembers_after[i]=0;
01047         }
01048          
01049         numberOfGroups_before=getGroups(adjacency, groups_before,numberOfMembers_before,array_size);
01050         
01051         splitNetwork(adjacency, betw, array_size);
01052         
01053         //adjacency is not renamed but it must be considered adjacency_after
01054         
01055         numberOfGroups_after=getGroups(adjacency, groups_after ,numberOfMembers_after,array_size);
01056         
01057         double **modularity=initialise_double_array(array_size);
01058         double **modularity_num=initialise_double_array(array_size);
01059         
01060         for (int i=0;i<numberOfGroups_after;i++) 
01061                 for (int j=0;j<numberOfGroups_after;j++) {
01062                         modularity_num[i][j]=0;         
01063                 }
01064                 
01065         for (int i=0;i<array_size;i++)
01066                 for (int j=0;j<array_size;j++) {
01067                         if (adjacency_before[i][j]==1)
01068                                 numberOfLinks_before=numberOfLinks_before+1;    
01069         }
01070 
01071 
01072         for (int i=0;i<array_size;i++) 
01073                 for (int j=0;j<array_size;j++) {
01074                         if (adjacency_before[i][j]==1)  {
01075                                 for (int g1=0;g1<numberOfGroups_after;g1++) {
01076                                         if (isInGroup(i+1,groups_after[g1],numberOfMembers_after[g1])) {
01077                                                 for (int g2=0;g2<numberOfGroups_after;g2++)
01078                                                         if (isInGroup(j+1,groups_after[g2],numberOfMembers_after[g2])) {
01079                                                         
01080                                                                 modularity_num[g1][g2]=modularity_num[g1][g2]+1;
01081                                                         }
01082                                         }
01083                                 }
01084                         }
01085         }               
01086         
01087         for (int i=0;i<numberOfGroups_after;i++) 
01088                 for (int j=0;j<numberOfGroups_after;j++) {
01089                         modularity[i][j]=modularity_num[i][j]/numberOfLinks_before;             
01090                 }
01091    
01092         //cout<<"The total number of links is:"<<numberOfLinks_before<<"\n";
01093         //cout<<"The e matrix is the following:\n";
01094         //print_array_double(modularity_num,numberOfGroups_after);
01095 
01096         //calculation of the modularity Q
01097         //Q= Tr e - ||e^2||
01098         //where ||x|| indicates the sum of all elements of x  
01099 
01100         //calculation of the trace of the matrix e
01101         double total1=0;   
01102         for (int i=0;i<numberOfGroups_after;i++)
01103         total1=total1+modularity[i][i];
01104 
01105         for (int i=0;i<numberOfGroups_after;i++) {
01106                 for (int j=0;j<numberOfGroups_after;j++) {
01107                                 double partial=0;
01108                                 for (int t=0;t<numberOfGroups_after;t++)
01109                                         totalResults[i][j]=partial+modularity[i][t]*modularity[t][j];
01110                 }
01111         }         
01112 
01113 
01114         //calculation of ||e^2|| 
01115         double total2=0;
01116         for (int i=0;i<numberOfGroups_after;i++)
01117         for (int j=0;j<numberOfGroups_after;j++)
01118                 total2=total2+totalResults[i][j];
01119         
01120         //sum of the two parts Tr e and ||e^2|| 
01121 
01122         double q=total1-total2;
01123 
01124         //cout<<"The scalar value of q is "<<q<<"\n";
01125 
01126 
01127         if (numberOfGroups_after==array_size) {
01128                 //cout<<"It is not possible to split the networks, since the number of groups is equal to the number of hosts.\n";
01129                 return -1;      
01130         }       
01131 
01132         if (q>modThreshold) {
01133         
01134                 //cout<<"Successful splitting!\n";
01135                 return q;
01136 
01137         }
01138 
01139         else {
01140                 for (int i=0;i<array_size;i++)
01141                         for (int j=0;j<array_size;j++) {
01142                                 adjacency[i][j]=adjacency_before[i][j];
01143                 }
01144                 return -1;
01145         }
01146 
01147         for (int i=0;i<array_size;i++) {
01148                 delete[] groups_before[i];
01149                 delete[] groups_after[i];
01150                 delete[] totalResults[i];
01151                 delete[] adjacency_before[i];
01152                 delete[] modularity[i];
01153                 delete[] modularity_num[i];
01154         }
01155 
01156         delete[] groups_before;
01157         delete[] groups_after;
01158         delete[] totalResults;
01159         delete[] adjacency_before;
01160         delete[] modularity;
01161         delete[] modularity_num;
01162         
01163 }

Generated on Mon Aug 13 18:29:03 2012 for ECMM by  doxygen 1.6.3