9 views

Uploaded by Pulkit Singh

- Scheduling
- ILOG-1
- Johnson’s Algorithm
- ES 84 Project
- D1,L9 Solving Linear Programming problems.ppt
- Analysis of Evolutionary Algorithms on the One-Dimensional Spin Glass with Power-Law Interactions
- unit 1 tracker 4w v2
- at wooord
- unit2_Approximations and Errors.pdf
- BITCOIN.docx
- LPinMatlab2012
- XII Record Programs 2011 2012
- Linear Programming Formulation Problems Set 3
- session 4
- 9A05403 Design & Analysis of Algorithms
- lesing
- Greedy Technique
- Pre-3A
- Newton Raphson Exercise
- Lecture9 - Dynamic Programming

You are on page 1of 24

V.S. Subrahmanian University of Maryland

© 5/7/2002 V.S. Subrahmanian

1

Knapsack Problem

• You have a knapsack that has capacity (weight) C. • You have several items I1,…,In. • Each item Ij has a weight wj and a benefit bj. • You want to place a certain number of copies of each item Ij in the knapsack so that:

– The knapsack weight capacity is not exceeded and – The total benefit is maximal.

© 5/7/2002 V.S. Subrahmanian

2

Subrahmanian 3 Benefit 60 75 90 .Example Item A B C Weight 2 3 4 Capacity = 5 © 5/7/2002 V.S.

Subrahmanian 4 . • We want to find (in the example) f(5).Key question • Suppose f(w) represents the maximal possible benefit of a knapsack with weight w. • Is there anything we can say about f(w) for arbitrary w? © 5/7/2002 V.S.

Key observation • To fill a knapsack with items of weight w.S. • Consider the knapsack with weight (w.wi). • Suppose the last such item was Ij with weight wi and benefit bi. we chose to add Ij to this knapsack because of all items with weight wi or less. © 5/7/2002 V. Clearly. we must have added items into the knapsack in some order. Ij had the max benefit bi. Subrahmanian 5 .

© 5/7/2002 V. f(w) = MAX { bj + f(w-wj) | Ij is an item}.S. Subrahmanian 6 . • This gives rise to an immediate recursive algorithm to determine how to fill a knapsack.Key observation • Thus.

Example Item Weight Benefit A B C 2 3 4 © 5/7/2002 V. Subrahmanian 60 75 90 7 .S.

Subrahmanian 8 . Why? The knapsack with capacity 0 can have nothing in it. • f(1) = 0. © 5/7/2002 V. There is no item with weight 1. f(1) • f(0) = 0.S.f(0).

• Choose A. Subrahmanian 9 . There is only one item with weight 60.S.f(2) • f(2) = 60. © 5/7/2002 V.

