-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_manager.cpp
117 lines (79 loc) · 2.66 KB
/
path_manager.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
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
#include <path_manager.h>
PathManager::PathManager(int _nodes_n, int _seqs_n) :
nodes_n(_nodes_n),
seqs_n(_seqs_n) {
}
PathManager::~PathManager() {
}
void PathManager::add_seq_words(const map< vector<int>, int > & seq_words) {
for (const auto & iter: seq_words) {
const vector<int> seq_path = iter.first;
int word_cnt = iter.second;
if (seq_paths.find(seq_path) != seq_paths.end()) {
auto val = seq_paths[seq_path];
val.first += word_cnt;
val.second++;
} else {
seq_paths[seq_path] = make_pair(word_cnt, 1);
}
}
}
void PathManager::print_seq_paths() {
for (const auto & iter: seq_paths) {
const vector<int> seq_path = iter.first;
const auto val = iter.second;
cout << "Path: ";
for (int vertex_idx: seq_path) {
cout << vertex_idx << " ";
}
cout << endl << "Word cnt: " << val.first << " seq cnt: " << val.second << endl;
}
}
void PathManager::build_path_graph() {
path_graph = vector< vector< pair<int, double> > >(nodes_n);
for (const auto & iter: seq_paths) {
const vector<int> seq_path = iter.first;
const auto val = iter.second;
double score = (double)val.first * seq_path.size() / val.second;
for (int i = 1; i < seq_path.size(); i++) {
bool found_flag = false;
int cur_node = seq_path[i - 1];
int next_node = seq_path[i];
for (auto & edge: path_graph[cur_node]) {
if (edge.first == next_node) {
edge.second += score;
found_flag = true;
break;
}
}
if (!found_flag) {
path_graph[cur_node].push_back(make_pair(next_node, score));
}
}
}
}
void PathManager::print_path_graph() {
for (int i = 0; i < nodes_n; i++) {
cout << "Node " << i << ": " << endl;
for (const auto & edge: path_graph[i]) {
cout << edge.first << " " << edge.second << endl;
}
}
}
vector<StripePair> PathManager::build_stripe_pairs() {
int small_w = 3;
for (int i = 0; i < nodes_n; i++) {
for (const auto & edge: path_graph[i]) {
if (edge.second <= small_w) continue;
stripe_pairs.push_back(StripePair(
i,
edge.first,
edge.second,
1,
false
));
}
}
sort(stripe_pairs.begin(), stripe_pairs.end());
return stripe_pairs;
}