clear
sprs = 1;
%m = 1;    % powers of the laplacian used 
%N_subs = 3032; % number of subgraphs to be considered

N_subs = 3139; % number of subgraphs to be considered
%N_subs = 300;
%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
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);
%create the lle kernel
% normalizing W
D1 = diag(sum(W,2));
L = D1-W;
K = pinv(full(L));
%K = (K+K')/2;


DST2 = diag(K)*ones(1,N) + ones(N,1)*diag(K)' - K - K';

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';
%save Dist_mat Dist_subg -append
%save DST2 DST2
clear vi vj di2 dj2 DST2 idx_in_graphs
time4 = toc;
save dist_lle7 Dist_subg
figure(3)
imagesc(Dist_subg);
clear Dist_subg
colormap(gray);    
%%return
clear
load good_cell_file
load dist_lle7
fclose('all');
% if exist('func_score_lle','file')
%     delete func_score_lle.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
fid = fopen('func_score_pseudo_lap.txt','a+');
%save dist_vector_lle_ascii dist_vector 
%fprintf(fid,'   %10.6f', dist_vector_alpha0);
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);
