-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagerank.cpp
More file actions
563 lines (496 loc) · 16.6 KB
/
Copy pathpagerank.cpp
File metadata and controls
563 lines (496 loc) · 16.6 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
// File: P1.cpp
// Date: 4/17/2021
// Author: Brian Hilnbrand
#include <iostream>
#include <iomanip>
#include <vector>
#include <bits/stdc++.h>
#include <math.h>
#include <cmath>
// Calculate greatest common denominator
long long gcd(long long a, long long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// Number class contains both fraction and double representation of number
class Number {
// long long num, den;
double value;
// bool isDouble;
public:
long long num, den;
bool isDouble;
// Construct fraction
Number(long long n, long long d) {
num = n;
den = d;
value = toDouble();
isDouble = false;
}
// Construct double
Number(double v) {
value = v;
isDouble = true;
}
// Reduce fraction
void reduce() {
if (num == 0) {
den = 1;
}
else if(num == den) {
num = den = 1;
}
else {
long long gcdVal = gcd(num, den);
num /= gcdVal;
den /= gcdVal;
}
}
// Use double instead of fraction. This will convert the fraction to a double.
void useDouble() {
if (!isDouble) {
value = toDouble();
isDouble = true;
num = 1;
den = 1;
}
}
// Overloaded multiply operator to another Number object
Number operator*(Number const& rhs) {
Number result = *this;
if (result.isDouble || rhs.isDouble) {
result.useDouble();
result.value *= rhs.toDouble();
}
else {
result.num *= rhs.num;
result.den *= rhs.den;
result.reduce();
result.value = result.toDouble();
}
return result;
}
// Overloaded multiply operator to a double
Number operator*(double val) {
Number result = *this;
if (result.isDouble) {
result.value *= val;
}
else {
result.num *= val;
result.reduce();
result.value = result.toDouble();
}
return result;
}
// Overloaded divide operator. If both numbers are fractions,
// multiply this fraction by inverse of the other fraction
Number operator/(Number const& rhs) {
Number result = *this;
if (result.isDouble || rhs.isDouble) {
result.useDouble();
result.value /= rhs.toDouble();
}
else {
result = result * Number(rhs.den, rhs.num);
}
return result;
}
// Overloaded addition operator
Number operator+(Number const& rhs) {
Number result = *this;
if (result.isDouble || rhs.isDouble) {
result.useDouble();
result.value += rhs.toDouble();
}
else {
long long lcd = result.den * rhs.den;
result.num *= rhs.den;
result.num += rhs.num * result.den;
result.den = lcd;
result.reduce();
result.value = result.toDouble();
}
return result;
}
// Overloaded subtraction operator
Number operator-(Number const& rhs) {
Number result = *this;
if (result.isDouble || rhs.isDouble) {
result.useDouble();
result.value -= rhs.toDouble();
}
else {
long long lcd = result.den * rhs.den;
result.num *= rhs.den;
result.num -= rhs.num * result.den;
result.den = lcd;
result.reduce();
result.value = result.toDouble();
}
return result;
}
// Return double representation of this number
double toDouble() const {
if (isDouble) {
return value;
}
double num_d = static_cast<double>(num);
double den_d = static_cast<double>(den);
return num_d/den_d;
}
// Return the delta (absolute distance) from this number to the other number
double delta(Number& other) {
return std::abs(toDouble() - other.toDouble());
}
// Return true if this number is equivalent to 0
bool isZero() {
if (isDouble) {
return value == 0.0;
}
return num == 0;
}
// Return true if this number is equivalent to 1
bool isOne() {
if (isDouble) {
return value == 1.0;
}
return num == den;
}
// Return true if this number is greater than the given double value
bool operator>(double val) {
return toDouble() > val;
}
// Overloaded output operator
friend std::ostream& operator<<(std::ostream& os, const Number& f);
};
// Implement the overloaded output operator with the given number.
// If the number is a fraction, print as a fraction.
// If the number is a decimal, print as a decimal.
std::ostream& operator<<(std::ostream& os, const Number& f)
{
if (f.isDouble) {
os << f.value;
}
else if (f.num == 0) {
os << 0;
}
else if(f.num == f.den) {
os << 1;
}
else {
long long gcdVal = gcd(f.num, f.den);
os << f.num / gcdVal << '/' << f.den / gcdVal;
}
return os;
}
// Define Matrix as a vector of vector of Numnbers
typedef std::vector< std::vector<Number> > Matrix;
// Print a matrix. Decimals will be printed with 15 decimal place precision.
void printMatrix(Matrix &matrix) {
for (int i = 0; i < matrix.size(); ++i) {
std::cout << " ";
for (int j = 0; j < matrix[i].size(); ++j) {
std::cout << std::setprecision(15) << matrix[i][j] << "\t";
}
std::cout << std::endl;
}
}
// Scale a matrix by the given Number
Matrix scaleMatrix(Matrix &matrix, Number scale) {
Matrix ret;
for (int i = 0; i < matrix.size(); ++i) {
std::vector<Number> temp;
for (int j = 0; j < matrix[i].size(); ++j) {
temp.push_back(matrix[i][j] * scale);
}
ret.push_back(temp);
}
return ret;
}
// Create a new matrix with size [rows, cols] with each element initial value as initial_value.
Matrix createNewMatrix(int rows, int cols, Number initial_value) {
std::vector<Number> row;
Matrix mat;
for (int i = 0; i < cols; ++i) {
row.push_back(initial_value);
}
for (int i = 0; i < rows; ++i) {
mat.push_back(row);
}
return mat;
}
// Add two matricies
Matrix matAdd(Matrix &a, Matrix &b) {
Matrix sum = createNewMatrix(a.size(), a[0].size(), Number(1,1));
for(int i = 0; i < a.size(); ++i) {
for(int j = 0; j < a[0].size(); ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
}
return sum;
}
// Multiply two matricies
Matrix matMul(Matrix &a, Matrix &b) {
const int rows1 = a.size();
const int cols1 = a[0].size();
const int cols2 = b[0].size();
std::vector<Number> m;
Matrix mult = createNewMatrix(rows1, cols2, Number(0,1));
// Multiplying matrix a and b and storing in array mult.
for(int i = 0; i < rows1; ++i) {
for(int j = 0; j < cols2; ++j) {
for(int k = 0; k < cols1; ++k)
{
mult[i][j] = (a[i][k] * b[k][j]) + mult[i][j];
}
}
}
return mult;
}
// Return true if the given string is a number
// (either whole number with no decimal places, or decimal number with only 1 '.')
bool is_number(const std::string& s)
{
bool isValidNumber = (s.find_first_not_of("0123456789.") == std::string::npos);
isValidNumber &= (s.find_first_of(".") == s.find_last_of("."));
return isValidNumber;
}
// Convert all values in the given matrix to doubles
void convertMatrixToDouble(Matrix &matrix) {
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[i].size(); ++j) {
matrix[i][j].useDouble();
}
}
}
// Read matrix from stdin with the following rules/functions:
// 1. Number of rows must match number of columns in the first inputted row.
// 2. All values must be a valid number.
// 3. Ignore all comments (from '#' to the end of the line)
// 4. Ignore eroneous whitespace
// 5. Exit program if matrix has bad formatting
Matrix readMatrix() {
std::string input;
Matrix matrix;
int matrix_size = 0;
bool initialized = false;
bool useDouble = false;
bool notNumeric = false;
int row_count = 0;
do {
bool found_comment = false;
std::string word;
int word_count = 0;
std::vector<Number> temp_row;
std::getline (std::cin,input);
if (input == "") {
break;
}
std::istringstream ss(input);
// Read each row column by column
while (!found_comment && ss >> word) {
// Remove comment
int comment_start = word.find('#');
if (comment_start != std::string::npos) {
if (comment_start == 0) {
found_comment = true;
}
word = word.substr(0,comment_start);
}
// If value contains values
if (!found_comment) {
int del = word.find('/');
// If value is a fraction
if (del != std::string::npos) {
double num, den;
std::istringstream num_ss(word.substr(0,del));
std::istringstream den_ss(word.substr(del+1));
if (!is_number(num_ss.str()) || !is_number(den_ss.str())) {
std::cout << "BAD FORMATTING, QUITTING.\n";
exit(0);
}
num_ss >> num;
den_ss >> den;
temp_row.push_back(Number(num,den));
}
// If value is not a fraction
else {
// If not a valid number
if (!is_number(word)) {
std::cout << "BAD FORMATTING, QUITTING.\n";
exit(0);
}
double val;
std::istringstream(word) >> val;
// If value is a decimal
if (word.find('.') != std::string::npos) {
temp_row.push_back(Number(val));
useDouble = true;
}
// If value is a whole number (not a fraction but no decimal places, i.e. '1')
// add as fraction (1/1)
else {
temp_row.push_back(Number(val,1));
}
}
// If matrix has not been initialized yet, set expected matrix size
if (!initialized) {
++matrix_size;
}
++word_count;
}
}
// If good row, push to matrix
if (!temp_row.empty()) {
matrix.push_back(temp_row);
initialized = true;
++row_count;
// If matrix is not initialized or row count is not as expected, there is bad formatting, quit.
if (row_count > matrix_size || word_count != matrix_size) {
std::cout << "BAD FORMATTING, QUITTING.\n";
exit(0);
}
}
} while (input != "");
if (!initialized) {
std::cout << "BAD FORMATTING, QUITTING.\n";
exit(0);
}
for (int c = 0; c < matrix_size; ++c) {
Number col_total = Number(0,1);
for (int r = 0; r < matrix_size; ++r) {
col_total = matrix[r][c] + col_total;
}
if (col_total.toDouble() != 1 && col_total.toDouble() != 0) {
std::cout << "BAD FORMATTING, QUITTING.\n";
exit(0);
}
}
// If there is a single decimal, convert entire matrix to double
if (useDouble) {
convertMatrixToDouble(matrix);
}
return matrix;
}
// Remove sink nodes
std::vector<int> reduceMatrix(Matrix &inputMatrix) {
std::vector<int> nodes_to_remove;
bool reduced = false;
for (int col = 0; col < inputMatrix[0].size(); ++col) {
int zero_count = 0;
for (int row = 0; row < inputMatrix.size(); ++row) {
if (inputMatrix[row][col].isZero()) {
zero_count++;
}
else if (inputMatrix[row][col].isOne() && row == col) {
nodes_to_remove.push_back(col);
break;
}
}
if (zero_count == inputMatrix[col].size()) {
nodes_to_remove.push_back(col);
}
}
if (nodes_to_remove.size() > 0) {
// Remove node columns and rows
// Only remove first node (node with the lowest index). The rest will be removed recursively.
int node_to_remove = nodes_to_remove[0];
for (int row = 0; row < inputMatrix.size(); ++row) {
inputMatrix[row].erase(inputMatrix[row].begin()+node_to_remove);
}
inputMatrix.erase(inputMatrix.begin()+node_to_remove);
reduced = true;
nodes_to_remove.clear();
nodes_to_remove.push_back(node_to_remove);
// Recursively call reduceMatrix on the current matrix and collect returned removed nodes indices
if (reduced) {
std::vector<int> rec_ret = reduceMatrix(inputMatrix);
for (int i = 0; i < rec_ret.size(); ++i) {
if (rec_ret[i] >= node_to_remove) {
rec_ret[i] += 1;
}
}
nodes_to_remove.insert(nodes_to_remove.end(), rec_ret.begin(), rec_ret.end());
}
// Redistribute weights
for (int col = 0; col < inputMatrix[0].size(); ++col) {
Number total = Number(0,1);
for (int row = 0; row < inputMatrix.size(); ++row) {
total = inputMatrix[row][col] + total;
}
for (int row = 0; row < inputMatrix.size(); ++row) {
inputMatrix[row][col] = inputMatrix[row][col] / total;
}
}
}
return nodes_to_remove;
}
// Complete one iteration with random walk.
// B is given.
// Keep track of iteration count. If > 5 iterations, convert matrix to double.
bool iterateWithRandomWalk(Matrix &matrix, Matrix &rank, Number B, int iterationCount) {
// If > 5 iterations, convert matrix to double
if (iterationCount == 6) {
convertMatrixToDouble(matrix);
}
// Scale matrix by B
Matrix scaledMatrix = scaleMatrix(matrix, B);
// Create random walk matrix with initial value (1/rows * (1 - B))
double rows = matrix.size();
Matrix randomWalkMatrix = createNewMatrix(rows, rows, Number(1,rows) * (Number(1,1)-B));
// Add scaled matrix to scaled random walk matrix
Matrix sum = matAdd(scaledMatrix, randomWalkMatrix);
// Multiply resulting matrix with rank matrix to create a new rank
Matrix newrank = matMul(sum, rank);
// Check if any value has changed more then epsilon. If so, allow another iteration.
double e = 0.000000000001;
bool cont = false;
for (int i = 0; i < newrank.size(); ++i) {
if (rank[i][0].delta(newrank[i][0]) > e) {
cont = true;
break;
}
}
rank = newrank;
return cont;
}
// Run PageRank algorithm via stdin
int main() {
// Read matrix from stdin
Matrix matrix = readMatrix();
// Reduce matrix
std::vector<int> removed_nodes = reduceMatrix(matrix);
// Print input matrix size as well as those nodes that have been removed
sort(removed_nodes.begin(), removed_nodes.end());
std::cout << "Input matrix A with dimension " << matrix.size() << "x" << matrix[0].size();
if (removed_nodes.size() > 0) {
std::cout << " by removing node ";
for (int i = 0; i < removed_nodes.size(); ++i) {
std::cout << removed_nodes[i] + 1;
if (i < removed_nodes.size()-1) {
std::cout << ", ";
}
}
}
// Print input matrix
std::cout << ":\n";
printMatrix(matrix);
// Run PageRank iterations. Print rank after every iteration.
// Stop when delta < epsilon. Stop at 100000 iterations to avoid never-ending program.
int iterationCount = 0;
Matrix rank = createNewMatrix(matrix.size(), 1, Number(1, matrix.size()));
std::cout << "Output matrix C with dimension " << rank.size() << "x" << rank[0].size() << ":\n";
while (iterateWithRandomWalk(matrix, rank, Number(7,8), ++iterationCount)) {
std::cout << "--Iteration " << iterationCount << ":" << std::endl;
printMatrix(rank);
if (iterationCount > 100000) {
break;
}
}
// Print final rank
std::cout << "--Iteration " << iterationCount << ":" << std::endl;
printMatrix(rank);
return 0;
}