/*------------------------------------------- DBSCAN Algo implementation -----------------------------------------/
*
*Currently supports only int entries.
*Inputs are used from file "newfile1" and output is sent to "output" file. the input is in the form of columns separated by a constant number of white characters like in an excel sheet.
*
*Developes clusters based on density i.e all elements within a distance Eps = K_Distance are segregated into a single cluster.
*Initially all points are considered to be belonging to the distinct clusters. Then elements within the Eps distance are merged into the same cluster, for every such action all elements given clusters numbers uptil now are tested to see if they are to be merged into this cluster only.
* The output is read out by the MATLAB file and plotted out as distinct clusters.
*Eps calculation can also be done by the similar code, EpsCalc.java
*/
import java.io.*;
import java.util.*;
import java.lang.Number.*;
/**
*
* @author Saurabh
*/
public class Main {
final int K_Factor= 20;
final int K_Distance = 15; // K_Distance will be calculate in the another code
final int MAXROW = 178; // Represents max row count
int Clust_no = 1; // Represents cluster number
int Border_Clust = 500;
int DistVector[][] = new int[200][200];
Queue myqueue = new LinkedList(); // To store vertices to be processed next
Integer X, Y; // Represent X and Y coordinates
int i = 0;
int Elements[][]= new int[MAXROW][5];
int Dist[]= new int[MAXROW];
/* File_Init function reads the data in the file into a 5 elements array. Column 1 and 2 represent the X and Y coordinate fo the element, 3 represents Distance to its nearest neighbour and 4 represents the Cluster number, 5 is left for future use*/
void File_Init()
{
try
{
FileReader reader = new FileReader("newfile1"); // File with all the input
Scanner in = new Scanner(reader);
// PrintWriter out = new PrintWriter (array[5]+"_dis.txt");
String line = in.nextLine();
while (in.hasNext())
{
System.out.println("Going to read");
line = in.nextLine();
System.out.println(line);
String [] Line_Token = line.split(" "); // Token delimiter is considered to be " "
System.out.println(Line_Token[1]);
X = Integer.parseInt(Line_Token[1]); // Column number to be used
Y = Integer.parseInt(Line_Token[8]); // Column number to be used
// System.out.println(X); // For testing purposes
Elements[i][0]=X;
Elements[i][1]=Y;
i++;
}
}catch(Exception e)
{
System.out.println(e);
}
}
/* Func_Dist function finds the minimum eauclidean distance between 2 points in 2 -d space*/
int Func_Dist(int X1, int Y1, int X2, int Y2)
{
int distance = (int)(Math.sqrt(((X2-X1)*(X2-X1)) + ((Y2-Y1)*(Y2-Y1))));
return distance;
}
/* Nearestk function finds the distance of the Kth closest neighbour for every elements and populates this in the 3rd column of the Elements array.
*This code can be used for calculating the K_Distance factor value.
*/
/* int NearestK(int X1, int Y1, int K)
{
int Need =0; // Represents the need to process myqueue at the end of this for or not
for(int k=0; k<200; k++)
{
Dist[k]= Func_Dist(X1, Y1, Elements[k][0], Elements[k][1]);
}
Arrays.sort(Dist);
return Dist[K_Factor];
}
*/
/*
*fincNum function helps in detemrining which all elements uptil now have been given the cluster number Cnumber. returns its index number
*/
int findNum(int Cnumber, int init)
{
int count = init;
while((count >0)&&(Elements[count][3]!=Cnumber))
count--;
return count;
}
/* Calc_K_dist function outputs the file and makes the clusters
*/
void Calc_K_dist()
{
try{
PrintWriter out = new PrintWriter ("c:/MATLAB7/work/outfile.txt");
for(int i=0; i<MAXROW; i++)
Elements[i][3]=i; // Initialise cluster field of all elements to zero
Elements[0][3]=Clust_no;
for(int i=0; i<MAXROW; i++) // Makes a matrix DistVector and fills distance between all elements into this
for(int j=0; j<MAXROW; j++)
{
DistVector[i][j] = Func_Dist(Elements[i][0], Elements[i][1], Elements[j][0], Elements[j][1]);
}
int tempState=0;
int temp=tempState;
for(int i=0; i<MAXROW; i++)
{
//Elements[i][2]= NearestK(Elements[i][0],Elements[i][1], Elements[i][3]); //Find and store K nearest distance
for(int j =i; j>0; j--)
if((DistVector[i][j] < K_Distance)&&(Elements[j][3]!=Elements[i][3]))
{
if(Elements[j][3]==0)
Elements[j][3] = Elements[i][3]; // If element has not been given a cluster number uptil now, give it the same cluster number as i
else {
int tempvar=i, tempvar2, tempclust;
tempclust = Elements[j][3]; // else give all the elements in the cluster of i the new cluster number
while(tempvar >= 0)
{
tempvar2 = findNum(tempclust, tempvar);
if(Elements[tempvar2][3] == tempclust)
{
//tempvar = (tempvar2)-1;
Elements[tempvar2][3]= Elements[i][3];
}
tempvar=(tempvar2)-1;
}
tempState = Elements[j][3];
temp = j;
//Elements[j][3] = Elements[i][3];
}
}
// Give element J same cluster number as I
else if((DistVector[i][j] == K_Distance)&&(Elements[j][3]==0)) // Assign J as I's border element
Elements[j][3] = Border_Clust;
else if(Elements[j][3
dbscan.rar_DBSCAN_DBScan java
版权申诉
41 浏览量
2022-09-24
17:10:13
上传
评论
收藏 3KB RAR 举报
周楷雯
- 粉丝: 80
- 资源: 1万+
最新资源
- 简单的Linux C代码,实现链表功能
- 汇编语言探索之旅:从基础到实践,解锁底层奥秘-markdown材料.zip
- Fortran语言:经典与现代交织的编程之旅-markdown材料.zip
- 基于python编写的appium自动化框架,采用PO模式,并集成了日志以及测试报告通过邮件发送的功能
- csdn.apk
- spleeter模型下载
- 微信小程序 - 图书管理系统源码.zip
- 微信小程序 - 图片自适应 ,富文本解析源码.zip
- 微信小程序 - 同乐居商城:购物车合算源码
- 1、根据输入的三条边值判断能组成何种三角形,并设计测试数据进行判定覆盖测试 三条边为变量a、b、c,范围为1≤边值≤10,不在范
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