%Generates simple, connected, bipartite, 3-regular graphs, calculates second largest
%eigenvalues, then uses a simplified Autocorrelation formula for aut(lag), and saves data
clear all; close all; pack
tic
options.disp=0;
EigenTwo=0;
SampleSize = 10000;%number of steps per walks
k=3;
nVec=[100 125 158 199 251 316 398 501 630 794 1000];%n is size of HALF of the graph.
EigenTwo=zeros(SampleSize, length(nVec));
%These values have been chosen so that on a loglog plot, the data points are evenly spaced on n-axis   
for c=1:length(nVec)
   n=nVec(c);
   for w = 1:SampleSize
      A=spalloc(n,n,3*n); 
      while(max(max(A))~=1)%Continues until SIMPLE graphs is foun
         %This loop uses Steven Miller's collapsing vectors method to generate
         %k-regular graphs.  For description see section 2 in N.C. Wormald's Models of Random Regular Graphs
         Pairs=[randperm(n*k); randperm(n*k)+n];
         A=spalloc(2*n,2*n,3*n);  
         for i=1:(n*k)
            m1= mod(Pairs(1,i),n)+1;
            m2= mod(Pairs(2,i),n)+n+1;         
            A(m1,m2) =  A(m1,m2) + 1;
            A(m2,m1) =  A(m2,m1) + 1;
         end
      end
      E=sort(eigs(A,2,'LA', options));%Sorted Eigenvalues of A.
      ZeroIfConnected=E(1)==E(2);   
      if(ZeroIfConnected==0)
         EigenTwo(w,c)=E(1);
      else
         w=w-1;
      end
   end
   save Mother1 EigenTwo SampleSize nVec
end
RunTime=toc;
save Mother1 EigenTwo n RunTime