• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Sheriff
Posts: 28321
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".
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic