% Recursive formula for LPM to find other L matrices
% Thanks to Melih Bilmez
% 1.6.2020 
%Mustak Yalcin
clear all;

mNum = 4;

L = zeros(mNum);
maxDiag = zeros(1,mNum-1);

%L1 is defined
L(:,:,1) = [-1 0 -1 -1; 4 -1 0 -1; 5 -1 -1 0; 5 -1 -1 -1];

%L(m+1) calculations

for m = 1:1:mNum-1
    for i = 1:1:mNum
       for j = 1:1:mNum

            a = (L(i,:,1) ~= -1);
            b = (L(:,j,m) ~= -1);
            
            % find K sets
            k = a.*b';
            
            % find l(m+1)(i,j) = max(-1,l(1)(i,k)+l(m)(k,j))
            if sum(k) == 0 % which means K is an ampty set
               
                L(i,j,m+1) = -1;
                
            else
                
                L(i,j,m+1) = max(k.*L(i,:,1) + k.*transpose(L(:,j,m)));
                
            end

       end
    end
    
    % find max. of the diagonals with scale factor
    maxDiag(m) = max((1/(m+1)).*diag(L(:,:,m+1)));
    
end

% find the iteration bound
T_inf = max(maxDiag);
