function [q,v]=mc_perdo(P)
%
% The algorithm is from "Periods of Connected Networks and Powers of
% Nongegative Matrices," by Eric V. Denardo, Mathematics of Operations
% Research, Vol. 2, No. 1, 1977, pp. 23-24.
% Input: P = Transition matrix of an irreducible Markov chain
% output: q = the period of the chain
% *** It uses subroutine GCD ***
%
[n,n]=size(P); v=zeros(1,n); v(1,1)=1; PS=[]; q=0; T=[1]; [m1,m2]=size(T);
while (m2>0)&(q~=1)
i=T(1,1); T(:,[1])=[]; PS=[PS i]; j=1;
while j <= n
if (P(i,j) > 0)
PUT=[PS T]; k=sum(j==PUT);
if (k > 0), b=v(1,i)+1-v(1,j); [q]=mc_gcd(q,b);
else T=[T j]; v(1,j)=v(1,i)+1;
end
end
j=j+1;
end
[m1,m2]=size(T);
end
fprintf('\n'); fprintf(' period = %3.0f \n',q); fprintf('\n');