function main()
% Main program to compute all the topological indices
% W = Wiener; hW = hyperWiener; rW = reverse Wiener
% Sz = Szeged; B = Balaban; Kf = Kirchhoff
%
% input: coordinates.xlsx (exel file with matrix of 3D coordinates)
% uses gplot3.m to visualize an adjacency matrix with Euclidean distances
%

clear all; cla;clf;
tic;

P = xlsread('Fullerene_C60-Ih.xls'); % read the coordinates 
%P=xlsread('Hex6.xls'); 
%P=xlsread('I-147.xls');
N = size(P,1), % N = number of lattice points;
Factor = 1.2; % factor defining the max distance for a neighbor

% compute A = adjacency matrix with Euclidean distances

A = 10*ones(N);
for i = 1:N
    for j = i+1:N
        A(i,j) = norm(P(i,:)-P(j,:),2);
        A(j,i) = A(i,j);   
    end;
    mindist = min(A(i,1:N));
    ind = find(A(i,:)>Factor*mindist);
    A(i,ind) = 0;
end

figure(1), gplot3(A,P,'-+'); grid on; % visualization

A = double(A>0); % A = adjacency matrix with 0-1 entries

T = topodist(A); % T = topological distances

% compute  n(i) is # of nodes with i edges (i = 1,...,12)
for i = 1:12
    n(i) = sum((sum(A,2) == i*ones(N,1))*1);
end

W  = wiener(T)
hW = hyperwiener(T)
rW = revwiener(T)
sz = szeged(T,A)
B  = balaban(T)
Kf = kirchoff(A)

toc 

function T = topodist(A)
% T = matrix of topological distances
% A = 0-1 adjacency matrix
%
if (exist('graphallshortestpaths'))
    T = graphallshortestpaths(sparse(A));
else
 N = length(A);
 T = A;
 for j = 1:N
     for i = 1:N
         if(T(i,j) == 0); T(i,j) = N; end;
     end
     T(j,j) = 0;
 end
 L = 1;
 while (L<N-1);
  for j = 1:N
     for i = 1:N
         if(T(i,j) == L);
             for k = 1:N
                 iakj = T(k,i) + L;
                 if (T(k,j)>iakj); T(k,j) = iakj; end;
             end
         end
     end
  end
 L = L+L;
 end
end


function W = wiener(T)
% compute Wiener index
% T = euclid or topological distances
%
 N = length(T);
 W = 0;
 for i = 1:N-1
    W = W + sum(T(i,i+1:N));
 end
 
function hW = hyperwiener(T)
% compute hyper Wiener index
% T = topological distances
%
 N = length(T);
 hW = 0;
 for i = 1:N-1
    hW = hW + sum(T(i,i+1:N) + T(i,i+1:N).^2);
 end
 
function rW = revwiener(T);
% compute reverse Wiener index
% T = topological distances
%
 N = length(T);
 rW = max(max(T,[],2))*N*(N-1)/2-wiener(T);
 
function sz = szeged(T,A);
% compute reverse Szeged index
% T = topological distances; A = 0-1 adjacency matrix
%
 N = length(T);
 sz = 0;
 for i = 1:N
    for j = i+1:N
        if (A(i,j) ~= 0)
            ni = 0; nj = 0;
            for k = 1:N
                if(T(i,k)<T(j,k)), ni = ni+1; end
                if(T(i,k)>T(j,k)), nj = nj+1; end
            end
        sz = sz+ni*nj;
        end
    end
 end

function B = balaban(T)
% computes Balaban index
% T = matrix of topological distances
%
 N = length(T);
 e = sum(sum(T==ones(N,N)))/2;
 for i = 1:N
     w(i) = sum(T(i,1:N));
 end
 B = 0;
 for j = 2:N
     for i = 1:j-1
         B = B + (T(i,j)==1)/sqrt(w(i)*w(j));
     end
 end
 B = B*e/(e-N+2);
 
function Kf = kirchoff(A)
% computes Kirchoff index
% A = 0-1 adjacency matrix
%
 N = length(A);
 lam = eig(diag(sum(A))-A); % Laplacian = diag(sum(A))-A
 Kf = N*sum(1./lam(2:end));

%%% END OF CODE %%%
