-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla_pagerank.cpp
77 lines (68 loc) · 1.43 KB
/
vanilla_pagerank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<string.h>
#include<fstream>
#include<cmath>
#include<omp.h>
using namespace std;
void calc_rank(long n)
{
long in_node,out_node,node,i,j,k;
double sum,damping_factor,donation,init_rank,rank_sum,rank_average;
init_rank = 1/(double)n;
vector<long>outdegree(n,0);
vector<double>rank(n,init_rank);
vector<vector<long> > mymap(n);
char file_name[30];
string str;
str = to_string(n);
strcpy(file_name,str.c_str());
strcat(file_name,"_nodes.txt");
ifstream file_read;
file_read.open(file_name);
while(file_read>>out_node>>in_node)
{
outdegree[out_node]++;
mymap[in_node].push_back(out_node);
}
damping_factor = 0.85;
for(k = 1 ; k >0 ; k++)
{
rank_sum = 0;
for(i = 0;i<n;i++)
{
sum = 0;
for(j = 0;j<mymap[i].size();j++)
{
node = mymap[i][j];
donation = rank[node]/outdegree[node];
sum += donation;
}
rank[i] = (1-damping_factor) + damping_factor*sum;
rank_sum += rank[i];
}
rank_average = rank_sum/n;
rank_average = rank_average*1000;
if(round(rank_average) == 1000)
break;
}
for(i=0;i<n;i++)
{
cout<<i<<" : "<<rank[i]<<"\n";
}
cout<<"Average = "<<rank_average/1000;
cout<<"No of iterations = "<<k<<"\n";
}
int main()
{
double t1, t2;
double cpu_time_used;
long n = 500;
t1 = omp_get_wtime();
calc_rank(n);
t2 = omp_get_wtime();
cpu_time_used = t2-t1;
cout<<"\n\n Time = "<<cpu_time_used;
}