function [hoplength, nexthop, lnks, ns_rt , dlnks] = shortest_path_routing(wadj, src, dst)
% use dijkstra algorithm (not a distributed one) to set up minimum-hop route for node pairs
%
% input parameter:
% wadj: weighted adjacncy matrix
% src, dst: given node pairs 

% output parameters:
%
% dist: length of paths
% nexthop: nexthop for node i to dst node j, this is an agregated routing
%          table for given node pairs, not for every possible node pair.
% lnks: all links used for those pairs, the links are ordered for each
%       node-pair. Thus, it is possible there are duplicate links
% ns_rt: routing table used for ns to do fixed_path routing. The entries are ordered for each
%        node-pair. Thus, it is impossible to have duplicate entries becasue of
%        the destination are all distinctive
%dlnks: distinctive links in lnks array

nn = length(wadj);
mm = length(src);
nexthop = zeros(nn);
dist = zeros(mm,1);
hoplength = zeros(mm,1);
lnks = zeros(nn*nn,2);
ns_rt = zeros(nn*nn,4);

cnt = 0;
for i=1:mm   
   [path,dist(i)] = dijkstra_wu(nn,wadj,src(i),dst(i)); 
   %plot the shortest path  
   %plot (xy(path,1), xy(path,2), 'r-');
   s = [ 'Flow ' int2str(i) ': '  int2str(path)];
   disp(s);  
   hoplength(i) = length(path) - 1;
   for j = 1:length(path)-1
       nexthop(path(j),dst(i)) = path(j+1);
       cnt = cnt + 1;
       lnks(cnt,:) = [path(j) path(j+1)];
       ns_rt(cnt,:) = [ path(j) dst(i) path(j+1) length(path)-j]; 
   end            
end

lnks = lnks(1:cnt,:);
ns_rt = ns_rt(1:cnt,:);
%find distinctive links, some links might be used by multiple flows
dlnks = zeros(cnt, 2);
dcnt = 0;
for i=1:cnt
    dup = 0;
    for j= 1:dcnt
         if ( lnks(i,1) == dlnks(j,1) & lnks(i,2) == dlnks(j,2) )
              dup = 1 ;
              break;
         end
    end
    if ( dup == 0 )
      dcnt = dcnt + 1;
      dlnks(dcnt,:)= lnks(i,:); 
    end
end
dlnks = dlnks(1:dcnt,:);
        
return;
