-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathknn.h
143 lines (135 loc) · 4.2 KB
/
knn.h
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
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <numeric>
// #include <windows.h>
// #include <omp.h>
std::vector<size_t> sort_indexes(const std::vector<float> &v) {
//initialize original index locations
std::vector<size_t> idx(v.size());
iota(idx.begin(), idx.end(), 0);
//sort indexes based on comparing values in v
std::stable_sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1] < v[i2]; });
return idx;
}
std::vector<size_t> sort_indexes(const std::vector<int> &v) {
//initialize original index locations
std::vector<size_t> idx(v.size());
iota(idx.begin(), idx.end(), 0);
//sort indexes based on comparing values in v
std::stable_sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1] < v[i2]; });
return idx;
}
void knn_sparse(float* cost_matrix, int n, int m, int k, bool sorted, int* sort_index_matrix) {
// DWORD st, ed;
// st = GetTickCount();
bool* connected = new bool[n * m];
memset(connected, false, n * m * sizeof(bool));
std::vector<size_t>* radish_sort_dist_index = new std::vector<size_t>[m];
int* hole_count = new int[n];
memset(hole_count, 0, n * sizeof(int));
int* radish_toCheck = new int[m];
if (!sorted) {
// #pragma omp parallel for
for (int j = 0; j < m; ++j) {
std::vector<float> col;
for (int i = 0; i < n; ++i) {
col.push_back(cost_matrix[m * i + j]);
}
// col_sort = sort_indexes(col);
radish_sort_dist_index[j] = sort_indexes(col);
radish_toCheck[j] = k;
}
for (int j = 0; j < m; ++j) {
for (int c = 0; c < k; ++c) {
hole_count[radish_sort_dist_index[j][c]] += 1;
connected[radish_sort_dist_index[j][c] * m + j] = true;
}
}
} else {
// #pragma omp parallel for
// sort_index_matrix: shape = m * n
for (int j = 0; j < m; ++j) {
radish_sort_dist_index[j] = std::vector<size_t>(sort_index_matrix + (j * n), sort_index_matrix + ((j + 1) * n));
radish_toCheck[j] = k;
}
for (int j = 0; j < m; ++j) {
for (int c = 0; c < k; ++c) {
hole_count[radish_sort_dist_index[j][c]] += 1;
connected[radish_sort_dist_index[j][c] * m + j] = true;
}
}
}
// ed = GetTickCount();
// printf("Sort Time: %dms\n", ed - st);
std::vector<int> hc(hole_count, hole_count + n);
std::vector<size_t> hc_sort = sort_indexes(hc);
for (int i = n - 1; i >= 0; --i) {
int hole = hc_sort[i];
if (hole_count[hole] <= k) {
break;
}
bool* hole_connected_start = &connected[m * hole];
float* hole_cost_start = &cost_matrix[m * hole];
std::vector<int> rs;
std::vector<float> distances;
for (int j = 0; j < m; ++j) {
if (hole_connected_start[j]) {
rs.push_back(j);
distances.push_back(hole_cost_start[j]);
}
}
std::vector<size_t> order = sort_indexes(distances);
for (int j = order.size() - 1; j >= 0 && hole_count[hole] > k; --j) {
int radish_idx = rs[order[j]];
int next_hole_to_assign;
while (true) {
if (radish_toCheck[radish_idx] == n) {
break;
}
next_hole_to_assign = radish_sort_dist_index[radish_idx][radish_toCheck[radish_idx]];
radish_toCheck[radish_idx]++;
if (hole_count[next_hole_to_assign] < k && !connected[next_hole_to_assign * m + radish_idx]) {
hole_count[next_hole_to_assign]++;
connected[next_hole_to_assign * m + radish_idx] = true;
hole_count[hole]--;
hole_connected_start[radish_idx] = false;
break;
}
}
}
}
for (int i = 0; i < n; ++i) {
float* tmp = &cost_matrix[m * i];
for (int j = 0; j < m; ++j) {
if (!connected[i * m + j]) {
tmp[j] = 0;
}
}
}
delete[] connected;
delete[] hole_count;
delete[] radish_sort_dist_index;
delete[] radish_toCheck;
// ed = GetTickCount();
// printf("Sparse Time: %dms\n", ed - st);
}
int main() {
std::vector<float> a;
a.push_back(1);
a.push_back(2);
a.push_back(1);
a.push_back(2);
a.push_back(1);
a.push_back(2);
a.push_back(1);
a.push_back(2);
std::vector<size_t> b = sort_indexes(a);
for (int i = 0; i < b.size(); ++i) {
printf("%d, ", b[i]);
}
}