function [s_0,badj,communityvec] = NF_modular(nNodes,p_in, p_out, commSize)
%% Node filtering model with edges added to create modular network.

% Inputs:
    % nNodes        Number of nodes.
    % p_in          Probability of intra-community edges.
    % p_out         Probability of inter-community edges.
    % commSize      Size of communities.
 
% Outputs:
    % s_0           Node ordering (1:nnodes).
    % badj          Binary adjacency matrix in s_0 order.
    
%
% Reference: Ann E. Sizemore, Elisabeth A. Karuza, Chad Giusti, Danielle S.
% Bassett, Knowledge gaps in the early growth of semantic networks.
% Submitted.
%
%% Main function:
    
    
% Preallocate
badj = zeros(nNodes);

communityvec = [];
i1 = 1;
while length(communityvec) < nNodes
    communityvec = [communityvec i1*ones(1,commSize)];
    i1 = i1+1;
end

% randomize community assignments
randOrder = randperm(length(communityvec));
communityvec = communityvec(randOrder);

% Run model
for n = 1:nNodes
    
    for j = 1:(n-1)
        
        % We will add an edge with probability 1/n^d
        
        r = rand;
        
        if communityvec(n) == communityvec(j)
            if r < p_in
                badj(n,j) = 1;
                badj(j,n) = 1;
            end
            
        else % then they are in different communities
            if r < p_out
                badj(n,j) = 1;
                badj(j,n) = 1;
            end
        end
        
    end
    
    
end
        
    
s_0 = 1:nNodes;

end

