
function DAT = epidemic(loc,init)

% This program simulates an epidemic on loc
%
% INPUTS:
%        (i)   loc = file of locations (xi,yi), i=1..m
%        (ii)  init = list of initial points
%
% OUTPUTS: DAT = (m x 3) array of location-times

[m,n] = size(loc) ;

%Compute pairwise distances

D = zeros(m) ; %Matrix of pairwise distances

Dmax = 0 ;

i = 1 ;

k = 1 ;  %counter for D.

while i < m
   
   j = i + 1 ;
   
   while j <= m
      
      D(i,j) = norm(loc(i,:) - loc(j,:));

      D(j,i) = D(i,j) ;      
                     
      if D(i,j) > Dmax
         
         Dmax = D(i,j) ;
         
      end
      
      j = j + 1 ;      
           
   end
   
   i = i + 1 ;
   
end

%Generate initial onset times ;

T = zeros(m,1) ;

t = length(init) ;

for i = 1:t
   
   T(init(i)) = i ;
   
end

%Generate subsequent onset times

while t < m
   
   i = 1;
   
   while i <= m ;      
      
      if t == m
         break
      end
      
      if T(i) > 0
         
         dist = Dmax ;
         
         %now find nearest uninfected neighbor to i
         
         j = 1 ;
         
         ind = 0 ;
         
         while j <= m            
                      
            if (j ~= i)&(T(j)==0)&(D(i,j)<dist)
               
               dist = D(i,j) ;
               
               ind = j ;
               
            end
            
            j = j + 1 ;
            
         end
         
         if ind == 0                        
            break           
         end         
                        
         t = t + 1 
            
         T(ind) = t ; %add infected case
            
         if t == m               
            break               
         end         
                                  
      end %end if statement
      
      i = i + 1 ;
      
   end
   
end

   %form output
   
   DAT = zeros(m,3) ;
   
   DAT(:,1:2) = loc ;
   
   DAT(:,3) = T ;            
            
   
   







