- Asymptotic Notation
- [2 points] State the definition of f(n) ∈ O(g(n))
- [4 points] Show that if f1(n) ∈ O(g1(n)) and f2(n) ∈ O(g2(n)), then f1(n) + f2(n) ∈ O(max(g1(n),g2(n))
- [2 points] What does the above property imply in terms of algorithm analysis?
- STOOGE_GCD: Compute the GCD of two positive integers
STOOGE_GCD(a,b)
- done ← false
- while not done do
- x ← rand() % (min(a,b))
- if a %x = 0 and b%x = 0
- return x * STOOGE_GCD(a/x,b/x)
- return 1
- [4 points] Is STOOGE_GCD an algorithm? Why or why not.
- [2 points] What design pattern is STOOGE_GCD attempting to employ?
- CLOSEST_PAIR: Given a set of n points P, find the closest two.
CLOSEST_PAIR(P)
- d ← ∞
- for i ← 1 to n-1 do
- for j ← i+1 to n do
- if d > distance(Pi,Pj)
- d = distance(Pi,Pj)
- a = i, b = j
- return Pa, Pb
- [2 points] Does this algorithm terminate? (why or why not)
- [2 points] Does this algorithm work? (why or why not)
- [2 points] What is the critical operation for this algorithm?
- [4 points] What is the performance of this algorithm? (Give a mathematical expression and relate the lines of the instructions to the terms of the expression)
- [3 points] Is there a different best, worst and average case performance for this algorithm? If so, state the different values, if not, state why.
- [2 points] What design pattern was applied to develop this algorithm?
- PATTERN_MATCH: Given a text T[1..n] and a pattern P[1..m] locate the first occurrence of P in T.
PATTERN_MATCH(P,T)
- L ← []
- for i ← 1 to n do
- if T[i] = P[1]
- L.append(i)
- for i ← 0 to L.size() do
- t = T.substring(L[i])
- if t == P
- return i
- return -1
- [2 points] Does this algorithm terminate?
- [2 points] Does this algorithm work?
- [2 points] What is the critical operation for this algorithm?
- [4 points] What is the performance of this algorithm? (Give a mathematical expression and relate the lines of the instructions to the terms of the expression)
- [3 points] Is there a different best, worst and average case performance for this algorithm? If so, state the different values, if not, state why.
- FLIP_IT (S): reverse a string
FLIP_IT(S)
- if S = "" or S.size() = 1
- return S
- else
- return FLIP_IT(S.substr(1,S.size()-1) + S[0]
- [4 points] Build a recurrence relation to represent the performance of this algorithm. Describe how your recurrence relation relates to the algorithm (use line numbers).
- [4 pints] Solve the recurrence relation.