This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 4 Dynamic programming By Sariel Har-Peled , September 6, 2007 The events of 8 September prompted Foch to draft the later legendary signal: “My centre is giving way, my right is in retreat, situation excellent. I attack.” It was probably never sent. – – The first world war, John Keegan. Version: 0.1 4.1 Basic Idea - Partition Number 6 = 6 6 = 5 + 1 6 = 4 + 2 6 = 4 + 1 + 1 6 = 3 + 3 6 = 3 + 2 + 1 6 + 3 + 1 + 1 + 1 6 = 2 + 2 + 2 6 = 2 + 2 + 1 + 1 6 = 2 + 1 + 1 + 1 + 1 6 = 1 + 1 + 1 + 1 + 1 + 1 Definition 4.1.1 For a positive integer n , the partition number of n , denoted by p ( n ), is the number of di ff erent ways to represent n as a decreasing sum of posi- tive integers. The di ff erent number of partitions of 6 are shown on the right. It is natural to ask how to compute p ( n ). The “trick” is to think about a recursive solution and observe that once we decide what is the leading number d , we can solve the problem recursively on the remaining budget n- d under the constraint that no number exceeds d .. TIP Suggestion 4.1.2 Recursive algorithms are one of the main tools in developing algorithms (and writing programs). If you do not feel comfortable with recursive algorithms you should spend time playing with recursive algorithms till you feel comfortable using them. Without the ability to think recursively, this class would be a long and painful torture to you. Speak with me if you need guidance on this topic. This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 1 PartitionsI ( num , d ) // d-max digit if ( num ≤ 1) or ( d = 1) return 1 if d > num d ← num res ← for i ← d down to 1 res = res + PartitionsI ( num- i , i ) return res Partitions ( n ) return PartitionsI ( n , n ) The resulting algorithm is depicted on the right. We are interested in analyzing its running time. To this end, draw the recursion tree of P - and observe that the amount of work spend at each node, is proportional to the number of children it has. Thus, the overall time spend by the algorithm is proportional to the size of the recurrence tree, which is proportional (since ev- ery node is either a leaf or has at least two chil- dren) to the number of leafs in the tree, which is Θ ( p ( n )). This is not very exciting, since it is easy ver- ify that 3 √ n / 4 ≤ p ( n ) ≤ n n . Exercise 4.1.3 Prove the above bounds on p ( n ) (or better bounds). Suggestion 4.1.4 Exercises in the class notes are a natural easy questions for inclusions in exams....
View Full Document