用C++實現(xiàn)DBSCAN聚類算法
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
這幾天由于工作需要,對DBSCAN聚類算法進行了C++的實現(xiàn)。時間復雜度O(n^2),主要花在算每個點領域內的點上。算法很簡單,現(xiàn)共享大家參考,也希望有更多交流。
數(shù)據(jù)點類型描述如下:
復制代碼 代碼如下:
#include <vector>
using namespace std;
const int DIME_NUM=2; //數(shù)據(jù)維度為2,全局常量
//數(shù)據(jù)點類型
class DataPoint
{
private:
unsigned long dpID; //數(shù)據(jù)點ID
double dimension[DIME_NUM]; //維度數(shù)據(jù)
long clusterId; //所屬聚類ID
bool isKey; //是否核心對象
bool visited; //是否已訪問
vector<unsigned long> arrivalPoints; //領域數(shù)據(jù)點id列表
public:
DataPoint(); //默認構造函數(shù)
DataPoint(unsigned long dpID,double* dimension , bool isKey); //構造函數(shù)
unsigned long GetDpId(); //GetDpId方法
void SetDpId(unsigned long dpID); //SetDpId方法
double* GetDimension(); //GetDimension方法
void SetDimension(double* dimension); //SetDimension方法
bool IsKey(); //GetIsKey方法
void SetKey(bool isKey); //SetKey方法
bool isVisited(); //GetIsVisited方法
void SetVisited(bool visited); //SetIsVisited方法
long GetClusterId(); //GetClusterId方法
void SetClusterId(long classId); //SetClusterId方法
vector<unsigned long>& GetArrivalPoints(); //GetArrivalPoints方法
};
這是實現(xiàn):
復制代碼 代碼如下:
#include "DataPoint.h"
//默認構造函數(shù)
DataPoint::DataPoint()
{
}
//構造函數(shù)
DataPoint::DataPoint(unsigned long dpID,double* dimension , bool isKey):isKey(isKey),dpID(dpID)
{
//傳遞每維的維度數(shù)據(jù)
for(int i=0; i<DIME_NUM;i++)
{
this->dimension[i]=dimension[i];
}
}
//設置維度數(shù)據(jù)
void DataPoint::SetDimension(double* dimension)
{
for(int i=0; i<DIME_NUM;i++)
{
this->dimension[i]=dimension[i];
}
}
//獲取維度數(shù)據(jù)
double* DataPoint::GetDimension()
{
return this->dimension;
}
//獲取是否為核心對象
bool DataPoint::IsKey()
{
return this->isKey;
}
//設置核心對象標志
void DataPoint::SetKey(bool isKey)
{
this->isKey = isKey;
}
//獲取DpId方法
unsigned long DataPoint::GetDpId()
{
return this->dpID;
}
//設置DpId方法
void DataPoint::SetDpId(unsigned long dpID)
{
this->dpID = dpID;
}
//GetIsVisited方法
bool DataPoint::isVisited()
{
return this->visited;
}
//SetIsVisited方法
void DataPoint::SetVisited( bool visited )
{
this->visited = visited;
}
//GetClusterId方法
long DataPoint::GetClusterId()
{
return this->clusterId;
}
//GetClusterId方法
void DataPoint::SetClusterId( long clusterId )
{
this->clusterId = clusterId;
}
//GetArrivalPoints方法
vector<unsigned long>& DataPoint::GetArrivalPoints()
{
return arrivalPoints;
}
DBSCAN算法類型描述:
復制代碼 代碼如下:
#include <iostream>
#include <cmath>
using namespace std;
//聚類分析類型
class ClusterAnalysis
{
private:
vector<DataPoint> dadaSets; //數(shù)據(jù)集合
unsigned int dimNum; //維度
double radius; //半徑
unsigned int dataNum; //數(shù)據(jù)數(shù)量
unsigned int minPTs; //鄰域最小數(shù)據(jù)個數(shù)
double GetDistance(DataPoint& dp1, DataPoint& dp2); //距離函數(shù)
void SetArrivalPoints(DataPoint& dp); //設置數(shù)據(jù)點的領域點列表
void KeyPointCluster( unsigned long i, unsigned long clusterId ); //對數(shù)據(jù)點領域內的點執(zhí)行聚類操作
public:
ClusterAnalysis(){} //默認構造函數(shù)
bool Init(char* fileName, double radius, int minPTs); //初始化操作
bool DoDBSCANRecursive(); //DBSCAN遞歸算法
bool WriteToFile(char* fileName); //將聚類結果寫入文件
};
聚類實現(xiàn):
復制代碼 代碼如下:
#include "ClusterAnalysis.h"
#include <fstream>
#include <iosfwd>
#include <math.h>
/*
函數(shù):聚類初始化操作
說明:將數(shù)據(jù)文件名,半徑,領域最小數(shù)據(jù)個數(shù)信息寫入聚類算法類,讀取文件,把數(shù)據(jù)信息讀入寫進算法類數(shù)據(jù)集合中
參數(shù):
char* fileName; //文件名
double radius; //半徑
int minPTs; //領域最小數(shù)據(jù)個數(shù)
返回值: true; */
bool ClusterAnalysis::Init(char* fileName, double radius, int minPTs)
{
this->radius = radius; //設置半徑
this->minPTs = minPTs; //設置領域最小數(shù)據(jù)個數(shù)
this->dimNum = DIME_NUM; //設置數(shù)據(jù)維度
ifstream ifs(fileName); //打開文件
if (! ifs.is_open()) //若文件已經(jīng)被打開,報錯誤信息
{
cout << "Error opening file"; //輸出錯誤信息
exit (-1); //程序退出
}
unsigned long i=0; //數(shù)據(jù)個數(shù)統(tǒng)計
while (! ifs.eof() ) //從文件中讀取POI信息,將POI信息寫入POI列表中
{
DataPoint tempDP; //臨時數(shù)據(jù)點對象
double tempDimData[DIME_NUM]; //臨時數(shù)據(jù)點維度信息
for(int j=0; j<DIME_NUM; j++) //讀文件,讀取每一維數(shù)據(jù)
{
ifs>>tempDimData[j];
}
tempDP.SetDimension(tempDimData); //將維度信息存入數(shù)據(jù)點對象內
//char date[20]="";
//char time[20]="";
////double type; //無用信息
//ifs >> date;
//ifs >> time; //無用信息讀入
tempDP.SetDpId(i); //將數(shù)據(jù)點對象ID設置為i
tempDP.SetVisited(false); //數(shù)據(jù)點對象isVisited設置為false
tempDP.SetClusterId(-1); //設置默認簇ID為-1
dadaSets.push_back(tempDP); //將對象壓入數(shù)據(jù)集合容器
i++; //計數(shù)+1
}
ifs.close(); //關閉文件流
dataNum =i; //設置數(shù)據(jù)對象集合大小為i
for(unsigned long i=0; i<dataNum;i++)
{
SetArrivalPoints(dadaSets[i]); //計算數(shù)據(jù)點領域內對象
}
return true; //返回
}
/*
函數(shù):將已經(jīng)過聚類算法處理的數(shù)據(jù)集合寫回文件
說明:將已經(jīng)過聚類結果寫回文件
參數(shù):
char* fileName; //要寫入的文件名
返回值: true */
bool ClusterAnalysis::WriteToFile(char* fileName )
{
ofstream of1(fileName); //初始化文件輸出流
for(unsigned long i=0; i<dataNum;i++) //對處理過的每個數(shù)據(jù)點寫入文件
{
for(int d=0; d<DIME_NUM ; d++) //將維度信息寫入文件
of1<<dadaSets[i].GetDimension()[d]<<'\t';
of1 << dadaSets[i].GetClusterId() <<endl; //將所屬簇ID寫入文件
}
of1.close(); //關閉輸出文件流
return true; //返回
}
/*
函數(shù):設置數(shù)據(jù)點的領域點列表
說明:設置數(shù)據(jù)點的領域點列表
參數(shù):
返回值: true; */
void ClusterAnalysis::SetArrivalPoints(DataPoint& dp)
{
for(unsigned long i=0; i<dataNum; i++) //對每個數(shù)據(jù)點執(zhí)行
{
double distance =GetDistance(dadaSets[i], dp); //獲取與特定點之間的距離
if(distance <= radius && i!=dp.GetDpId()) //若距離小于半徑,并且特定點的id與dp的id不同執(zhí)行
dp.GetArrivalPoints().push_back(i); //將特定點id壓力dp的領域列表中
}
if(dp.GetArrivalPoints().size() >= minPTs) //若dp領域內數(shù)據(jù)點數(shù)據(jù)量> minPTs執(zhí)行
{
dp.SetKey(true); //將dp核心對象標志位設為true
return; //返回
}
dp.SetKey(false); //若非核心對象,則將dp核心對象標志位設為false
}
/*
函數(shù):執(zhí)行聚類操作
說明:執(zhí)行聚類操作
參數(shù):
返回值: true; */
bool ClusterAnalysis::DoDBSCANRecursive()
{
unsigned long clusterId=0; //聚類id計數(shù),初始化為0
for(unsigned long i=0; i<dataNum;i++) //對每一個數(shù)據(jù)點執(zhí)行
{
DataPoint& dp=dadaSets[i]; //取到第i個數(shù)據(jù)點對象
if(!dp.isVisited() && dp.IsKey()) //若對象沒被訪問過,并且是核心對象執(zhí)行
{
dp.SetClusterId(clusterId); //設置該對象所屬簇ID為clusterId
dp.SetVisited(true); //設置該對象已被訪問過
KeyPointCluster(i,clusterId); //對該對象領域內點進行聚類
clusterId++; //clusterId自增1
}
//cout << "孤立點\T" << i << endl;
}
cout <<"共聚類" <<clusterId<<"個"<< endl; //算法完成后,輸出聚類個數(shù)
return true; //返回
}
/*
函數(shù):對數(shù)據(jù)點領域內的點執(zhí)行聚類操作
說明:采用遞歸的方法,深度優(yōu)先聚類數(shù)據(jù)
參數(shù):
unsigned long dpID; //數(shù)據(jù)點id
unsigned long clusterId; //數(shù)據(jù)點所屬簇id
返回值: void; */
void ClusterAnalysis::KeyPointCluster(unsigned long dpID, unsigned long clusterId )
{
DataPoint& srcDp = dadaSets[dpID]; //獲取數(shù)據(jù)點對象
if(!srcDp.IsKey()) return;
vector<unsigned long>& arrvalPoints = srcDp.GetArrivalPoints(); //獲取對象領域內點ID列表
for(unsigned long i=0; i<arrvalPoints.size(); i++)
{
DataPoint& desDp = dadaSets[arrvalPoints[i]]; //獲取領域內點數(shù)據(jù)點
if(!desDp.isVisited()) //若該對象沒有被訪問過執(zhí)行
{
//cout << "數(shù)據(jù)點\t"<< desDp.GetDpId()<<"聚類ID為\t" <<clusterId << endl;
desDp.SetClusterId(clusterId); //設置該對象所屬簇的ID為clusterId,即將該對象吸入簇中
desDp.SetVisited(true); //設置該對象已被訪問
if(desDp.IsKey()) //若該對象是核心對象
{
KeyPointCluster(desDp.GetDpId(),clusterId); //遞歸地對該領域點數(shù)據(jù)的領域內的點執(zhí)行聚類操作,采用深度優(yōu)先方法
}
}
}
}
//兩數(shù)據(jù)點之間距離
/*
函數(shù):獲取兩數(shù)據(jù)點之間距離
說明:獲取兩數(shù)據(jù)點之間的歐式距離
參數(shù):
DataPoint& dp1; //數(shù)據(jù)點1
DataPoint& dp2; //數(shù)據(jù)點2
返回值: double; //兩點之間的距離 */
double ClusterAnalysis::GetDistance(DataPoint& dp1, DataPoint& dp2)
{
double distance =0; //初始化距離為0
for(int i=0; i<DIME_NUM;i++) //對數(shù)據(jù)每一維數(shù)據(jù)執(zhí)行
{
distance += pow(dp1.GetDimension()[i] - dp2.GetDimension()[i],2); //距離+每一維差的平方
}
return pow(distance,0.5); //開方并返回距離
}
算法調用就簡單了:
復制代碼 代碼如下:
#include "ClusterAnalysis.h"
#include <cstdio>
using namespace std;
int main()
{
ClusterAnalysis myClusterAnalysis; //聚類算法對象聲明
myClusterAnalysis.Init("D:\\1108\\XY.txt",500,9); //算法初始化操作,指定半徑為15,領域內最小數(shù)據(jù)點個數(shù)為3,(在程序中已指定數(shù)據(jù)維度為2)
myClusterAnalysis.DoDBSCANRecursive(); //執(zhí)行聚類算法
myClusterAnalysis.WriteToFile("D:\\1108\\XYResult.txt");//寫執(zhí)行后的結果寫入文件
system("pause"); //顯示結果
return 0; //返回
}
您可能感興趣的文章
- 04-02c語言函數(shù)調用后清空內存 c語言調用函數(shù)刪除字符
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求階乘
- 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應用分析詳解
- 01-10深入理解C++中常見的關鍵字含義


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery