Data Structures: Unit 4 Part 1 Bca, NGMC
Data Structures: Unit 4 Part 1 Bca, NGMC
Unit 4 Part 1
BCA, NGMC
Greedy Method
• A greedy algorithm, as the name
suggests, always makes the choice that seems
to be the best at that moment. This means
that it makes a locally-optimal choice in the
hope that this choice will lead to a globally-
optimal solution.
• The Greedy algorithm has only one shot to
compute the optimal solution so that it never
goes back and reverses the decision.
• Assume that you have an objective
function that needs to be optimized (either
maximized or minimized) at a given point. A
Greedy algorithm makes greedy choices at
each step to ensure that the objective
function is optimized.
Advantages and Disadvantages
• It is easy to come up with a greedy algorithm for a
problem.
• Analyzing the run time for greedy algorithms will
generally be much easier than for other techniques.
• The difficult part is that for greedy algorithms you
have to work much harder to understand
correctness issues. Even with the correct algorithm,
it is hard to prove why it is correct. Proving that a
greedy algorithm is correct is more of an art than a
science.
Traveling Salesperson Problem
• It is focused on optimization. In this context, better
solution often means a solution that is cheaper,
shorter, or faster.
• TSP is a mathematical problem. It is most easily
expressed as a graph describing the locations of a set
of nodes.
• The traveling salesman problem was defined in the
1800s by the Irish mathematician W. R. Hamilton and
by the British mathematician Thomas Kirkman.
Solution
1) Consider city 1 as the
starting and ending point.
2) Generate all (n-
1)! permutations of cities.
3) Calculate cost of every
permutation and keep
track of minimum cost
permutation.
4) Return the permutation
with minimum cost.
Traveling Salesperson Problem