C++聚类算法中的增量式聚类更新

发布时间:2024-11-11 13:03:45 作者:小樊
来源:亿速云 阅读:91

在C++中实现增量式聚类更新,可以使用一种称为"增量更新"的方法

#include <iostream>
#include <vector>
#include <cmath>
#include <queue>

class Point {
public:
    double x, y;

    Point(double x, double y) : x(x), y(y) {}

    double distance(const Point& other) const {
        return std::sqrt(std::pow(x - other.x, 2) + std::pow(y - other.y, 2));
    }
};

class Cluster {
public:
    Point centroid;
    std::vector<Point> points;

    Cluster(const Point& centroid) : centroid(centroid) {}
};

class IncrementalClustering {
public:
    std::vector<Cluster> clusters;

    void addPoint(const Point& point) {
        bool merged = false;
        for (auto& cluster : clusters) {
            if (isClose(point, cluster.centroid)) {
                cluster.points.push_back(point);
                updateCentroid(cluster);
                merged = true;
                break;
            }
        }

        if (!merged) {
            clusters.push_back(Cluster(point));
        }
    }

    void removePoint(const Point& point) {
        for (auto it = clusters.begin(); it != clusters.end(); ++it) {
            if (std::find(it->points.begin(), it->points.end(), point) != it->points.end()) {
                it->points.erase(std::remove(it->points.begin(), it->points.end(), point), it->points.end());

                if (it->points.empty()) {
                    clusters.erase(it);
                } else {
                    updateCentroid(*it);
                }
                break;
            }
        }
    }

private:
    double distance(const Point& p1, const Point& p2) const {
        return p1.distance(p2);
    }

    bool isClose(const Point& p1, const Point& p2) const {
        return distance(p1, p2) < 0.5;
    }

    void updateCentroid(Cluster& cluster) {
        double sumX = 0, sumY = 0;
        for (const auto& point : cluster.points) {
            sumX += point.x;
            sumY += point.y;
        }

        cluster.centroid = Point(sumX / cluster.points.size(), sumY / cluster.points.size());
    }
};

int main() {
    IncrementalClustering clustering;

    clustering.addPoint(Point(1, 1));
    clustering.addPoint(Point(2, 2));
    clustering.addPoint(Point(3, 3));
    clustering.addPoint(Point(4, 4));

    for (const auto& cluster : clustering.clusters) {
        std::cout << "Centroid: (" << cluster.centroid.x << ", " << cluster.centroid.y << ")\n";
        for (const auto& point : cluster.points) {
            std::cout << "Point: (" << point.x << ", " << point.y << ")\n";
        }
    }

    clustering.removePoint(Point(2, 2));

    for (const auto& cluster : clustering.clusters) {
        std::cout << "Centroid: (" << cluster.centroid.x << ", " << cluster.centroid.y << ")\n";
        for (const auto& point : cluster.points) {
            std::cout << "Point: (" << point.x << ", " << point.y << ")\n";
        }
    }

    return 0;
}

这个示例中,我们定义了一个IncrementalClustering类,它包含一个addPoint方法用于添加新点,一个removePoint方法用于删除点。当添加或删除点时,类会自动更新聚类中心。这个实现是一个简单的示例,实际应用中可能需要根据具体需求进行调整。

推荐阅读:
  1. 高性能服务器开发基础系列 (七)——开源一款即时通讯软件的源码
  2. 服务器端编程心得(六)—— 关于网络编程的一些实用技巧和细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:C++聚类算法在电力负荷预测中的作用

下一篇:C++聚类算法在文本摘要生成中的应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》