-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhitsrank.cpp
74 lines (61 loc) · 2.04 KB
/
hitsrank.cpp
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
// HITS Rank Algorithm
#include "stdafx.h"
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("HITS Rank. Build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
const TStr Iput = Env.GetIfArgPrefixStr("-i:", "Input.txt", "Input File");
const TStr Oput = Env.GetIfArgPrefixStr("-o:", "Output.txt", "Output File");
FILE* fpI = fopen(Iput.CStr(), "r");
FILE* fpO = fopen(Oput.CStr(), "w");
PNGraph Graph = TSnap::LoadEdgeList< PNGraph > (Iput);
fprintf(fpO, "\nNodes: %d, Edges: %d\n\n", Graph->GetNodes(), Graph->GetEdges());
const int NNodes = Graph->GetNodes();
const int MaxIter = 50;
TIntFltH NIdHubH, NIdAuthH;
NIdHubH.Gen(NNodes);
NIdAuthH.Gen(NNodes);
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NIdHubH.AddDat(NI.GetId(), 1.0);
NIdAuthH.AddDat(NI.GetId(), 1.0);
}
double Norm = 0;
for (int iter = 0; iter < MaxIter; iter++) {
// update authority scores
Norm = 0.0;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
double& Auth = NIdAuthH.GetDat(NI.GetId()).Val;
Auth = 0;
for (int e = 0; e < NI.GetInDeg(); e++) {
Auth += NIdHubH.GetDat(NI.GetInNId(e));
}
Norm += Auth*Auth;
}
Norm = sqrt(Norm);
for (int i = 0; i < NIdAuthH.Len(); i++) {
NIdAuthH[i] /= Norm;
}
// update hub scores
Norm = 0.0;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
double& Hub = NIdHubH.GetDat(NI.GetId()).Val;
Hub = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
Hub += NIdAuthH.GetDat(NI.GetOutNId(e));
}
Norm += Hub*Hub;
}
Norm = sqrt(Norm);
for (int i = 0; i < NIdHubH.Len(); i++) {
NIdHubH[i] /= Norm;
}
}
fprintf(fpO, "Node ID\t\tAuthority Value\t\tHub Value\n");
for (int i = 0; i < NNodes; i++){
fprintf(fpO, "%d\t\t\t%.5lf\t\t\t\t%.5lf\n", i, (double) NIdAuthH[i], (double) NIdHubH[i]);
}
Catch
printf("\nRun Time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}