0% found this document useful (0 votes)
52 views6 pages

Analysis and Design of Algorithm: B. Tech. (CSE), VII Semester Bhagyashree Naruka

The document discusses two algorithms: the fractional knapsack algorithm and Huffman coding. The fractional knapsack algorithm uses a greedy approach to solve the knapsack problem by sorting items by their profit-to-weight ratio and filling the knapsack accordingly. Huffman coding assigns variable-length binary codes to messages based on their frequency, with more frequent messages getting shorter codes, to minimize transmission costs. An example shows symbols assigned Huffman codes based on their frequencies.

Uploaded by

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

Analysis and Design of Algorithm: B. Tech. (CSE), VII Semester Bhagyashree Naruka

The document discusses two algorithms: the fractional knapsack algorithm and Huffman coding. The fractional knapsack algorithm uses a greedy approach to solve the knapsack problem by sorting items by their profit-to-weight ratio and filling the knapsack accordingly. Huffman coding assigns variable-length binary codes to messages based on their frequency, with more frequent messages getting shorter codes, to minimize transmission costs. An example shows symbols assigned Huffman codes based on their frequencies.

Uploaded by

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

ASET

Analysis and Design of Algorithm


B. Tech. (CSE), VII Semester
Bhagyashree Naruka

ASET

knapsack algorithm by greedy


method (Fractional Knapsack)

The knapsack problem

ASET

n objects, each with a weight wi > 0


a profit pi > 0
capacity of knapsack: M

pi xi

1 i n

Maximize
w i xi M
1 i n
Subject to
0 xi 1, 1 i n
3 -3

The knapsack algorithm(FractionalASET


Knapsack)
The greedy algorithm:
Step 1: Sort pi/wi into nonincreasing order.
Step 2: Put the objects into the knapsack according
to the sorted sequence as possible as we can.

e. g.
n = 3, M = 20, (p1, p2, p3) = (25, 24, 15)
(w1, w2, w3) = (18, 15, 10)
Sol: p1/w1 = 25/18 = 1.32
p2/w2 = 24/15 = 1.6
p3/w3 = 15/10 = 1.5
Optimal solution: x1 = 0, x2 = 1, x3 = 1/2

total profit = 24 + 7.5 = 31.5


3 -4

Huffman codes

ASET

In telecommunication, how do we represent a


set of messages, each with an access
frequency, by a sequence of 0s and 1s?
To minimize the transmission and decoding
costs, we may use short strings to represent
more frequently used messages.
This problem can by solved by using an
extended binary tree which is used in the 2-way
merging problem.
3 -5

Example of Huffman algorithm

ASET

Symbols: A, B, C, D, E, F, G
freq. : 2, 3, 5, 8, 13, 15, 18
Huffman codes:
A: 10100 B: 10101 C: 1011
D: 100
E: 00
F: 01
G: 11

A Huffman code Tree


3 -6

You might also like