Greedy Algorithms
Greedy Algorithm Introduction
• "Greedy Method finds out of many options,
but you have to choose the best option."
• In this method, we have to find out the best
method/option out of many present ways.
• In this approach/method we focus on the first
stage and decide the output, don't think
about the future.
A greedy algorithm works if a problem exhibits the
following two properties:
Greedy Choice Property: A globally optimal solution can be
reached at by creating a locally optimal solution. In other
words, an optimal solution can be obtained by creating
"greedy" choices.
Optimal substructure: Optimal solutions contain optimal
subsolutions. In other words, answers to subproblems of an
optimal solution are optimal.
Example:
• machine scheduling
• Fractional Knapsack Problem
• Minimum Spanning Tree
• Huffman Code
• Job Sequencing
• Activity Selection Problem
Steps for achieving a Greedy Algorithm
• Feasible: Here we check whether it satisfies all
possible constraints or not, to obtain at least
one solution to our problems.
• Local Optimal Choice: In this, the choice
should be the optimum which is selected from
the currently available
• Unalterable: Once the decision is made, at
any subsequence step that option is not
altered.
Knapsack Problem-
You are given the following-
• A knapsack (kind of shoulder bag) with limited weight capacity.
• Few items each having some weight and value.
The problem states-
Which items should be placed into the knapsack such that-
• The value or profit obtained by putting the items into the knapsack is maximum.
• And the weight limit of the knapsack does not exceed.
Knapsack Problem Variants-
Knapsack problem has the following two variants-
• Fractional Knapsack Problem
• 0/1 Knapsack Problem
Fractional Knapsack Problem-
In Fractional Knapsack Problem,
• As the name suggests, items are divisible here.
• We can even put the fraction of any item into the
knapsack if taking the complete item is not
possible.
• It is solved using Greedy Method.
Fractional Knapsack Problem Using
Greedy Method-
Fractional knapsack problem is solved using greedy method in the
following steps-
Step-01:
For each item, compute its value / weight ratio.
Step-02:
Arrange all the items in decreasing order of their value / weight
ratio.
Step-03:
Start putting the items into the knapsack beginning from the item
with the highest ratio.Put as many items as you can into the
knapsack.
0/1 Knapsack Problem-
In 0/1 Knapsack Problem,
• As the name suggests, items are indivisible here.
• We can not take the fraction of any item.
• We have to either take an item completely or
leave it completely.
• It is solved using dynamic programming
approach.
• 3. You are given a knapsack that can carry a
maximum weight of 60. There are 4 items with
weights {20, 30, 40, 70} and values {70, 80, 90,
200}. What is the maximum value of the items
you can carry using the knapsack?
a) 160
b) 200
c) 170
d) 90
• 5. What is the time complexity of the brute
force algorithm used to solve the Knapsack
problem?
a) O(n)
b) O(n!)
c) O(2n)
d) O(n3)

Greedy algorithms

  • 1.
  • 2.
    Greedy Algorithm Introduction •"Greedy Method finds out of many options, but you have to choose the best option." • In this method, we have to find out the best method/option out of many present ways. • In this approach/method we focus on the first stage and decide the output, don't think about the future.
  • 3.
    A greedy algorithmworks if a problem exhibits the following two properties: Greedy Choice Property: A globally optimal solution can be reached at by creating a locally optimal solution. In other words, an optimal solution can be obtained by creating "greedy" choices. Optimal substructure: Optimal solutions contain optimal subsolutions. In other words, answers to subproblems of an optimal solution are optimal. Example: • machine scheduling • Fractional Knapsack Problem • Minimum Spanning Tree • Huffman Code • Job Sequencing • Activity Selection Problem
  • 4.
    Steps for achievinga Greedy Algorithm • Feasible: Here we check whether it satisfies all possible constraints or not, to obtain at least one solution to our problems. • Local Optimal Choice: In this, the choice should be the optimum which is selected from the currently available • Unalterable: Once the decision is made, at any subsequence step that option is not altered.
  • 5.
    Knapsack Problem- You aregiven the following- • A knapsack (kind of shoulder bag) with limited weight capacity. • Few items each having some weight and value. The problem states- Which items should be placed into the knapsack such that- • The value or profit obtained by putting the items into the knapsack is maximum. • And the weight limit of the knapsack does not exceed.
  • 7.
    Knapsack Problem Variants- Knapsackproblem has the following two variants- • Fractional Knapsack Problem • 0/1 Knapsack Problem
  • 8.
    Fractional Knapsack Problem- InFractional Knapsack Problem, • As the name suggests, items are divisible here. • We can even put the fraction of any item into the knapsack if taking the complete item is not possible. • It is solved using Greedy Method.
  • 9.
    Fractional Knapsack ProblemUsing Greedy Method- Fractional knapsack problem is solved using greedy method in the following steps- Step-01: For each item, compute its value / weight ratio. Step-02: Arrange all the items in decreasing order of their value / weight ratio. Step-03: Start putting the items into the knapsack beginning from the item with the highest ratio.Put as many items as you can into the knapsack.
  • 10.
    0/1 Knapsack Problem- In0/1 Knapsack Problem, • As the name suggests, items are indivisible here. • We can not take the fraction of any item. • We have to either take an item completely or leave it completely. • It is solved using dynamic programming approach.
  • 11.
    • 3. Youare given a knapsack that can carry a maximum weight of 60. There are 4 items with weights {20, 30, 40, 70} and values {70, 80, 90, 200}. What is the maximum value of the items you can carry using the knapsack? a) 160 b) 200 c) 170 d) 90
  • 12.
    • 5. Whatis the time complexity of the brute force algorithm used to solve the Knapsack problem? a) O(n) b) O(n!) c) O(2n) d) O(n3)