K - Means algoritam   for the data set 𝒜 ⊂ ℝ , or 𝒜 ⊂ (ℝ^2)  started with an initial partition

Input: initial partition (Π^((0))), parameter p = 1 or 2. 
One can choose the p-distance like function for p = 1 or 2. Options
	for printout and graphical presentation of intermediate
	results are provided (Ind=1), as well as options for printing the
	results as fractions or as decimal numbers (num=1);
	Examples from Test-example 9.1 can also be used;
Output: partition Π, centers (Subscript[c, 1]),⃛,(Subscript[c, k]), objective function values ℱ(Π), \[FormalCapitalF](c), 
	or 𝒢(Π), values of CH index and DB index;
Submodules: d[],FF[], Fmin[], Figure1[], Figure2[]

KMeansPart[PI0_,p_:2,Ind_:1,num_:1]:=Module[{m,A,k=Length[PI0],it=0,F0,F1,PI,tab,imin,c,sigma2,G,KK,CH,DB},
A=Flatten[PI0,1];m=Length[A];
If[p==2,c=Table[Mean[PI0[[j]]],{j,k}]; G=Sum[Length[PI0[[j]]]Norm[c[[j]]-Mean[A]]^2,{j,k}],c=Table[Median[PI0[[j]]],{j,k}];G={}];
If[num==1,c=c//N;G=G//N]; 
Print[it,": PI=",PI0,"; c=",c,"; F=",F0=FF[PI0,c,p],"; G=",G,"; Fmin=",Fmin[A,c,p]];
If[Length[A[[1]]]==0,Figure1[PI0]];
If[Length[A[[1]]]==2,Figure2[PI0]];
While[
PI=Table[{},{j,k}];
Do[
tab=Table[d[A[[i]],c[[j]],p],{j,k}]//N;
imin=Ordering[tab,1][[1]];
PI[[imin]]=Append[PI[[imin]],A[[i]]],
{i,m}]; 
PI=DeleteCases[PI,{}];
k=Length[PI];
If[k<Length[PI0],Print["There has been a reduction in the number of clusters from ",Length[PI0]]," to ",k];
If[p==2,c=Table[Mean[PI[[j]]],{j,k}]; G=Sum[Length[PI[[j]]]Norm[c[[j]]-Mean[A]]^2,{j,k}],c=Table[Median[PI[[j]]],{j,k}]];
If[num==1,c=c//N;G=G//N]; 
F1=FF[PI,c,p];it=it+1;
Print[it,": PI=",PI,"; c=",c,"; F=",F1,"; G=",G,"; Fmin=",Fmin[A,c,p]];
Chop[F0-F1]≠0, 
If[Length[A[[1]]]==0,Figure1[PI]];
If[Length[A[[1]]]==2,Figure2[PI]];
F0=F1];
If[p==2,
CH=(G/(k-1))/(F1/(m-k));Print["CH = ",CH];
KK=Table[j,{j,k}];
sigma2=Table[Sum[Norm[c[[j]]-PI[[j,s]]]^2,{s,Length[PI[[j]]]}]/Length[PI[[j]]],{j,k}];
Print["centri:",c];Print["sigma2:",Sqrt[sigma2]];
DB=Sum[Max[Table[(Sqrt[sigma2[[j]]]+Sqrt[sigma2[[s]]])/Norm[c[[j]]-c[[s]]],{s,Complement[KK,{j}]}]],{j,k}]/k;
Print["DB = ",DB]];
{PI,c,F1,G,CH,DB}
]