/******************************************************************** * Calculates the modularity for the clustering obtained. Obviously, * The clustering must have already been made. * IMPORTANT! We have a "virtual" cluster 0, that represents all hubs * and outliers. It will exist here only so that we don't lose coherence * of proportions, but e_{0,0} SHOULD NOT be considered for the trace Tr e. * ... (I think) ********************************************************************/ float ClusterEvaluator::getModularity() { // 1 - Generate Matrix e, where: // e_{i,j} = (edges linking ci to cj)/(all edges in G) float** e; //std::cout << "num_clusters = " << num_clusters << std::endl; SquareMatrix<float> matrix(num_clusters+1); e = matrix; buildAssortativityMatrix(e); //std::cout << "Matrix e:" << std::endl; //printSquareMatrix(e, num_clusters+1); //std::cout << "---------------------------------------" << std::endl; // 2 - Calculate Trace(e) = SUM_{i} e_{ii} float tr = 0.0; for (int i = 0; i <= num_clusters; ++i){ tr += e[i][i]; } //std::cout << "Trace is: " << tr << std::endl; //std::cout << "Calculate trace (e): DONE!" << std::endl; // 3 - Calculate e^2 SquareMatrix<float> matrix2(num_clusters+1); float** e_squared; e_squared = matrix2; squareMatrixMultiplication(e, e, e_squared, num_clusters+1); //std::cout << "Matrix e squared:" << std::endl; //printSquareMatrix(e_squared, num_clusters+1); //std::cout << "---------------------------------------" << std::endl; //std::cout << "Calculate e^2: DONE!" << std::endl; // 4 - Sum all elements of e^2 (||e^2||) float sum_e = sumElementsSquareMatrix(e_squared, num_clusters+1); //std::cout << "Sum is: " << sum_e << std::endl; //std::cout << "Sum all elements e^2 (||e^2||): DONE!" << std::endl; // 5 - Q = tr - ||e^2|| //std::cout << "Modularity is: " << tr - sum_e << std::endl; return tr - sum_e; }
/** * Exponecia a matriz até a norma da matriz ^ m ser semelhante à norma * da matriz ^ n, determinado por um valor de convergência. * @param matrixExp [description] * @param matrixBase [description] * @param size [description] * @param iteration [description] */ void expontiationUntilConverge(double ***matrixExp, double **matrixBase, unsigned size, unsigned iteration) { double **matrixExpPlusOne = NULL; matrixAlloc(&matrixExpPlusOne, size, size); squareMatrixMultiplication(*matrixExp, matrixBase, matrixExpPlusOne, size); if (!matrixHasConverged(matrixExpPlusOne, *matrixExp, size) && iteration <= maximumExponent) { expontiationUntilConverge(&matrixExpPlusOne, matrixBase, size, ++iteration); } matrixFree(*matrixExp, size); *matrixExp = matrixExpPlusOne; }