CME222 ALGORITHMS Midterm, April, 20, 2013 Number/Name

advertisement
CME222 ALGORITHMS
Midterm, April, 20, 2013
Number/Name:............................................................. Duration: 120 minutes
Problem 1. Asymptotic Running Times [16 points]
For each algorithm listed below,
 give a recurrence that describes its worst-case running time, and
 give its worst-case running time using Θ-notation.
(a) Binary search
(b) Insertion sort
(c) Strassen’s algorithm
(d) Merge sort
Problem 2. Substitution Method [12 points]
T(n) = T( n / 2 ) + T( n / 4) + n ,
T(m) = 1 m≤5.
Use the substitution method to prove a tight asymptotic upper bound (O-notation) on
the solution to the recurrence
Problem 3. Recurrence [10 points]
Solve the following recurrence relation.
3
T(n) = 2T( n / 8) + √𝑛
Problem 4. Growth of Functions [6 points]
Order the following functions by asymptotic growth rate in non-decreasing order:
n2, n, nlgn, √𝑛, 2n, nn, n!
Çözüm:
√𝑛 < n < nlgn < n2 < 2n < n! < nn
Problem 5. Red-black trees [18 points]
(a) Assign the keys 2, 3, 5, 7, 11, 13, 17, 19 to the nodes of the binary search tree
below so that they satisfy the binary-search-tree property.
(b) Explain why this binary search tree cannot be colored to form a legal red-black
tree.
(c) The binary search tree can be transformed into a red-black tree by performing a
single rotation. Draw the red-black tree that results, labeling each node with “red” or
“black.” Include the keys from part (a).
Problem 6. True or False, and Justify [15 points]
Circle T or F for each of the following statements to indicate whether the statement is
true or false, respectively. If the statement is correct, briefly state why. If the
statement is wrong, explain why. The more content you provide in your justification,
the higher your grade, but be brief. Your justification is worth more points than your
true-or-false designation.

T F In a BST, we can find the next smallest element to a given element in
O(1) time.
Yanlış: Successor O(h) zamanda bulunur.

T F Polynomial: good. Exponential: bad.
Doğru: polinom zamanlı algoritmalar hızlıdır exponensiyeller ise çok çok
yavaştır.

T F The height of any binary search tree with n nodes is O(log n).
Yanlış: bir BST dengeli olmak zorunda değildir. lgn< h < n arasında olabilir.

T F Inserting into a Binary Search Tree guarantees to keep the tree height
balanced.
Yanlış: Kırmızı-Siyah ağaçlarda bu durum geçerlidir BST de değil.

T F Recursive algorithms are always better than iterative algorithms.
Yanlış: Genellikle olabilir ama herzaman değildir. İteratif algoritmalardan daha
kötü özyinelemeli algoritmalar vardır.
Problem 7. [8 points]
How many lines, as a function of n, does the following program print. You may
assume n is a power of 2.
function f(n)
if n >1
print “still going”
f(n/2)
Çözüm: O(lgn) veya O(log2n).
T(n)=T(n/2)+1 veya T(n)=T(n/2)+ Θ(1) bağıntısını yazıp T(n)= Θ(lgn) olarak
çözüm bulunabilir.
Problem 8. Pseudocode[10 points]
Suppose you are given a sorted sequence of distinct integers {a1,a2,...,an}. Give a
O(lgn) algorithm to determine whether there exists an i index such as ai=i. For
example, in {-2,0,1,3,5,9,11}, a5=5. In {2,3,4,5,6,7}, there is no such i. The algorithm
returns 0 if there exists no such an index.
index_bul(A,p,q)
mid= floor((p+q)/2)
if A[mid]=mid
then return mid
else if A[mid]<mid
then index_bul(A,mid+1,q)
else
then index_bul(A,p,mid-1)
Problem 9. Dynamic Programming [15 points]
Find an optimal parenthesization of a matrix-chain product A1A2A3A4A5 whose
sequence of dimensions is < 5, 10, 12, 5, 50, 6 >. You may use the following tables.
2500
3
Download