Greedy algorithms make locally optimal choices at each step to try to find a global optimum. They choose the best option available at each state. For the travelling salesman problem, the greedy approach is to always choose the nearest unvisited city from the current city to build the route. While greedy algorithms are simple to implement, they do not always find the true optimal solution, especially for large complex problems, as they only consider the best choice at each step rather than the overall route.
What is GreedyAlgorithm?
In the hard words: A greedy algorithm is
an algorithm that follows the problem solving
heuristics of making the locally optimal choice at each
stage with the hope of finding a global optimum.
(src: http://en.wikipedia.org/wiki/Greedy_algorithm)
Simplify: Choose the best choice that ‘reachable’ at
current state
3.
Sample Usage ofGreedy
For better explanation we use old simple problem:
Travelling Salesman Problem:
4.
TSP
The Problemis how to travel from city A and visit all
city on the map, then back to city A again.
The rules: you only visit each city once and you can’t
pass through any traversed path.
5.
Solution:
Find theshortest path from city A(start) to any other
city.
A B
Because the nearest city is B, so we go to B
6.
From B,we find any other city but A(because A has
been visited) that has nearest path. So we choose C:
A B
C
Keep tuning on…
7.
From C,we look to nearest city again, but don’t look
for A and B, because both has been visited. So we
choose D.
A B
C D
Soon end…
8.
At thisnode(D), we can’t go to any city, because all
neighbor of D has been visited. We go back to first
city(A).
A B
C D
And that was how to solve TSP problem.
9.
Advantage of Greedy
Greedy is easy to be implemented. Just search the best
choice from the current state that ‘reachable’ (has any
paths or any connections).
In simple case, greedy often give you the best solution.
10.
Drawback of Greedy
In large and complex case, greedy doesn’t always give
you the best solution, because it’s just search and take
the best choice that you can reach from the current
state.
It takes longer time than any other algorithms for big
case of problem