COT 3100 Spring 2010-Midterm2_new_Solution

COT 3100 Spring 2010-Midterm2_new_Solution - Name:_ UFID:_...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Name:__________________________________________________ UFID:____________________________ 1 COT 3100 Spring 2010 Midterm 2 For the first two questions #1 and #2, do ONLY ONE of them. If you do both, only question #1 will be graded. 1. (20 pts) Give iterative and recursive algorithms for finding the n th term of the sequence {a n } defined by a 0 = 2, a 1 = 2 and a n+1 = a n ×(a n-1 ) 2 . Which algorithm is more efficient and why? Solution: + Input: n, a 0 = 2, a 1 = 2, a n+1 = a n ×(a n-1 ) 2 ; + Output: The n th term of the sequence {a n }. Iterative algorithm: for (int i = 2; i <=n; i++) { result = a 1 * a 0 * a 0 ; a 0 = a 1 ; a 1 = result ; } return result ; Recursive algorithm: int a (int k) { if (k <= 1 ) return 2; else return a (k-1)* a (k-2)* a (k-2); } return result = a (n); Which algorithm is more efficient and why? Iterative algorithm performs (n-2) iterations and in each iteration, updates values of 3 variables, thus its time complexity is O(n). The recursive algorithm, in order to find a (n), loops into itself 3 times to find a (n-1) and two a (n-2). To find a (n-1), it again loops into itself 3 times to find a (n-2) and two a (n-2), so on and so forth. Thus, its creates a complete 3-leaf tree with height n. Thus, the total time complexity is O(3 n ). Therefore, the iterative algorithm is more efficient .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Name:__________________________________________________ UFID:____________________________ 2 2. (20 pts) Give iterative and recursive algorithms for determining whether a string is a palindrome or not. (Note: A string is a palindrome if it can be read the same way in either direction, e.g., ‘ABBA’, ‘madam’, ‘level’). Solution + Input: A string w of length n ; + Output: YES if w is a palindrome, NO otherwise. Iterative algorithm w is a palindrome iff ( w [i] == w [ n +1-i]) for any i th index from 1 to n . Therefore, we test this property on every i th position and stop when the index i hits the middle of the string w . int middle = n/2; for(int i = 1; i<= middle ; i++) { if ( w [i] != w [n+1-i] ) return NO ; } return YES ; Recursive algorithm A string w of length k is a palindrome iff ( w [1] == w [ k ]) and the substring w[2. .. k -1] is also a palindrome. This observation suggests the following recursive algorithm
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/22/2012 for the course COT 3100 taught by Professor Staff during the Spring '08 term at University of Florida.

Page1 / 6

COT 3100 Spring 2010-Midterm2_new_Solution - Name:_ UFID:_...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online