f(3) • f(3) = MAX { bj + f(w-wj) | Ij is an item}. 75 + 0 } = 75. 75 + f(3-3)} = MAX { 60 + 0. Subrahmanian 10 . Choose B. = MAX { 60+f(3-2). © 5/7/2002 V.S.

75.S. 90+f(4-4)} = MAX { 60 + 60. 75 + f(4-3). 90 + f(0)} = MAX { 120.f(4) • f(4) = MAX { bj + f(w-wj) | Ij is an item}. = MAX { 60 + f(4-2). Choose A. 90} =120. Subrahmanian 11 . © 5/7/2002 V. 75 + f(1).

75 + f(2). 75 + f(5-3). 75 + 60.f(5) • f(5) = MAX { bj + f(w-wj) | Ij is an item}. = MAX { 60 + f(5-2). Subrahmanian 12 .S. © 5/7/2002 V. 90 + f(1)} = MAX { 60 + 75. 90+f(5-4)} = MAX { 60 + f(3). Choose A or B. 90+0} = 135.

• Two possible optimal solutions: – Choose A during computation of f(5). © 5/7/2002 V. • Both solutions coincide. Choose B in computation of f(3). – Choose B during computation of f(5). Subrahmanian 13 .Result • Optimal knapsack weight is 135. Choose A in computation of f(2). Take A and B.S.

Subrahmanian 14 . benefit 120.benefit 100 – Item 3 has weight 30.S. benefit 60 – Item 2 has weight 20. © 5/7/2002 V.Another example • Knapsack of capacity 50. • 3 items – Item 1 has weight 10.

Subrahmanian 15 . © 5/7/2002 V..S..f(0).f(9) • All have value 0..

• Choose Item 1..f(19) • All have value 10.. © 5/7/2002 V.f(10). Subrahmanian 16 .S..

.f(20). Subrahmanian 17 .. © 5/7/2002 V. 100+0} =120.S. Choose Item 1.. 100 + f(0) } = MAX { 60+60.f(29) • F(20) = MAX { 60 + f(10).

120 + f(0) } = MAX { 60 + 120.f(39) • f(30) = MAX { 60 + f(20). © 5/7/2002 V.….f(30). 100 + f(10). 120+0} = 180 Choose item 1.S. Subrahmanian 18 . 100+60.

© 5/7/2002 V.S. Subrahmanian 19 .f(49) • F(40) = MAX { 60 + f(30).…. 100+120. 120 + f(10)} = MAX { 60 + 180. 120 + 60} = 240.f(40). Choose item 1. 100 + f(20).

120 + f(20) } = MAX { 60 + 240. Choose item 1.f(50) • f(50) = MAX { 60 + f(40). 120 + 120} = 300. 100 + f(30). © 5/7/2002 V. 100+180.S. Subrahmanian 20 .

Subrahmanian 21 . Has the same constraint as 0/1 knapsack.Knapsack Problem Variants • 0/1 Knapsack problem: Similar to the knapsack problem except that for each item. • Fractional knapsack problem: You can take a fractional number of items. © 5/7/2002 V. Can solve using a greedy algorithm.S. only 1 copy is available (not an unlimited number as we have been assuming so far).

w] = best solution having weight exactly w after considering j elements • If j >= C: f[j.w-wj]+bj } Replace an item with the k’th item © 5/7/2002 V. Subrahmanian 22 .w].S.0/1 Knapsack • f[j.w] • Otherwise: f[j.w] = f[j-1.w] = Best solution after adding k-1 items • MAXremaining items { – f[j-1. – f[j-1.

0/1 Knapsack Algorithm For w = 0 to C do f[w]=0. (* initialize *) For j=1 to n do for w=C downto wj do if f[w-wj] + bj > f[w] then f[w] = f[w-wj] + bj O(n.S. Subrahmanian 23 .C) algorithm © 5/7/2002 V.

let rj = bj/wj.S. © 5/7/2002 V. • Sort the items in descending order of rj • Pack the knapsack by putting as many of each item as you can walking down the sorted list.Fractional knapsack • Much easier • For item Ij. This gives you the benefit per measure of weight. Subrahmanian 24 .

- SchedulingUploaded byPunith Kp
- ILOG-1Uploaded byDavid
- Johnson’s AlgorithmUploaded byumar
- ES 84 ProjectUploaded byKian Dave Bucol
- D1,L9 Solving Linear Programming problems.pptUploaded bymokhtarppg
- Analysis of Evolutionary Algorithms on the One-Dimensional Spin Glass with Power-Law InteractionsUploaded byMartin Pelikan
- unit 1 tracker 4w v2Uploaded byapi-373547874
- at wooordUploaded byNurul A T Fadhly
- unit2_Approximations and Errors.pdfUploaded byTuanbv93qng
- BITCOIN.docxUploaded byarunkumar
- LPinMatlab2012Uploaded byPreksha Pandey
- XII Record Programs 2011 2012Uploaded byLakshmi Manoharan
- Linear Programming Formulation Problems Set 3Uploaded byamit056
- session 4Uploaded byJoel Fdez
- 9A05403 Design & Analysis of AlgorithmsUploaded bysivabharathamurthy
- lesingUploaded bysarathsreekumar
- Greedy TechniqueUploaded bygorakhnnath
- Pre-3AUploaded bySudip799
- Newton Raphson ExerciseUploaded byk yong
- Lecture9 - Dynamic ProgrammingUploaded byMrZaggy
- Homework 3Uploaded bygedeus8072
- FFT 100 yrUploaded byKarl Todd
- NLMF(I,Options)Uploaded byAkash Agrawal
- Merge Sort.pdfUploaded byboss199410
- Write-UpUploaded byRevathi Prasad
- cctutUploaded byLORDNECROS
- Hsn & Maio(Good)Uploaded byYogesh Kumar
- RecurrencesUploaded byanandan0
- The Discrete Fourier TransformUploaded byapi-19951707
- An algorithm using GARCH process , Monte-Carlo simulation and wavelets analysis for stock predictionUploaded bygiovanis3202

- Premises as per GMPUploaded bydivvela
- Aerocad InfoUploaded byEdgar Guzman
- amphinalUploaded byapi-221753872
- Alarm InterpretationUploaded byHussam Safaa
- M1 Geology of Sandstone in Ecuador 2017Uploaded byEduardo Benalcazar
- TheisMatchFt comUploaded byBarbu Ionel
- SATII Math2 SummaryUploaded byTulong Zhu
- Cad SyllabusUploaded bymuru0105
- Francesco Costrini CV 2017 GiugnoUploaded byFrancesco Costrini
- Telenor HistoryUploaded byMoeen Ahmed
- Evidencia 3 de Actividad 24Uploaded byingesdac
- Morrell v Workers Saving Loan BankUploaded byPersephone West
- Uses and Grats 2.0Uploaded byShane Duffner
- Comm140-i.korbaki (Credits 2)Uploaded bychartemis
- Productionguide OnionUploaded byJonas Reduta Cabacungan
- IDM96 (N00407) EnglishUploaded byDiego Cordova
- hw3_solnUploaded byWazy Rahman
- Density Based Traffic Control System(09261A0215) (1)Uploaded byDarmula Anil
- Pneumatic Sheet Metal Cutting MachineUploaded bysuraj dhulannavar
- 3. Ijmcar - Influence of Heat and Mass Transfer on ViscoUploaded byTJPRC Publications
- 4.nodal and mesh an(done)(1).docUploaded byMurad
- Cost of QualityUploaded bySachin Methree
- Cbse Grade 10 Science WorksheetsUploaded byDavid
- Sandoa Acoustic TelemetryUploaded bygsmsby
- Complementary Technical Information GuideUploaded byAlex Georgescu
- Nature of Indian EconomyUploaded byEktaa Bhusari
- 12. IEEE 1149.1 (JTAG) Boundary-Scan Testing for the Cyclone III Device FamilyUploaded bySimon Wang
- AL1 Chapter 10Uploaded byjanet_tran
- Subnetting_White_Paper.docxUploaded byOmingo Kingi
- PrintUploaded bySagar Bisht