-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted_pagerank.cpp
More file actions
586 lines (493 loc) · 17.7 KB
/
weighted_pagerank.cpp
File metadata and controls
586 lines (493 loc) · 17.7 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <unordered_map>
using namespace std;
class Paper{
public:
vector< int > successorList;
vector< int > predecessorList;
vector< int > authorList;
vector< int > affiliationList;
int year;
int conference;
int journal;
int conferenceHindex;
int journalHindex;
};
class PageRank{
private:
double dampingFactor;
double convergenceThreshold;
double smallPositiveValue;
vector< double > paperScoreList;
int getConferenceCount(vector< Paper > &graph){
int paperCount = graph.size();
int maxConference = -1;
bool isFirst = true;
for(int paper = 0; paper < paperCount; paper ++){
int conference = graph[paper].conference;
if(conference < 0){
continue;
}
if(isFirst || maxConference < conference){
maxConference = conference;
isFirst = false;
}
}
return maxConference + 1;
}
int getJournalCount(vector< Paper > &graph){
int paperCount = graph.size();
int maxJournal = -1;
bool isFirst = true;
for(int paper = 0; paper < paperCount; paper ++){
int journal = graph[paper].journal;
if(journal < 0){
continue;
}
if(isFirst || maxJournal < journal){
maxJournal = journal;
isFirst = false;
}
}
return maxJournal + 1;
}
int getAuthorCount(vector< Paper > &graph){
int paperCount = graph.size();
int maxAuthor = -1;
bool isFirst = true;
for(int paper = 0; paper < paperCount; paper ++){
for(auto &author: graph[paper].authorList){
if(isFirst || maxAuthor < author){
maxAuthor = author;
isFirst = false;
}
}
}
return maxAuthor + 1;
}
int getAffiliationCount(vector< Paper > &graph){
int paperCount = graph.size();
int maxAffiliation = -1;
bool isFirst = true;
for(int paper = 0; paper < paperCount; paper ++){
for(auto &affiliation: graph[paper].affiliationList){
if(isFirst || maxAffiliation < affiliation){
maxAffiliation = affiliation;
isFirst = false;
}
}
}
return maxAffiliation + 1;
}
int getCurrentYear(vector< Paper > &graph){
int paperCount = graph.size();
int latestYear = 0;
bool lateFirst = true;
for(int paper = 0; paper < paperCount; paper ++){
int year = graph[paper].year;
if(lateFirst || latestYear < year){
latestYear = year;
lateFirst = false;
}
}
return latestYear + 1;
}
void computeConferenceWeights(vector< Paper > &graph, vector< double > &paperWeightList, vector< double > &conferenceScoreList, double &conferenceScoreMean){
printf("\tCompute conference scores\n");
int paperCount = graph.size();
int conferenceCount = this -> getConferenceCount(graph);
conferenceScoreList.assign(conferenceCount, 0.0);
vector< int > conferencePaperCountList(conferenceCount, 0);
for(int paper = 0; paper < paperCount; paper ++){
int conference = graph[paper].conference;
if(conference < 0){
continue;
}
conferenceScoreList[conference] += paperWeightList[paper];
conferencePaperCountList[conference] += 1;
}
conferenceScoreMean = 0.0;
int conferenceEffectiveCount = 0;
for(int conference = 0; conference < conferenceCount; conference ++){
if(conferencePaperCountList[conference] > 0){
conferenceScoreList[conference] /= conferencePaperCountList[conference];
conferenceScoreMean += conferenceScoreList[conference];
conferenceEffectiveCount += 1;
}
}
if(conferenceEffectiveCount > 0){
conferenceScoreMean /= conferenceEffectiveCount;
}
}
void computeJournalWeights(vector< Paper > &graph, vector< double > &paperWeightList, vector< double > &journalScoreList, double &journalScoreMean){
printf("\tCompute journal scores\n");
int paperCount = graph.size();
int journalCount = this -> getJournalCount(graph);
journalScoreList.assign(journalCount, 0.0);
vector< int > journalPaperCountList(journalCount, 0);
for(int paper = 0; paper < paperCount; paper ++){
int journal = graph[paper].journal;
if(journal < 0){
continue;
}
journalScoreList[journal] += paperWeightList[paper];
journalPaperCountList[journal] += 1;
}
journalScoreMean = 0.0;
int journalEffectiveCount = 0;
for(int journal = 0; journal < journalCount; journal ++){
if(journalPaperCountList[journal] > 0){
journalScoreList[journal] /= journalPaperCountList[journal];
journalScoreMean += journalScoreList[journal];
journalEffectiveCount += 1;
}
}
if(journalEffectiveCount > 0){
journalScoreMean /= journalEffectiveCount;
}
}
void computeAuthorWeights(vector< Paper > &graph, vector< double > &paperWeightList, vector< double > &authorScoreList, double &authorScoreMean){
printf("\tCompute author scores\n");
int paperCount = graph.size();
int authorCount = this -> getAuthorCount(graph);
authorScoreList.assign(authorCount, 0.0);
vector< int > authorPaperCountList(authorCount, 0);
for(int paper = 0; paper < paperCount; paper ++){
int paperAuthorCount = graph[paper].authorList.size();
if(paperAuthorCount > 0){
double vote = paperWeightList[paper] / paperAuthorCount;
for(auto &author: graph[paper].authorList){
authorScoreList[author] += vote;
authorPaperCountList[author] += 1;
}
}
}
authorScoreMean = 0.0;
int authorEffectiveCount = 0;
for(int author = 0; author < authorCount; author ++){
if(authorPaperCountList[author] > 0){
authorScoreList[author] /= authorPaperCountList[author];
authorScoreMean += authorScoreList[author];
authorEffectiveCount += 1;
}
}
if(authorEffectiveCount > 0){
authorScoreMean /= authorEffectiveCount;
}
}
void computeAffiliationWeights(vector< Paper > &graph, vector< double > &paperWeightList, vector< double > &affiliationScoreList, double &affiliationScoreMean){
printf("\tCompute affiliation scores\n");
int paperCount = graph.size();
int affiliationCount = this -> getAffiliationCount(graph);
affiliationScoreList.assign(affiliationCount, 0.0);
vector< int > affiliationPaperCountList(affiliationCount, 0);
for(int paper = 0; paper < paperCount; paper ++){
int paperAffiliationCount = graph[paper].affiliationList.size();
if(paperAffiliationCount > 0){
double vote = paperWeightList[paper] / paperAffiliationCount;
for(auto &affiliation: graph[paper].affiliationList){
affiliationScoreList[affiliation] += vote;
affiliationPaperCountList[affiliation] += 1;
}
}
}
affiliationScoreMean = 0.0;
int affiliationEffectiveCount = 0;
for(int affiliation = 0; affiliation < affiliationCount; affiliation ++){
if(affiliationPaperCountList[affiliation] > 0){
affiliationScoreList[affiliation] /= affiliationPaperCountList[affiliation];
affiliationScoreMean += affiliationScoreList[affiliation];
affiliationEffectiveCount += 1;
}
}
if(affiliationEffectiveCount > 0){
affiliationScoreMean /= affiliationEffectiveCount;
}
}
void computeInitialPaperWeights(vector< Paper > &graph, vector< double > &paperWeightList){
int paperCount = graph.size();
int currentYear = this -> getCurrentYear(graph);
paperWeightList.assign(paperCount, 0.0);
for(int paper = 0; paper < paperCount; paper ++){
int indegree = graph[paper].predecessorList.size();
paperWeightList[paper] = (indegree > 0)? (double)indegree / (currentYear - graph[paper].year): this -> smallPositiveValue;
}
}
void generatePaperWeightList(vector< Paper > &graph, vector< double > &paperWeightList){
int paperCount = graph.size();
this -> computeInitialPaperWeights(graph, paperWeightList);
vector< double > conferenceScoreList;
double conferenceScoreMean = 0.0;
this -> computeConferenceWeights(graph, paperWeightList, conferenceScoreList, conferenceScoreMean);
vector< double > journalScoreList;
double journalScoreMean = 0.0;
this -> computeJournalWeights(graph, paperWeightList, journalScoreList, journalScoreMean);
for(int paper = 0; paper < paperCount; paper ++){
double conferenceScore = conferenceScoreMean;
int conference = graph[paper].conference;
if(conference >= 0){
conferenceScore = conferenceScoreList[conference];
}
paperWeightList[paper] += conferenceScore;
}
for(int paper = 0; paper < paperCount; paper ++){
double journalScore = journalScoreMean;
int journal = graph[paper].journal;
if(journal >= 0){
journalScore = journalScoreList[journal];
}
paperWeightList[paper] += journalScore;
}
vector< double > authorScoreList;
double authorScoreMean = 0.0;
this -> computeAuthorWeights(graph, paperWeightList, authorScoreList, authorScoreMean);
vector< double > affiliationScoreList;
double affiliationScoreMean = 0.0;
this -> computeAffiliationWeights(graph, paperWeightList, affiliationScoreList, affiliationScoreMean);
for(int paper = 0; paper < paperCount; paper ++){
double authorScore = authorScoreMean;
int paperAuthorCount = graph[paper].authorList.size();
if(paperAuthorCount > 0){
authorScore = 0.0;
for(auto &author: graph[paper].authorList){
authorScore += authorScoreList[author];
}
authorScore /= paperAuthorCount;
}
paperWeightList[paper] += authorScore;
}
for(int paper = 0; paper < paperCount; paper ++){
double affiliationScore = affiliationScoreMean;
int paperAffiliationCount = graph[paper].affiliationList.size();
if(paperAffiliationCount > 0){
affiliationScore = 0.0;
for(auto &affiliation: graph[paper].affiliationList){
affiliationScore += affiliationScoreList[affiliation];
}
affiliationScore /= paperAffiliationCount;
}
paperWeightList[paper] += affiliationScore;
}
}
void runPaperPageRank(vector< Paper > &graph){
printf("\tDeclare variables and compute weights\n");
int paperCount = graph.size();
vector< double > lastPaperScoreList(paperCount, 1.0 / paperCount);
vector< double > paperWeightList;
this -> generatePaperWeightList(graph, paperWeightList);
vector< double > paperSuccessorWeightSumList(paperCount, 0.0);
for(int paper = 0; paper < paperCount; paper ++){
for(auto &neighbor: graph[paper].successorList){
paperSuccessorWeightSumList[paper] += paperWeightList[neighbor];
}
}
vector< double > paperRegularizationList(paperWeightList);
double paperWeightSum = 0.0;
for(int paper = 0; paper < paperCount; paper ++){
paperWeightSum += paperRegularizationList[paper];
}
if(paperWeightSum > 0){
for(int paper = 0; paper < paperCount; paper ++){
paperRegularizationList[paper] /= paperWeightSum;
}
}
printf("\tStart algorithms\n");
for(int iteration = 1; ; iteration ++){
printf("\t\tIteration %d", iteration);
this -> paperScoreList.assign(paperCount, 0.0);
double zeroOutdegreePaperScore = 0.0;
for(int paper = 0; paper < paperCount; paper ++){
if(graph[paper].successorList.size() == 0 || paperSuccessorWeightSumList[paper] == 0.0){
zeroOutdegreePaperScore += lastPaperScoreList[paper];
}
else{
double score = lastPaperScoreList[paper] / paperSuccessorWeightSumList[paper];
for(auto &neighbor: graph[paper].successorList){
double vote = score * paperWeightList[neighbor];
this -> paperScoreList[neighbor] += vote;
}
}
}
double scoreDifference = 0.0;
for(int paper = 0; paper < paperCount; paper ++){
double zeroVote = zeroOutdegreePaperScore * paperRegularizationList[paper];
this -> paperScoreList[paper] = (1.0 - this -> dampingFactor) * paperRegularizationList[paper]
+ this -> dampingFactor * (zeroVote + this -> paperScoreList[paper]);
scoreDifference += fabs(lastPaperScoreList[paper] - this -> paperScoreList[paper]);
}
printf("\t\tScore difference = %f\n", scoreDifference);
if(scoreDifference < this -> convergenceThreshold){
printf("\t\tConvergence\n");
break;
}
lastPaperScoreList = this -> paperScoreList;
}
}
public:
PageRank(double dampingFactor, double convergenceThreshold){
this -> dampingFactor = dampingFactor;
this -> convergenceThreshold = convergenceThreshold;
this -> smallPositiveValue = 1e-10;
}
void run(vector< Paper > &graph){
int paperCount = graph.size();
this -> paperScoreList.assign(paperCount, 0.0);
this -> runPaperPageRank(graph);
}
void printResults(char *fileName, vector< unsigned int > &paperNameList){
int paperCount = this -> paperScoreList.size();
double maxValue = 0, minValue = 0;
bool isMaxFirst = true, isMinFirst = true;
for(int paper = 0; paper < paperCount; paper ++){
double value = this -> paperScoreList[paper];
if(isMaxFirst || maxValue < value){
maxValue = value;
isMaxFirst = false;
}
if(isMinFirst || minValue > value){
minValue = value;
isMinFirst = false;
}
}
FILE *outFile = fopen(fileName, "w");
for(int paper = 0; paper < paperCount; paper ++){
unsigned int name = paperNameList[paper];
fprintf(outFile, "%08X\t%.15e\n", name, (this -> paperScoreList[paper] - minValue) / (maxValue - minValue));
}
fclose(outFile);
}
};
void readGraphFile(char *graphFileName, vector< Paper > &graph, unordered_map< unsigned int, int > &namePaperDict, vector < unsigned int > &paperNameList){
FILE *inFile = fopen(graphFileName, "r");
unsigned int nameFrom, nameTo;
int paperIndex= 0;
while(fscanf(inFile, "%X%X", &nameFrom, &nameTo) == 2){
if(namePaperDict.find(nameFrom) == namePaperDict.end()){
namePaperDict[nameFrom] = paperIndex;
Paper newPaper;
graph.push_back(newPaper);
paperNameList.push_back(nameFrom);
paperIndex += 1;
}
if(namePaperDict.find(nameTo) == namePaperDict.end()){
namePaperDict[nameTo] = paperIndex;
Paper newPaper;
graph.push_back(newPaper);
paperNameList.push_back(nameTo);
paperIndex += 1;
}
int paperFrom = namePaperDict[nameFrom];
int paperTo = namePaperDict[nameTo];
graph[paperFrom].successorList.push_back(paperTo);
graph[paperTo].predecessorList.push_back(paperFrom);
}
fclose(inFile);
}
void readFeatureFile(char *paperFileName, unordered_map< unsigned int, int > &namePaperDict, vector< Paper > &graph){
unordered_map< unsigned int, int > nameConferenceDict, nameJournalDict, nameAuthorDict, nameAffiliationDict;
int conferenceIndex = 0, journalIndex = 0, authorIndex = 0, affiliationIndex = 0;
FILE *inFile = fopen(paperFileName, "r");
char line[100000];
while(fgets(line, 100000, inFile)){
char *linePtr = line;
int charCount;
unsigned int paperName;
sscanf(linePtr, "%X%n", &paperName, &charCount);
linePtr += charCount;
int paper = namePaperDict[paperName];
int year;
sscanf(linePtr, "%d%n", &year, &charCount);
linePtr += charCount;
graph[paper].year = year;
int conferenceJournalExists;
sscanf(linePtr, "%d%n", &conferenceJournalExists, &charCount);
linePtr += charCount;
switch(conferenceJournalExists){
case 1:{
unsigned int conferenceName;
sscanf(linePtr, "%X%n", &conferenceName, &charCount);
linePtr += charCount;
if(nameConferenceDict.find(conferenceName) == nameConferenceDict.end()){
nameConferenceDict[conferenceName] = conferenceIndex;
conferenceIndex += 1;
}
int conference = nameConferenceDict[conferenceName];
graph[paper].conference = conference;
graph[paper].journal = -1;
break;
}
case 2:{
unsigned int journalName;
sscanf(linePtr, "%X%n", &journalName, &charCount);
linePtr += charCount;
if(nameConferenceDict.find(journalName) == nameConferenceDict.end()){
nameConferenceDict[journalName] = journalIndex;
journalIndex += 1;
}
int journal = nameConferenceDict[journalName];
graph[paper].journal = journal;
graph[paper].conference = -1;
break;
}
default:
graph[paper].conference = -1;
graph[paper].journal = -1;
}
int authorCount;
sscanf(linePtr, "%d%n", &authorCount, &charCount);
linePtr += charCount;
for(int a = 0; a < authorCount; a ++){
unsigned int authorName;
sscanf(linePtr, "%X%n", &authorName, &charCount);
linePtr += charCount;
if(nameAuthorDict.find(authorName) == nameAuthorDict.end()){
nameAuthorDict[authorName] = authorIndex;
authorIndex += 1;
}
int author = nameAuthorDict[authorName];
graph[paper].authorList.push_back(author);
}
int affiliationCount;
sscanf(linePtr, "%d%n", &affiliationCount, &charCount);
linePtr += charCount;
for(int a = 0; a < affiliationCount; a ++){
unsigned int affiliationName;
sscanf(linePtr, "%X%n", &affiliationName, &charCount);
linePtr += charCount;
if(nameAffiliationDict.find(affiliationName) == nameAffiliationDict.end()){
nameAffiliationDict[affiliationName] = affiliationIndex;
affiliationIndex += 1;
}
int affiliation = nameAffiliationDict[affiliationName];
graph[paper].affiliationList.push_back(affiliation);
}
}
fclose(inFile);
}
int main(int argc, char *argv[]){
char *graphFileName = argv[1];
char *paperFileName = argv[2];
char *outputFileName = argv[3];
double dampingFactor = 0.5;
double convergenceThreshold = 1e-4;
vector< Paper > graph;
unordered_map< unsigned int, int > namePaperDict;
vector < unsigned int > paperNameList;
printf("Read graph file\n");
readGraphFile(graphFileName, graph, namePaperDict, paperNameList);
int paperCount = graph.size();
printf("\tNumber of papers = %d\n", paperCount);
printf("Read feature file\n");
readFeatureFile(paperFileName, namePaperDict, graph);
printf("Run PageRank algorithm\n");
PageRank pagerank(dampingFactor, convergenceThreshold);
pagerank.run(graph);
printf("Print PageRank scores\n");
pagerank.printResults(outputFileName, paperNameList);
printf("OK\n");
return 0;
}