mat 3770
play

Mat 3770 Problem Bin Packing Dynamic Programming Basic Problem - PowerPoint PPT Presentation

Mat 3770 Bin Packing or The Knapsack Mat 3770 Problem Bin Packing Dynamic Programming Basic Problem or Algorithm Problem Variation The Knapsack Problem Exhaustive Search Greedy Dynamic Pgmg Hierarchical Math Pgmg Spring 2014


  1. Mat 3770 Bin Packing or The Knapsack Mat 3770 Problem Bin Packing Dynamic Programming Basic Problem or Algorithm Problem Variation The Knapsack Problem Exhaustive Search Greedy Dynamic Pgmg Hierarchical Math Pgmg Spring 2014

  2. Dynamic Programming Mat 3770 Bin Packing or Used when a problem can be partitioned into The Knapsack non–independent sub–problems Problem Dynamic Programming Solve each sub–problem once; solution is saved for use in Basic Problem Algorithm other sub–problems Problem Variation Exhaustive Search Greedy Combine solutions of sub–problems into a solution for the Dynamic Pgmg Hierarchical Math Pgmg original problem Effective when a given sub–problem may arise from more than one partial set of choices

  3. Approach Mat 3770 Bin Packing or The Knapsack The structure of an optimal solution is recursively defined. Problem Dynamic Programming Basic Problem The value of an optimal solution is computed in a Algorithm Problem Variation bottom–up fashion. Exhaustive Search Greedy Dynamic Pgmg Hierarchical Math Pgmg An example: Fibonacci numbers F 1 = 1, F 2 = 1, F n = F n − 2 + F n − 1

  4. Problem Definition Mat 3770 Bin Packing Generally: Given a knapsack with weight capacity K and n or The objects of weights w 1 , w 2 , . . . , w n , is it possible to find a Knapsack Problem collection of objects such that their weights add up to K , i.e. Dynamic m Programming Basic Problem � find w i 1 , w i 2 , . . . , w i m , such that K = w i j ? Algorithm Problem Variation j =1 Exhaustive Search Greedy Dynamic Pgmg Hierarchical Math Pgmg For example, given a list: { 12, 10, 40, 3, 11, 26, 37, 28, 9, 18 } , does any subset add up to 72 ? yes: { 12, 3, 22, 37, 9 }

  5. This Problem Has Many Variations Mat 3770 Allow same weights to be used multiple times Bin Packing or Ask: how close to full (without going over) can we get? The Knapsack I.e., if W = { w 1 , w 2 , . . . , w n } , we want to minimize Problem w over all W ′ ⊆ W � K − Dynamic Programming w ∈ W ′ Basic Problem Algorithm subject to the constraint that Problem Variation Exhaustive Search Greedy � K − w ≥ 0 Dynamic Pgmg Hierarchical w ∈ W ′ Math Pgmg Assume there’s a corresponding value v i for each w i (e.g., gold’s value & its weight). Maximize the total value given that the weight must be at most K : � � Maximize over I ⊆ { 1 , . . . , n } , v i , given w i ≤ K i ∈ I i ∈ I

  6. The Basic Problem Mat 3770 Bin Packing or The Knapsack Problem Is there I ⊆ { 1 , . . . , n } such that � i ∈ I w i = K Dynamic Programming Let predicate Basic Problem Algorithm Problem Variation � : if ∃ I ⊆ { 1 , . . . , i } ∋ � j ∈ I w j = k . true Exhaustive P [ i , k ] = Search : false otherwise Greedy Dynamic Pgmg Hierarchical Math Pgmg and we want the value of P [ n , K ]

  7. Observation Mat 3770 Bin Packing or � P [ i − 1 , k ] The : if w i is not used Knapsack P [ i , k ] = Problem P [ i − 1 , k − w i ] : if w i is used Dynamic Programming Basic Problem Furthermore: Algorithm Problem Variation P [ i , k ] is false if k < 0 Exhaustive Search Greedy P [ i , k ] is true for k = 0 Dynamic Pgmg Hierarchical P [1 , k ] is true IFF k = w 1 Math Pgmg So, we can build a table to solve this type of problem.

  8. Bin Packing Algorithm Mat 3770 // initialize first row Bin Packing or P[0, 0] = true The Knapsack for currentWeight := 1 to K Problem P[0, currentWeight] = false Dynamic Programming Basic Problem // calculate rest of rows 1 through n Algorithm Problem for i := 1 to n Variation Exhaustive Search for currentWeight := 0 to K Greedy Dynamic Pgmg P[i, currentWeight] = false Hierarchical Math Pgmg if P[i - 1, currentWeight] then P[i, currentWeight] = true else if currentWeight - wi >= 0 then if P[i - 1, currentWeight - wi] then P[i, currentWeight] = true

  9. Mat 3770 Bin Packing or The Knapsack Problem Dynamic Programming Basic Problem Algorithm Worksheet Problem Variation Exhaustive Search Greedy Dynamic Pgmg Hierarchical Math Pgmg

  10. Time Complexity Mat 3770 Bin Packing The idea behind dynamic programming is to build a table or The iteratively, where we solve a problem by storing solutions to Knapsack Problem subproblems efficiently. Dynamic Programming Basic Problem What is our complexity for finding P [ n , K ]? Algorithm Problem Variation Exhaustive Search We have to fill in the table out to the K th column for n − 1 Greedy Dynamic Pgmg rows, then consider the n th row at column K if Hierarchical Math Pgmg P [ n − 1 , K ] � = true . Thus: O (( n − 1) K + 1) = O ( nk ) which could be bad if K = 2 n .

  11. Follow–up Mat 3770 Bin Packing or The Knapsack Problem To make it easier to know which objects were used, we could mark P [ i , currentWeight ] if w i was used Dynamic Programming (i.e., if P [ i − 1 , k − w i ] = true ) Basic Problem Algorithm Problem Variation Exhaustive The we can trace back through the table to find which w i Search Greedy were used. Dynamic Pgmg Hierarchical Math Pgmg Note that recapturing the subset would take O ( n ) time.

  12. A Variation On The Problem Mat 3770 Bin Packing or The Knapsack Given a collection of items, T = { t 1 , . . . , t n } where t i has Problem (integer) size s i and a set B of bins each with a fixed (integer) capacity b . Dynamic Programming Basic Problem Algorithm Problem Variation A subset of the items can be packed in one bin as long as Exhaustive Search Greedy the sum of the sizes of the items is less than or equal to b . Dynamic Pgmg Hierarchical Math Pgmg The goal is to pack all items, minimizing the number of bins used.

  13. An Example Mat 3770 Bin Packing or The Suppose there are 7 items Knapsack Problem with sizes 1, 4, 2, 1, 2, 3, 5, and the bin capacity b = 6. Dynamic Programming Basic Problem Algorithm Problem Variation Exhaustive Search One (optimal) solution: Greedy Dynamic Pgmg Hierarchical Math Pgmg Bin 1 : 1 and 5 = 6 Bin 2 : 4 and 2 = 6 Bin 3 : 1, 2, and 3 = 6

  14. Solution Methods Mat 3770 Bin Packing or The Knapsack Problem Exhaustive Search Dynamic Programming Greedy Approach Basic Problem Algorithm Problem Variation Dynamic Programming Exhaustive Search Greedy Dynamic Pgmg Hierarchical Hierarchical or Divide–and–Conquer Math Pgmg Mathematical Programming

  15. Exhaustive Search Mat 3770 Bin Packing or The Many Possibilities — One is: Knapsack Problem Find all partitions of the items — from all in a single set, to Dynamic Programming Basic Problem all items in separate sets. Algorithm Problem Variation Exhaustive Search Determine the feasible partitions, and Greedy Dynamic Pgmg Hierarchical Math Pgmg choose one of the feasible solutions that uses a minimum number of bins.

  16. Greedy Approach Mat 3770 Bin Packing or Optionally, can sort items in increasing order by size: The Knapsack 1, 1, 2, 2, 3, 4, 5 Problem Dynamic Programming Pack a b in with the items given in order until the bin is full Basic Problem Algorithm Problem Variation Exhaustive get another (empty bin) and keep packing until all items are Search Greedy in bins. Dynamic Pgmg Hierarchical Math Pgmg Results: Bin 1 : 1, 1, 2, 2 Bin 2 : 3 Bin 3 : 4 Bin 4 : 5 Suboptimal since it uses 4 bins instead of 3

  17. Dynamic Programming Mat 3770 Bin Packing or The Knapsack Problem Dynamic Programming Strategy: solve the problem for the first k items, then consider Basic Problem Algorithm the ( k + 1) st iteration and determine the best way to place the Problem Variation ( k + 1) st item in previous (or a new) bins. Exhaustive Search Greedy Dynamic Pgmg Hierarchical Math Pgmg

  18. Mat 3770 Bin Packing or Items: 1, 4, 2, 1, 2, 3, 5 Bin capacity: 6 The Knapsack Problem Step 1 : place 1 1 Dynamic Step 2 : place 4 1, 4 Programming Basic Problem Algorithm Step 3 : place 2 1, 4 2 Problem Variation Exhaustive Search Step 4 : place 1 1, 1, 4 2 Greedy Dynamic Pgmg Hierarchical Step 5 : place 2 1, 1, 4 2, 2 Math Pgmg Step 6 : place 3 1, 1, 4 2, 2 3 Step 7 : place 5 1, 1, 4 2, 2 3 5

  19. Mat 3770 Bin Packing or The Knapsack Problem Note: order makes a difference in this algorithm. Dynamic Programming Basic Problem If they are sorted in descending order, 5, 4, 3, 2, 2, 1, 1, we Algorithm Problem happen to get an optimal solution, but this isn’t guaranteed Variation Exhaustive Search in all cases. Greedy Dynamic Pgmg Hierarchical Math Pgmg 5, 1 4, 2 3, 2, 1

  20. Divide and Conquer Mat 3770 Bin Packing or The Partition the problem into 2 subproblems Knapsack Problem For example: 1, 4, 2, 1, 2, 3, 5 ⇒ 1, 4, 2, 1 and 2, 3, 5 Dynamic Programming Basic Problem Algorithm Problem Variation Recursively partition each subproblem until each subset can Exhaustive Search fit into a bin. Greedy Dynamic Pgmg 1, 4, 2, 1 ⇒ 1, 4 and 2, 1 bins 1 & 2 Hierarchical Math Pgmg 2, 3, 5 ⇒ 2, 3 and 5 bins 3 & 4 Uses four bins – suboptimal

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend