分享

基于C 实现DBSCAN聚类算法

 飘渺o40uv5vs24 2023-02-13 发布于陕西

DBSCAN聚类算法进行了C++的实现,时间复杂度为O(n^2)。

1、数据点类型描述如下(DataPoint.h)

  1. #include <vector>
  2. using namespace std;
  3. const int DIME_NUM=2; //数据维度为2,全局常量
  4. //数据点类型
  5. class DataPoint
  6. {
  7. private:
  8. unsigned long dpID; //数据点ID
  9. double dimension[DIME_NUM]; //维度数据
  10. long clusterId; //所属聚类ID
  11. bool isKey; //是否核心对象
  12. bool visited; //是否已访问
  13. vector<unsigned long> arrivalPoints; //领域数据点id列表
  14. public:
  15. DataPoint(); //默认构造函数
  16. DataPoint(unsigned long dpID,double* dimension , bool isKey); //构造函数
  17. unsigned long GetDpId(); //GetDpId方法
  18. void SetDpId(unsigned long dpID); //SetDpId方法
  19. double* GetDimension(); //GetDimension方法
  20. void SetDimension(double* dimension); //SetDimension方法
  21. bool IsKey(); //GetIsKey方法
  22. void SetKey(bool isKey); //SetKey方法
  23. bool isVisited(); //GetIsVisited方法
  24. void SetVisited(bool visited); //SetIsVisited方法
  25. long GetClusterId(); //GetClusterId方法
  26. void SetClusterId(long classId); //SetClusterId方法
  27. vector<unsigned long>& GetArrivalPoints(); //GetArrivalPoints方法
  28. };

2、对应实现(DataPoint.cpp)

  1. #include 'DataPoint.h'
  2. //默认构造函数
  3. DataPoint::DataPoint()
  4. {
  5. }
  6. //构造函数
  7. DataPoint::DataPoint(unsigned long dpID,double* dimension , bool isKey):isKey(isKey),dpID(dpID)
  8. {
  9. //传递每维的维度数据
  10. for(int i=0; i<DIME_NUM;i++)
  11. {
  12. this->dimension[i]=dimension[i];
  13. }
  14. }
  15. //设置维度数据
  16. void DataPoint::SetDimension(double* dimension)
  17. {
  18. for(int i=0; i<DIME_NUM;i++)
  19. {
  20. this->dimension[i]=dimension[i];
  21. }
  22. }
  23. //获取维度数据
  24. double* DataPoint::GetDimension()
  25. {
  26. return this->dimension;
  27. }
  28. //获取是否为核心对象
  29. bool DataPoint::IsKey()
  30. {
  31. return this->isKey;
  32. }
  33. //设置核心对象标志
  34. void DataPoint::SetKey(bool isKey)
  35. {
  36. this->isKey = isKey;
  37. }
  38. //获取DpId方法
  39. unsigned long DataPoint::GetDpId()
  40. {
  41. return this->dpID;
  42. }
  43. //设置DpId方法
  44. void DataPoint::SetDpId(unsigned long dpID)
  45. {
  46. this->dpID = dpID;
  47. }
  48. //GetIsVisited方法
  49. bool DataPoint::isVisited()
  50. {
  51. return this->visited;
  52. }
  53. //SetIsVisited方法
  54. void DataPoint::SetVisited( bool visited )
  55. {
  56. this->visited = visited;
  57. }
  58. //GetClusterId方法
  59. long DataPoint::GetClusterId()
  60. {
  61. return this->clusterId;
  62. }
  63. //SetClusterId方法
  64. void DataPoint::SetClusterId( long clusterId )
  65. {
  66. this->clusterId = clusterId;
  67. }
  68. //GetArrivalPoints方法
  69. vector<unsigned long>& DataPoint::GetArrivalPoints()
  70. {
  71. return arrivalPoints;
  72. }

3、DBSCAN算法类型描述(ClusterAnalysis.h)

  1. #include <iostream>
  2. #include <cmath>
  3. #include 'DataPoint.h'
  4. #include<QVector>
  5. using namespace std;
  6. //聚类分析类型
  7. class ClusterAnalysis
  8. {
  9. private:
  10. vector<DataPoint> dadaSets; //数据集合
  11. unsigned int dimNum; //维度
  12. double radius; //半径
  13. unsigned int dataNum; //数据数量
  14. unsigned int minPTs; //邻域最小数据个数
  15. double GetDistance(DataPoint& dp1, DataPoint& dp2); //距离函数
  16. void SetArrivalPoints(DataPoint& dp); //设置数据点的领域点列表
  17. void KeyPointCluster( unsigned long i, unsigned long clusterId ); //对数据点领域内的点执行聚类操作
  18. public:
  19. ClusterAnalysis(){} //默认构造函数
  20. bool Init(QVector<QVector<QString>> Data, double radius, int minPTs); //初始化操作
  21. unsigned long DoDBSCANRecursive(); //DBSCAN递归算法
  22. bool WriteToFile(char* fileName); //将聚类结果写入文件
  23. };

