• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retracing steps during search.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the usual word in the algorithms field is "backtracking" for that process, not "backpedalling".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic