This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 15-750 — Graduate Algorithms — Spring 2009Miller and Dinitz and TangwongsanAssignment 1 Solutions1Fibonaccimal Numbers(25 pts.)Suppose instead of using powers of two, we now represent integers as the sum of Fibonacci numbers.That is, rather than representing a number as an array of bits, we keep an array of “fibbits” sothat (xkxk-1...x1)Fdenotes the number∑ki=1xiFi. As an example, the Fibonaccimal number(1101)F=F4+F3+F1= 1 + 2 + 3 = 6. Recall that the Fibonacci numbers satisfy the recurrenceF= 0,F1= 1 andFn+2=Fn+1+Fn.a) Show that every positive integerncan be represented as a Fibonaccimal number.b) Give an algorithm to increment a Fibonaccimal number in constant amortized time.Solution:a) We will use strong induction onn. For the base case, we note that it is trivial to representn= 1sinceF1= 1. For the inductive step, suppose that we have proved the claim for every value atmostn-1, and now we want to prove it forn. Ifnis a Fibonacci number then the representationis trivial – just put1in the corresponding place. Otherwise, letkbe the maximum value suchthatFk≤n; that is,Fk≤n < Fk+1. Letn=n-Fk, and note that by induction there isa Fibonaccimal representation ofn. Sincen=n-Fk< Fk+1-Fk=Fk-1, we know thatn< Fk-1and thusn< Fk(since the Fibonacci numbers are nondecreasing). Sincen< Fkits Fibonaccimal representation cannot have a1in thekth position, so by simply changing thatposition to a1we get a Fibonaccimal representation forn+Fk=n.b) We first note that a representation of a number that involves the substring011can always bechanged to100, so by making this change from left to right across our representation we can geta new representation without any consecutive1’s (this will clearly terminate since every time wereduce the number of1s by1). So we can assume that the input to our increment algorithm isalways in this form, as long as we make sure the output is always in this form. We will call thisform acanonical representation. We will also not have to make use ofF1, sinceF2=F1= 1. Tosee this, suppose that we have a representation in canonical form. Then ifx1= 1we know thatx2= 0, so we can simply setx2= 1andx1= 0and represent the same number. Now we justdo the011→100trick to get it back in canonical form.Our algorithm is simple: ifx2= 0then setx2= 1, and otherwise setx3to1(note that it wasbefore because the representation was canonical) andx2to. Now we “ripple” through, fromright to left, replacing011with100. Correctness is obvious: the first step clearly adds1, and thesecond step clearly puts the representation into canonical form without changing the value of thenumber.To prove our amortize time bound, we will let our potential functionΦbe three times the numberof1s in the representation. Now suppose when we do an increment there arek“ripples”. Thenthe number of operations we do is3k+1, since we first set a bit to1and then every ripple causes15-750 HW 1 Solutions2us to change3bits. But the change in potential is3(-k+1) =-3k...
View Full Document
This document was uploaded on 11/03/2009.
- Spring '09