Lecture 13: The Knapsack Problem



Similar documents
CSCE 310J Data Structures & Algorithms. Dynamic programming 0-1 Knapsack problem. Dynamic programming. Dynamic Programming. Knapsack problem (Review)

Dynamic Programming Problem Set Partial Solution CMPSC 465

Tutorial 8. NP-Complete Problems

Dynamic Programming. Lecture Overview Introduction

CS473 - Algorithms I

Unit 4: Dynamic Programming

Warshall s Algorithm: Transitive Closure

Lecture Notes on Database Normalization

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

CSC2420 Spring 2015: Lecture 3

Databases -Normalization III. (N Spadaccini 2010 and W Liu 2012) Databases - Normalization III 1 / 31

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

2.1 Complexity Classes

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

Chapter 6: Episode discovery process

Scheduling Shop Scheduling. Tim Nieberg

[Refer Slide Time: 05:10]

Binary Multiplication

Binary Heap Algorithms

On the k-path cover problem for cacti

Change Impact Analysis

MapReduce and Distributed Data Analysis. Sergei Vassilvitskii Google Research

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

CS473 - Algorithms I

Single machine models: Maximum Lateness -12- Approximation ratio for EDD for problem 1 r j,d j < 0 L max. structure of a schedule Q...

Near Optimal Solutions

Loop Invariants and Binary Search

Some programming experience in a high-level structured programming language is recommended.

Notes on Factoring. MA 206 Kurt Bryan

Matrix-Chain Multiplication

How To Develop Software

MEN'S FASHION UK Items are ranked in order of popularity.

MATH1231 Algebra, 2015 Chapter 7: Linear maps

Solutions to Homework 6

Lecture 6 Online and streaming algorithms for clustering

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Classification - Examples

Closest Pair Problem

Four Unsolved Problems in Congruence Permutable Varieties

SCADE SUITE SOFTWARE VERIFICATION PLAN FOR DO-178B LEVEL A & B

INTEGER PROGRAMMING. Integer Programming. Prototype example. BIP model. BIP models

Introduction Solvability Rules Computer Solution Implementation. Connect Four. March 9, Connect Four

POLYNOMIAL RINGS AND UNIQUE FACTORIZATION DOMAINS

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

FUZZY CLUSTERING ANALYSIS OF DATA MINING: APPLICATION TO AN ACCIDENT MINING SYSTEM

CS154. Turing Machines. Turing Machine. Turing Machines versus DFAs FINITE STATE CONTROL AI N P U T INFINITE TAPE. read write move.

Scheduling Home Health Care with Separating Benders Cuts in Decision Diagrams

Fast Optimal Load Balancing Algorithms for 1D Partitioning

Implementation exercises for the course Heuristic Optimization

Approximation Algorithms

Distributed Computing over Communication Networks: Maximal Independent Set

Algorithms and Data Structures

Query Processing C H A P T E R12. Practice Exercises

Tensor Methods for Machine Learning, Computer Vision, and Computer Graphics

A Propositional Dynamic Logic for CCS Programs

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Cost Minimization and the Cost Function

Constructive alignment (CA) for Degree projects intended learning outcomes, teaching & assessment. - or

Network Algorithms for Homeland Security

Graph Security Testing

Determination of the normalization level of database schemas through equivalence classes of attributes

Generating Elementary Combinatorial Objects

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Boolean Algebra Part 1

Introduction to Big Data with Apache Spark UC BERKELEY

Chapter 3: Distributed Database Design

Relational Database Design

CS711008Z Algorithm Design and Analysis

Well-Separated Pair Decomposition for the Unit-disk Graph Metric and its Applications

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Programming Using Python

Regular Languages and Finite Automata

Scheduling Single Machine Scheduling. Tim Nieberg

Welcome to... Problem Analysis and Complexity Theory , 3 VU

Introduction Decomposition Simple Synthesis Bernstein Synthesis and Beyond. 6. Normalization. Stéphane Bressan. January 28, 2015

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

1.2 Solving a System of Linear Equations

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Lecture 7: NP-Complete Problems

Static Load Balancing

Stochastic Inventory Control

LECTURE 5: DUALITY AND SENSITIVITY ANALYSIS. 1. Dual linear program 2. Duality theory 3. Sensitivity analysis 4. Dual simplex method

Design of Relational Database Schemas

Relational Database Design: FD s & BCNF

Integrating Benders decomposition within Constraint Programming

Big Data & Scripting Part II Streaming Algorithms

Lecture 4 Online and streaming algorithms for clustering

G = G 0 > G 1 > > G k = {e}

Notes from Week 1: Algorithms for sequential prediction

Lecture 8 February 4

In Search of Influential Event Organizers in Online Social Networks

Data Structures and Algorithms

1.204 Lecture 10. Greedy algorithms: Job scheduling. Greedy method

Transcription:

Lecture 13: The Knapsack Problem Outline of this Lecture Introduction of the 0-1 Knapsack Problem. A dynamic programming solution to this problem. 1

0-1 Knapsack Problem Informal Description: We have computed data files that we want to store, and we have available bytes of storage. File has size bytes and takes minutes to recompute. We want to avoid as much recomputing as possible, so we want to find a subset of files to store such that The files have combined size at most. The total computing time of the stored files is as large as possible. We can not store parts of files, it is the whole file or nothing. How should we select the files? 2

0-1 Knapsack Problem Formal description: Given two -tuples of positive numbers and and, we wish to determine the subset!#" %$&'( *) (of files to store) that maximizes. subject to,+-,+- 0/ Remark: This is an optimization problem. Brute force: Try all $ possible subsets. Question: Any solution better than the brute-force? 3

Recall of the Divide-and-Conquer 1. Partition the problem into subproblems. 2. Solve the subproblems. 3. Combine the solutions to solve the original one. Remark: If the subproblems are not independent, i.e. subproblems share subsubproblems, then a divideand-conquer algorithm repeatedly solves the common subsubproblems. Thus, it does more work than necessary! Question: Any better solution? Yes Dynamic programming (DP)! 4

The Idea of Dynamic Programming Dynamic programming is a method for solving optimization problems. The idea: Compute the solutions to the subsub-problems once and store the solutions in a table, so that they can be reused (repeatedly) later. Remark: We trade space for time. 5

The Idea of Developing a DP Algorithm Step1: Structure: Characterize the structure of an optimal solution. Decompose the problem into smaller problems, and find a relation between the structure of the optimal solution of the original problem and the solutions of the smaller problems. Step2: Principle of Optimality: Recursively define the value of an optimal solution. Express the solution of the original problem in terms of optimal solutions for smaller problems. 6

The Idea of Developing a DP Algorithm Step 3: Bottom-up computation: Compute the value of an optimal solution in a bottom-up fashion by using a table structure. Step 4: Construction of optimal solution: Construct an optimal solution from computed information. Steps 3 and 4 may often be combined. 7

Remarks on the Dynamic Programming Approach Steps 1-3 form the basis of a dynamic-programming solution to a problem. Step 4 can be omitted if only the value of an optimal solution is required. 8

Developing a DP Algorithm for Knapsack Step 1: Decompose the problem into smaller problems. We construct an array 1 2 34 5 3 6. For " / /, and / /, the entry 1 27 8( 6 will store the maximum (combined) computing time of any subset of files!#" %$&( 9) of (combined) size at most. If we can compute all the entries of this array, then the array entry 1 27 5 6 will contain the maximum computing time of files that can fit into the storage, that is, the solution to our problem. 9

