Dynamic Programming • The most important algorithmic technique covered in CSE 421 CSE 421 • Key ideas Algorithms – Express solution in terms of a polynomial number of sub problems Richard Anderson – Order sub problems to avoid recomputation Lecture 18 Dynamic Programming Today - Examples Billboard Placement • 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 • 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 bi = (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 1
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 j := j + 1; – Limit number of hyphens j := j – 1; – Avoid widows and orphans Opt[ k] = Max(Opt[ k-1] , V[ k ] + Opt[ j ]); – Etc. Design a Dynamic Programming Penalty Function Algorithm for Optimal Line Breaking • Pen(i, j) – penalty of starting a line a • Compute Opt[1], Opt[2], . . ., Opt[n] position i, and ending at position j • What is Opt[k]? Opt-i-mal line break-ing and hyph-en-a-tion is com-put-ed with dy-nam-ic pro-gram-ming • Key technical idea – Number the breaks between words/syllables Opt[k] = fun(Opt[0],…,Opt[k-1]) Solution • How is the solution determined from sub for k := 1 to n problems? Opt[ k ] := infinity; for j := 0 to k-1 Opt[ k ] := Min(Opt[k], Opt[ j ] + Pen(j, k)); 2
But what if you want to layout the Solution text? • And not just know the minimum penalty? for k := 1 to n Opt[ k ] := infinity; for j := 0 to k-1 temp := Opt[ j ] + Pen(j, k); if (temp < Opt[ k ]) Opt[ k] = temp; Best[ k ] := j; String approximation Formal Model • Given a string S, and a library of strings B • Strings from B assigned to non- = {b 1 , …b m }, construct an approximation of overlapping positions of S the string S by using copies of strings in B. • Strings from B may be used multiple times • Cost of δ for unmatched character in S • Cost of γ for mismatched character in S B = {abab, bbbaaa, ccbb, ccaacc} – MisMatch(i, j) – number of mismatched characters of b j , when aligned starting with S = abaccbbbaabbccbbccaabab position i in s. Design a Dynamic Programming Opt[k] = fun(Opt[0],…,Opt[k-1]) Algorithm for String Approximation • Compute Opt[1], Opt[2], . . ., Opt[n] • How is the solution determined from sub problems? • What is Opt[k]? Target string S = s 1 s 2 …s n Target string S = s 1 s 2 …s n Library of strings B = {b 1, …,b m } Library of strings B = {b 1, …,b m } MisMatch(i,j) = number of mismatched characters with b j when aligned MisMatch(i,j) = number of mismatched characters with b j when aligned starting at position i of S. starting at position i of S. 3
Solution for i := 1 to n 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)); 4
Recommend
More recommend