Other Dynamic Programming Problems

Slides:



Advertisements
Similar presentations
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advertisements

Angstrom Care 培苗社 Quadratic Equation II
AP STUDY SESSION 2.
1
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
& dding ubtracting ractions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Multiplication X 1 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5 6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 10 x 1 = x 1 = x 1 = 12 X 2 1.
Division ÷ 1 1 ÷ 1 = 1 2 ÷ 1 = 2 3 ÷ 1 = 3 4 ÷ 1 = 4 5 ÷ 1 = 5 6 ÷ 1 = 6 7 ÷ 1 = 7 8 ÷ 1 = 8 9 ÷ 1 = 9 10 ÷ 1 = ÷ 1 = ÷ 1 = 12 ÷ 2 2 ÷ 2 =
Chapter 1 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Dynamic Programming Introduction Prof. Muhammad Saeed.
Introduction to Algorithms
and 6.855J Spanning Tree Algorithms. 2 The Greedy Algorithm in Action
We need a common denominator to add these fractions.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
CALENDAR.
1 1  1 =.
1  1 =.
2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt ShapesPatterns Counting Number.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
Photo Slideshow Instructions (delete before presenting or this page will show when slideshow loops) 1.Set PowerPoint to work in Outline. View/Normal click.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Break Time Remaining 10:00.
Find the total of 5 hours 10 minutes 3 hours 23 minutes 6 hours 39 minutes Click for solution + hours minutes7214 More than 60 minutes? YES: convert 60.
The basics for simulations
Factoring Quadratics — ax² + bx + c Topic
Randomized Algorithms Randomized Algorithms CS648 1.
PP Test Review Sections 6-1 to 6-6
Abstract Data Types and Algorithms
ABC Technology Project
VOORBLAD.
15. Oktober Oktober Oktober 2012.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
We are learning how to read the 24 hour clock
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Before Between After.
25 seconds left…...
Subtraction: Adding UP
: 3 00.
5 minutes.
Types of clocks. Types of clocks Sand clock or Hourglass clock.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
We will resume in: 25 Minutes.
Essential Cell Biology
Converting a Fraction to %
CSE Lecture 17 – Balanced trees
Clock will move after 1 minute
PSSA Preparation.
& dding ubtracting ractions.
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Minimum Vertex Cover in Rectangle Graphs
Presentation transcript:

Other Dynamic Programming Problems View in slide-show mode Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Problem 1 Subset Sum Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Subset-Sum Problem Given: Find: a set of integers X = {x1, x2, …, xn}, and an integer B Find: a subset of X that has maximum sum not exceeding B. Notation: Sn,B = {x1, x2, …, xn: B} is the subset-sum problem The integers to choose from: x1, x2, …, xn Desired sum: B Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Subset-Sum Problem Example: x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 S12,99: {20, 30, 14, 70, 40, 50, 15, 25, 80, 60, 10, 95: 99} Find a subset of X with maximum sum not exceeding 99. An optimal solution: x1 x3 x5 x8 Nopt = {20, 14, 40, 25} with sum 20 + 14 + 40 + 25 = 99 Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Substructure Property Consider the solution as a sequence of n decisions: ith decision: whether we pick number xi or not Let Nopt be an optimal solution for Sn,B Let xk be the highest-indexed number in Nopt Nopt (optimal for Sn,B) xk Nʹopt = Nopt – {xk} Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Substructure Property Lemma: Nʹopt = Nopt – {xk} is an optimal solution for the subproblem Sk-1,B-xk = {x1, x2, …, xk-1: B-xk} and c(Nopt) = xk + c(Nʹopt) where c(N) is the sum of all numbers in subset N Nopt (optimal for Sn,B) xk Nʹopt = Nopt – {xk} (optimal for Sk-1, B-xk) Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Substructure Property - Proof Proof: By contradiction, assume that there exists another solution Aʹ for Sk-1, B – xk for which: c(Aʹ) > c(Nʹopt) and c(Aʹ) ≤ B – xk i.e. Aʹ is a better solution than Nʹopt for Sk-1, B-xk Then, we can construct A = Aʹ ∪{xk} as a solution to Sk, B. We have: c(A) = c(Aʹ) + xk > c(Nʹopt) + xk = c(Nopt) Contradiction! Nopt was assumed to be optimal for Sk,B. Proof complete. Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Substructure Property - Example x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 S12,99: {20, 30, 14, 70, 40, 50, 15, 25, 80, 60, 10, 95: 99} x1 x3 x5 x8 Nopt = {20, 14, 40, 25} is optimal for S12, 99 Nʹopt = Nopt – {x8} = {20, 14, 40} is optimal for x1 x2 x3 x4 x5 x6 x7 the subproblem S7,74 = {20, 30, 14, 70, 40, 50, 15: 74} and c(Nopt) = 25 + c(Nʹopt) = 25 + 74 = 99 Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Recursive Definition an Optimal Solution c[i, b]: the value of an optimal solution for Si,b = {x1, …, xi: b} According to this recurrence, an optimal solution Ni,b for Si,b: either contains xi ⟹ c(Ni,b) = xi + c(Ni-1, b-xi) or does not contain xi ⟹ c(Ni,b) = c(Ni-1, b) Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

1 b-xi b n 1 Need to process: c[i, b] after computing: c[i-1, b], c[i-1, b-xi] i-1 i c[i, b] n Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

