-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHIERARCHICAL.cpp
More file actions
97 lines (90 loc) · 2.61 KB
/
Copy pathHIERARCHICAL.cpp
File metadata and controls
97 lines (90 loc) · 2.61 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
#include "HIERARCHICAL.h"
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iostream>
void HIERARCHICAL::PrintToFile_H() {
std::ofstream f;
f.open("D:\\First need\\Desktop\\C++ projects\\clasterscurs\\field.txt", std::ios::out);
int actlable, counter = 0;
actlable = h_arrpoints[0].GiveLable();
for (int i = 0; i < h_arrpoints.size(); ++i) {
if (h_arrpoints[i].GiveLable() == actlable) {
h_arrpoints[i].Setlable(actlable + counter);
}
else {
actlable = h_arrpoints[i].GiveLable();
counter++;
h_arrpoints[i].Setlable(actlable + counter);
}
}
for (int i = 0; i < h_arrpoints.size(); ++i) {
f << h_arrpoints[i].Givexcord() << '\t' << h_arrpoints[i].Giveycord() << '\t' << h_arrpoints[i].GiveLable() << std::endl;
}
f.close();
}
void HIERARCHICAL::MatrixFill() {
for (int i = 0; i < h_arrpoints.size(); ++i) {
h_matrix.push_back(std::vector<double>());
for (int j = 0; j < h_arrpoints.size(); ++j) {
h_matrix[i].push_back(h_arrpoints[i].Distance(h_arrpoints[j]));
}
}
}
void HIERARCHICAL::H_Init() {
int lable = 1;
for (int i = 0; i < h_arrpoints.size(); ++i) {
h_arrpoints[i].Setlable(lable);
lable++;
}
}
void HIERARCHICAL::H_Search() {
MatrixFill();
H_Init();
int clusters = h_arrpoints.size();
while (clusters > num) {
int min_d = 1000000;
int minind1 = 0, minind2 = 0;
for (int i = 0; i < h_arrpoints.size(); ++i) {
for (int j = 0; j < h_arrpoints.size(); ++j) {
if ((h_matrix[i][j] < min_d) && (h_matrix[i][j] > 0)) {
min_d = h_matrix[i][j];
minind1 = i;
minind2 = j;
}
}
}
int actlable = std::min(h_arrpoints[minind1].GiveLable(), h_arrpoints[minind2].GiveLable());
int oldlable = std::max(h_arrpoints[minind1].GiveLable(), h_arrpoints[minind2].GiveLable());
for (int i = 0; i < h_arrpoints.size(); ++i) {
if (h_arrpoints[i].GiveLable() == oldlable) {
h_arrpoints[i].Setlable(actlable);
}
}
DeleteClusters(actlable);
clusters = AmountOfClusters();
}
}
int HIERARCHICAL::AmountOfClusters() {
int cnt = 0;
std::vector<int> ppoints;
for (int i = 0; i < h_arrpoints.size(); ++i) {
ppoints.push_back(h_arrpoints[i].GiveLable());
}
std::sort(ppoints.begin(), ppoints.end());
ppoints.erase(unique(ppoints.begin(), ppoints.end()), ppoints.end());
cnt = ppoints.size();
return cnt;
}
void HIERARCHICAL::DeleteClusters(int p_lable) {
for (int i = 0; i < h_arrpoints.size(); ++i) {
for (int j = 0; j < h_arrpoints.size(); ++j) {
if (h_arrpoints[i].GiveLable() == p_lable && h_arrpoints[j].GiveLable() == p_lable) {
h_matrix[i][j] = -1;
}
}
}
}
std::vector<Point> HIERARCHICAL::Get_Hierarc() {
return h_arrpoints;
}