Richard Anderson Lecture 18 Dynamic Programming

Slides:



Advertisements
Similar presentations
Algorithm Design Methods Spring 2007 CSE, POSTECH.
Advertisements

Knapsack Problem Section 7.6. Problem Suppose we have n items U={u 1,..u n }, that we would like to insert into a knapsack of size C. Each item u i has.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
CSEP 521 Applied Algorithms Richard Anderson Lecture 7 Dynamic Programming.
1 ECE 665 Spring 2005 Computer Algorithms Dynamic Programming Knapsack revisited.
Dynamic Programming Reading Material: Chapter 7..
0-1 Knapsack Problem A burglar breaks into a museum and finds “n” items Let v_i denote the value of ith item, and let w_i denote the weight of the ith.
CSE 421 Algorithms Richard Anderson Lecture 19 Longest Common Subsequence.
CSE 421 Algorithms Richard Anderson Lecture 16 Dynamic Programming.
KNAPSACK PROBLEM A dynamic approach. Knapsack Problem  Given a sack, able to hold K kg  Given a list of objects  Each has a weight and a value  Try.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 16.
1 Chapter 6 Dynamic Programming. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, optimizing some local criterion. Divide-and-conquer.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
CSE 421 Algorithms Richard Anderson Lecture 18 Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Lecture 32 CSE 331 Nov 16, 2016.
Algorithm Design Methods
Weighted Interval Scheduling
CS 3343: Analysis of Algorithms
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
The Greedy Method and Text Compression
Seminar on Dynamic Programming.
Richard Anderson Lecture 17 Dynamic Programming
Richard Anderson Lecture 18 Dynamic Programming
Topic 25 Dynamic Programming
CS38 Introduction to Algorithms
Prepared by Chen & Po-Chuan 2016/03/29
Richard Anderson Lecture 18 Dynamic Programming
CS 3343: Analysis of Algorithms
Lecture 35 CSE 331 Nov 27, 2017.
Lecture 34 CSE 331 Nov 20, 2017.
CS4335 Design and Analysis of Algorithms/WANG Lusheng
Lecture 33 CSE 331 Nov 18, 2016.
Lecture 33 CSE 331 Nov 17, 2017.
Richard Anderson Lecture 17 Dynamic Programming
Exam 2 LZW not on syllabus. 73% / 75%.
Lecture 35 CSE 331 Nov 28, 2016.
Richard Anderson Lecture 16 Dynamic Programming
ICS 353: Design and Analysis of Algorithms
Richard Anderson Lecture 28 Coping with NP-Completeness
Richard Anderson Lecture 19 Longest Common Subsequence
ICS 353: Design and Analysis of Algorithms
CSEP 521 Applied Algorithms
Richard Anderson Lecture 16a Dynamic Programming
Richard Anderson Lecture 16 Dynamic Programming
PTAS for Bin-Packing.
Richard Anderson Lecture 30 NP-Completeness
Richard Anderson Lecture 6 Greedy Algorithms
Lecture 32 CSE 331 Nov 15, 2017.
Richard Anderson Autumn 2016 Lecture 7
Lecture 34 CSE 331 Nov 21, 2016.
Richard Anderson Lecture 7 Greedy Algorithms
Algorithm Design Methods
Lecture 36 CSE 331 Nov 28, 2011.
Lecture 36 CSE 331 Nov 30, 2012.
Algorithm Design Methods
Richard Anderson Lecture 27 Survey of NP Complete Problems
Richard Anderson Lecture 19 Longest Common Subsequence
Dynamic Programming Sequence of decisions. Problem state.
Richard Anderson Lecture 16, Winter 2019 Dynamic Programming
Week 2: Greedy Algorithms
Richard Anderson Lecture 18 Dynamic Programming
Richard Anderson Lecture 17, Winter 2019 Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Data Structures and Algorithm Analysis Lecture 15
Advance Algorithm Dynamic Programming
Dynamic Programming Sequence of decisions. Problem state.
Knapsack Problem A dynamic approach.
Algorithm Design Methods
Seminar on Dynamic Programming.
Presentation transcript:

Richard Anderson Lecture 18 Dynamic Programming CSE 421 Algorithms Richard Anderson Lecture 18 Dynamic Programming

Knapsack Problem Items have weights and values The problem is to maximize total value subject to a bound on weght Items {I1, I2, … In} Weights {w1, w2, …,wn} Values {v1, v2, …, vn} Bound K Find set S of indices to: Maximize SieSvi such that SieSwi <= K

Midterm Probem wi = 1 or wi = 2 Idea one: sort items by vi/wi greedy packing 7 12 11 1 K = 4

