-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpagerank.cpp
More file actions
212 lines (177 loc) · 4.64 KB
/
pagerank.cpp
File metadata and controls
212 lines (177 loc) · 4.64 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <algorithm>
#include <chrono>
#include <cstring>
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
using Probability = long double;
const int MAX_N = 40000000;
int numberOfPages;
int currentState = 0;
Probability *state[2];
string *names;
vector<int> *incoming;
int *outcoming;
bool *valid;
Probability randomHopProbability = 0.1;
inline Probability absolute(Probability x) {
return (x>=0)?x:-x;
}
bool cmp(int a, int b) {
return state[currentState][a] > state[currentState][b];
}
struct Page {
string name;
int id;
bool redirect;
vector<int> links;
};
void addEdge(int from, int to) {
incoming[to].push_back(from);
outcoming[from]++;
}
void addPage(const Page &page) {
names[page.id] = page.name;
for (auto id : page.links)
addEdge(page.id, id);
}
void loadFromFile(ifstream &in) {
string line;
while (getline(in, line)) {
string tmp;
int k = 0, n;
Page p;
for (int i = 0; i < line.length(); i++) {
char c = line[i];
if (c == '|') {
if (k == 0)
p.id = stoi(tmp);
else if (k == 1)
p.name = tmp;
else if (k == 2)
p.redirect = (stoi(tmp))>0?true:false;
else if (k == 3)
n = stoi(tmp);
else
p.links.push_back(stoi(tmp));
k++;
tmp = "";
} else
tmp += c;
}
if(tmp!="")
p.links.push_back(stoi(tmp));
addPage(p);
}
}
bool load(const string &path) {
ifstream in(path);
bool exists = in.good();
if (exists) {
cout << "loading " << path << "..." << endl;
loadFromFile(in);
}
in.close();
return exists;
}
string printName(const string &name)
{
string ret = name;
bool prevIsSpace = true;
for(int i = 0;i < ret.length(); i++)
{
bool needUpper = prevIsSpace;
if(ret[i]>='a' && ret[i] <= 'z' && needUpper)
ret[i] = ret[i]-'a'+'A';
else if(ret[i] >= 'A' && ret[i] <= 'Z' && !needUpper)
ret[i] = ret[i] - 'A' +'a';
prevIsSpace = !(ret[i]>='a' && ret[i] <= 'z') && !(ret[i] >= 'A' && ret[i] <= 'Z');
}
return ret;
}
void finalize() {
cout << "Checkpointing..." << endl;
vector<int> v;
for (int i = 0; i < numberOfPages; i++)
if (valid[i])
v.push_back(i);
sort(v.begin(), v.end(), cmp);
ofstream out("out");
for (int i = 0; i < numberOfPages; i++) {
int id = v[i];
out << printName(names[id]) << "|" << state[currentState][id] << endl;
}
out.close();
cout << "Checkpoint saved.." << endl;
}
void iteration() {
int nextState = 1 - currentState;
Probability stayProbability = 1.0 - randomHopProbability;
for (int i = 0; i < MAX_N; i++)
state[nextState][i] = 0.0;
Probability unusedProbability = 0.0;
for (int i = 0; i < MAX_N; i++) {
if (!valid[i])
continue;
for (auto from : incoming[i]) {
Probability probPerConnection = stayProbability / outcoming[from];
state[nextState][i] += probPerConnection * state[currentState][from];
}
}
for (int i = 0; i < MAX_N; i++) {
if (!valid[i])
continue;
if (outcoming[i] == 0)
unusedProbability += state[currentState][i];
else
unusedProbability += randomHopProbability * state[currentState][i];
}
cout << unusedProbability << endl;
Probability prob = unusedProbability / numberOfPages;
for (int i = 0; i < MAX_N; i++)
if (valid[i])
state[nextState][i] += prob;
double diff = 0.0;
for (int i = 0; i < numberOfPages; i++)
diff += absolute(state[nextState][i] - state[currentState][i]);
cout << "Diff: " << diff << endl;
currentState = nextState;
}
void cleanup() {
for (int i = 0; i < MAX_N; i++)
if (outcoming[i] > 0 || incoming[i].size() > 0)
valid[i] = true, numberOfPages++;
}
int main() {
state[0] = new Probability[MAX_N];
state[1] = new Probability[MAX_N];
names = new string[MAX_N];
incoming = new vector<int>[MAX_N];
outcoming = new int[MAX_N];
valid = new bool[MAX_N];
string file = "../data/wiki";
int i = 0;
while (load(file + to_string(i++)));
cleanup();
for (int i = 0; i < MAX_N; i++)
if (valid[i])
state[currentState][i] = 1.0 / numberOfPages;
for (int i = 0; i < MAX_N; i++)
incoming[i].shrink_to_fit();
cout << "Initalization finished.." << endl;
int iterationCount = 0;
while (true) {
auto begin = chrono::high_resolution_clock::now();
iteration();
iterationCount++;
auto end = chrono::high_resolution_clock::now();
auto dur = end - begin;
auto ms = chrono::duration_cast<chrono::milliseconds>(dur).count();
cout << "Iteration #" << iterationCount << " Time: " << ms << endl;
if (iterationCount % 10 == 0)
finalize();
}
return 0;
}