CS 38 PROBLEM SET 2: SOLUTIONS
Problem 1. (Pibonacci numbers) [10 points] For a nonnegative real number n, we dene the pibonacci number P (n) as follows: P (n) = 0 P (n 1) + P (n ) 0n<4 n4
Find an algorithm to compute P (n) in time quadratic in n. You may
CPSC 629: Analysis of Algorithms, Fall 2003
Solutions to Homework 2
Solution to 1 (17.1-3)
lg n lg n
f (n) = n +
i=1
(2i 1) = n lg n +
i=1
2i < n lg n + 2n < 3n.
Hence, amortized cost is 3 per operation. Solution to 2 (17.2-2) Charge 3 for each operation.
Priority Queues
Binary and Binomial Heaps
Supports the following operations.
s
Insert element x. Return min element. Return and delete minimum element. Decrease key of element x to k.
s
s
s
These lecture slides are adapted from CLRS, Chapters 6, 19.
Appli
- 19 -
Binomial Heaps
Mergeable Heap: a data structure supporting the following 5 operations. Make-Heap(): return an empty heap Insert(H, x): add x into H Minimum(H): return the minimum Extract-Min(H): return and remove merge two heaps Union(H1, H2): In a
22
Binomial Heaps
Big Picture (Figure 19.1, Pg 456)
New Operations: UNION( , ) creates and returns a new heap that contains all the nodes of heaps
(which are destroyed by this operation).
current value. DELETE(
)
22.1
Binomial Trees
An ordered tree that i
Chapter 19: Binomial Heaps We will study another heap structure called, the binomial heap. The binomial heap allows for ecient union, which can not be done eciently in the binary heap. The extra cost paid is the minimum operation, which now requires O(log