Math for Liberal Studies.  The knapsack problem is a variation of the bin packing problems we have been discussing  This time, there is just one bin.

Slides:



Advertisements
Similar presentations
Math for Liberal Studies. There is a list of numbers called weights These numbers represent objects that need to be packed into bins with a particular.
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta
Greedy Algorithms.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Knapsack Problem: Greedy vs. Brute Force pp (Section 7.6)
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
CS223 Advanced Data Structures and Algorithms 1 Greedy Algorithms Neil Tang 4/8/2010.
Greedy Algorithms Clayton Andrews 2/26/08. What is an algorithm? “An algorithm is any well-defined computational procedure that takes some value, or set.
Analysis of Algorithms
Games of Prediction or Things get simpler as Yoav Freund Banter Inc.
Operations Research Assignment Problem.
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
Week 2: Greedy Algorithms
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Lecture 7: Greedy Algorithms II
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a.
Objective A: Convert from one American unit of length to another.
Binary “There are 10 types of people in the world… those that understand binary and those that don’t.”
Improved results for a memory allocation problem Rob van Stee University of Karlsruhe Germany Leah Epstein University of Haifa Israel WADS 2007 WAOA 2007.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Shortest Path. Dijkstra’s Algorithm finds the shortest path from the start vertex to every other vertex in the network. We will find the shortest path.
Scott Perryman Jordan Williams.  NP-completeness is a class of unsolved decision problems in Computer Science.  A decision problem is a YES or NO answer.
Bean Bags By Mrs. Steiner MP3 1. Underline Key Information Lima beans come in 3-pound and 5-pound bags which cost $1.15 and $1.63 respectively. How many.
1.8: Intro to Equations Equation: A mathematical sentence that uses the equal ( = ) sign. Ex: 3x=12, -1(x + 5) = 8, Open Sentence: An equation.
Multiplying and Dividing Decimals by 10, 100, and 1,000
Metric to English Standard and back again. Units and Standards What is a standard? ___________________ ___________________ ___________________ Suppose.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Ordering Decimals. Learning Goals LG: Demonstrate the ability to order decimals in an accurate order Kid Friendly: Show that you understand how to determine.
The bin packing problem. For n objects with sizes s 1, …, s n where 0 < s i ≤1, find the smallest number of bins with capacity one, such that n objects.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the.
Greedy Algorithms Analysis of Algorithms.
Lesson 16 Solving Problems With Measures. Example 1  Jeff cut a string 4 feet long into three equal pieces. How long are the pieces?  Strategy: Change.
Objective: Students will change units within the metric system (4-9).
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Units of Weight.
Dynamic Programming (optimization problem) CS3024 Lecture 10.
Scientific Notation Notes Physical Science (Freshman Physics)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Introduction to Algorithms: Brute-Force Algorithms.
Graphs – Part III CS 367 – Introduction to Data Structures.
CSC317 Greedy algorithms; Two main properties:
Bin Packing First fit decreasing algorithm
Bin Packing First fit algorithm
Topic 25 Dynamic Programming
Algorithm Design Methods
CS4335 Design and Analysis of Algorithms/WANG Lusheng
Shortest Path.
Shortest Path.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Bin Packing First fit decreasing algorithm
Bin packing – First fit algorithm
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Solving Quadratic Equations by Factoring
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Objective: To solve multi-step equations.
Bin Packing First fit decreasing algorithm
Shortest Path.
Lecture 4 Dynamic Programming
Bin Packing First fit algorithm
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Presentation transcript:

Math for Liberal Studies

 The knapsack problem is a variation of the bin packing problems we have been discussing  This time, there is just one bin  The weights have values, and you can use each weight as many times as you want  The goal is to pack the bin so that it has the highest possible value

 In this example, we have 4 weights, and our bin (“knapsack”) can hold 10 pounds  We label the weights to make it easier

 How should we pack our knapsack?  There are many possible solutions  This one has a total value of $15

 How should we pack our knapsack?  There are many possible solutions  This one also has a total value of $15

 Notice that we have 1 lb of space left over  Since none of our weights weigh 1 lb, this space must remain empty

 We will use a recursive algorithm to find the best solution to this problem  Think about the last weight that we would pack into the best possible 10 lb knapsack

 The optimal 10 lb knapsack will look like one of these:  But how do we determine, for example, what the optimal 8 lb knapsack is?

 To find the optimal 8 lb knapsack, we will use the same idea  The best 8 lb knapsack is one of these:  The best 6 lb knapsack + A  The best 5 lb knapsack + B  The best 4 lb knapsack + C  The best 3 lb knapsack + D

 If we keep going backwards like this, our knapsacks can’t keep getting smaller forever  Eventually we will be asking ourselves what the optimal 0 lb knapsack is

 If a knapsack can hold zero pounds, then it has to be empty!  This empty knapsack becomes our starting point

 Make a chart that lists capacities from 0 up through the capacity of the knapsack  Starting with 0, 1, 2, etc., determine the optimal knapsack of each capacity until you reach the capacity of the original knapsack

 We start off by making our chart  The 0-capacity knapsack must be empty CapacityContentsValue 0empty$

 Since our smallest weight is 2 lbs, the 1-lb knapsack must also be empty CapacityContentsValue 0empty$0 1empty$

 Once we reach a capacity of 2, the only way to pack our knapsack is to use a single “A”, worth $3 CapacityContentsValue 0empty$0 1empty$0 2A$

 At capacity 3, we have a choice:  pack A with 1 lb left over  pack B  Since B is worth $4, this is the better option CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$

 At capacity 4, we start having more options:  optimal 2-lb knapsack + A  optimal 1-lb knapsack + B  optimal 0-lb knapsack + C  Note we still can’t fit a D CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4??

 optimal 2-lb knapsack + A  This is two A’s, worth $6  optimal 1-lb knapsack + B  This is just one B, worth $4  optimal 0-lb knapsack + C  This is one C, worth $7  The third option is best CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$

 Capacity 5 options:  optimal 3 + A  optimal 2 + B  optimal 1 + C  optimal 0 + D CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5??

 Capacity 5 options:  optimal 3 + A ▪ This is B & A, worth $7  optimal 2 + B  optimal 1 + C  optimal 0 + D CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5??

 Capacity 5 options:  optimal 3 + A ▪ This is B & A, worth $7  optimal 2 + B ▪ This is A & B, worth $7  optimal 1 + C  optimal 0 + D CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5??

 Capacity 5 options:  optimal 3 + A ▪ This is B & A, worth $7  optimal 2 + B ▪ This is A & B, worth $7  optimal 1 + C ▪ This is C, worth $7  optimal 0 + D CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5??

 Capacity 5 options:  optimal 3 + A ▪ This is B & A, worth $7  optimal 2 + B ▪ This is A & B, worth $7  optimal 1 + C ▪ This is C, worth $7  optimal 0 + D ▪ This is D, worth $8 CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5??

 Capacity 6 options:  optimal 4 + A ▪ This is C & A, worth $10  optimal 3 + B ▪ This is two B’s, worth $8  optimal 2 + C ▪ This is A & C, worth $10  optimal 1 + D ▪ This is D, worth $8 CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5D$8 6??

 Capacity 7 options:  optimal 5 + A ▪ This is D & A, worth $11  optimal 4 + B ▪ This is C & B, worth $11  optimal 3 + C ▪ This is B & C, worth $11  optimal 2 + D ▪ This is A & D, worth $11 CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5D$8 6A, C$10 7??

 Since all of our options at capacity 7 had the same value, we can put any of them in our table  We’ll use A & D, but it would be equally correct to use B & C CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5D$8 6A, C$10 7A, D$

 We determine the best 8-lb and 9-lb knapsacks in the same way CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5D$8 6A, C$10 7A, D$11 8C, C$14 9C, D$15 10

 Capacity 10 options:  optimal 8 + A ▪ This is A and two C’s, worth $17  optimal 7 + B ▪ This is A, B, and D, worth $15  optimal 6 + C ▪ This is A and two C’s, worth $17  optimal 5 + D ▪ This is two D’s, worth $16 CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5D$8 6A, C$10 7A, D$11 8C, C$14 9C, D$15 10??

 We have now completed our problem  Using this information, it would be easy to figure out the optimal 11-lb, 12-lb, etc. knapsacks for this problem CapacityContentsValue 0empty$0 1empty$0 2A$3 3B$4 4C$7 5D$8 6A, C$10 7A, D$11 8C, C$14 9C, D$15 10A, C, C$17