function supergraphs = generateSupergraphs(plcs,rho,parallelFlag)
% GENERATESUPERGRAPHS builds the supergraphs per time slice of the
% trajectory sample. Note that the placements are simply treated as nodes
% without regard for trajectory index or agent number, mearly a proximity
% based graph

Tk = size(plcs,1);
Tj = size(plcs,2);
N = size(plcs{1,1},1);

supergraphs = cell(Tk,1); % storage for all supergraphs

if parallelFlag
    
    parfor s=1:Tk
        % Generate supergraph incidence for this timeslice
        G = zeros(N*Tj);
        for i=1:N
            for j=1:Tj
                for k=1:N
                    if(k == i)
                        continue;
                    end
                    for l=1:Tj
                        if(norm(plcs{s,j}(i,:)-plcs{s,l}(k,:)) <= rho)
                            idx1 = (i-1)*Tj + j;
                            idx2 = (k-1)*Tj + l;
                            G(idx1,idx2) = 1;
                        end
                    end
                end
            end
        end
        
        supergraphs{s} = G; % we just get the adjacency matrix now, for speed purposes
    end
    
else
    
    for s=1:Tk
        % Generate supergraph incidence for this timeslice
        G = zeros(N*Tj);
        for i=1:N
            for j=1:Tj
                for k=1:N
                    if(k == i)
                        continue;
                    end
                    for l=1:Tj
                        if(norm(plcs{s,j}(i,:)-plcs{s,l}(k,:)) <= rho)
                            idx1 = (i-1)*Tj + j;
                            idx2 = (k-1)*Tj + l;
                            G(idx1,idx2) = 1;
                        end
                    end
                end
            end
        end
        
        supergraphs{s} = G;
    end
    
end

end