0

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
shamalaia
  • 2,282
  • 3
  • 23
  • 35
rezzz
  • 151
  • 1
  • 1
  • 8
  • Is there any reason why you can't calculate the paths, then filter out any that have the starting nodes after the start? This isn't the most computationally efficient, but it might be the easiest to achieve. – TTT Jul 06 '16 at 14:39
  • enter code here - LMAO homework again? – GameOfThrows Jul 06 '16 at 14:54
  • @ TTT thank you for your comment: but for instance for node 1, I have 1074 paths that include nodes 2 and 3. Also, for nodes 2, 3 the thousands of paths exist. So, filtering is not applicable in this case, I think, – rezzz Jul 06 '16 at 15:09
  • It's all a matter of computational complexity. But, the more robust solution to this problem is to erase (1) any direct connectivity between the starting nodes at the beginning of the problem, and (2) all the connectivity to the starting nodes after the first step is taken. – TTT Jul 06 '16 at 15:23

0 Answers0