当前位置: 首页 > article >正文

DIANA算法c++实现

 第一步对具有最大直径的簇中每个点计算平均相异度找出最大的点放入splinter group,其余放在放入splinter group

第二步 在old party里找出到splinter group中点的最近距离 <= 到old party中点的最近距离的点,并将该点加入splinter group

重复第二步的工作,直到没有新的old party中的点分配给splinter group且满足分裂的簇数k,如果没有到达终止条件,则从分裂好的簇中选一个最大的簇按刚才的分裂方法继续分裂

#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

struct Point
{
    double x;
    double y;
};


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

vector<Point> findmaxC(vector<vector<Point>>& clusters)
{
    double max = 0;
    int index = 0;
    for (int i =0;i< clusters.size(); i++)
    {
        double max1 = 0;
        for (int j = 0; j < clusters[i].size(); j++)
        {
            for (int k = j + 1; k < clusters[i].size(); k++)
            {
                double dis = distance(clusters[i][j], clusters[i][k]);
               
                if (max < dis)
                {
                    max1 = dis;
                }
                
            }
        }
        if (max1 > max)
        {
            max1 = max;
            index = i;
        }
    }
    return clusters[index];
}
int findMaxD(vector<Point> cluster)
{
    double max = 0;
    int maxIndex = 0;
    for (int i = 0; i < cluster.size(); i++)
    {
        double avg = 0;
        double dis = 0;
        for (int j = 0; j < cluster.size(); j++)
        {
            if (i != j)
            {
                dis += distance(cluster[i], cluster[j]);
            }
        }
        avg = dis / (cluster.size() - 1);
        if (avg > max)
        {
            max = avg;
            maxIndex = i;
        }
    }
    return maxIndex;
}

void spilt(vector<Point>& cluster, int k,int maxIndex)
{
    int flag = 0;
    vector<Point> splinter;
    vector<Point> oldParty;
    vector<vector<Point>> sum;
  
    splinter.push_back(cluster[maxIndex]);// splinter.push_back[maxIndex]   +1?
    for (int i = 0; i < cluster.size(); i++)
    {
        if (i != maxIndex)
        {
            oldParty.push_back(cluster[i]); //oldParty.push_back i
        }
    }

    sum.push_back(splinter);
    sum.push_back(oldParty);

    while (sum.size() <= k && flag == 0)
    {
        double min = numeric_limits<double>::max();
        int in = 0;
        for (int i = 0; i < sum[1].size(); i++)
        {
            for (int j = 0; j < sum[1].size(); j++)
            {
                if (j != i)
                {
                    double d = distance(sum[1][i], sum[1][j]);
                    if (d < min)
                    {
                        min = d;
                        in = i;                 //min
                    }
                }
            }
             cout << min << endl;
             double min1 = numeric_limits<double>::max();
             int in1 = 0;
             for (int m = 0;m < sum[0].size(); m++)
             {
                 double ds = distance(sum[0][m], sum[1][in]);
                 if (ds < min1)
                 {
                        min1 = ds;
                            //in1 = i;            
                  }
              }
             cout << min1<<endl;
              if (min1 <= min)
              {
                  sum[0].push_back(sum[1][in]);              //要sum[0]
                  sum[1].erase(sum[1].begin() + in);
               }
        }
        flag = 1;
        
    }
    while (sum.size() < k)
    {
        vector<Point> temp=findmaxC(sum);
        int i=findMaxD(temp);
        vector<Point> re;
        re.push_back(temp[i]);
        sum.push_back(re);
        //未完
    }
    for (int i = 0; i < sum.size(); i++)
    {
        cout << "{";
        for (int j = 0; j < sum[i].size(); j++)
        {
            cout << "(" << sum[i][j].x << ", " << sum[i][j].y << ")" <<",";
        }
        cout<<"}" << endl;
   }

}

vector<Point> ReadData(string filename)
{
    vector<Point> data;
    ifstream file(filename);
    if (file.is_open())
    {
        string line;
        while (getline(file, line))
        {
            istringstream iss(line);
            double x, y;
            string token;
            Point point;
            if (getline(iss, token, ',') && istringstream(token) >> point.x &&
                getline(iss, token, ',') && istringstream(token) >> point.y) {
                data.push_back(point);
            }
        }
    }
    else
    {
        cout << "open fail";
    }
    file.close();
    return data;
}

int main()
{
    vector<Point> dataset = ReadData("data1.txt");
    int index=findMaxD(dataset);
    int k;
    cout << "终止条件为几个簇" << endl;
    cin >> k;
    spilt(dataset, k, index);

}
//{ {1.0, 1.0}, {1.0, 2.0}, {2.0, 1.0}, {2.0, 2.0}, {3.0, 4.0}, {3.0, 5.0}, {4.0, .0}, {4.0, 5.0} };

 data1:

data2:

 


http://www.kler.cn/news/108937.html

相关文章:

  • Annotorious入门教程:图片注释工具
  • React 生成传递给无障碍属性的唯一 ID
  • 【Git企业开发】第一节.Git 初识
  • 队列(Queue)概念+通过单、双链表来模拟队列+环形队列+OJ面试题(用队列实现栈、用栈实现队列、设计环形队列)
  • [Python]unittest-单元测试
  • Pytorch detach()方法
  • Transformers实战(二)快速入门文本相似度、检索式对话机器人
  • ChatGPT扩展系列之ChatExcel
  • Python连接数据库报错处理
  • 数组OJ题汇总(一)
  • PHP下载文件
  • Linux shell编程学习笔记16:bash中的关联数组
  • 高级深入--day42
  • 缓解大模型幻觉问题的解决方案
  • Python算法例2 判断平方数
  • python基础语法(十一)
  • 【wespeaker】模型ECAPA_TDNN介绍
  • 【MATLAB源码-第58期】基于蛇优化算法(SO)和粒子群优化算法(PSO)的栅格地图路径规划最短路径和适应度曲线对比。
  • 【Flutter】Flutter 中的图片管理 图片优化的最佳实践
  • pandas 统计函数
  • UE5使用Dash插件实现程序化地形场景制作
  • 「实验记录」CS144 Lab0 networking warmup
  • docker 部署prometheus和grafana
  • Python之函数-函数概念
  • HTTPS协议:保障网络安全的加密通信协议
  • 一天掌握python爬虫【基础篇】 涵盖 requests、beautifulsoup、selenium
  • 一个基于Excel模板快速生成Excel文档的小工具
  • Python爬虫基础之Requests详解
  • USACO12OPEN Balanced Cow Subsets G(meet in the middle)
  • TensorRT量化实战课YOLOv7量化:pytorch_quantization介绍