name='edited_s&p500.txt';
ne=640;
nb=2;
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;
    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)];
        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)];
                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
 R=[];
R=corrcoef(logm);
dist=[];
dist=sqrt(2*(1-R));
R1=[];
R1=prim2(dist);
hold on;

d=zeros(1,col);%Array of degrees in a graph.

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
cords=random('unif',0,1,col,2);


v=[];
area=1;
k=sqrt(area/col);
t=0.4;
E=tree2lst(R1);
for i=1:400
    for j=1:col
        v(j).disp=[0,0];
        for jj=1:col
            if j~=jj
                delta=[cords(j,1)-cords(jj,1),cords(j,2)-cords(jj,2)];
                ad=sqrt(delta(1)*delta(1)+delta(2)*delta(2));
                if (2*k-ad)>0
                    v(j).disp=v(j).disp+delta/ad*k*k/ad;
                end
            end
        end
    end
        for j=1:size(E,1)
        delta=cords(E(j,1),:)-cords(E(j,2),:);
        ad=sqrt(delta(1)*delta(1)+delta(2)*delta(2));
        v(E(j,1)).disp=v(E(j,1)).disp-delta/ad*ad*ad/k;
        v(E(j,2)).disp=v(E(j,2)).disp+delta/ad*ad*ad/k;
    end
    for j=1:col
        vda=sqrt(v(j).disp(1)*v(j).disp(1)+v(j).disp(2)*v(j).disp(2));
        cords(j,:)=cords(j,:)+v(j).disp/vda*min([vda t]);
                
    end
    t=t-0.001;
end
for i=1:col
    plot(cords(i,1),cords(i,2),'go');
end
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)]);
end
                