I am new to recursive functions. So, I'm trying to wrap my head around why we use recursive functions and how recursive functions work, and I think I have a pretty good understanding about this.
Two days ago, I tried to solve the problem with the shortest path. I have the following graph (it is in python):
graph = {'a': ['b', 'c'], 'b': ['c', 'd'], 'c': ['d'], 'd': ['c'], 'e': ['f'], 'f': ['c']}
I'm just trying to find a path, not the shortest path. So here is the code:
def find_path(graph,start,end,path=[]): path = path + [start]
My starting vertex is "a" and the ending vertex is "d".
In the fourth line, I simply typed โpathโ to see where the path goes.
On line 17, I also typed โpath,โ again just for verification. And here is the result:
['a'] ['a', 'b'] ['a', 'b', 'c'] ['a', 'b', 'c', 'd'] ['a', 'b', 'c'] ['a', 'b'] ['a'] ['a', 'b', 'c', 'd']
The first four lines of the result are the result of "print (path)" in line 4 of the code. But line 5, 6 and 7 is the result of "print (path)" on line 17 of the code.
My question is why the list of paths decreases one vertex each time?
I tried to find a solution within 2 days. I went to the forums, read the recursion documentation, and watched the video. But no luck.
It would be great if someone could answer.