Based on starting notes 0.15 for everyone and then add note as explained in link
If we consider pages 1, 2 and 3 to be:
1: link1
2: link2
3: link3
The paginas.txt file for this example would be:
1 link1
2 link2
3 link3
While the links.txt file would be:
1 2
2 1
2 3
Your program should then calculate the PageRank of each of the pages contained in the paginas.txt file (based on the links.txt file), and generate as output, a file called PageRanks.txt, where the pages are sorted from highest to lowest PageRank. If two pages have the same PageRank, the file with the lowest id must first come in the file.
Would anyone please let me know why this loop is giving me the wrong results? My link.txt looks like this:
1 3
2 1
3 1
3 2
3 4
My paginas.txt 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 = '1: link1
2: link2
3: link3
';
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 is delivering R1 = 0.277500 PR2 = 0.267938 PR3 = 0.623184 PR4 = 0.150000.
Does anyone know why my loop is delivering inconsistent results?