-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequential.cpp
85 lines (72 loc) · 2.36 KB
/
sequential.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
#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <sstream>
#include <vector>
#include <tuple>
#include <math.h>
#include <float.h>
#include <map>
#include <algorithm>
#include <chrono>
#include <iomanip>
#include "utils.h"
typedef std::pair<double, int> pi;
void writeFile(std::map<int, std::vector<pi> >& knn,std::string filename){
std::ofstream outFile;
outFile.open(filename);
for (auto const& x : knn){
outFile << x.first << ": ";
auto neighbours = x.second;
for(int i = 0; i < neighbours.size(); i++){
outFile << neighbours[i].second << " " ;
}
outFile << std::endl;
}
outFile.close();
}
int main(int argc, char **argv) {
if(argc != 4){
std::cout << "Wrong input format!" << std::endl;
std::cout << "Usage: " << argv[0] << " K inputFile outputFile" << std::endl;
exit(-1);
}
//reading input values
int k = std::atoi(argv[1]);
std::string inFilename = argv[2];
std::string outFilename = argv[3];
std::ifstream inputFile;
double distance;
auto t_start = std::chrono::high_resolution_clock::now();
std::vector<std::pair<double,double>> points;
std::map<int, std::vector<pi> > knn;
readFile(points,inFilename);
// KNN computetion
for(int i = 0; i < points.size(); i++){
std::vector<pi> neighbours(k,std::make_pair(DBL_MAX,0));
double max = DBL_MAX;
int index_max = 0;
for(int j = 0; j < points.size(); j++){
if(i != j){
distance = computeDistance(points[i],points[j]);
pi newPoint = std::make_pair(distance,j);
if(neighbours.size() >= k){
if(distance < max){
neighbours[index_max] = newPoint;
std::pair<int,double> max_point = findMax(neighbours);
max = max_point.second;
index_max = max_point.first;
}
}
}
}
std::sort(neighbours.begin(),neighbours.end());
knn[i] = neighbours;
}
writeFile(knn,outFilename);
auto t_end = std::chrono::high_resolution_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
std::cout << elapsed_time_ms << std::endl;
return 0;
}