function allpaths = allpathn(from,to,a)
% All paths between two nodes
%
% function allpaths = allpathn(from,to,a)
%
% from from-node, a number
% to to-node, a number
% a square successor matrix (n,n)
%
% allpaths matrix (?,n), each row contains the node numbers in a walk on a
% path; the matrix is padded with 0's to the right
%
% The algorithm is a traversal of the digraph. It uses the reachability matrix
% to prune the traversal.
a=a&1;
r=reachabi(a);
[n,dum] = size(a) ;
paths = from ;
allpaths = [] ;
%%%%%%%%%%%%%%%%%%%%%%%%%
emptypath = zeros(1,n+1) ;
%%%%%%%%%%%%%%%%%%%%%%%%%
while ~isempty(paths),
[maxp,dist] = size(paths) ;
newpaths = [] ;
for i=1:maxp,
curpath = paths(i,:) ;
%%%%%%%%%%%%%
candids = find(a(:,curpath(dist))&r(to,:)') ;
%%%%%%%%%%%%%
for j = 1:length(candids),
cand = candids(j) ;
%%%%%%%%%%%%%%%%%%%%%%%%%
p1 = all(cand ~= curpath) ;
%%%%%%%%%%%%%%%%%%%%%%%%%
p2 = cand == to ;
if p2,
newpath = emptypath ;
%%%%%%%%%%%%%%%%%%%%%%%%%
newpath(1:(length(curpath)+1)) = [curpath,cand] ;
allpaths = [allpaths;newpath];
%%%%%%%%%%%%%%%%%%%%%%%%%
[p,dum]=size(allpaths);
% disp([int2str(p),' paths found']);
elseif p1 ,
newpath = [curpath,cand] ;
newpaths = [newpaths;newpath] ;
end ;
end ;
end ;
paths = newpaths ;
end ;