Cannot understand the detailedly the following code

reachmsn at hotmail.com reachmsn at hotmail.com
Wed Apr 9 04:02:10 EDT 2008


On Apr 8, 5:45 pm, "A.T.Hofkamp" <h... at se-162.se.wtb.tue.nl> wrote:
> On 2008-04-08, reach... at hotmail.com <reach... at hotmail.com> wrote:
>
> [deleted a long piece of text by our BDFL about recursive graph path-finding algorithm]
>
> > after first writing the inductive part ... for node in
> > graph[start] ....
> > and then by trial and error put square brackets around path in the
> > Basis part. Can someone please explain how to write this code. Thanks!
>
> The same as any other function.
> (the trick with recursive functions is not to think about recursion. Instead,
> pretend you are calling another function that happens to have the same name.)
>
> As for the actual procedure of writing a function:
>
> First define the input and output parameters/values of the function.
> (ie what goes in, and what comes out)
>
> For recursive functions, there are always two cases, a terminating case, and a
> reduction case. In the first case, you may not use the recursive function, in
> the latter function you should.
> Both cases should use the information available from the input parameters, and
> provide a result that matches with the output requirements of the function. Add
> a if/then/else that distinguishes between what case you have, and you're done.
>
> Sincerely,
> Albert


Ok following these instructions one gets

def find_all_paths(graph, start, end, path=[]):
 path= path+ [start]

 for node in graph[start]:

   find_all_paths(graph, node, end, path)

Now >
> First define the input and output parameters/values of the function.
> (ie what goes in, and what comes out)

Now what will be the output parameters - there is a Return statement.
Input parameters are graph, vertexes start, node, end and path. Also
how would you write the terminating and reduction cases after this.
Actually i'm not clear how to proceed writing this recursive function.
Thanks!



More information about the Python-list mailing list