You are on page 1of 15

GREEDY TECHNIQUES

BCSE204L

Dr.S.Vaishnavi
SCOPE
Vaishnavi.s@vit.ac.in
Optimization Problems

• The primary idea in the business world is to maximize profit. However,


it's not as simple as trying to sell as many products as possible.
• Often, the answer to maximizing profit is not simply producing and
selling as many products as possible.
• optimization can help find the answer that maximizes profit subject to
the constraints of the real world.
What is Greedy Strategy?
• Greedy algorithms are often used to solve optimal problems (find best
solutions of the problem according to a particular criterion).
There are two critical components of greedy decisions:
• Way of greedy selection. You can select which solution is best at present
and then solve the subproblem arising from making the last selection. The
selection of greedy algorithms may depend on previous selections. But it
cannot depend on any future selection or depending on the solutions of
subproblems. The algorithm evolves in a way that makes selections in a
loop, at the same time shrinking the given problem to smaller subproblems.
• Optimal substructure. You perform the optimal substructure for a problem
if the optimal solution of this problem contains optimal solutions to its
subproblems.
Knapsack problem
• knapsack is like a container or a bag. Suppose we have given some
items which have some weights or profits. We have to put some items
in the knapsack in such a way total value produces a maximum profit.
• For example, the weight of the container is 20 kg. We have to select
the items in such a way that the sum of the weight of items should be
either smaller than or equal to the weight of the container, and the
profit should be maximum.
EXAMPLE-1

M=20, N=3
OBJECT1 OBJECT2 OBJECT3
PROFIT 25 24 15
WEIGHT 18 15 10

METHOD1- greedy about Profit


METHOD2- greedy about weight
METHOD3-greedy about profit/weight
Algorithm
Fractional Knapsack (Array P, Array W, int M)
1. for i =0 to n (n-no.of objects)
2. calculate cost[i] =P[i] / W[i]
3. Sort objects in Descending order of cost
4. for i=1 to n from sorted list
5. if(m>0 && W[i]<=m)
6. m=m-W[i]
7. total=total+P[i]
8. else break
9. If (m>0)
10. total=total+P[i]*(m/W[i])
EXAMPLE-2
M= 15 N=7
OBJECT 1 2 3 4 5 6 7
PROFIT 10 5 15 7 6 18 3
WEIGHT 2 3 5 7 1 4 1
COST 5 1.6 3 1 6 4.5 3
Algorithm
Fractional Knapsack (Array P, Array W, int M)
1. for i =0 to n (n-no.of objects)
2. calculate cost[i] =P[i] / W[i] 1
3. Sort objects in Descending order of cost 2
4. for i=1 to n from sorted list
5. if(m>0 && W[i]<=m) 3
6. m=m-W[i] 4
7. total=total+P[i] 5
8. else break
9. If (m>0) 6
10. total=total+P[i]*(m/W[i]) 7
complexity
• The complexity of the algorithm:
• If using a simple sort algorithm (selection, bubble…) then the complexity of
the whole problem is O(n^2).
• If using quick sort or merge sort then the complexity of the whole problem
is O(nlogn).

Note:If weight is same for all object in any problem then you can sort by
profit.
EXAMPLE-3
M= 15 N=5
OBJECT 1 2 3 4 5
PROFIT 2 28 25 18 9
WEIGHT 1 4 5 3 3
Huffman coding
• Huffman Coding is a technique of compressing data to reduce its size
without losing any of the details. 

• Huffman Coding prevents any ambiguity in the decoding process using


the concept of prefix code.
ALGORITHM
1. GREEDY-HUFFMAN-CODE(C)
2. n=|C| //min_queue.build(C),C-set of all characters
3. make a min value from ‘Q’ (min_queue) with C
4. for i=1 to n-1
5. z = new node // allocate new node
6. z.left = min_queue.extract()
7. z.right = min_queue.extract()
8. z.freq = z.left.freq + z.right.freq
9. min_queue.insert(z)
10. return min_queue.extract()
EXAMPLE1

A B C D
50 40 5 5

EXAMPLE 2

A B C D E F
42 20 5 10 11 12
complexity
The time complexity of the algorithm:
• If using a simple sort algorithm (selection, bubble…) then the
complexity of the whole problem is O(n^2).
• If using min value then the complexity of the whole problem is
O(nlogn).

The Space complexity of the algorithm:


• complexity of the whole problem is O(n).
EXAMPLE 3

A B C D E F
45 13 12 16 9 5

You might also like