4-GREEDY ALGORITHM
4-GREEDY ALGORITHM
1
Department of CSE
Topic:
Greedy Method
AIM OF THE SESSION
INSTRUCTIONAL OBJECTIVES
This Session is designed to:
1.Demonstrate :- Greedy Method.
2.Describe :- The optimization problem, Types in Greedy strategy.
LEARNING OUTCOMES
At the end of this session, you should be able to:
1.Define :- Greedy Method.
2.Describe :- The optimization problem, Types in Greedy strategy
3.Summarize:- Comparision between Divide and Conquer and Greedy
Method.
Introduction to Optimization problem
Optimization problems are those for which the objective is to Maximize or
Minimize some values.
For example,
• Finding the minimum number of colors needed to color a given graph.
• Finding the shortest path between two vertices in a graph
4
Greedy Method
• Greedy method solves problems by making the best choice that seems best at the
particular moment.
• In some real-world games we need to think about the future before taking a decision and
some games require a decision that is best at that moment.
Example : In chess game, every time we make a decision we need to think about the
future consequences as well, where as, in Tennis or volleyball game, we make a decision
which looks good at that situation.
• In computers, to solve some kind of problems, we need an algorithm that need to take a
decision which looks good at that situation and there comes our greedy algorithm.
• Every greedy based problems will be given with a set of inputs and a set of
constraints.
6
Greedy Choice Property
• Global Optimal Solutions will be made with local optimal (Greedy) choices
• In Greedy algorithm, the best choice will be selected at the moment to solve the
sub problem that remains
• The choice of the greedy may depend on the previous choices made but it cannot
depend on future choices or on the solutions to the sub problems
7
• A feasible solution is any subset of original input that satisfies a given set of
constraints.
• Irrevocable - Once the choices are made in a particular step , it can not be
changed in subsequent steps.
8
Algorithm For Greedy Method Control Abstraction
9
Counting Coins Problem :
This problem is to count to a desired value by choosing the least possible coins and the greedy approach forces the
algorithm to pick the largest possible coin. If we are provided coins of ₹ 1, 2, 5 and 10 and we are asked to count
₹18 then the greedy procedure will be,
•Though, it seems to be working fine, for this count we need to pick only 4 coins. But if we slightly change the
problem then the same approach may not be able to produce the same optimum result.
•For the currency system, where we have coins of 1, 7, 10 value, counting coins for value 18 will be absolutely
optimum but for count like 15, it may use more coins than necessary. For example, the greedy approach will use 10
+ 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the same problem could be solved by using only 3 coins (7 + 7 + 1)
•Hence, we may conclude that the greedy approach picks an immediate optimized solution and may fail where
global optimization is a major concern.
10
Greedy Strategy
Subset Paradigm:
• The selection procedure itself based on some optimization measures
Example:
•Knapsack problem
• job sequencing with deadlines
Order Paradigm:
•The selection procedure does not depends on optimization measures. But,
decisions by considering the inputs some particular order.
Example:
•Minimum Spanning tree (Prim’s and Kruskal’s method))
11
Types of Greedy Problems
• Subset Paradigm: To solve a problem (or possibly find the optimal/best solution),
greedy approach generate subset by selecting one or more available choices.
Example:
•Knapsack problem
•Job sequencing with deadlines
Example:
•Single source shortest path problem
12
Three Important Activities
3.Optimality: From the set of feasible solutions, the particular solution which minimizes
or maximizes the given objective function
Applications of Greedy Method
• Knapsack Problem
• Huffman Codes
14
Divide and conquer Greedy Algorithm
Divide and conquer is used to find the solution, it does A greedy algorithm is optimization technique. It tries to
not aim for the optimal solution. find an optimal solution from the set of feasible solutions
DC approach divides the problem into small sub In greedy approach, the optimal solution is obtained from
problems, each sub problem is solved independently and a set of feasible solutions.
solutions of the smaller problems are combined to find
the solution of the large problem.
Sub problems are independent, so DC might solve same Greedy algorithm does not consider the previously solved
sub problem multiple time. instance again, thus it avoids the re-computation.
DC approach is recursive in nature, so it is slower and Greedy algorithms are iterative in nature and hence
inefficient. faster.
Divide and conquer algorithms mostly runs in Greedy algorithms also run in polynomial time but takes
polynomial time less time than Divide and conquer
Example: Example:
Merge sort, Knapsack problem,
15
Quick sort Job scheduling problem
SUMMARY
•Selection: Make decisions based on the best available option at each step without
reconsidering previous choices.
•Efficiency: Efficient for problems where making locally optimal choices leads to an
optimal solution overall, but may not guarantee the best solution in all cases.
SELF-ASSESSMENT QUESTIONS
Which step is crucial in the greedy method to ensure that each chosen option contributes to a feasible solution?
Which step is crucial in the greedy method to ensure that each chosen option contributes to a feasible solution?
Reference Books :
1. Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein., 3rd, 2009, The MIT Press.
2 Algorithm Design Manual, Steven S. Skiena., 2nd, 2008, Springer.
3 Data Structures and Algorithms in Python, Michael T. Goodrich, Roberto Tamassia,
and Michael H. Goldwasser., 2nd, 2013, Wiley.
4 The Art of Computer Programming, Donald E. Knuth, 3rd, 1997, Addison-Wesley
Professiona.
MOOCS :
1. https://www.coursera.org/specializations/algorithms?=
2.https://www.coursera.org/learn/dynamic-programming-greedy-algorithms#modules
THANK YOU