4、算法实现(ClusterAnalysis.cpp)

  1. #include 'ClusterAnalysis.h'
  2. #include <fstream>
  3. #include <iosfwd>
  4. #include <math.h>
  5. /*
  6. 函数:聚类初始化操作
  7. 说明:将数据文件名,半径,领域最小数据个数信息写入聚类算法类,读取文件,把数据信息读入写进算法类数据集合中
  8. 参数:
  9. QVector<QVector<QString>> Data; //数据
  10. double radius; //半径
  11. int minPTs; //领域最小数据个数
  12. 返回值: true; */
  13. bool ClusterAnalysis::Init(char* fileName, double radius, int minPTs)
  14. {
  15. this->radius = radius; //设置半径
  16. this->minPTs = minPTs; //设置领域最小数据个数
  17. this->dimNum = DIME_NUM; //设置数据维度
  18. ifstream ifs(fileName); //打开文件
  19. if (! ifs.is_open()) //若文件已经被打开,报错误信息
  20. {
  21. cout << 'Error opening file'; //输出错误信息
  22. exit (-1); //程序退出
  23. }
  24. unsigned long i=0; //数据个数统计
  25. while (! ifs.eof() ) //从文件中读取POI信息,将POI信息写入POI列表中
  26. {
  27. DataPoint tempDP; //临时数据点对象
  28. double tempDimData[DIME_NUM]; //临时数据点维度信息
  29. for(int j=0; j<DIME_NUM; j++) //读文件,读取每一维数据
  30. {
  31. ifs>>tempDimData[j];
  32. }
  33. tempDP.SetDimension(tempDimData); //将维度信息存入数据点对象内
  34. //char date[20]='';
  35. //char time[20]='';
  36. double type; //无用信息
  37. //ifs >> date;
  38. //ifs >> time; //无用信息读入
  39. tempDP.SetDpId(i); //将数据点对象ID设置为i
  40. tempDP.SetVisited(false); //数据点对象isVisited设置为false
  41. tempDP.SetClusterId(-1); //设置默认簇ID为-1
  42. dadaSets.push_back(tempDP); //将对象压入数据集合容器
  43. i++; //计数+1
  44. }
  45. ifs.close(); //关闭文件流
  46. dataNum =i; //设置数据对象集合大小为i
  47. for(unsigned long i=0; i<dataNum;i++)
  48. {
  49. SetArrivalPoints(dadaSets[i]); //计算数据点领域内对象
  50. }
  51. return true; //返回
  52. }
  53. /*
  54. 函数:将已经过聚类算法处理的数据集合写回文件
  55. 说明:将已经过聚类结果写回文件
  56. 参数:
  57. char* fileName; //要写入的文件名
  58. 返回值: true */
  59. bool ClusterAnalysis::WriteToFile(char* fileName )
  60. {
  61. ofstream of1(fileName); //初始化文件输出流
  62. for(unsigned long i=0; i<dataNum;i++) //对处理过的每个数据点写入文件
  63. {
  64. for(int d=0; d<DIME_NUM ; d++) //将维度信息写入文件
  65. of1<<dadaSets[i].GetDimension()[d]<<'\t';
  66. of1 << dadaSets[i].GetClusterId() <<endl; //将所属簇ID写入文件
  67. }
  68. of1.close(); //关闭输出文件流
  69. return true; //返回
  70. }
  71. /*
  72. 函数:设置数据点的领域点列表
  73. 说明:设置数据点的领域点列表
  74. 参数:
  75. 返回值: true; */
  76. void ClusterAnalysis::SetArrivalPoints(DataPoint& dp)
  77. {
  78. for(unsigned long i=0; i<dataNum; i++) //对每个数据点执行
  79. {
  80. double distance =GetDistance(dadaSets[i], dp); //获取与特定点之间的距离
  81. if(distance <= radius && i!=dp.GetDpId()) //若距离小于半径,并且特定点的id与dp的id不同执行
  82. dp.GetArrivalPoints().push_back(i); //将特定点id压力dp的领域列表中
  83. }
  84. if(dp.GetArrivalPoints().size() >= minPTs) //若dp领域内数据点数据量> minPTs执行
  85. {
  86. dp.SetKey(true); //将dp核心对象标志位设为true
  87. return; //返回
  88. }
  89. dp.SetKey(false); //若非核心对象,则将dp核心对象标志位设为false
  90. }
  91. /*
  92. 函数:执行聚类操作
  93. 说明:执行聚类操作
  94. 参数:
  95. 返回值: true; */
  96. unsigned long ClusterAnalysis::DoDBSCANRecursive()
  97. {
  98. unsigned long clusterId=0; //聚类id计数,初始化为0
  99. for(unsigned long i=0; i<dataNum;i++) //对每一个数据点执行
  100. {
  101. DataPoint& dp=dadaSets[i]; //取到第i个数据点对象
  102. if(!dp.isVisited() && dp.IsKey()) //若对象没被访问过,并且是核心对象执行
  103. {
  104. dp.SetClusterId(clusterId); //设置该对象所属簇ID为clusterId
  105. dp.SetVisited(true); //设置该对象已被访问过
  106. KeyPointCluster(i,clusterId); //对该对象领域内点进行聚类
  107. clusterId++; //clusterId自增1
  108. }
  109. //cout << '孤立点\T' << i << endl;
  110. }
  111. // cout <<'共聚类' <<clusterId<<'个'<< endl; //算法完成后,输出聚类个数
  112. return clusterId; //返回
  113. }
  114. /*
  115. 函数:对数据点领域内的点执行聚类操作
  116. 说明:采用递归的方法,深度优先聚类数据
  117. 参数:
  118. unsigned long dpID; //数据点id
  119. unsigned long clusterId; //数据点所属簇id
  120. 返回值: void; */
  121. void ClusterAnalysis::KeyPointCluster(unsigned long dpID, unsigned long clusterId )
  122. {
  123. DataPoint& srcDp = dadaSets[dpID]; //获取数据点对象
  124. if(!srcDp.IsKey()) return;
  125. vector<unsigned long>& arrvalPoints = srcDp.GetArrivalPoints(); //获取对象领域内点ID列表
  126. for(unsigned long i=0; i<arrvalPoints.size(); i++)
  127. {
  128. DataPoint& desDp = dadaSets[arrvalPoints[i]]; //获取领域内点数据点
  129. if(!desDp.isVisited()) //若该对象没有被访问过执行
  130. {
  131. //cout << '数据点\t'<< desDp.GetDpId()<<'聚类ID为\t' <<clusterId << endl;
  132. desDp.SetClusterId(clusterId); //设置该对象所属簇的ID为clusterId,即将该对象吸入簇中
  133. desDp.SetVisited(true); //设置该对象已被访问
  134. if(desDp.IsKey()) //若该对象是核心对象
  135. {
  136. KeyPointCluster(desDp.GetDpId(),clusterId); //递归地对该领域点数据的领域内的点执行聚类操作,采用深度优先方法
  137. }
  138. }
  139. }
  140. }
  141. //两数据点之间距离
  142. /*
  143. 函数:获取两数据点之间距离
  144. 说明:获取两数据点之间的欧式距离
  145. 参数:
  146. DataPoint& dp1; //数据点1
  147. DataPoint& dp2; //数据点2
  148. 返回值: double; //两点之间的距离 */
  149. double ClusterAnalysis::GetDistance(DataPoint& dp1, DataPoint& dp2)
  150. {
  151. double distance =0; //初始化距离为0
  152. for(int i=0; i<DIME_NUM;i++) //对数据每一维数据执行
  153. {
  154. distance += pow(dp1.GetDimension()[i] - dp2.GetDimension()[i],2); //距离+每一维差的平方
  155. }
  156. return pow(distance,0.5); //开方并返回距离
  157. }

5、算法调用

input.txt文件内容 根据myClusterAnalysis.Init方法自行构建。

  1. #include 'ClusterAnalysis.h'
  2. #include <cstdio>
  3. using namespace std;
  4. int main()
  5. {
  6. ClusterAnalysis myClusterAnalysis; //聚类算法对象声明
  7. myClusterAnalysis.Init('input.txt',100,5); //算法初始化操作,指定半径为100,领域内最小数据点个数为5
  8. myClusterAnalysis.DoDBSCANRecursive(); //执行聚类算法
  9. myClusterAnalysis.WriteToFile('out.txt');//写执行后的结果写入文件
  10. system('pause'); //显示结果
  11. return 0; //返回
  12. }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多