具有一些限制的无向图中的所有可能路径

时间:2016-07-06 14:02:01

标签: matlab graph search-path

我应该找到所有带图形的路径(24个节点和42个顶点)。我的起始节点是1,2或3,最终节点是10,12,13,16,17,18,20,21,22,其余节点是中间节点。我的图A的稀疏邻接矩阵如下。 我找到了以下Matlab代码来查找从起点到目标点的所有路径,但问题是例如如果起点是1,我们不应该在路径中有节点2。换句话说,路径中只应出现一个起点。谁能帮我这个?

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

示例图:

  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

0 个答案:

没有答案