08-amortize - Algorithms Lecture 8 Amortized Analysis The...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Algorithms Lecture 8: Amortized Analysis The goode workes that men don whil they ben in good lif al amortised by synne folwyng. — Geoffrey Chaucer, “The Persones [Parson’s] Tale” (c.1400) I will gladly pay you Tuesday for a hamburger today. — J. Wellington Wimpy, “Thimble Theatre” (1931) I want my two dollars! — Johnny Gasparini [Demian Slade], “Better Off Dead” (1985) 8 Amortized Analysis 8.1 Incrementing a Binary Counter It is a straightforward exercise in induction, which often appears on Homework 0, to prove that any non-negative integer n can be represented as the sum of distinct powers of 2. Although some students correctly use induction on the number of bits—pulling off either the least significant bit or the most significant bit in the binary representation and letting the Recursion Fairy convert the remainder—the most commonly submitted proof uses induction on the value of the integer, as follows: Proof: The base case n = 0 is trivial. For any n > 0, the inductive hypothesis implies that there is set of distinct powers of 2 whose sum is n - 1. If we add 2 0 to this set, we obtain a multiset of powers of two whose sum is n , which might contain two copies of 2 0 . Then as long as there are two copies of any 2 i in the multiset, we remove them both and insert 2 i + 1 in their place. The sum of the elements of the multiset is unchanged by this replacement, because 2 i + 1 = 2 i + 2 i . Each iteration decreases the size of the multiset by 1, so the replacement process must eventually terminate. When it does terminate, we have a set of distinct powers of 2 whose sum is n . This proof is describing an algorithm to increment a binary counter from n - 1 to n . Here’s a more formal (and shorter!) description of the algorithm to add 1 to a binary counter. The input B is an (infinite) array of bits, where B [ i ] = 1 if and only if 2 i appears in the sum. I NCREMENT ( B [ 0.. ]) : i 0 while B [ i ] = 1 B [ i ] 0 i i + 1 B [ i ] 1 We’ve already argued that I NCREMENT must terminate, but how quickly? Obviously, the running time depends on the array of bits passed as input. If the first k bits are all 1s, then I NCREMENT takes Θ( k ) time. The binary representation of any positive integer n is exactly b lg n c + 1 bits long. Thus, if B represents an integer between 0 and n , I NCREMENT takes Θ( log n ) time in the worst case. 8.2 Counting from 0 to n Now suppose we want to use I NCREMENT to count from 0 to n . If we only use the worst-case running time for each I NCREMENT , we get an upper bound of O ( n log n ) on the total running time. Although this bound is correct, we can do better. There are several general methods for proving that the total running time is actually only O ( n ) . Many of these methods are logically equivalent, but different formulations are more natural for different problems.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern