0 views

Uploaded by Enmusk

373

373

© All Rights Reserved

- Communication 2
- MIT6_02F12_chap03
- Ppt on Greedy Algo by OP
- Huff Man
- High Resolution ISAR Processing Using Real Data_ISAR-to-SAR Approach
- IEEE Format
- Intro_Arith_coding.pdf
- TMTE2002.pdf
- Multifocus Image Fusion Using Multiresolution Approach with Bilateral Gradient Based Sharpness Criterion
- Fast Pick
- Business English123
- 11ex Asmt Codes and Matrices Crit AD Shorter Version (3)
- booth alg
- Unsupervised Change Detection in SAR Image based on Contour let Fusion and Guided Filter
- юникод
- 1 Morse Mismatches
- DIP 2014(2).doc
- Homework 03
- NI Tutorial 6848 En
- Request Response

You are on page 1of 15

Greedy Algorithms

CSE373: Design and Analysis of Algorithms

The Fractional Knapsack Problem

Given: A set S of n items, with each item i having

bi - a positive benefit

wi - a positive weight

Goal: Choose items with maximum total benefit but with weight at most W.

“knapsack”

Solution: P

• 1 ml of 5 50$

Items:

1 2 3 4 5 • 2 ml of 3 40$

• 6 ml of 4 30$

Weight: 4 ml 8 ml 2 ml 6 ml 1 ml • 1 ml of 2 4$

Benefit: $12 $32 $40 $30 $50 10 ml

•Total Profit:124$

Value: 3 4 20 5 50

($ per ml)

The Fractional Knapsack Problem

Given: A set S of n items, with each item i having

bi - a positive benefit

wi - a positive weight

Goal: Choose items with maximum total benefit but with weight at most W.

If we are allowed to take fractional amounts, then this is the fractional knapsack

problem.

In this case, we let xi denote the amount we take of item i

Objective: maximize

Constraint: b ( x / w )

iS

i i i

x

iS

i W ,0 xi wi

The Fractional Knapsack Algorithm

Greedy choice: Keep taking item with highest value (benefit to weight ratio)

Since

b ( x / w ) (b / w ) x

iS

i i i

iS

i i i

Algorithm fractionalKnapsack(S, W)

Input: set S of items w/ benefit bi and weight wi; max. weight W

Output: amount xi of each item i to maximize benefit w/ weight at most W

xi 0

vi bi / wi {value}

w0 {total weight}

while w < W

remove item i with highest vi

xi min{wi , W - w}

w w + min{wi , W - w}

The Fractional Knapsack Algorithm

Running time: Given a collection S of n items, such that each item i has a benefit

bi and weight wi, we can construct a maximum-benefit subset of S, allowing for

fractional amounts, that has a total weight W in O(nlogn) time.

Use heap-based priority queue to store S

Removing the item with the highest value takes O(logn) time

In the worst case, need to remove all items

Huffman Codes

Widely used technique for data compression

Fixed-Length Codes

E.g.: Data file containing 100,000 characters

a b c d e f

Frequency (thousands) 45 13 12 16 9 5

3 bits needed

Huffman Codes

Idea:

Use the frequencies of occurrence of characters to build a

optimal way of representing each character

a b c d e f

Frequency (thousands) 45 13 12 16 9 5

Variable-Length Codes

E.g.: Data file containing 100,000 characters

a b c d e f

Frequency (thousands) 45 13 12 16 9 5

Assign short codewords to frequent characters and long

codewords to infrequent characters

a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100

(45 1 + 13 3 + 12 3 + 16 3 + 9 4 + 5 4) 1,000

= 224,000 bits

Prefix Codes

Prefix codes:

Codes for which no codeword is also a prefix of some other

codeword

codes

We will restrict our attention to prefix codes

Encoding with Binary Character Codes

Encoding

Concatenate the codewords representing each character in the

file

E.g.:

a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100

Decoding with Binary Character Codes

Prefix codes simplify decoding

No codeword is a prefix of another the codeword that begins

an encoded file is unambiguous

Approach

Identify the initial codeword

Translate it back to the original character

Repeat the process on the remainder of the file

E.g.:

a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100

001011101 =

0 0 101 1101 = aabe

Constructing a Huffman Code

A greedy algorithm that constructs an optimal prefix code called a

Huffman code

Assume that:

C is a set of n characters

Each character has a frequency f(c)

The tree T is built in a bottom up manner

Idea:

Start with a set of |C| leaves f: 5 e: 9 c: 12 b: 13 d: 16 a: 45

At each step, merge the two least frequent objects: the frequency of the new

node = sum of two frequencies

Use a min-priority queue Q, keyed on f to identify the two least frequent

objects

Constructing a Huffman Code

Alg.: HUFFMAN(C)

Running time: O(nlgn)

1. n C

2. Q C

3. for i 1 to n – 1 O(n)

4. do allocate a new node z

5. left[z] x EXTRACT-MIN(Q)

6. right[z] y EXTRACT-MIN(Q) O(lgn)

7. f[z] f[x] + f[y]

8. INSERT (Q, z)

9. return EXTRACT-MIN(Q)

Example

f: 5 e: 9 c: 12 b: 13 d: 16 a: 45 c: 12 b: 13 14 d: 16 a: 45

0 1

f: 5 e: 9

14 d: 16 25 a: 45 25 30 a: 45

0 1 0 1 0 1 0 1

f: 5 e: 9 c: 12 b: 13 c: 12 b: 13 14 d: 16

0 1

f: 5 e: 9

a: 45 55 0 100 1

0 1

a: 45 55

25 30 0 1

0 1 0 1

25 30

c: 12 b: 13 14 d: 16 0 1 0 1

0 1 c: 12 b: 13 d: 16

14

f: 5 e: 9 0 1

f: 5 e: 9

- Communication 2Uploaded byKheianne Daveigh
- MIT6_02F12_chap03Uploaded bygautruc408
- Ppt on Greedy Algo by OPUploaded byapi-3844034
- Huff ManUploaded byKailash Pathak
- High Resolution ISAR Processing Using Real Data_ISAR-to-SAR ApproachUploaded byWesley George
- IEEE FormatUploaded bystalinrajesh143
- Intro_Arith_coding.pdfUploaded byState Iulian Horia
- TMTE2002.pdfUploaded byLazarescu Simona
- Multifocus Image Fusion Using Multiresolution Approach with Bilateral Gradient Based Sharpness CriterionUploaded byCS & IT
- Fast PickUploaded byVuong Luu
- Business English123Uploaded byjo1641
- 11ex Asmt Codes and Matrices Crit AD Shorter Version (3)Uploaded byNopakit Lerthirunvibul
- booth algUploaded bySivajisadhana Sadhana
- Unsupervised Change Detection in SAR Image based on Contour let Fusion and Guided FilterUploaded byIJIRST
- юникодUploaded by0l0l0otake
- 1 Morse MismatchesUploaded byAlfredo Ayala Ortega
- DIP 2014(2).docUploaded byMdSalahUddinYusuf
- Homework 03Uploaded byPeerapong Benjarungroj
- NI Tutorial 6848 EnUploaded byLuis Martínez Hernández
- Request ResponseUploaded byUriel Ugalde
- module9-0.pdfUploaded byAriel Mendoza
- ChannelCoding_TurboCode.pdfUploaded byThương HD
- Mikro p3 AdcUploaded byAsfa Eka Prasetya
- hexadecimalUploaded byapi-297910907
- Curren Symbols.pdfUploaded byCITRIX
- 01 SimplexUploaded bygabboudeh
- r5321502-Mathematical Modeling & SimulationUploaded byIbmWasuser
- Control System LoopshapingUploaded byBlue
- LTI SystemsUploaded byMuhammad Atiq Ur Rehman 22-FET/PHDEE/S19
- zqa7Uploaded byharde2lah

