% Calculate topological distance from GO adjacency matrix

go_process_adj_sparse;	% Load data

m = length(node);
max_count = m*(m - 1)/2;

A = sparse(A_sparse(:,1), A_sparse(:,2), A_sparse(:,3), m, m);
% D = zeros(m);			% Distance matrix (to be computed)
D = spalloc(m, m, max_count);	% Distance matrix (to be computed)

i = 1;
Ai = A;
[k, l, a] = find(Ai == 1);
d = i*sparse(k, l, a, m, m);
count = 0;

while count < max_count
	i = i + 1;
	Ai = Ai*A;

	% Determine where Ai has non-zero elements for the first time

	D = D + d;
	
	Nz = (Ai > 0)*(D == 0);
	[k, l, a] = find(Nz == 1);
	d = i*sparse(k, l, a, m, m);
	d_max = max(max(d));

	count = nnz(triu(D,1));
	emp = 100.0*(max_count - count)/max_count;
	fprintf('Iteration %d, d_max = %d, %.1f percent empty elements in distance matrix...\n', i, d_max(1, 1), emp);
end % while count 

D = D - diag(diag(D));	% Set main diagonal to zero 
