This preview shows pages 1–2. 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: 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 nonnegative 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 to this set, we obtain a multiset of powers of two whose sum is n , which might contain two copies of 2 . 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 ← while B [ i ] = 1 B [ i ] ← 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 worstcase 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....
View
Full
Document
This note was uploaded on 12/15/2009 for the course 942 cs taught by Professor A during the Spring '09 term at University of Illinois at Urbana–Champaign.
 Spring '09
 A

Click to edit the document details