Developing a DP Algorithm for Knapsack Step 2: Recursively define the value of an optimal solution in terms of solutions to smaller problems. Initial Settings: Set 1 2 : 60; for / /, no item 1 2< 8 65; = > for?, illegal Recursive Step: Use 1 27 8( 65; @ ACBED 1 27 = " 6 ( (F 1 27 = " ( = 6.G for " / /, / /. 10

Correctness of the Method for Computing 1 27 8( 6 Lemma: For " / /, / /, 1 27 8( 6H; @ ACBED 1 27 = " : 6 F 1 27 = " = 6 G Proof: To compute 1 2< 8 6 we note that we have only two choices for file : Leave!#" file : The best we can do with files %$&( = " ) and storage limit 1 27 = " 8 6 is. Take file (only possible if I / ): Then we gain of computing time, but have spent bytes of our storage. The best we can do with remaining files!j" %$K = " ) and storage D = G is 1 27 = " = 6. Totally, we get L F 1 27 = " : = 6. Note that if, then F 1 27 = " 8 = 60; = > so the lemma is correct in any case. 11

Developing a DP Algorithm for Knapsack Step 3: Bottom-up computing 1 27 8 6 (using iteration, not recursion). Bottom: 1 2 6H; for all / /. Bottom-up computation: Computing the table using 1 2< 8 6H; @ AMBED 1 2< = " 6 : NF 1 2< = " = 6.G row by row. V[i,w] w=0 1 2 3...... W i= 0 1 2. n 0 0 0 0...... 0 bottom up 12

Example of the Bottom-up computation Let ; " and O V V 1 2 3 4 10 40 30 50 5 4 6 3 P,QSRUT XW 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 10 10 10 10 10 10 2 0 0 0 0 40 40 40 40 40 50 50 3 0 0 0 0 40 40 40 40 40 50 70 4 0 0 0 50 50 50 50 90 90 90 90 Remarks: Y The final output is O P[Z Q\V]TXW ^_V. Y The method described does not tell which subset gives the optimal solution. (It is ` CQ Zba in this example). 13

v The Dynamic Programming Algorithm KnapSack(c QSRdQ Qfe ) g (R W V e O P VCQhRiTW V for ( W ) ; for to (R W V ) e for (R P 3Tj to ) O P,QkRUTW R if ) l mon ` O P Mp qqhrutrq c P 3Tts O P Mp qqhr p R P 3T3T a ; else O P,QkRUTW OIP Mp qqhrut ; return O P Que T ; Time complexity: Clearly, w D G. 14

Constructing the Optimal Solution The algorithm for computing 1 27 8 6 described in the previous slide does not keep record of which subset of items gives the optimal solution. To compute the actual subset, we can add an auxiliary boolean array x#y]y(z*27 8{ 6 which is 1 if we decide to take the -th file in 1 2< 8 6 and 0 otherwise. Question: How do we use all the values x#y]y(z*2< 8 6 to determine the subset of files having the maximum computing time? 15

Constructing the Optimal Solution Question: How do we use the values x#yqy(z 27 8% 6 to determine the subset of items having the maximum computing time? If keep[ 5 } ] is 1, then. We can now repeat this argument for keep[ = " = ]. If keep[ H } ~ ] is 0, the and we repeat the argument for keep[ = " ]. Therefore, the following partial program will output the elements of : ; ; for ( ; downto 1) if (keep2< 8 60; ; " ) output ; i; = 27 6 ; 16

v v v The Complete Algorithm for the Knapsack Problem KnapSack(c QSRdQ Qfe ) g for (R W V to e ) O P VMQhRUTW V ; for ( W to ) for (R W V to e ) if ((R P 3TXj R ) and (c P 3Tts O P Cp qqhr p R P 3T3T O P Mp qqhrut )) g OIP,QhRUTW c P 3Tts O P Cp qqhr p R P 3T3T ; keep P,QƒRUTW ; g else OIP,QhRUTW O P Mp qqhrut v P,QƒRUTW V ; keep ; W e ( W ; for downto P,Q g TJW 1) W if (keep ) output W i; p R P 3T ; return O P Q e T ; 17