Unit III - Session 17
Unit III - Session 17
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
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
⚫ 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
⚫ 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