knapsack problem
play

Knapsack Problem Items have weights and values The problem is to - PDF document

Knapsack Problem Items have weights and values The problem is to maximize total value subject to CSE 421 a bound on weght Algorithms g Items {I 1 , I 2 , I n } Weights {w 1 , w 2 , ,w n } Values {v 1 , v 2 , , v n


  1. Knapsack Problem • Items have weights and values • The problem is to maximize total value subject to CSE 421 a bound on weght Algorithms g • Items {I 1 , I 2 , … I n } – Weights {w 1 , w 2 , …,w n } – Values {v 1 , v 2 , …, v n } Richard Anderson – Bound K Lecture 18 • Find set S of indices to: – Maximize Σ i ε S v i such that Σ i ε S w i <= K Dynamic Programming Midterm Probem Midterm Probem w i = 1 or w i = 2 w i = 1 or w i = 2 • Idea one: • Idea two: – sort items by v i /w i – pair up items of weight 1 – greedy packing – greedy packing K = 4 K = 6 7 12 11 1 7 12 6 6 11 3 Subset Sum Problem Adding a variable for Weight • Let w 1 ,…,w n = {6, 8, 9, 11, 13, 16, 18, 24} • Opt[ j, K ] the largest subset of {w 1 , …, w j } that sums to at most K • Find a subset that has as large a sum as possible, without exceeding 50 • {2, 4, 7, 10} – Opt[2, 7] = O t[2 7] – Opt[3, 7] = – Opt[3,12] = – Opt[4,12] = 1

  2. Subset Sum Recurrence Subset Sum Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – w j ] + w j ) • Opt[ j, K ] the largest subset of {w 1 , …, w j } that sums to at most K 4 3 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 {2, 4, 7, 10} Subset Sum Code Knapsack Problem • Items have weights and values for j = 1 to n • The problem is to maximize total value subject to for k = 1 to W a bound on weght Opt[j, k] = max(Opt[j-1, k], Opt[j-1, k-w j ] + w j ) • Items {I 1 , I 2 , … I n } – Weights {w 1 , w 2 , …,w n } – Values {v 1 , v 2 , …, v n } – Bound K • Find set S of indices to: – Maximize Σ i ε S v i such that Σ i ε S w i <= K Knapsack Recurrence Knapsack Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – w j ] + v j ) Subset Sum Recurrence: Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – w j ] + w j ) 4 3 Knapsack Recurrence: Knapsack Recurrence: 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Weights {2, 4, 7, 10} Values: {3, 5, 9, 16} 2

  3. Dynamic Programming Billboard Placement Examples • Examples • Maximize income in placing billboards – Optimal Billboard Placement – b i = (p i , v i ), v i : value of placing billboard at position p i • Text, Solved Exercise, Pg 307 – Linebreaking with hyphenation – Linebreaking with hyphenation • Constraint: • Constraint: • Compare with HW problem 6, Pg 317 – At most one billboard every five miles – String approximation • Example • Text, Solved Exercise, Page 309 – {(6,5), (8,6), (12, 5), (14, 1)} Design a Dynamic Programming Opt[k] = fun(Opt[0],…,Opt[k-1]) Algorithm for Billboard Placement • Compute Opt[1], Opt[2], . . ., Opt[n] • How is the solution determined from sub problems? • What is Opt[k]? Input b 1 , …, b n , where b i = (p i , v i ), position and value of billboard i Input b 1 , …, b n , where bi = (p i , v i ), position and value of billboard i Optimal line breaking and hyphen- Solution ation • Problem: break lines and insert hyphens to j = 0; // j is five miles behind the current position make lines as balanced as possible // the last valid location for a billboard, if one placed at P[k] for k := 1 to n • Typographical considerations: while (P[ j ] < P[ k ] – 5) – Avoid excessive white space A id i hit j := j + 1; j j + 1 j := j – 1; – Limit number of hyphens Opt[ k] = Max(Opt[ k-1] , V[ k ] + Opt[ j ]); – Avoid widows and orphans – Etc. 3

  4. Penalty Function String approximation • Pen(i, j) – penalty of starting a line a • Given a string S, and a library of strings B position i, and ending at position j = {b 1 , …b m }, construct an approximation of the string S by using copies of strings in B. Opt-i-mal line break-ing and hyph-en-a-tion is com-put-ed with dy-nam-ic pro-gram-ming B = {abab, bbbaaa, ccbb, ccaacc} S = abaccbbbaabbccbbccaabab • Key technical idea – Number the breaks between words/syllables Design a Dynamic Programming Formal Model Algorithm for String Approximation • Strings from B assigned to non- • Compute Opt[1], Opt[2], . . ., Opt[n] overlapping positions of S • What is Opt[k]? • Strings from B may be used multiple times • Cost of δ for unmatched character in S f δ f C t t h d h t i S • Cost of γ for mismatched character in S – MisMatch(i, j) – number of mismatched characters of b j , when aligned starting with position i in s. Target string S = s 1 s 2 …s n Library of strings B = {b 1, …,b m } MisMatch(i,j) = number of mismatched characters with b j when aligned starting at position i of S. Opt[k] = fun(Opt[0],…,Opt[k-1]) Solution • How is the solution determined from sub for i := 1 to n problems? Opt[k] = Opt[k-1] + δ ; for j := 1 to |B| p = i – len(b j ); Opt[k] = min(Opt[k], Opt[p-1] + γ MisMatch(p, j)); Target string S = s 1 s 2 …s n Library of strings B = {b 1, …,b m } MisMatch(i,j) = number of mismatched characters with b j when aligned starting at position i of S. 4

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