algorithmshw4
algorithmshw4
F (n) = F (n − 2) + F (n − 1)
Example:
1. palindromeBottomUp(string):
2. …n = length of string
3. …ARR[][] //length of n x n
4. …for I=0 to n-1
5. ……set all length 1 strings to 1
6. //two characters match
7. …If string[i] == string[j]
8. ……if string length == 2:
9. ………ARR[i][j] = 2
10. ……else
11. ………ARR[i][j] = ARR[i+1][j-1] + 2
12. //characters don’t match
13. …else if string[i] != string[j]
14. ……ARR[i][j] = Max(ARR[i+1][j], ARR[i][j-1])
Hints
ˆ Each entry ARR[i][j] should store the longest subsequence
palindrome between the characters i and j.
ˆ You can initialize each entry ARR[i][j] where i == j with a
value of 1, because any single character is a length 1
palindrome.
ˆ Think about what happens when you have two adjacent
characters, i.e., if you have j = i + 1, and string[i] ==
3
string[j], then what is the length of the longest subsequence
palindrome at ARR[i][j]?
4
ˆ Think about what happens when j > i + 1 and string[i] ==
string[j], we will need to look “back”, at previously computed
values, to figure out our new longest subsequence palindrome
length.
ˆ What happens when string[i] /= string[j]? We will need to look
“back” in the 2-d array to keep track of which subsequence had a
longer palindrome to fill in ARR[i][j].
ˆ You only need to fill in the matrix at locations ARR[i][j] when
i ≤ j, because the entry at ARR[j][i] = ARR[i][j], so you only
need to consider the “upper-right side” of the 2-d array.
ˆ Think about filling out ARR from the diagonal “outward” to the
top-right corner.
ˆ Consider looking at the Longest Common Subsequence or Matrix
Multi- plication algorithms using dynamic programming in the
textbook.
5
Bonus Problem (5 points)
Choose one of the following people: Margaret Hamilton, Donald
Knuth, or Edsger W. Disjkstra. Write a short (roughly a paragraph)
summary on their contributions to the world of computing. Feel free to
include some biographical details as well.