# hw1solns - 15-750 Graduate Algorithms Spring 2009 Miller...

This preview shows pages 1–3. Sign up to view the full content.

15-750 — Graduate Algorithms — Spring 2009 Miller and Dinitz and Tangwongsan Assignment 1 Solutions 1 Fibonaccimal 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” so that ( x k x k - 1 . . . x 1 ) F denotes the number k i =1 x i F i . As an example, the Fibonaccimal number (1101) F = F 4 + F 3 + F 1 = 1 + 2 + 3 = 6. Recall that the Fibonacci numbers satisfy the recurrence F 0 = 0 , F 1 = 1 and F n +2 = F n +1 + F n . a) Show that every positive integer n can 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 on n . For the base case, we note that it is trivial to represent n = 1 since F 1 = 1 . For the inductive step, suppose that we have proved the claim for every value at most n - 1 , and now we want to prove it for n . If n is a Fibonacci number then the representation is trivial – just put 1 in the corresponding place. Otherwise, let k be the maximum value such that F k n ; that is, F k n < F k +1 . Let n 0 = n - F k , and note that by induction there is a Fibonaccimal representation of n 0 . Since n 0 = n - F k < F k +1 - F k = F k - 1 , we know that n 0 < F k - 1 and thus n 0 < F k (since the Fibonacci numbers are nondecreasing). Since n 0 < F k its Fibonaccimal representation cannot have a 1 in the k th position, so by simply changing that position to a 1 we get a Fibonaccimal representation for n 0 + F k = n . b) We first note that a representation of a number that involves the substring 011 can always be changed to 100 , so by making this change from left to right across our representation we can get a new representation without any consecutive 1 ’s (this will clearly terminate since every time we reduce the number of 1 s by 1 ). So we can assume that the input to our increment algorithm is always in this form, as long as we make sure the output is always in this form. We will call this form a canonical representation . We will also not have to make use of F 1 , since F 2 = F 1 = 1 . To see this, suppose that we have a representation in canonical form. Then if x 1 = 1 we know that x 2 = 0 , so we can simply set x 2 = 1 and x 1 = 0 and represent the same number. Now we just do the 011 100 trick to get it back in canonical form. Our algorithm is simple: if x 2 = 0 then set x 2 = 1 , and otherwise set x 3 to 1 (note that it was 0 before because the representation was canonical) and x 2 to 0 . Now we “ripple” through, from right to left, replacing 011 with 100 . Correctness is obvious: the first step clearly adds 1 , and the second step clearly puts the representation into canonical form without changing the value of the number. To prove our amortize time bound, we will let our potential function Φ be three times the number of 1 s in the representation. Now suppose when we do an increment there are k “ripples”. Then the number of operations we do is 3 k +1 , since we first set a bit to 1 and then every ripple causes

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

View Full Document
15-750 HW 1 Solutions 2 us to change 3 bits. But the change in potential is
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern