Professional Documents
Culture Documents
#include <fstream>
#include <vector>
#include <cmath>
#include <limits>
// Hàm gán nhóm cho mỗi điểm dữ liệu dựa trên các trung tâm
void assignToClusters(const std::vector<Point>& data, const
std::vector<Point>& centroids, std::vector<int>& assignments) {
for (size_t i = 0; i < data.size(); ++i) {
double minDistance = std::numeric_limits<double>::max();
int clusterIndex = -1;
assignments[i] = clusterIndex;
}
}
// Hàm kiểm tra xem liệu trung tâm đã thay đổi đủ chưa
bool hasConverged(const std::vector<Point>& oldCentroids, const
std::vector<Point>& newCentroids, double tolerance) {
for (size_t i = 0; i < oldCentroids.size(); ++i) {
if (euclideanDistance(oldCentroids[i], newCentroids[i]) >
tolerance) {
return false;
}
}
return true;
}
// Áp dụng thuật toán K-means và lấy thông tin về phân nhóm của
mỗi đỉnh
std::vector<int> assignments(data.size(), -1);
kMeans(data, n, centroids, assignments);
// In kết quả
std::cout << "Tâm cụm sau thuật toán K-means:\n";
for (size_t i = 0; i < centroids.size(); ++i) {
std::cout << "Tâm cụm " << i + 1 << ": (" << centroids[i].x << ", "
<< centroids[i].y << ")\n";
}
return 0;
}