1. Algorithms for the Fibonacci Function
To prove:
Induction. Base case: n = 2. By simple matrix multiplication,
, so the base case holds.
Induction hypothesis: Suppose the claim holds for n
= k. In other words,
.
Induction step: Prove that the claim holds for n =
k+1:
So the induction step holds, and the result follows for all
n>1 by induction.
2. Binary Search
Strong induction on n.
Base case: n = 1.
. Base case holds.
Induction hypothesis: Suppose the claim holds for all n
< k.
Induction step: Prove that the claim holds for n =
k. Two cases: k is even, or k is odd.
Case 1: k = 2j for some integer j.
Case 2: k = 2j+1 for some integer j.
Note that
, since, aside from the base case,
. Note also that,
,
, since the
only changes in increasing from an odd to an even (eg,
), never from an even to an odd.
So in both cases the induction step holds, and the result
follows for all
by induction.
3. The Skyline Problem in Computer Graphics
Merging two skylines is doable in linear time. Here is the sketch
of the algorithm:
- (a) initializing step: Advance to the first line causing
an intersection of the two skylines.
- (b) Detect which of the 2 skylines has the lowest y value
at this x-position.
- (c) Draw this skyline until the next intersection.
- (d) go back to 2 until the end of a skyline has been reached.
Now, practically how do we do this in linear time?
Let's assume a skyline is given by two arrays storing the x
and y values, we then have:
Sky1x, Sky1y both of size n
Sky2x, Sky2y both of size m
Our goal is to build Sky3x, Sky3y 2 arrays of size n+m representing
the intersection of the 2 skylines.
In other words Sky3x is the merged sorted list of Sky1x and
Sky2x
and Sky3y contains the lowest value of Sky1y and Sky2y when
the skylines are overlapping and 0 when they are not.
Here is the initialization:
i = 0;
j = 0;
k = 0;
// Advance to the first intersection point
// and sets the height of all points previous
// to the intersection to 0
while (Sky1x[i] < Sky2x[0])
Sky3x[i] = Sky1x[i];
Sky3y[k] = 0;
i = i + 1;
k = k + 1;
end
while (Sky2x[j] < Sky1x[0])
Sky3x[i] = Sky1x[i];
Sky3y[i] = 0;
j = j + 1;
k = k + 1;
end
Now the main loop
while (i<n) and (j<n)
// Check which x value to insert
if (Sky1x[i]<Sky2x[j])
// Insert the x value
Sky3x[k] = Sky1x[i];
// Select the lowest height and insert it
Sky3y[k] = min(Sky1y[i],Sky2y[j-1])
i = i + 1;
else
Sky3x[k] = Sky2x[i];
Sky3y[k] = min(Sky1y[i-1],Sky2y[j])
j = j + 1;
k = k + 1;
end
Clearly this algorithm is linear as it scans only once each
input array.
4. Analysis of Heap Sorting
- (a) Check Luc Devroye's class notes on heaps
-
-
- (b) To prove:
-
Sylvain Bouix
2000-10-19