-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_yu.cpp
More file actions
75 lines (63 loc) · 1.75 KB
/
Copy paths_yu.cpp
File metadata and controls
75 lines (63 loc) · 1.75 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
MatrixXd readEdgeList(const string& filename, int& n) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error: Cannot open file " << filename << endl;
exit(1);
}
vector<pair<int,int>> edges;
int u, v;
int max_node = -1;
string line;
while (getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
stringstream ss(line);
if (ss >> u >> v) {
edges.emplace_back(u, v);
max_node = max(max_node, max(u, v));
}
}
n = max_node + 1;
MatrixXd A = MatrixXd::Zero(n, n);
for (auto& e : edges) {
if (e.first != e.second) {
A(e.first, e.second) = 1.0;
}
}
return A;
}
MatrixXd makeColumnStochastic(const MatrixXd& A) {
VectorXd col_sums = A.colwise().sum();
MatrixXd Q = A;
for (int j = 0; j < Q.cols(); j++) {
if (col_sums(j) > 0)
Q.col(j) /= col_sums(j);
}
return Q;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <edge_list_file.txt>\n";
return 1;
}
int n;
MatrixXd A = readEdgeList(argv[1], n);
cout << "Graph has " << n << " nodes\n";
MatrixXd Q = makeColumnStochastic(A);
double c = 0.6;
int K = 50;
MatrixXd I = MatrixXd::Identity(Q.rows(), Q.cols());
MatrixXd S = I;
for (int iter = 0; iter < K; iter++) {
S = (c / 2.0) * (Q.transpose() * S + S * Q) + (1 - c) * I;
}
cout << "Iterative SimRank* (" << K << " iters, top-left 5x5):\n"
<< S.topLeftCorner(min(5,n), min(5,n)) << endl;
return 0;
}