6

I'm looking for some algorithm which will help me find all possible paths in a graph. Everything I found so far is not fully satisfying.

Let's imagine we have a graph (tree) like this one:
enter image description here

And let's use some algorithm like Breadth-First Search or Depth-First Search. In return we'll get something like

1, 2, 4, (2), 5, (2), 6, (2), (1), 3, 7, 8, (7), 9

Which is how we go through this tree and this is not what I'm looking for. I'd love to get all paths, like:

1
1, 2
1, 2, 4
1, 2, 5
1, 2, 6
1, 3
1, 3, 7
1, 3, 7, 8
1, 3, 7, 9

The thing is that I want just to specify root node and algorithm should be able to provide me with all possible paths of any length.


So far, the simple code I have looks like:

func dfs(_ graph: Graph, source: Node) -> [String] {
    var nodesExplored = [source.label]
    source.visited = true

    for edge in source.neighbors {
        if !edge.neighbor.visited {
            nodesExplored += dfs(graph, source: edge.neighbor)
        }
    }

    return nodesExplored
}
Dominique Fortin
  • 2,212
  • 15
  • 20
cojoj
  • 6,405
  • 4
  • 30
  • 52
  • As you wrote BFS or DFS are the way to go, with a minor modification of updating the paths upon each step and making sure you're not adding the same path more than once (you can use a *Set* to accumulate the Path objects). – Nir Alfasi Dec 20 '16 at 18:34
  • Well, all paths in an undirected or cyclic graph is an infinite set. And if you're actually searching for an algorithm for search in a tree you should probably ask explicitly for such algorithm in your question rather than generally for trees. –  Dec 20 '16 at 20:50
  • @Paul this thing is a tree, but it's possible in my case to have cycles. For example, we can have edge between nodes 2 and 3. – cojoj Dec 21 '16 at 07:51

2 Answers2

0

You can use this algorithm on a binary tree to print out all of its root-to-leaf paths. The function treePaths traverses the nodes depth-first (DFS), pre-order, recursively.

treePaths(root, path[1000], 0) // initial call, 1000 is a path length limit

// treePaths traverses nodes of tree DFS, pre-order, recursively
treePaths(node, path[], pathLen)
    1) If node is not NULL then 
        a) push data to path array: 
            path[pathLen] = node->data.
        b) increment pathLen 
            pathLen++
    2) If node is a leaf node, then print the path array, from 0 to pathLen-1
    3) Else
        a) Call treePaths for left subtree
            treePaths(node->left, path, pathLen)
        b) Call treePaths for right subtree.
            treePaths(node->right, path, pathLen)
jrbedard
  • 3,662
  • 5
  • 30
  • 34
0

Just fold the result and count the new possibilities: Result = 9 (you forgott the path [1] )

n0  n1  n2  n3
1,   2,  4,       +3
    (2), 5,       +1
    (2), 6,       +1
    (2),          +0
(1), 3,  7,  8,   +3
        (7), 9    +1
Jojo
  • 11
  • 3
  • I wonder how would you do it? I can see that my current return has everything I need, but I can't split it. – cojoj Dec 21 '16 at 07:49