clear
for m = 5:9
    save m_file m
    %sprs = 0;
    %alpha = 0;
    N_subs = 3139; % number of subgraphs to be considered
    %file_name = 'new_data.txt';
    file_name = 'individual_dom_matrices_process_component.out';
    if N_subs < 3139
        [W,good_cell,idx_in_graphs] = create_graph_oct05(file_name,N_subs);
    else
        load graph_info_file_weight1 % weights 1 are in subgraphs, but for the 
                                     % whole graph it is the inverse of the
                                     % sum of those ones...r
    end
    N_subs = length(idx_in_graphs); %number of good subgraphs
    save good_cell_file good_cell
    save idx_in_graphs_file idx_in_graphs
    save W_file W
    clear good_cell idx_in_graphs W


    load W_file
    tic
    N = length(W);
    D = diag(sum(W,2));
    %D2_half = D2.^0.5;
    D_inv_half = D.^-0.5;
    W2 = D_inv_half*W*D_inv_half; %normalized laplacian
    W2 = W2^(2*m);
    W2 = 0.5*(W2+W2');
    %                 en = D2_half*ones(N,1);
    clear D* W

    W2 = full(W2);
    DST2 = diag(W2)*ones(1,N) + ones(N,1)*diag(W2)' - 2*W2;
    clear W2


    time3 = toc;
    tic
    Dist_subg = zeros(N_subs,N_subs);
    load idx_in_graphs_file
    for i = 1:N_subs
        for j = (i+1):N_subs
            if sum(idx_in_graphs{i}) == 0 | sum(idx_in_graphs{j}) ==0
                Dist_subg(i,j) = NaN;
            else
                vi = idx_in_graphs{i};
                dj2 = zeros(length(vi),1);
                for k =1:length(vi)
                    dj2(k) = min( DST2( vi(k), idx_in_graphs{j}) );
                end
                vj = idx_in_graphs{j};
                di2 = zeros(length(vj),1);
                for k =1:length(vj)
                    di2(k) = min( DST2(vj(k), idx_in_graphs{i}) );
                end
                Dist_subg(i,j) = 0.5*(sqrt(sum(di2)/length(di2)) + sqrt(sum(dj2)/length(dj2)));
            end
        end
    end
    Dist_subg = Dist_subg + Dist_subg';
    clear vi vj di2 dj2 idx_in_graphs
    time4 = toc;
    save dist_alpha0_nosprs_all Dist_subg
    DST2_alpha0_nosprs = DST2;
    Dist_subg_alpha0_nosprs = Dist_subg;
    save Dist_mat_dfsn_all Dist_subg_alpha0_nosprs DST2_alpha0_nosprs
    clear Dist_subg_alpha0_nosprs DST2_alpha0_nosprs
    figure
    imagesc(Dist_subg);
    clear Dist_subg DST2
    colormap(gray);
    %return
    clear
    load m_file
    load good_cell_file
    %for ii = 1:2
    alpha = 0;
    fclose('all');
    load dist_alpha0_nosprs_all
%     if exist('func_score_alpha0_nosprs_oct05_all.txt','file')
%         delete func_score_alpha0_nosprs_oct05_all.txt
%     end
    N = length(Dist_subg);
    num_comp = (2*N-(N-1))*(N-1-1)/2 + 1;
    dist_vector = zeros(num_comp,1);
    cell_col1 = cell(num_comp,1);
    cell_col2 = cell(num_comp,1);
    count = 0;
    for ii = 1:(N-1) %row
        for j = (ii+1):N %coloumn, recall that ii<j
            count = count + 1;
            dist_vector(count,1) = Dist_subg(ii,j);
            cell_col1{count} = good_cell{ii};
            cell_col2{count} = good_cell{j};
        end
    end
    clear Dist_subg
    str = ['fid = fopen(''func_score_alpha0_nosprs_oct05_all' int2str(m) '.txt'',''a+'')'];
    eval(str)
    save dist_vector_alpha0_nosprs_ascii_all dist_vector
    len_str = 12;
    for ii=1:num_comp
        %fprintf(fid0,'%10.6f', dist_vector_alpha0(ii));
        fprintf(fid,'%s',cell_col1{ii});
        for j=1:(len_str-length(cell_col1{ii}))
            fprintf(fid,' ');
        end
        fprintf(fid,'%s', cell_col2{ii});
        for j=1:(len_str-length(cell_col2{ii}))
            fprintf(fid,' ');
        end
        fprintf(fid,'%12.6f\n', dist_vector(ii));
    end
    fclose(fid);
end