hw1 Clrs
hw1 Clrs
hw1 Clrs
November 3, 2012
Applying rule (1) to each of these, this will equal Θ(max(n3 , n2 , n, 1)).
Now we can apply rule (4) to see that1
1. the part of the array from a[1] to a[i − 1] inclusive is already sorted;
2. for every element x in the part of the array from a[1] to a[i − 1] and y
in the part of the array from a[i] to a[#a], x ≤ y.
Let’s write n for #a. Then the invariant is true when i = 1 because a[1] to
a[0] is empty, so it comes for free. When the loop index increases to some
new value i + 1, the sorted portion increases because the element we just put
1
also using the fact that 1 = n0 .
1
function selection_sort (a)
for i = 1, #a-1 do
local min = a[i]
local min_loc = i
for j = i+1, #a do
if a[j] < min -- less than min?
then
min = a[j] -- remember it
min_loc = j
end
end
-- swap em! This works in Lua
a[i], a[min_loc] = a[min_loc], a[i]
end
end
into a[i] was (by part 2) greater than or equal to anything already in the
sorted part up to i − 1. And it was chosen to be minimal in the remainder.
We can stop after i = n − 1 because the last thing (by the invariant part
2) is already greater than anything earlier.
Looking at the two nested loops, you see that we have to walk through
all the entries in a[i + 1] to a[n] once for each i. So the execution time will
be: X
c · (n − i).
1≤i≤n
This c reflects the proportion of the cases where we have to update the
minimum, since in that case we do some more work. However, if cworst is
the amount of work we do when we have to do the updates, and cbest is the
amount we do when we don’t, we always have c reflecting some mixture of
those two cases. So cbest ≤ c ≤ worst. Now
X X X
c · (n − i) = c · (i) ∈ Θ( i).
1≤i≤n 0≤i≤n−1 0≤i≤n−1
equal to n · (n − 1)/2.
P
Finally, 0≤i≤n−1 i
2.2-3. We’re asked to assume that the target value is found in each of the
n slots equally often. When the object is in slot i, linear search looks in the
2
first i slots and then finds it. So the expected work is
X
i/n = (n + 1) · n/2n = (n + 1)/2.
1≤i≤n
Sec. 3.1. 3.1-1. To prove: max(f (n), g(n)) ∈ Θ(f (n) + g(n)). We are
allowed to assume that f and g are asymptotically non-negative, meaning
that there’s an N0 such that for all n > N0 , f (n) ≥ 0 and g(n) ≥ 0.
Letting n > N0 , we certainly have max(f (n), g(n)) < f (n) + g(n), so the
constant 1 works in establishing max(f (n), g(n)) ∈ O(f (n) + g(n)). But any
constant > 2 works in establishing f (n) + g(n) ∈ O(max(f (n), g(n))), since
the maximum is at least half the total.
3.1-4. 2n+1 = 2 · (2n ), so the constant 2 works for c for any N0 .
On the other hand, 22n = 2n ·2n . So suppose we want to make 22n ≤ c·2n .
Dividing through by 2n , we have 2n ≤ c. But no c is greater than all the
numbers 2n for n > N0 .
3
Ch. 3. 3-1. (a) If k ≥ d, we want to show that p(n) ∈ O(nk ). So we need
to choose N0 , c so that p(n) ≤ c · nk for all n > N0 .
First, you can choose N0 > max a1 , . . . , ad . This ensures that the leading
term of p(n) is the largest. Now choose c = (d + 1)ad . So p(n) is a sum of
d + 1 values, of which ad · xd is the largest. But (d + 1)ad xk is the sum of
d + 1 values, all equal to this largest term if k = d, and larger otherwise.
3-1. (b) We want to show nk ≤ c · (p(n)) when k ≤ d. But if we c ≥ 1/ad ,
this holds.
3-1. (c) Combine (a) and (b).
3-1. (d) By (c), we know that p(n) ∈ Θ(nd ). So it’s enough to show that
nd ∈ o(nk ) when d < k. We must show that for every c, there is an N0 such
that for n > N0 we have nd < c · nk . Dividing through, this means:
So we can let N0 be any number greater than the (k − d)th root of 1/c.
3-1. (e) By (c), we know that p(n) ∈ Θ(nd ). So it’s enough to show that
nd ∈ ω(nk ) when k < d. But applying part (d) with k, d reversed, we know
exactly that.
3-2. You can use the Seven Rules.
(a) Rule 5 says that the answer is o. f ∈ O(g) is also true (it says less).
(b) Rule 6 says that the answer is o. f ∈ O(g) is also true (it says less).
(c) Note that nsin n ∈ O(n1 ) since sin n ≤ 1. So the answer is ω. f ∈ Ω(g)
is also true (it says less).
(d) Here the answer is ω and Ω.
(e,f ) Here the answer is o and O.
3-3. See sheet in http://web.cs.wpi.edu/~guttman/cs2223/hw1_3-3.
pdf.