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

[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

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
     r=rand(1);
     cc=0;
     c=0;
	      while(r>cc),
	   c=c+1;
cc=cc+Pk(x(k),c);
end
	  x(k+1)=c;
Pk=Pkp;
          
end