1 b-xi b n 1 for i ⟵ 1 to m for b ⟵ 1 to n …. c[i, b] = i-1 i c[i, b] Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Computing the Optimal Subset-Sum Value SUBSET-SUM (x, n, B) for b ← 0 to B do c[0, b] ← 0 for i ← 1 to n do c[i, 0] ← 0 for b ← 1 to B do if xi ≤ b then c[i, b] ← Max{xi + c[i-1, b-xi], c[i-1, b]} else c[i, b] ← c[i-1, b] return c[n, B] Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Finding an Optimal Subset SOLUTION-SUBSET-SUM (x, b, B, c) i ← n b ← B N ← ∅ while i > 0 do if c[i, b] = c[i-1, b] then i ← i – 1 else N ← N ∪ {xi} i ← i – 1 b ← b – xi return N Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Binary Search Tree Problem 2 Optimal Binary Search Tree Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Reminder: Binary Search Tree (BST) All keys in the left subtree less than 8 All keys in the right subtree greater than 8 This property holds for all nodes. Image from Wikimedia Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Binary Search Tree Example Example: English-to-French translation Organize (English, French) word pairs in a BST Keyword: English word Satellite data: French word end We can search for an English word (node key) efficiently, and return the corresponding French word (satellite data). do then begin else if while Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Binary Search Tree Example Suppose we know the frequency of each keyword in texts: begin do else end if then while 5% 40% 8% 4% 10% 10% 23% end 4% do then 40% 10% begin else if while 5% 8% 10% 23% Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Cost of a Binary Search Tree Example: If we search for keyword “while”, we need to access 3 nodes. So, 23% of the queries will have cost of 3. end 4% do then 40% 10% begin else if while 5% 8% 10% 23% = 1x0.04 + 2x0.4 + 2x0.1 + 3x0.05 + 3x0.08 + 3x0.1 + 3x0.23 = 2.42 Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Cost of a Binary Search Tree do 40% A different binary search tree (BST) leads to a different total cost: begin 5% while 23% Total cost = 1x0.4 + 2x0.05 + 2x0.23 + 3x0.1 + 4x0.08 + 4x0.1 + 5x0.04 = 2.18 if 10% else 8% then 10% This is in fact an optimal BST. end 4% Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Binary Search Tree Problem Given: A collection of n keys K1 < K2 < … Kn to be stored in a BST. The corresponding pi values for 1 ≤ i ≤ n pi: probability of searching for key Ki Find: An optimal BST with minimum total cost: Note: The BST will be static. Only search operations will be performed. No insert, no delete, etc. Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Cost of a Binary Search Tree Lemma 1: Let Tij be a BST containing keys Ki < Ki+1 < … < Kj. Let TL and TR be the left and right subtrees of T. Then we have: Intuition: When we add the root node, the depth of each node in TL and TR increases by 1. So, the cost of node h increases by ph. In addition, the cost of root node r is pr. That’s why, we have the last term at the end of the formula above. TL TR Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Optimal Substructure Property Lemma 2: Optimal substructure property Consider an optimal BST Tij for keys Ki < Ki+1 < … < Kj Let Km be the key at the root of Tij Then: Ti,m-1 is an optimal BST for subproblem containing keys: Ki < … < Km-1 Tm+1,j is an optimal BST for subproblem containing keys: Km+1 < … < Kj Km Ti,m-1 Tm+1,j Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Recursive Formulation Note: We don’t know which root vertex leads to the minimum total cost. So, we need to try each vertex m, and choose the one with minimum total cost. c[i, j]: cost of an optimal BST Tij for the subproblem Ki < … < Kj where Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Bottom-up computation How to choose the order in which we process c[i, j] values? Before computing c[i, j], we have to make sure that the values for c[i, r-1] and c[r+1,j] have been computed for all r. Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

c[i,j] must be processed after c[i,r-1] and c[r+1,j] c[i, j] i Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

If the entries c[i,j] are computed in the shown 1 i j n If the entries c[i,j] are computed in the shown order, then c[i,r-1] and c[r+1,j] values are guaranteed to be computed before c[i,j]. 1 c[i,j] i j n Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Computing the Optimal BST Cost COMPUTE-OPTIMAL-BST-COST (p, n) for i ← 1 to n+1 do c[i, i-1] ← 0 PS[1] ← p[1] // PS[i]: prefix_sum(i): Sum of all p[j] values for j ≤ i for i ← 2 to n do PS[i] ← p[i] + PS[i-1] // compute the prefix sum for d ← 0 to n−1 do for i ← 1 to n – d do j ← i + d c[i, j] ← ∞ for r ← i to j do c[i, j] ← min{c[i, j], c[i,r-1] + c[r+1, j] + PS[j] – PS[i-1]} return c[1, n] Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Note on Prefix Sum We need Pij values for each i, j (1 ≤ i ≤ n and 1 ≤ j ≤ n), where: If we compute the summation directly for every (i, j) pair, the total runtime would be Θ(n3). Instead, we spend O(n) time in preprocessing to compute the prefix sum array PS. Then we can compute each Pij in O(1) time using PS. Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University

Note on Prefix Sum In preprocessing, compute for each i: PS[i]: the sum of p[j] values for 1 ≤ j ≤ i Then, we can compute Pij in O(1) time as follows: Pij = PS[i] – PS[j-1] Example: 1 2 3 4 5 6 7 8 p: 0.05 0.02 0.06 0.07 0.20 0.05 0.08 0.02 PS: 0.05 0.07 0.13 0.20 0.40 0.45 0.53 0.55 P27 = PS[7] – PS[1] = 0.53 – 0.05 = 0.48 P36 = PS[6] – PS[2] = 0.45 – 0.07 = 0.38 Cevdet Aykanat and Mustafa Ozdal Computer Engineering Department, Bilkent University