-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerIterationPPR.cpp
More file actions
executable file
·67 lines (53 loc) · 1.77 KB
/
PowerIterationPPR.cpp
File metadata and controls
executable file
·67 lines (53 loc) · 1.77 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
#include <iostream>
#include <cassert>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include "PowerIterationPPR.h"
using namespace std;
PowerIterationPPR::PowerIterationPPR(Graph* graph) {
this->graph = graph;
}
void PowerIterationPPR::run(int seed, double alpha, int iterations,
unordered_map<int, double>* scores) {
assert(iterations >= 1);
scores->clear();
unordered_map<int, double> rProb[2];
rProb[0][seed] = 1.0;
int array = 0;
for (int i = 0; i < iterations; i++) {
cerr << "iter : " << i << endl;
int new_array = 1 - array;
rProb[new_array].clear();
for (const auto& node_score : rProb[array]) {
int degreeU = graph->out_degree(node_score.first);
// If dangling node it always jump back
if (degreeU == 0) {
rProb[new_array][seed] += 1.0 * rProb[array][node_score.first];
} else {
vector<int> neighbors;
graph->out_neighbors(node_score.first, &neighbors);
rProb[new_array][seed] += alpha
* rProb[array][node_score.first];
for (const auto& neighbor : neighbors) {
rProb[new_array][neighbor] += (1.0 - alpha)
/ static_cast<double>(degreeU)
* rProb[array][node_score.first];
}
}
}
array = new_array;
}
// Ensure all nodes in output
vector<int> nodes;
graph->nodes(&nodes);
for (const auto& n : nodes) {
scores->insert(make_pair(n, 0.0));
}
for (const auto& node_prob : rProb[array]) {
scores->at(node_prob.first) = node_prob.second;
}
}
PowerIterationPPR::~PowerIterationPPR() {
}