溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2024-11-11 13:03:45 來源:億速云 閱讀:78 作者:小樊 欄目:編程語(yǔ)言

在C++中實(shí)現(xiàn)增量式聚類更新,可以使用一種稱為"增量更新"的方法

#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;
}

這個(gè)示例中,我們定義了一個(gè)IncrementalClustering類,它包含一個(gè)addPoint方法用于添加新點(diǎn),一個(gè)removePoint方法用于刪除點(diǎn)。當(dāng)添加或刪除點(diǎn)時(shí),類會(huì)自動(dòng)更新聚類中心。這個(gè)實(shí)現(xiàn)是一個(gè)簡(jiǎn)單的示例,實(shí)際應(yīng)用中可能需要根據(jù)具體需求進(jìn)行調(diào)整。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI