-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageRank.cpp
More file actions
124 lines (113 loc) · 3.38 KB
/
PageRank.cpp
File metadata and controls
124 lines (113 loc) · 3.38 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
// PageRank.cpp : This file contains the 'main' function. Program execution
// begins and ends there.
//
#include <iostream>
#include <stdlib.h>
#include <time.h>
// Function declerations
void generateStartingData();
void printMatrix();
void getPageRank();
void printReadableRankResults();
// Constants
// A good value for damping factor is 0.810;
const float DAMPING_FACTOR = 0.81;
const int NUM_PAGES = 25;
const int PRINT_FREQUENCY = 2;
const int USE_STATIC_LINK_MATRIX = 0;
const int STATIC_LINK_SEED = 10;
const int STARTING_PAGERANK = 1;
const float LINK_GENERATION_CHANCE = 0.6f; // Should be between 1 and 0
const int NUM_ITERATIONS = 500000;
// Declerations
int **linkMatrix;
int *countMatrix;
float *pageRankMatrix;
int main() {
// Initialization
if (USE_STATIC_LINK_MATRIX)
srand(STATIC_LINK_SEED);
else
srand(time(NULL));
// Allocation of dynamic memory
countMatrix = new int[NUM_PAGES];
pageRankMatrix = new float[NUM_PAGES];
linkMatrix = new int *[NUM_PAGES];
for (int i = 0; i < NUM_PAGES; i++) {
linkMatrix[i] = new int[NUM_PAGES];
countMatrix[i] = 0;
}
generateStartingData();
getPageRank();
printReadableRankResults();
// Deallocation of dynamic memory
delete[] pageRankMatrix;
delete[] countMatrix;
for (int i = 0; i < NUM_PAGES; i++) {
delete[] linkMatrix[i];
}
delete[] linkMatrix;
}
void generateStartingData() {
std::cout << "Generating page link matrix and resultant link counts...\n";
for (int i = 0; i < NUM_PAGES; i++) {
for (int j = 0; j < NUM_PAGES; j++) {
// Create a link if:
// - Page j doesn't already have a link to page i
// - Page i and page j are not the same page
// - And a random number is greater than the link generation chance
// (converted to percentage)
if (linkMatrix[i][j] != 1 && i != j &&
rand() % 100 <= 100 * LINK_GENERATION_CHANCE) {
linkMatrix[i][j] = 1;
countMatrix[j]++;
} else
linkMatrix[i][j] = 0;
}
pageRankMatrix[i] = STARTING_PAGERANK;
}
printMatrix();
}
void getPageRank() {
std::cout << "\nPerforming " << NUM_ITERATIONS << " iterations...\n";
for (int iteration = 1; iteration <= NUM_ITERATIONS; iteration++) {
for (int i = 0; i < NUM_PAGES; i++) {
// float averagePageRankChange = 0.0f;
float prevPageRank = pageRankMatrix[i];
pageRankMatrix[i] = 0;
for (int j = 0; j < NUM_PAGES; j++) {
pageRankMatrix[i] +=
linkMatrix[i][j] > 0 ? pageRankMatrix[j] / countMatrix[j] : 0;
}
pageRankMatrix[i] *= DAMPING_FACTOR;
pageRankMatrix[i] += (1 - DAMPING_FACTOR);
// averagePageRankChange +=
}
if (PRINT_FREQUENCY > 0 &&
iteration % (NUM_ITERATIONS / PRINT_FREQUENCY) == 0) {
std::cout << "\n Iteration " << iteration << ": \n";
printMatrix();
}
}
}
void printMatrix() {
std::cout << "\n";
for (int i = 0; i < NUM_PAGES; i++) {
for (int j = 0; j < NUM_PAGES; j++) {
std::cout << " " << linkMatrix[i][j] << " ";
}
std::cout << "= " << pageRankMatrix[i];
std::cout << "\n";
}
for (int i = 0; i < NUM_PAGES; i++) {
std::cout << "(" << countMatrix[i] << ") ";
}
std::cout << "\n";
}
void printReadableRankResults() {
std::cout << "\n";
for (int i = 0; i < NUM_PAGES; i++) {
std::cout << "Page " << i << " had rank: " << (int)(pageRankMatrix[i] * 10)
<< "\n";
}
}