0% found this document useful (0 votes)
15 views15 pages

Unit III - Session 17

This document discusses greedy algorithms and the knapsack problem. It introduces greedy algorithms and their characteristics. It then describes the fractional knapsack problem and provides an example problem showing how to solve it using a greedy approach by selecting items based on their value to weight ratio.

Uploaded by

eshaanmishra304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Unit III - Session 17

This document discusses greedy algorithms and the knapsack problem. It introduces greedy algorithms and their characteristics. It then describes the fractional knapsack problem and provides an example problem showing how to solve it using a greedy approach by selecting items based on their value to weight ratio.

Uploaded by

eshaanmishra304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT III

GREEDY AND DYNAMIC


PROGRAMMING

Session – 17
Syllabus
⚫ Introduction - Greedy: Huffman Coding - Knapsack
Problem - Minimum Spanning Tree (Kruskals
Algorithm). Dynamic Programming: 0/1 Knapsack
Problem - Travelling Salesman Problem – Multistage
Graph- Forward path and backward path.
Introduction
⚫ Greedy is a strategy that works well on
optimization problems
⚫ Characteristics:
1. Greedy-choice property: A global
optimum can be arrived at by selecting a
local optimum.
2. Optimal substructure: An optimal
solution to the problem contains an
optimal solution to subproblems.
Knapsack Problem
⚫ There are n items in a store.
⚫ For i =1,2, . . . , n, item i has weight wi > 0 and
worth vi > 0. Thief can carry a maximum weight
of W pounds in a knapsack.
⚫ In this version of a problem the items can be
broken into smaller piece, so the thief may
decide to carry only a fraction xi of object i,
where 0 ≤ xi ≤ 1. Item i contributes xiwi to the
total weight in the knapsack, and xivi to the
value of the load.
Applications
⚫ The problem often arises in resource
allocation where there are financial
constraints and is studied in fields such
as combinatory, computer science,
complexity theory, cryptography
and applied mathematics.
Algorithm
Greedy-fractional-knapsack (w, v, W)
For i =1 to n
do x[i] =0
weight = 0
while weight < W
do i = best remaining item
If weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (w - weight) / w[i]
weight = W
return x
Example Problem
⚫ Input: 5 objects, C = 100
W 10 20 30 40 50
V 20 30 66 40 60

Solve the knapsack problem using both


greedy approach
Example Problem
⚫ Input: 5 objects, C = 100
W 10 20 30 40 50
V 20 30 66 40 60
Solution:
⚫ Given Total no of items = 5, sack capacity = 100
Step 1 : Find the Value/weight ratio
Ratio = vi/wi

Object 1 2 3 4 5
Ratio = vi/wi 2 1.5 2.2 1 1.2
⚫ Step 2 : Sort the items according to the ratio and Select
the item according to its highest ratio. First item is
selected

Object 3 1 2 5 4
W 30 10 20 50 40
Ratio = vi/wi 2.2 2 1.5 1.2 1
Selected item 1

⚫ Sack Weight = 30 <100


⚫ Sack value = 2.2 * 30 = 66
⚫ Step 3: Second item is selected
Object 3 1 2 5 4
W 30 10 20 50 40
Ratio = Vi/Wi 2.2 2 1.5 1.2 1
Selected item 1 1
⚫ Sack Weight = 30 +10 = 40 <100
⚫ Sack value = 66 + (2*10) =86
⚫ Step 4: Third item is selected
Object 3 1 2 5 4
W 30 10 20 50 40
Ratio = Vi/Wi 2.2 2 1.5 1.2 1
Selected item 1 1 1

⚫ Sack Weight = 40 +20 = 60 <100


⚫ Sack value = 86 + (1.5*20) = 116
⚫ Step 5: Fourth item is selected
Object 3 1 2 5 4
W 30 10 20 50 40
Ratio = Vi/Wi 2.2 2 1.5 1.2 1
Selected item 1 1 1 1

Sack Weight = 60 +50 = 110 >100 Hence item 4 is selected partially.


Sack Weight = 60+(100-60) = 100 <=100
Sack value = 116 + (1.2*40) = 116+48= 164

⚫ Now the sack is FULL. Hence we stop


⚫ Total selected weight 100 and
Total value = 2.2*30+2*10+1.5*20+1.2*40
⚫ Total value = 164
Analysis
⚫ If the items are already sorted into
decreasing order of vi /wi, then the while-
loop takes a time in O(n);
⚫ Therefore, the total time including the sort
is in O(n log n)
⚫ The greedy algorithm that always selects the most valuable object does not always
find an optimal solution to the Fractional Knapsack problem.

⚫ The greedy algorithm that always selects the lighter object does not always find an
optimal solution to the Fractional Knapsack problem.

⚫ Theorem: The greedy algorithm that always selects the object with better ratio
value/weight always finds an optimal solution to the Fractional Knapsack

problems.

⚫ A greedy algorithm for an optimization problem always makes the choice that
looks best at the moment and adds it to the current sub solution. What’s output at

the end is an optimal solution.

⚫ Greedy algorithms don’t always yield optimal solutions but, when they do, they’re
usually the simplest and most efficient algorithms available.
Worksheet No. 17

You might also like