• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retracing steps during search.  RSS feed

 
Angie Hawk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm having some problems with my AI homework. We're studying A* pathfinding and I'm trying to implement a greedy best-first search algorithm before building on it to create the A* algorithm. My algorithm should return a graph from A to B once the algorithm has done its thing.

As a parameter, I supply the search function with a grid (two-dimensional array) to find a path through. Each time the algorithm decides the next step to take, it creates a new node and adds this node to an ArrayList (of type Node). My problems start when my program finds that it's walked down a dead end due to being greedy and needs to retrace steps to try the next quickest route from where it went wrong. How would I implement this behavior? (Recording how the current position was reached and backpedalling until the point where it went bad).

Thanks in advance for your help!
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angie Hawk wrote:My problems start when my program finds that it's walked down a dead end due to being greedy and needs to retrace steps to try the next quickest route from where it went wrong.


It's hard to be specific but you need to save state in some way as you advance. One way is to push state on a stack so you can pop it back when you retreat. The simplest way usually is to use recursion. Then the state will be preserved when you make a new recursive call and it will be restored again when you return from the call.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the usual word in the algorithms field is "backtracking" for that process, not "backpedalling".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!