function calculate_max_dice(func_file_1, func_file_2, num_branches)

% Use func_file_2 to rearrange and compare with func_file_1
mask    = func_file_2; 
data_1  = double(nonzeros(readimgfile(func_file_1)));

rearrange = perms(1:num_branches); 
% 8 branches could have about 40,000 ways of permuations to check! 
% Sit back and enjoy a few cups of coffee. 
% Yes, a few. 

for i_arrange = 1:length(rearrange)
    
    % Keeping you informed of the progress if there are more than 1000
    % ways to rearrange
    if mod(i_arrange,1000)==0
        fprintf('Running in the range: %d / %d \n',i_arrange, length(rearrange));
    end
    
    rearrange_this = rearrange(i_arrange,:);
    
    % Take the original mask and replace the branch numbers with new ones
    make_func_dice_compatible(mask,rearrange_this);
    
    % Load new .func.gii file
    data_2 = double(nonzeros(readimgfile(['rearranged_' mask])));
    
    % Delete this file to save memory 
    delete(['rearranged_' mask]);
    
    % Calculate dice 
    jac                         = jaccard(data_1,data_2);
    similarity(i_arrange,:)     = 2 * jac ./ (1 + jac);
    average_dice(i_arrange,1)   = mean(similarity(i_arrange,:));
end

save all_dice_coefficients
end
