|
@ -72,7 +72,7 @@ int pagerank(CsrSparseMatrix *transitionMatrix, double **pagerankVector, |
|
|
if (parameters.history) { |
|
|
if (parameters.history) { |
|
|
// Outputs pagerank vector to file
|
|
|
// Outputs pagerank vector to file
|
|
|
savePagerankToFile(parameters.outputFilename, iterations != 0, |
|
|
savePagerankToFile(parameters.outputFilename, iterations != 0, |
|
|
*pagerankVector, numberOfPages); |
|
|
*pagerankVector, numberOfPages, realIterations); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Periodically checks for convergence
|
|
|
// Periodically checks for convergence
|
|
@ -149,11 +149,11 @@ int pagerank(CsrSparseMatrix *transitionMatrix, double **pagerankVector, |
|
|
} |
|
|
} |
|
|
} while (!*convergenceStatus && (parameters.maxIterations == 0 || |
|
|
} while (!*convergenceStatus && (parameters.maxIterations == 0 || |
|
|
iterations < parameters.maxIterations)); |
|
|
iterations < parameters.maxIterations)); |
|
|
|
|
|
(*parameters).realIterations = iterations; |
|
|
if (!parameters.history) { |
|
|
if (!parameters.history) { |
|
|
// Outputs last pagerank vector to file
|
|
|
// Outputs last pagerank vector to file
|
|
|
savePagerankToFile(parameters.outputFilename, false, *pagerankVector, |
|
|
savePagerankToFile(parameters.outputFilename, false, *pagerankVector, |
|
|
numberOfPages); |
|
|
numberOfPages, parameters.realIterations); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Frees memory
|
|
|
// Frees memory
|
|
@ -195,7 +195,7 @@ void initialize(CsrSparseMatrix *transitionMatrix, |
|
|
"\nGraph filename: %s\n", (*parameters).convergenceCriterion, |
|
|
"\nGraph filename: %s\n", (*parameters).convergenceCriterion, |
|
|
(*parameters).dampingFactor, (*parameters).graphFilename); |
|
|
(*parameters).dampingFactor, (*parameters).graphFilename); |
|
|
} |
|
|
} |
|
|
|
|
|
(*parameters).realIterations = 0; |
|
|
// Allocates memory for the pagerank vector
|
|
|
// Allocates memory for the pagerank vector
|
|
|
(*pagerankVector) = (double *) malloc((*parameters).numberOfPages * sizeof(double)); |
|
|
(*pagerankVector) = (double *) malloc((*parameters).numberOfPages * sizeof(double)); |
|
|
double webUniformProbability = 1. / (*parameters).numberOfPages; |
|
|
double webUniformProbability = 1. / (*parameters).numberOfPages; |
|
@ -215,6 +215,8 @@ void calculateNextPagerank(CsrSparseMatrix *transitionMatrix, |
|
|
double *linksFromConvergedPagesPagerankVector, |
|
|
double *linksFromConvergedPagesPagerankVector, |
|
|
double *convergedPagerankVector, int vectorSize, double dampingFactor) { |
|
|
double *convergedPagerankVector, int vectorSize, double dampingFactor) { |
|
|
// Calculates the web uniform probability once.
|
|
|
// Calculates the web uniform probability once.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double webUniformProbability = 1. / vectorSize; |
|
|
double webUniformProbability = 1. / vectorSize; |
|
|
|
|
|
|
|
|
csrSparseMatrixVectorMultiplication(*transitionMatrix, previousPagerankVector, |
|
|
csrSparseMatrixVectorMultiplication(*transitionMatrix, previousPagerankVector, |
|
@ -228,8 +230,9 @@ void calculateNextPagerank(CsrSparseMatrix *transitionMatrix, |
|
|
vectorNorm(*pagerankVector, vectorSize); |
|
|
vectorNorm(*pagerankVector, vectorSize); |
|
|
|
|
|
|
|
|
for (int i=0; i<vectorSize; ++i) { |
|
|
for (int i=0; i<vectorSize; ++i) { |
|
|
(*pagerankVector)[i] += normDifference * webUniformProbability + |
|
|
//(*pagerankVector)[i] += normDifference * webUniformProbability +
|
|
|
linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i]; |
|
|
//linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i];
|
|
|
|
|
|
(*pagerankVector)[i] += 0.5*normDifference* webUniformProbability +linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i]; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -414,34 +417,33 @@ void generateNormalizedTransitionMatrixFromFile(CsrSparseMatrix *transitionMatri |
|
|
|
|
|
|
|
|
// Calculates the outdegree of each page and assigns the uniform probability
|
|
|
// Calculates the outdegree of each page and assigns the uniform probability
|
|
|
// of transition to the elements of the corresponding row
|
|
|
// of transition to the elements of the corresponding row
|
|
|
int currentRow = tempMatrix.elements[0]->rowIndex, pageOutdegree = 1; |
|
|
|
|
|
for (int i=1; i<tempMatrix.size; ++i) { |
|
|
int* pageOutdegree = malloc((*parameters).numberOfPages*sizeof(int)); |
|
|
CooSparseMatrixElement *currentElement = tempMatrix.elements[i]; |
|
|
for (int i=0; i<(*parameters).numberOfPages; ++i){ |
|
|
if (currentElement->rowIndex == currentRow) { |
|
|
pageOutdegree[i] = 0; |
|
|
++pageOutdegree; |
|
|
|
|
|
} else { |
|
|
|
|
|
double pageUniformProbability = 1. / pageOutdegree; |
|
|
|
|
|
for (int j=i-pageOutdegree; j<i; ++j) { |
|
|
|
|
|
tempMatrix.elements[j]->value = pageUniformProbability; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
currentRow = currentElement->rowIndex; |
|
|
|
|
|
pageOutdegree = 1; |
|
|
for (int i=0; i<numberOfEdges; ++i) { |
|
|
|
|
|
int currentRow = tempMatrix.elements[i]->rowIndex; |
|
|
|
|
|
|
|
|
|
|
|
if (currentRow == tempMatrix.elements[i]->rowIndex) { |
|
|
|
|
|
++pageOutdegree[currentRow]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Does the last row
|
|
|
for (int i=0; i<tempMatrix.size; ++i) { |
|
|
double pageUniformProbability = 1. / pageOutdegree; |
|
|
tempMatrix.elements[i]->value = 1./pageOutdegree[tempMatrix.elements[i]->rowIndex]; |
|
|
for (int j=tempMatrix.size-pageOutdegree; j<tempMatrix.size; ++j) { |
|
|
|
|
|
tempMatrix.elements[j]->value = pageUniformProbability; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Transposes the temporary transition matrix (P^T).
|
|
|
// Transposes the temporary transition matrix (P^T).
|
|
|
transposeSparseMatrix(&tempMatrix); |
|
|
transposeSparseMatrix(&tempMatrix); |
|
|
|
|
|
|
|
|
allocMemoryForCsr(transitionMatrix, numberOfEdges); |
|
|
allocMemoryForCsr(transitionMatrix, numberOfEdges); |
|
|
// Transforms the temporary COO matrix to the desired CSR format
|
|
|
// Transforms the temporary COO matrix to the desired CSR format
|
|
|
transformToCSR(tempMatrix, transitionMatrix); |
|
|
transformToCSR(tempMatrix, transitionMatrix); |
|
|
|
|
|
//printCsrSparseMatrix(*transitionMatrix);
|
|
|
destroyCooSparseMatrix(&tempMatrix); |
|
|
destroyCooSparseMatrix(&tempMatrix); |
|
|
|
|
|
|
|
|
fclose(graphFile); |
|
|
fclose(graphFile); |
|
@ -479,7 +481,7 @@ int checkIncrement(int previousIndex, int maxIndex, char *programName) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void savePagerankToFile(char *filename, bool append, double *pagerankVector, |
|
|
void savePagerankToFile(char *filename, bool append, double *pagerankVector, |
|
|
int vectorSize) { |
|
|
int vectorSize, int realIterations) { |
|
|
FILE *outputFile; |
|
|
FILE *outputFile; |
|
|
|
|
|
|
|
|
if (append) { |
|
|
if (append) { |
|
@ -492,11 +494,13 @@ void savePagerankToFile(char *filename, bool append, double *pagerankVector, |
|
|
printf("Error while opening the output file.\n"); |
|
|
printf("Error while opening the output file.\n"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
//Save numberofPages and convergence time
|
|
|
|
|
|
|
|
|
for (int i=0; i<vectorSize; ++i) { |
|
|
for (int i=0; i<vectorSize; ++i) { |
|
|
fprintf(outputFile, "%f ", pagerankVector[i]); |
|
|
fprintf(outputFile, "%f ", pagerankVector[i]); |
|
|
} |
|
|
} |
|
|
fprintf(outputFile, "\n"); |
|
|
fprintf(outputFile, "\n"); |
|
|
|
|
|
//fprintf(outputFile, "%d\t", vectorSize);
|
|
|
|
|
|
//fprintf(outputFile, "%d\t", realIterations);
|
|
|
fclose(outputFile); |
|
|
fclose(outputFile); |
|
|
} |
|
|
} |