4
Using the starting notes 0.15 for all and then add note as explained in the article http://cmup.fc.up.pt/cmup/mecs/googlePR.pdf
If we consider pages 1, 2 and 3 to be:
1: link1
2: link2
3: link3
The.txt page file of this example would be:
1 link1
2 link2
3 link3
Whereas the.txt links file would be:
1 2
2 1
2 3
Your program should then calculate the Pagerank of each of the pages contained in the.txt page file (based on the.txt links file), and output a file called Pages.txt, where the pages are ordered from the largest to the smallest Pagerank. If two pages have the same Pagerank, the one with the smallest id should come first.
Would anyone know why this loop of mine is giving the wrong results? My.txt link looks like this :
1 3
2 1
3 1
3 2
3 4
My.txt page looks like this:
1 link1
2 link2
3 link3
4 link4
my Loop:
void calculoPageRank(struct page *pageRank, int numLinhas){
int h=1,g=1;
int j=1,i=1;
float soma = 0.0, difer= 0.2;
while (difer>=0.05) {
while (h<=numLinhas) {
soma= 0.0;
i=pageRank[h].numeroDeLinksRecebe;
while (i!=0) {
g = pageRank[h].quaisLinks[i];
soma += 0.85 * (pageRank[g].rank/pageRank[g].numeroDeLinksEnviados);
i--;
difer = (soma+0.15) - pageRank[h].rank;
}
pageRank[h].rank=soma+0.15;
printf("%f ",difer);
h++;
}
j++;
}
}
And the complete algorithm:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct page{
char pagina[20];
int numeroDeLinksRecebe;
int numeroDeLinksEnviados;
int quaisLinks[30];
float rank;
}page;
int numeroLinhas(){
char caracter = '\0';
int numLinhas = 0;
FILE *arq;
arq = fopen("paginas.txt", "r");
while (!feof(arq)) {
fread(&caracter, 1, 1, arq);
if (caracter=='\n') {
numLinhas++;
}
}
fclose(arq);
return numLinhas;
}
void calculoPageRank(struct page *pageRank, int numLinhas){
int h=1,g=1;
int j=1,i=1;
float soma = 0.0, difer= 0.2;
while (difer>=0.05) {
while (h<=numLinhas) {
soma= 0.0;
i=pageRank[h].numeroDeLinksRecebe;
while (i!=0) {
g = pageRank[h].quaisLinks[i];
soma += 0.85 * (pageRank[g].rank/pageRank[g].numeroDeLinksEnviados);
i--;
difer = (soma+0.15) - pageRank[h].rank;
}
pageRank[h].rank=soma+0.15;
printf("%f ",difer);
h++;
}
j++;
}
}
void leitura(struct page *pageRank, int numLinhas){
int i=1,j=1;
FILE *arq;
arq = fopen("paginas.txt", "r");
while (fscanf(arq, "%d %s", &j, pageRank[i].pagina)!=EOF) {
i++;
}
fclose(arq);
int g = 1;
for (i=1; i<=numLinhas; i++) {
pageRank[i].numeroDeLinksRecebe = 0;
pageRank[i].numeroDeLinksEnviados=0;
pageRank[i].rank = 0.15;
}
int aux;
FILE *arq1;
arq1=fopen("links.txt", "r");
while (fscanf(arq1, "%d %d", &i, &aux)!=EOF) {
if (i==j) {
g++;
pageRank[i].numeroDeLinksRecebe++;
pageRank[aux].numeroDeLinksEnviados++;
}
else{
j=i;
g=1;
pageRank[i].numeroDeLinksRecebe++;
pageRank[aux].numeroDeLinksEnviados++;
}
pageRank[i].quaisLinks[g] = aux;
}
fclose(arq1);
}
int main(){
int numLinhas = numeroLinhas()+1;
page pageRank[80];
int i=1;
leitura(pageRank, numLinhas);
for (i=1; i<=numLinhas; i++) {
printf("i=%d recebe=%d envia=%d rank=%f\n", i, pageRank[i].numeroDeLinksRecebe, pageRank[i].numeroDeLinksEnviados,pageRank[i].rank);
}
calculoPageRank(pageRank, numLinhas);
for (i=1; i<=numLinhas; i++) {
printf("\n %d rank: %f\n", i, pageRank[i].rank);
}
}
Within this context the loop is returning an output that I did not expect. The correct return would be: PR1 = 1.4901 PR2 = 0.7833 PR3 = 1.5766 PR4 = 0.1500 and delivering R1 = 0.277500 PR2 = 0.267938 PR3 = 0.623184 PR4 = 0.150000.
Does anyone know why my loop is delivering inconsistent results?
What a result your program is generating?
– Gabe
PR1 = 0.277500 PR2 = 0.267938 PR3 = 0.623184 PR4 = 0.150000
– Nicolas Bontempo
Too bad the people who voted negative didn’t come back to check on you now.
– Maniero