-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSignificanceTester.cpp
More file actions
170 lines (145 loc) · 4.58 KB
/
Copy pathSignificanceTester.cpp
File metadata and controls
170 lines (145 loc) · 4.58 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
//
// Created by Rohit on 21-Dec-17.
//
#include "SignificanceTester.h"
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include "Split.h"
#include <set>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
std::map<std::string, set<int>> data;
std::map<std::string, int> all_signatures;
void prepareData(std::string dataFile, std::string pathFile, bool reverseEdge) {
std::vector<std::string> templine;
ifstream infile(dataFile.c_str());
int src, dst;
int timestamp;
string line;
string key;
while (infile >> line) {
templine = Tools::Split(line, ',');
src = stoi(templine[0]);
dst = stoi(templine[1]);
if (reverseEdge) {
src = stoi(templine[1]);
dst = stoi(templine[0]);
}
timestamp = stol(templine[2].substr(0, 10).c_str());
key = to_string(src) + "-" + to_string(dst);
data[key].insert(timestamp);
}
cout << "parsed all data" << endl;
ifstream pfile(pathFile.c_str());
int length;
int idx;
string signature = "";
while (pfile >> line) {
signature = "";
templine = Tools::Split(line, ',');
length = stoi(templine[1]);
idx = 2;
while (idx < length * 3) {
signature = signature + templine[idx + 1] + "-" + templine[idx + 2] + ",";
idx = idx + 3;
}
if (all_signatures.count(signature) == 0) {
all_signatures[signature] = 0;
}
all_signatures[signature]++;
}
cout << "# of unique signatures:: " << all_signatures.size() << endl;
}
void getSignificantCycle(int window, string output) {
int window_bracket = window * 60 * 60;
std::vector<std::string> tempsig;
int length;
int significant_count=0;
int not_significant_count=0;
for (map<string, int>::iterator it = all_signatures.begin(); it != all_signatures.end(); ++it) {
tempsig = Tools::Split(it->first, ',');
length = tempsig.size();
vector<double> freq_table;
vector<set<int>> time_table;
set<int> T;
double P = 1.0;
double T_mod = 0.0;
double Ti_mod = 0.0;
P = 1.0;
for (int i = 0; i < length; ++i) {
time_table.push_back(data[tempsig[i]]);
T.insert(data[tempsig[i]].begin(), data[tempsig[i]].end());
}
T_mod = T.size();
for (int i = 0; i < time_table.size(); i++) {
Ti_mod = time_table[i].size();
P = P * (Ti_mod / T_mod);
}
bool is_first = true;
set<int> W;
int temp_t;
int t_next;
int t_1 = *T.begin();
int cnt = 0;
for (set<int>::iterator it = T.begin(); it != T.end(); ++it) {
temp_t = *it;
if (is_first) {
if (temp_t - t_1 <= window_bracket) {
W.insert(temp_t);
} else {
cnt = binomialCoeff(W.size(), length);
t_next = temp_t;
W.clear();
W.insert(t_next);
is_first = false;
}
} else {
if (temp_t - t_next <= window_bracket) {
W.insert(temp_t);
} else {
cnt =cnt+ binomialCoeff(W.size()-1, length-1);
t_next = temp_t;
W.clear();
W.insert(t_next);
}
}
}
if(is_first){
cnt = binomialCoeff(W.size(), length);
}else{
if(W.size()>0){
cnt =cnt+ binomialCoeff(W.size()-1, length-1);
W.clear();
}
}
double expected_cnt=P*cnt;
if(it->second>2*expected_cnt){
significant_count++;
}else{
// cout<<"non significant cycle of length"<<length<<" : "<<it->second<<endl;
not_significant_count++;
}
cout << "P, " << P ;
cout<<",count, "<<cnt;
cout<<",cnt time p ,"<<expected_cnt;
cout<<",actual count, "<<it->second;
cout<<",length, "<<length<<endl;
}
cout<<"significant count,"<<significant_count<<endl;
cout<<"not significant count,"<<not_significant_count<<endl;
}
int binomialCoeff(int n, int k) {
int C[k + 1];
memset(C, 0, sizeof(C));
C[0] = 1; // nC0 is 1
for (int i = 1; i <= n; i++) {
// Compute next row of pascal triangle using
// the previous row
for (int j = min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}