Uploaded by Sujith Kumar

COMP 6651 Fall 2023 Assignment 1

advertisement
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
CONCORDIA UNIVERSITY
COMP 6651 Algorithm Design Techniques
Fall 2023
ASSIGNMENT 1
Due: September 24
1. Let f (n) and g(n) be asymptotically nonnegative functions. Using the basic definition of
Θ-notation, prove that max{f (n), g(n)} = Θ(f (n) + g(n)).
2. Let f (n) and g(n) be asymptotically positive functions. Prove or disprove each of the following conjectures.
(a) f (n) + g(n) = Θ(min{f (n), g(n)}).
(b) f (n) = O(g(n)) implies lg f (n) = O(lg g(n)), where lg g(n) ≥ 1 and f (n) ≥ 1 for all
sufficiently large n.
(c) f (n) = O(g(n)) implies 2f (n) = O(2g(n) ).
(d) f (n) = O((f (n))2 ).
3. Use the substitution method to prove that recurrence
T (n) = T (n/3) + T (2n/3) + Θ(n)
has the solution T (n) = Ω(n lg n)
4. Use the master method to give tight asymptotic bounds for the following recurrences.
√
(a) T (n) = 2T (n/4) + n lg2 n.
(b) T (n) = 2T (n/4) + n2 .
5. Show that for suitable constants a, b, and ϵ, the function f (n) = 2⌈lg n⌉ satisfies all the
conditions in case 3 of the master theorem except the regularity condition.
6. What value of q does PARTITION (duplicated below from the textbook) return when
all elements in the subarray A[p..r] have the same value? Modify PARTITION so that
q = ⌈(p + r)/2⌉ when all elements in the subarray A[p..r] have the same value.
PARTITION(A, p, r)
1: x ← A[r]
2: i ← p–1
3: for j ← p, r–1 do
4:
if A[j] ≤ x then
5:
i←i+1
6:
exchange A[i] with A[j]
7: exchange A[i + 1] with A[r]
8: return i + 1
▷ the pivot
▷ highest index into the low side
▷ process each element other than the pivot
▷ does this element belong on the low side?
▷ index of a new slot in the low side
▷ put this element there
▷ pivot goes just to the right of the low side
▷ new index of the pivot
7. Consider the sorting algorithm INSERTION-SORT from Section 2.1 of the CLRS textbook:
INSERTION-SORT(A, n)
1: for i ← 2, n do
1
2:
3:
4:
5:
6:
7:
8:
key ← A[i]
\\ Insert A[i] into the sorted subarray A[1..i − 1]
j ←i−1
while j > 0 and A[j] > key do
A[j + 1] ← A[j]
j ←j−1
A[j + 1] ← key
Banks often record transactions on an account in order of the times of the transactions, but
many people like to receive their bank statements with checks listed in order by check number.
People usually write checks in order by check number, and merchants usually cash them with
reasonable dispatch. The problem of converting time-of-transaction ordering to check-number
ordering is therefore the problem of sorting almost-sorted input. Explain persuasively why
the procedure INSERTION-SORT might tend to beat the procedure QUICKSORT on
this problem.
8. Professors Howard, Fine, and Howard have proposed a deceptively simple sorting algorithm,
named stooge sort in their honor, appearing on the following page.
(a) Argue that the call STOOGE-SORT(A, 1, n) correctly sorts the array A[1..n].
(b) Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic (Θ-notation) bound on the worst-case running time.
(c) Compare the worst-case running time of STOOGE-SORT with that of insertion sort,
merge sort, and quicksort. Do the professors deserve tenure?
STOOGE-SORT(A, p, r)
if A[p] > A[r] then
exchange A[p] with A[r]
if p + 1 < r then
k ← ⌊(r − p + 1)/3⌋
STOOGE-SORT(A, p, r − k)
STOOGE-SORT(A, p + 1, r)
STOOGE-SORT(A, p, r − k)
▷ round down
▷ first two-thirds
▷ last two-thirds
▷ first two-thirds again
9. The kth quantiles of an n-element set are the k–1 order statistics that divide the sorted set
into k equal-sized sets (to within 1). Give an O(n lg k)-time algorithm to list the kth quantiles
of a set.
2
Download