0

It's me trying to understand recursion, the graph is here as support to help me ask my question.

I have this graph :

enter image description here

And I'm using this function to find all the paths possible from one vertex to an other.

def find_all_path(self, start_vertex, end_vertex, path=[]):
    graph = self.__graph_dict
    path = path + [start_vertex]
    if end_vertex == start_vertex:
        return [path]

    paths = []
    for neighbour in graph[start_vertex]:
        if neighbour not in path:
            extended_paths = self.find_all_path(neighbour, end_vertex, path)

            for p in extended_paths:
                paths.append(p)
    return paths

Which would give, from a to d:

[['a', 'b', 'd'], ['a', 'b', 'c', 'e', 'd'], ['a', 'b', 'c', 'e', 'f', 'd'], ['a', 'c', 'b', 'd'], ['a', 'c', 'e', 'd'], ['a', 'c', 'e', 'f', 'd']]

1. Is paths passed by reference? Meaning that, paths is changing throughout each stack even though they're not related.

2. How does paths gets all the path appended to it? For example, a has b and c as neighbours. Let's say it goes through b first, path = [a,b], and then it calls the function again with (b,d,[a,b]) as parameters. It goes again until it reaches the base case where d == d.

At this point, it returns [a,b,d] and so forth until... what? The stack is empty, how does that work?

Obviously, that's the part I don't get, since paths is returned to the top how come all the other path can be appended to it?

Here's me trying to understand this stack appending process :

enter image description here

I think my confusion is related to the flow ("how does the computing process works?"). At first, I though it was the longest path which would be appended as the first element in paths, meaning the process wouldn't end until the longest path is found, but it's not, since [a,b,d] is first.

Community
  • 1
  • 1
Pobe
  • 364
  • 3
  • 13
  • Canonical duplicate: http://stackoverflow.com/q/1132941/3001761. *"Is `paths` passed by reference?"* - see http://stackoverflow.com/q/986006/3001761 – jonrsharpe Jul 02 '15 at 14:34
  • Yes, lists in python are passed by reference. – Rob Foley Jul 02 '15 at 14:35
  • @RobFoley Yes, but how come, as it reaches the end, the `[[a,b,d]]` just don't print? – Pobe Jul 02 '15 at 14:37
  • 1
    Assuming your code starts with a single call to the recursive function, then prints the result, the very first recursive function does not finish until all of its children finish. It builds the stack from the top of the tree down, then works its way back up as each function returns, until the root finally finishes. – Rob Foley Jul 02 '15 at 14:38
  • @RobFoley "As the call made at `a` is not done yet, the `return paths` is not called until every path is found". It's starting to make it's way in my mind. Thanks! :) – Pobe Jul 02 '15 at 14:50
  • 1
    Exactly, even though the recursive call is in the return statement, it still has to follow that to find out what it does, which it keeps doing until it is done recursing. – Rob Foley Jul 02 '15 at 14:57

0 Answers0