mips_class_adj;	% Load data

[m, m] = size(A);
A = A - diag(ones(m, 1));	% Make sure the main diagonal is zero
D = zeros(m);	% Distance matrix (to be computed)

i = 1;
Ai = A;
[k, l, a] = find(Ai == 1);
d = i*sparse(k, l, a, m, m);
D = D + d;
max_count = m*(m - 1)/2;
count = nnz(triu(D,1));

while count < max_count
	i = i + 1
	Ai = Ai*A;
	[k, l, a] = find(Ai == 1);
	d = i*sparse(k, l, a, m, m);
	D = D + d;
	count = nnz(triu(D,1));
end % while count 

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