-1

My intuition and assumption is whenever we can't use greedy, then A* will be the way to go, but I am not 100% sure. I need some more examples and patterns on how to recognize and spot A* algorithm.

Can someone give some special extreme cases that when you first see it and you know this wouldn't be greedy or it has to be A* without even bother trying.

peter
  • 8,333
  • 17
  • 71
  • 94
  • 1
    What is your greedy algorithm? A* *is* a greedy algorithm. – Peter Alexander Sep 14 '12 at 16:56
  • 1
    If not greedy then `A*` is not at all correct. `A*` is used when you have an estimate of the direction to go and you apply that to `DFS`. There are a lot of problems that can't/shouldn't be solved with DFS and so can't/shouldn't they be solved with `A*`. – Shahbaz Sep 14 '12 at 16:58
  • 1
    @Shahbaz A* is not like DFS, it uses a priority queue so it is more like BFS which uses a simple queue whereas DFS uses a stack. – Abhinav Sarkar Sep 14 '12 at 17:06
  • You may want to clarify your question. Are you trying to determine the difference in behavior between a naive algorithm and A*? Or are you trying to determine failure cases for the naive algorithm? – Chris Mansley Sep 14 '12 at 17:07
  • @ChrisMansley I am just trying to see (just like the title) how to spot the a* algorithm while my intuition was if not greedy then a*, which i can be totally wrong – peter Sep 14 '12 at 17:08
  • @PeterAlexander not quite I think a* is not purely greedy. it has heuristic condition – peter Sep 14 '12 at 17:11
  • @user1389813: What do you mean by purely greedy? How can you do greedy graph search without a heuristic? Please clarify the question. – Peter Alexander Sep 14 '12 at 17:14
  • 1
    greedy is more like comparing the "current" "local" best result at each stage-> whereas a* search you will basically kind of like a brute force(so that's why BFS) check en.wikipedia.org/wiki/A*_search_algorithm . go to the example, if you use greedy it will only take the shortest at each step, but that will not give you the right answer – peter Sep 14 '12 at 17:32
  • You can recognize it by its colorful feathers and distinct chirp. – zmbq Sep 14 '12 at 21:34
  • @AbhinavSarkar, Queue, Priority Queue, Stack, it's the same algorithm. – Shahbaz Sep 15 '12 at 11:50

3 Answers3

6

Normally the term greedy is used to describe algorithms that do not backtrack. These are algorithms that make a choice by maximizing a heuristic (typically a fairly "local" one), and then do not ever revisit that choice. Think of a greedy algorithm as someone who picks a cake and then eats it right away, instead of putting it to one side and investigating whether another cake might be better.

By contrast, A* is a backtracking algorithm: it explores choices, possibly in some depth, but then is capable of abandoning these choices later and trying other possibilities.

So if your problem has "dead ends" (local maxima) where no further progress towards a solution is possible, then greedy algorithms are most likely not suitable. But that doesn't necessarily mean that A* and its variants are your only alternatives. There are many other types of search algorithm that use different techniques to escape from dead ends: simulated annealing, Monte Carlo tree search, tabu search, particle swarm optimization, ...

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • Hilariously, Monte-Carlo Tree Search (MCTS) has exactly the same problem with so called "trap states". While the algorithm does explore, it can be lead astray and "trapped" in sub-optimal states. There was a good paper on this phenomenon at ICAPS 2011. – Chris Mansley Sep 15 '12 at 07:05
3

A classic case where a greedy algorithm fails is a hallway situation that dead-ends near the goal. If you have a heuristic of distance to the goal the greedy algorithm will go down the hallway because those locations are closer to the goal. For example,

_ _ _ _ _ _ _ _ _ _
| . S . . . . | G |
| . _ _ _ _ _ | . |
| . . . . . . . . |
| _ _ _ _ _ _ _ _ |

Notice that the agent must go "away" from the goal (G) to get to the goal from the start (S), which is not what the greedy algorithm would suggest.

Chris Mansley
  • 782
  • 5
  • 12
0

A* is a single-source single-destination shortest path algorithm.

You can use it when You can present the problem at hand as a shortest distance problem, and You can find a heuristic that does not overestimate (for example use the euclidean distance).

maniek
  • 7,087
  • 2
  • 20
  • 43