function x=fgpfsp2(Pk)
% generate a permutation from P_t
% generation using node transitions
% and COMPOSITION

[n,m]=size(Pk); % n==m

Pkp=Pk;

% random row
r=randperm(n);
r=r(1);

zzz(1)=0;

for i=1:n,
zzz(i+1)=zzz(i)+Pkp(r,i);
end

r=rand(1);
for i=2:(n+1),
if r<zzz(i),
      x(1)=i-1;
break;
end
end

if(i==(n+1)),
     x(1)=n;
end

for k=1:(n-1),
Pkp(:,x(k))=zeros(n,1);
for i=1:n,
	if(sum(Pkp(i,:)~=0)),
	Pkp(i,:)=Pkp(i,:)/sum(Pkp(i,:));
end
end

     i1=((Pkp(x(k),:)<1/n)&(Pkp(x(k),:)~=0));
     p1=Pkp(x(k),i1);
     i2=((Pkp(x(k),:)>=1/n)&(Pkp(x(k),:)<=2/n)&(Pkp(x(k),:)~=0));
     p2=Pkp(x(k),i2);
     i3=((Pkp(x(k),:)>2/n)&(Pkp(x(k),:)~=0));
     p3=Pkp(x(k),i3);

[p3,ii3]=sort(p3);

tau1=length(p1);
tau2=length(p2);
tau3=length(p3);

d1=sum(p1);
d2=sum(p2);
d3=1-d1-d2;

     r=rand(1);
         if(r<=d1),
         yy=1;
         elseif (r>(d1+d2)),
         yy=3;
         else
         yy=2;
         end

         if(yy==1),
         pe=randperm(tau1);
q=pe(1);
         elseif(yy==2),
         pe=randperm(tau2);
    q=tau1+pe(1);
else
     pe=rand(1);
zz(1)=0;

for i=1:tau3,
zz(i+1)=zz(i)+p3(i);
end

for i=2:(tau3+1),
if pe<zz(i),
      q=tau1+tau2+i-1;
break;
end
end

if(i==(tau3+1)),
      q=tau1+tau2+tau3;
end
         end

ind=(Pkp(x(k),:)~=0);
sss=0;
for i=1:n,
	sss=sss+ind(i);
if(sss==q),
      c=i;
break;
end
end

x(k+1)=c;

Pk=Pkp;
          
end

