import java.util.Stack;
paths=Stack();
conn=[0 1 0 0 0 0; 0 0 1 1 0 0; 0 0 0 0 1 0; 0 0 0 0 1 1; 0 0 0 0 0 0;
0 0 0 0 0 0];
% independent paths are [1,2,3,5],[1,2,4,5] and [1,2,4,6]
% initialize paths
for i=1:size(conn,1)
if isempty(find(conn(:,i),1))
path=Stack();
path.push(i);
paths.push(path);
end
end
active=paths.size();
while active>0
newpaths=Stack();
for i=1:paths.size()
path=paths.get(i-1);
last=path.peek();
next=find(conn(last,:));
active=active+numel(next)-1;
if isempty(next)
newpaths.add(path);
else
for j=1:numel(next)
current=path.clone();
current.push(next(j));
newpaths.add(current);
end
end
end
paths=newpaths;
end