Midterm Probem wi = 1 or wi = 2 Idea two: pair up items of weight 1 greedy packing 7 12 6 6 11 3 K = 6

Subset Sum Problem Let w1,…,wn = {6, 8, 9, 11, 13, 16, 18, 24} Find a subset that has as large a sum as possible, without exceeding 50 6, 8, 9, 11, 16

Adding a variable for Weight Opt[ j, K ] the largest subset of {w1, …, wj} that sums to at most K {2, 4, 7, 10} Opt[2, 7] = Opt[3, 7] = Opt[3,12] = Opt[4,12] =

Subset Sum Recurrence Opt[ j, K ] the largest subset of {w1, …, wj} that sums to at most K Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – wj] + wj)

Subset Sum Grid Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – wj] + wj) 4 3 2 1 {2, 4, 7, 10}

Subset Sum Code for j = 1 to n for k = 1 to W Opt[j, k] = max(Opt[j-1, k], Opt[j-1, k-wj] + wj) Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – wj] + wj)

Knapsack Problem Items have weights and values The problem is to maximize total value subject to a bound on weght Items {I1, I2, … In} Weights {w1, w2, …,wn} Values {v1, v2, …, vn} Bound K Find set S of indices to: Maximize SieSvi such that SieSwi <= K

Knapsack Recurrence Subset Sum Recurrence: Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – wj] + wj) Knapsack Recurrence:

Knapsack Grid Weights {2, 4, 7, 10} Values: {3, 5, 9, 16} 4 3 2 1 Opt[ j, K] = max(Opt[ j – 1, K], Opt[ j – 1, K – wj] + vj) 4 3 2 1 Weights {2, 4, 7, 10} Values: {3, 5, 9, 16}

Dynamic Programming Examples Optimal Billboard Placement Text, Solved Exercise, Pg 307 Linebreaking with hyphenation Compare with HW problem 6, Pg 317 String approximation Text, Solved Exercise, Page 309

Billboard Placement Maximize income in placing billboards Constraint: bi = (pi, vi), vi: value of placing billboard at position pi Constraint: At most one billboard every five miles Example {(6,5), (8,6), (12, 5), (14, 1)}

Design a Dynamic Programming Algorithm for Billboard Placement Compute Opt[1], Opt[2], . . ., Opt[n] What is Opt[k]? Input b1, …, bn, where bi = (pi, vi), position and value of billboard i

Opt[k] = fun(Opt[0],…,Opt[k-1]) How is the solution determined from sub problems? Input b1, …, bn, where bi = (pi, vi), position and value of billboard i

Solution j = 0; // j is five miles behind the current position // the last valid location for a billboard, if one placed at P[k] for k := 1 to n while (P[ j ] < P[ k ] – 5) j := j + 1; j := j – 1; Opt[ k] = Max(Opt[ k-1] , V[ k ] + Opt[ j ]);

Optimal line breaking and hyphen-ation Problem: break lines and insert hyphens to make lines as balanced as possible Typographical considerations: Avoid excessive white space Limit number of hyphens Avoid widows and orphans Etc.

Penalty Function Pen(i, j) – penalty of starting a line a position i, and ending at position j Key technical idea Number the breaks between words/syllables Opt-i-mal line break-ing and hyph-en-a-tion is com-put-ed with dy-nam-ic pro-gram-ming

String approximation Given a string S, and a library of strings B = {b1, …bm}, construct an approximation of the string S by using copies of strings in B. B = {abab, bbbaaa, ccbb, ccaacc} S = abaccbbbaabbccbbccaabab

Formal Model Strings from B assigned to non-overlapping positions of S Strings from B may be used multiple times Cost of d for unmatched character in S Cost of g for mismatched character in S MisMatch(i, j) – number of mismatched characters of bj, when aligned starting with position i in s.

Design a Dynamic Programming Algorithm for String Approximation Compute Opt[1], Opt[2], . . ., Opt[n] What is Opt[k]? Target string S = s1s2…sn Library of strings B = {b1,…,bm} MisMatch(i,j) = number of mismatched characters with bj when aligned starting at position i of S.

Opt[k] = fun(Opt[0],…,Opt[k-1]) How is the solution determined from sub problems? Target string S = s1s2…sn Library of strings B = {b1,…,bm} MisMatch(i,j) = number of mismatched characters with bj when aligned starting at position i of S.

Solution for i := 1 to n Opt[k] = Opt[k-1] + d; for j := 1 to |B| p = i – len(bj); Opt[k] = min(Opt[k], Opt[p-1] + g MisMatch(p, j));