I am trying to record a path in a recursive depth first search. What I
want to do is:
each time I recurse, find the list of the next edges to follow (works
fine)
add the next edge to the path (works fine)
recurseively call my method.
When I reach the endpoint in teh graph, I want to compare the cost of
the path I've just built to the cost if the best path found so far, and
return the best one. So, for example, if my best path is:
1,2,3,4
and I find that 1,2,3,6,7,4 is better, then I want to return
1,2,3,6,7,4.
My problem is that if I pass in a path into each call, each part of the
graph that gets explored gets added onto the path. But when I end the
recursion, I want that part of the path to go away so I can add to it
in the next iteration over the edges. ie. assuming that 3 points to 4
and 6, I want the calls to result in:
1,2,3 (current path)
1,2,3,4 (endpoint found, check to see if path is best, if so, return
1,2,3,4. Otherwise, return the original path, or just terminate the
call.)
1,2,3,6 (recursive call)
1,2,3,6,7
1,2,3,6,7,4 (endpoint found, check to see if path is best, if so,
return this)
If I DON'T pass a path in, then a new copy of the path is created each
time, and I never build the whole path.
What can I do?
Thanks!
Harald - 17 Apr 2005 20:10 GMT
> I am trying to record a path in a recursive depth first search. What I
> want to do is:
[quoted text clipped - 7 lines]
> the path I've just built to the cost if the best path found so far, and
> return the best one. So, for example, if my best path is:
Looks to me like you need a least three bookkeeping parameters:
1) cp - current path to be extended until you find a leaf,
2) bestPath - current best path so far,
3) bestPathScore - score of current best path.
When hitting a leaf node, compute the score sc of cp.
If sc is better than bestPathScore,
a) copy cp into bestPath
b) bestPathScore = sc
For (a), if bestPath and cp are for example an ArrayLists, you need
something like
bestPath.clear();
bestPath.addAll(cp);
Do I need to explain why bestPath=cp won't work?
Harald.

Signature
---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
nooneinparticular314159@yahoo.com - 17 Apr 2005 20:56 GMT
Thank you.
Yes. :-) Please explain. :)