- List_of_vitamins.docUploaded byEnmusk
- Last ClassUploaded byEnmusk
- L16 Graph Part05Uploaded byEnmusk
- L14 Graph Part03Uploaded byEnmusk
- Lec 19 Diabetes, Lipid ProfileUploaded byEnmusk
- L09 DynamicProgramming_Part03Uploaded byEnmusk
- L15 Graph Part04Uploaded byEnmusk
- L13 Graph Part02Uploaded byEnmusk
- Lec 1,2 Intro to Biology and LifeUploaded byEnmusk
- L12 Graph Part01Uploaded byEnmusk
- L11 DynamicProgramming_Part05Uploaded byEnmusk
- L10 DynamicProgramming_Part04Uploaded byEnmusk
- L08 DynamicProgramming_Part02Uploaded byEnmusk
- L01 AlgoAnalysisUploaded byEnmusk
- L07 DynamicProgramming_Part01Uploaded byEnmusk
- L04 DivideConquer_Part03Uploaded byEnmusk
- L03 DivideConquer_Part02Uploaded byEnmusk
- L05 Greedy Part01Uploaded byEnmusk
- L02 DivideConquer_Part01Uploaded byEnmusk
- 373 Assignment 1Uploaded byEnmusk
- Assignment 5CapacitorUploaded byEnmusk
- Lec 16 DigestionUploaded byEnmusk
- Lec 1,2,3 RMUploaded byEnmusk
- Lec 17 Circulatory SystemUploaded byEnmusk
- Lec 15 HomeostasisUploaded byEnmusk
- Lec 18 Respiratory, Excretory SystemUploaded byEnmusk
- Lec 3 Classification of Living ThingsUploaded byEnmusk
- Lec 4,5 Chemistry of LifeUploaded byEnmusk
- Lec 1,2,3 RM.pdfUploaded byEnmusk

- Compensation Plan SyntekUploaded bysappiah57
- Lecture 1 - Linear and Non-Linear Wavelet ApproximationUploaded byJuan Manuel Mauro
- unilever-siebel-casestudyUploaded byajai
- EasyFind ManualUploaded byscribbler72
- Excel GuideUploaded byArun Naik
- RTS Series Down-Hole Pressure Gauge User ManualUploaded byshareng
- DspUploaded bygmujtaba
- SAP Community Network Forums_ Sales Order Open Quantity ..Uploaded byPrasanna Kumar
- Android Secret CodesUploaded byGaurav Raina
- Network Protection and Info Security PolicyUploaded byAnzaro Nazaruddin
- TutorialUploaded bymariana
- Temporal Logic in Information SystemsUploaded bySamer Abu Gahgah
- MAUPUploaded bywittyandy
- Unit 1finalUploaded byPreethish Janardhanan
- Elasticsearch PyUploaded byRicardo Bolanos
- Error Code ListUploaded byAndrei Filip
- BNA Telecom Outlook - 1.7.16Uploaded byAnonymous ykgk2UhhK
- LectureUploaded byArun Yadav
- Csharp Basics Reference SheetUploaded byHarish
- ComputingUploaded byMarsel Nakuçi
- A Method for Predicting Drawdown.pdfUploaded bynoraafshin
- Ray tracing and velocity estimationUploaded byDebora Cores Carrera
- Mudasir CvUploaded byRana Qamar
- LTE University Drop Call 2Uploaded bybfeas
- 2Uploaded byDavid Ribeiro
- IntroductionUploaded byPrateek Goyal
- 3 Major Reasons to Migrate to BW 7.4 on HANAUploaded bychandhureddy
- math quizUploaded bymahabalu123456789
- Letter of Agreement February 13, 2009 Mr. Randall SmithUploaded byapi-10533238
- Peter j Carroll Psybermagick PDFUploaded bycesargnomo