-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomVisit.cpp
More file actions
executable file
·67 lines (51 loc) · 1.53 KB
/
RandomVisit.cpp
File metadata and controls
executable file
·67 lines (51 loc) · 1.53 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 "RandomVisit.h"
using namespace std;
RandomVisit::RandomVisit(Graph* graph) {
this->graph = graph;
}
void RandomVisit::run(int seed, double alpha, unordered_map<int, double>* scores) {
scores->clear();
// Ensure all nodes in output
vector<int> nodes;
graph->nodes(&nodes);
for (const auto& n : nodes) {
scores->insert(make_pair(n, 0.0));
}
//priority_queue<RankedNode, std::vector<RankedNode>, compareRankedNodesClass> pq;
//RankedNode r;
//r.node = seed;
//pq.push(r);
unordered_set<int> visited;
//visited.insert(seed);
int curr_node = seed;
// int pos = 0;
int max_lenght = 5*graph->num_nodes();
int pos = graph->num_nodes()+1;
for (int i = 0; i < max_lenght; i++) {
//RankedNode r = pq.top();
//pq.pop();
//scores->at(r.node) = pos--;
if (visited.find(curr_node) == visited.end()){
scores->at(curr_node) = pos--;
visited.insert(curr_node);
}
double ran = ((double) rand() / (RAND_MAX));
int out_deg = graph->out_degree(curr_node);
if (ran < alpha || out_deg == 0){ // jumpback
curr_node = seed;
} else {
vector<int> neighbors;
graph->out_neighbors(curr_node, &neighbors);
int ran = rand() % out_deg;
curr_node = neighbors[ran];
}
}
}
RandomVisit::~RandomVisit() {
}