Find the paths between two given nodes?

前端 未结 8 2063
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 11:55

Say I have nodes connected in the below fashion, how do I arrive at the number of paths that exist between given points, and path details?

1,2 //node 1 and 2         


        
相关标签:
8条回答
  • 2020-11-27 12:20

    given the adjacency matrix:

    {0, 1, 3, 4, 0, 0}

    {0, 0, 2, 1, 2, 0}

    {0, 1, 0, 3, 0, 0}

    {0, 1, 1, 0, 0, 1}

    {0, 0, 0, 0, 0, 6}

    {0, 1, 0, 1, 0, 0}

    the following Wolfram Mathematica code solve the problem to find all the simple paths between two nodes of a graph. I used simple recursion, and two global var to keep track of cycles and to store the desired output. the code hasn't been optimized just for the sake of code clarity. the "print" should be helpful to clarify how it works.

    cycleQ[l_]:=If[Length[DeleteDuplicates[l]] == Length[l], False, True];
    getNode[matrix_, node_]:=Complement[Range[Length[matrix]],Flatten[Position[matrix[[node]], 0]]];
    
    builtTree[node_, matrix_]:=Block[{nodes, posAndNodes, root, pos},
        If[{node} != {} && node != endNode ,
            root = node;
            nodes = getNode[matrix, node];
            (*Print["root:",root,"---nodes:",nodes];*)
    
            AppendTo[lcycle, Flatten[{root, nodes}]];
            If[cycleQ[lcycle] == True,
                lcycle = Most[lcycle]; appendToTree[root, nodes];,
                Print["paths: ", tree, "\n", "root:", root, "---nodes:",nodes];
                appendToTree[root, nodes];
    
            ];
        ];
    
    appendToTree[root_, nodes_] := Block[{pos, toAdd},
        pos = Flatten[Position[tree[[All, -1]], root]];
        For[i = 1, i <= Length[pos], i++,
            toAdd = Flatten[Thread[{tree[[pos[[i]]]], {#}}]] & /@ nodes;
            (* check cycles!*)            
            If[cycleQ[#] != True, AppendTo[tree, #]] & /@ toAdd;
        ];
        tree = Delete[tree, {#} & /@ pos];
        builtTree[#, matrix] & /@ Union[tree[[All, -1]]];
        ];
    ];
    

    to call the code: initNode = 1; endNode = 6; lcycle = {}; tree = {{initNode}}; builtTree[initNode, matrix];

    paths: {{1}} root:1---nodes:{2,3,4}

    paths: {{1,2},{1,3},{1,4}} root:2---nodes:{3,4,5}

    paths: {{1,3},{1,4},{1,2,3},{1,2,4},{1,2,5}} root:3---nodes:{2,4}

    paths: {{1,4},{1,2,4},{1,2,5},{1,3,4},{1,2,3,4},{1,3,2,4},{1,3,2,5}} root:4---nodes:{2,3,6}

    paths: {{1,2,5},{1,3,2,5},{1,4,6},{1,2,4,6},{1,3,4,6},{1,2,3,4,6},{1,3,2,4,6},{1,4,2,5},{1,3,4,2,5},{1,4,3,2,5}} root:5---nodes:{6}

    RESULTS:{{1, 4, 6}, {1, 2, 4, 6}, {1, 2, 5, 6}, {1, 3, 4, 6}, {1, 2, 3, 4, 6}, {1, 3, 2, 4, 6}, {1, 3, 2, 5, 6}, {1, 4, 2, 5, 6}, {1, 3, 4, 2, 5, 6}, {1, 4, 3, 2, 5, 6}}

    ...Unfortunately I cannot upload images to show the results in a better way :(

    http://textanddatamining.blogspot.com

    0 讨论(0)
  • 2020-11-27 12:31

    Breadth-first search traverses a graph and in fact finds all paths from a starting node. Usually, BFS doesn't keep all paths, however. Instead, it updates a prededecessor function π to save the shortest path. You can easily modify the algorithm so that π(n) doesn't only store one predecessor but a list of possible predecessors.

    Then all possible paths are encoded in this function, and by traversing π recursively you get all possible path combinations.

    One good pseudocode which uses this notation can be found in Introduction to Algorithms by Cormen et al. and has subsequently been used in many University scripts on the subject. A Google search for “BFS pseudocode predecessor π” uproots this hit on Stack Exchange.

    0 讨论(0)
  • 2020-11-27 12:31

    In Prolog (specifically, SWI-Prolog)

    :- use_module(library(tabling)).
    
    % path(+Graph,?Source,?Target,?Path)
    :- table path/4.
    
    path(_,N,N,[N]).
    path(G,S,T,[S|Path]) :-
        dif(S,T),
        member(S-I, G), % directed graph
        path(G,I,T,Path).
    

    test:

    paths :- Graph =
        [ 1- 2  % node 1 and 2 are connected
        , 2- 3 
        , 2- 5 
        , 4- 2 
        , 5-11
        ,11-12
        , 6- 7 
        , 5- 6 
        , 3- 6 
        , 6- 8 
        , 8-10
        , 8- 9
        ],
        findall(Path, path(Graph,1,7,Path), Paths),
        maplist(writeln, Paths).
    
    ?- paths.
    [1,2,3,6,7]
    [1,2,5,6,7]
    true.
    
    0 讨论(0)
  • 2020-11-27 12:35

    Dijkstra's algorithm applies more to weighted paths and it sounds like the poster was wanting to find all paths, not just the shortest.

    For this application, I'd build a graph (your application sounds like it wouldn't need to be directed) and use your favorite search method. It sounds like you want all paths, not just a guess at the shortest one, so use a simple recursive algorithm of your choice.

    The only problem with this is if the graph can be cyclic.

    With the connections:

    • 1, 2
    • 1, 3
    • 2, 3
    • 2, 4

    While looking for a path from 1->4, you could have a cycle of 1 -> 2 -> 3 -> 1.

    In that case, then I'd keep a stack as traversing the nodes. Here's a list with the steps for that graph and the resulting stack (sorry for the formatting - no table option):

    current node (possible next nodes minus where we came from) [stack]

    1. 1 (2, 3) [1]
    2. 2 (3, 4) [1, 2]
    3. 3 (1) [1, 2, 3]
    4. 1 (2, 3) [1, 2, 3, 1] //error - duplicate number on the stack - cycle detected
    5. 3 () [1, 2, 3] // back-stepped to node three and popped 1 off the stack. No more nodes to explore from here
    6. 2 (4) [1, 2] // back-stepped to node 2 and popped 1 off the stack.
    7. 4 () [1, 2, 4] // Target node found - record stack for a path. No more nodes to explore from here
    8. 2 () [1, 2] //back-stepped to node 2 and popped 4 off the stack. No more nodes to explore from here
    9. 1 (3) [1] //back-stepped to node 1 and popped 2 off the stack.
    10. 3 (2) [1, 3]
    11. 2 (1, 4) [1, 3, 2]
    12. 1 (2, 3) [1, 3, 2, 1] //error - duplicate number on the stack - cycle detected
    13. 2 (4) [1, 3, 2] //back-stepped to node 2 and popped 1 off the stack
    14. 4 () [1, 3, 2, 4] Target node found - record stack for a path. No more nodes to explore from here
    15. 2 () [1, 3, 2] //back-stepped to node 2 and popped 4 off the stack. No more nodes
    16. 3 () [1, 3] // back-stepped to node 3 and popped 2 off the stack. No more nodes
    17. 1 () [1] // back-stepped to node 1 and popped 3 off the stack. No more nodes
    18. Done with 2 recorded paths of [1, 2, 4] and [1, 3, 2, 4]
    0 讨论(0)
  • 2020-11-27 12:35

    The original code is a bit cumbersome and you might want to use the collections.deque instead if you want to use BFS to find if a path exists between 2 points on the graph. Here is a quick solution I hacked up:

    Note: this method might continue infinitely if there exists no path between the two nodes. I haven't tested all cases, YMMV.

    from collections import deque
    
    # a sample graph
      graph = {'A': ['B', 'C','E'],
               'B': ['A','C', 'D'],
               'C': ['D'],
               'D': ['C'],
               'E': ['F','D'],
               'F': ['C']}
    
       def BFS(start, end):
        """ Method to determine if a pair of vertices are connected using BFS
    
        Args:
          start, end: vertices for the traversal.
    
        Returns:
          [start, v1, v2, ... end]
        """
        path = []
        q = deque()
        q.append(start)
        while len(q):
          tmp_vertex = q.popleft()
          if tmp_vertex not in path:
            path.append(tmp_vertex)
    
          if tmp_vertex == end:
            return path
    
          for vertex in graph[tmp_vertex]:
            if vertex not in path:
              q.append(vertex)
    
    0 讨论(0)
  • 2020-11-27 12:37

    What you're trying to do is essentially to find a path between two vertices in a (directed?) graph check out Dijkstra's algorithm if you need shortest path or write a simple recursive function if you need whatever paths exist.

    0 讨论(0)
提交回复
热议问题