DBSCAN algorithm

Input: data set 𝒜 ⊂ (ℝ^n), MinPts, ϵ-density ϵ(𝒜);The algorithm can use data sets from DataDiscs.txt, DataGenCirc.txt,DataEllipseGarland.txt, Coral.txt, constructed using modules DataCircle[],DataOval[], DataCAoval[], DataEllipseGarland[];
Output: partition Π of the set 𝒜 with marked border points SSE and outliers SSN;
Submodules: Neps[], EPSILON[], CorePoint[], DBSCAN1[];

DBSCAN[A_,MinPts_,eps_,Ind_:0]:=Module[{kk=0,It0=.1Length[A],TH=.1 Length[A],time=0,B,cp,PPP,PP,SPP,SE,SSN},
PPP=SSN=SSE=SPP={};
B=A; TH=.01 Length[A];
While[Length[B]>TH MinPts,kk=kk+1;cp=CorePoint[B,eps,It0];If[Ind≠0,Print["cp: ",cp]];  
dbran=DBSCAN1[B,cp[[1]],MinPts,eps]//Timing;
PP=dbran[[2,1]];Border=dbran[[2,2]]; 
SPP=Append[SPP,PP];
Noise=dbran[[2,3]];  
time=time+dbran[[1]]; 
B=Complement[B,Union[PP,Border,Noise]]; 
If[Ind≠0,Print[kk,". izabrana točka cp: ",cp[[1]],"; |Cp|=",cp[[2]],"; |PP|=",Length[PP],"; |Border|= ",Length[Border],"; |B|=",Length[B],"; Time(DBRAN)=",time]];
 (* Izabrani cluster + Ostatak skupa - može se uključiti 
slizbor=ListPlot[B,PlotStyle->{Blue,PointSize[.005]},AspectRatio->Automatic,GridLines->Automatic,PlotRange->AG,PlotLabel->"Ostatak"];
slPP=ListPlot[PP,PlotStyle->{Blue,PointSize[.001]},AspectRatio->Automatic,GridLines->Automatic,PlotRange->AG,PlotLabel->"Izabrani klaster"];
Print[GraphicsGrid[{{slPP,slizbor}},ImageSize->Medium]];
*)
If[Ind≠0,Print[Show[ListPlot[Flatten[SPP,1],PlotStyle->{Gray,PointSize[.001]}],ListPlot[B,PlotStyle->{Blue,PointSize[.005]}],AspectRatio->Automatic,GridLines->Automatic,PlotRange->AG ,Frame->True,AxesOrigin->{AG[[1,1]],AG[[2,1]]},ImageSize->Small]]];
PPP=AppendTo[PPP,PP]; SSE=Append[SSE,Border];SSN=Append[SSN,Noise];
];
SSN=Complement[A,Flatten[PPP,1]];
Print["|PPP| = ",Sum[Length[PPP[[j]]],{j,Length[PPP]}]," od toga |SSE| = ",Sum[Length[SSE[[j]]],{j,Length[SSE]}],";  |SSN| = ",Length[SSN], ";  TOTAL[A] = ",Length[A],"; Total[time]=",time];
slPPP=ListPlot[Flatten[PPP,1]];
slSN=ListPlot[SSN,PlotStyle->{Red,PointSize[.025]}];
slSE=ListPlot[Flatten[SSE,1],PlotStyle->{Brown,PointSize[.02]}];
sl1=ListPlot[PPP, AspectRatio->Automatic,Frame->True,GridLines->Automatic,PlotRange->AG];
sl2=Show[sl1,slSN,slSE,AspectRatio->Automatic,Frame->True,GridLines->Automatic,PlotRange->AG,ImageSize->Small];
Print[sl2];
Print[GraphicsGrid[{{sl1,sl2}},ImageSize->Medium]];
{PPP,SSE,SSN}
]