I should find all paths with a graph (24 nodes and 42 vertices). My starting nodes are 1, 2 or 3, and the final nodes are 10, 12, 13, 16, 17, 18, 20, 21, 22 and the rest are intermediate nodes. The sparse adjacency matrix of my graphs A is as follow. I found the following Matlab code to find the all paths from a starting point to a target point, but the problem is that for example if the starting point is 1, we should not have node 2 in path. In the other words, only one starting point should appear in a path. Can anyone help me with this?
function paths = findpaths(Adj, nodes, currentPath, start, target)
paths = {};
nodes(start) = 0;
currentPath = [currentPath start];
childAdj = Adj(start,:) & nodes;
childList = find(childAdj);
childCount = numel(childList);
if childCount == 0 || start == target
if start == target
paths = [paths; currentPath];
end
return;
end
for idx = 1:childCount
currentNode = childList(idx);
newNodes = nodes;
newNodes(currentNode) = 0;
newPaths = findpaths(Adj, newNodes, currentPath, currentNode, target);
paths = [paths; newPaths];
end
end
Example graph:
A =
(4,1) 1
(5,1) 1
(9,1) 1
(10,1) 1
(12,1) 1
(5,2) 1
(7,2) 1
(8,2) 1
(8,3) 1
(1,4) 1
(5,4) 1
(6,4) 1
(9,4) 1
(15,4) 1
(1,5) 1
(2,5) 1
(4,5) 1
(14,5) 1
(17,5) 1
(4,6) 1
(16,6) 1
(19,6) 1
(20,6) 1
(22,6) 1
(2,7) 1
(20,7) 1
(23,7) 1
(2,8) 1
(3,8) 1
(23,8) 1
(1,9) 1
(4,9) 1
(13,9) 1
(1,10) 1
(12,10) 1
(13,10) 1
(14,11) 1
(17,11) 1
(1,12) 1
(10,12) 1
(16,12) 1
(18,12) 1
(9,13) 1
(10,13) 1
(16,13) 1
(18,13) 1
(5,14) 1
(11,14) 1
(17,14) 1
(4,15) 1
(6,16) 1
(12,16) 1
(13,16) 1
(18,16) 1
(5,17) 1
(11,17) 1
(14,17) 1
(12,18) 1
(13,18) 1
(16,18) 1
(19,18) 1
(21,18) 1
(22,18) 1
(6,19) 1
(18,19) 1
(21,19) 1
(6,20) 1
(7,20) 1
(18,21) 1
(19,21) 1
(24,21) 1
(6,22) 1
(19,22) 1
(24,22) 1
(7,23) 1
(8,23) 1
(19,24) 1
(21,24) 1
(22,24) 1