 %name='edited_s&p500.txt';
%ne=640;
nb=2;
name='edted s&p_global_100.txt';
ne=500;
%strt=
%l=
if name~=0
    fd=fopen(name);
    col=fscanf(fd,'%d',1);
    res.col=col;
    n=fscanf(fd,'%d',1);
    res.dnei=n;
    a=[];
    for j=1:col
        b=fscanf(fd,'%e',n);
        a=[a;transpose(b)];
    end
    a=transpose(a);
    res.matr=a;
    str=[];
    for i=1:col
        b=fscanf(fd,'%s',1);
        str(i).name=b;
        %disp(b);
    end
    for i=1:col
        b=fscanf(fd,'%i',1);
        str(i).type=b;
        str(i).color=[0 0 0]; 
    end
    res.str=str;
    fclose(fd);
    calaz=[];
    fd=fopen('colors.ini');
    if fd~=-1
        n=fscanf(fd,'%e',1);
        for i=1:n
            li=fscanf(fd,'%e',4);
            calaz=[calaz;transpose(li)];
            %disp(calaz);
        end
        for i=1:col
            for j=1:n
                if calaz(j,1)==res.str(i).type
                    res.str(i).color=[calaz(j,2) calaz(j,3) calaz(j,4)];
                    %disp(res.str(i).color);
                end
            end
        end
        f=0;
    else
        f=-1;
    end
else
    f=-1;
    res=[];
end


n=res.dnei;
col=res.col;
a=res.matr;
str=res.str;
logm=zeros(ne-nb+1,col);
for j=1:col
    for i=nb:ne
        logm(i-nb+1,j)=log(a(i,j))-log(a(i-1,j));
    end
end
%for j=1:col
 %   for i=nb:ne-1
 %       logm(i-nb+1,j)=-log(a(i,j))+log(a(i+1,j));
 %   end
 %end
 R=[];
R=corrcoef(logm);
dist=[];
dist=sqrt(2*(1-R));
R1=[];
R1=prim2(dist);
hold on;
%axis off;
d=zeros(1,col);%Array of degrees in a graph.
%max=0;
for i=1:col
    d(i)=0;
    for j=1:col
        if i~=j & R1(i,j)~=inf
        d(i)=d(i)+1;
        end
    end
end
max=0;
nmax=1;
for i=1:col		%Determining the max degree
    if (d(i)>max)
        max=d(i);
        nmax=i;
    end
end
R2=[];
R2=R1;
temp=R2(1,:);
R2(1,:)=R2(nmax,:);
R2(nmax,:)=temp;
temp=R2(:,1);
R2(:,1)=R2(:,nmax);
R2(:,nmax)=temp;
clear temp;
E=tree2lst(R2);
count=[];
count=[count,1];
level=zeros(1,col);
level(1,1)=1;
z=1;
while z~=0;
    clear temp;
    temp=zeros(1,col);
    k=1;
    for i=1:count(z)
        for j=1:col
            if level(z,i)~=j & R2(level(z,i),j)~=inf
                temp(1,k)=j;
                R2(j,level(z,i))=inf;
                k=k+1;
            end
        end
    end
    if k==1
       z=0;
   else
       level=[level;temp];
       count=[count,k-1];
       z=z+1;
   end
end
cords=zeros(col,2);
for i=1:size(count,2)
    for j=1:count(i)
        cords(level(i,j),1)=j/(count(i)+1);
        cords(level(i,j),2)=-i;
        %xx=plot(j/(count(i)+1),-i,'ko');
        %set(xx,'MarkerFaceColor',str(level(i,j)).color);
    end
end
clear R2;
for i=1:size(E,1)
    plot([cords(E(i,1),1),cords(E(i,2),1)],[cords(E(i,1),2),cords(E(i,2),2)],'k-');
end
for i=1:size(count,2)
    for j=1:count(i)
    end
end