Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

algorithmshw4

This document outlines Homework 04, which includes various programming and algorithmic problems related to Fibonacci numbers, palindromes, and unit intervals. The assignment is graded out of 100 points, with specific instructions for submission and collaboration. Each problem requires students to provide pseudocode and analyze the time complexity of their solutions.

Uploaded by

kadeheppner
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

algorithmshw4

This document outlines Homework 04, which includes various programming and algorithmic problems related to Fibonacci numbers, palindromes, and unit intervals. The assignment is graded out of 100 points, with specific instructions for submission and collaboration. Each problem requires students to provide pseudocode and analyze the time complexity of their solutions.

Uploaded by

kadeheppner
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Homework 04

Homework must be neatly typeset or handwritten, and submitted


individ- ually as a PDF by 23:59. Please turn in any source code if the
assignment has programming components (i.e., the *.py files). This
assignment is graded out of 100 points, the last question is a bonus
problem worth 5 additional points. You may work in groups of 1
or 2 for this assignment. If you work in a group of 2, make
sure that both your names are clearly stated at the top of the
assignment and submit only a single copy (on one of your
Canvas accounts).
When I state ”the textbook” I am referring to our course
textbook: Intro- duction to Algorithms 3rd edition by Cormen,
Leiserson, Rivest, and Stein.

Problem 1 (30 points)


Adapted from problem 15.1-5 in the textbook. The nth Fibonacci Number
is defined recursively by

F (n) = F (n − 2) + F (n − 1)

with F (0) = 0 and F (1) = 1.

Example:

F (4) = F (3) + F (2)


= (F (2) + F (1)) + (F (1) + F (0))
= ((F (1) + F (0)) + 1) + (1 + 0)
= ((1 + 0) + 1) + (1 + 0)
= 3

Problem 1.a (10 points)


Determine and explain the runtime (referencing line numbers) of the following
implementation of computing the Fibonacci Sequence:
1: procedure FIB(n)
2: if n == 0 or n == 1 then
3: return 1
4: end if
1
5: return FIB(n − 2) + FIB(n − 1)
6: end procedure
The runtime for this recursive Fibonacci algorithm will be O(2^n) because it
creates two recursive trees that overlap constantly saving many redundant
answers. 2^n because 2 recursive call trees are made for each call at every
level which essentially doubles the number of calls at each level of the tree.

Problem 1.b(15 points)


Provide pseudocode for an O(n) time algorithm to compute the nth
Fibonacci Number using dynamic programming.
1. FibMemoization(n):
2. …T[]
3. …T[0] = 1
4. …T[1] = 1
5. …if n == 0 or n == 1
6. ……return T[n]
7. …else
8. ……return fibHelp(n, T)
9. //
10. fibHelp(n, T[]):
11. …if T[n] is in table
12. ……Return T[n]
13. …If T[n] is not in table
14. ……T[n] = fibHelp(n-1, T) + (n-2, T)
15. …return T[n]

Problem 1.c (5 points)


Referencing line numbers, prove that the time complexity of your
algorithm from 1.b is O(n).
In the fibMemoization function each line takes O(1) time because it is
just initializing and if n = 1 or 0 it just returns n which also takes O(1),
the function call at the end is also only called once. In fib help lines 11
and 12 are also O(1) because they just return t[n]. line 14 fibHelp -1
and fibHelp-2 are called but all previous answers are saved into T[] so
2
each specific number is only called 1 time making there be o(n) calls at
this level instead of the recursive call tree doubling in size for each call.

Problem 2 (40 points)


Palindromes!

Problem 2.a (30 points)


Adapted from problem 15-2 from the textbook: A palindrome is a
nonempty string over some alphabet that reads the same forwards
and backwards. For example: mom, dad, and racecar are all
palindromes. Provide an algorithm (in the form of pseudocode, not
an implementation) to find the longest subsequence of a string that
is a palindrome. For example, the string “character” has a longest
subsequence palindrome of “carac”. Notice that you can not just go
through each character, finding the longest palindrome starting at that
character. You are finding a subsequence which can delete certain
characters as long as the order of the remaining characters is
consistent. Your algorithm must use dynamic programming (hint,
think a 2-dimensional array).
Another example: helloworld has a solution of lowol. You are NOT
allowed to just call the longest common subsequence
algorithm on the string forwards and backwards. I really want
you to develop this solution from the ground up.

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.

Problem 2.b (10 points)


Provide a time complexity for your algorithm from 2.a and prove the runtime
referencing line numbers.

Problem 3 (30 points)


Unit Intervals!

Problem 3.a (10 points)


Adapted from problem 16.2-5 in the textbook. Provide pseudocode
for an ef- ficient algorithm that, given a sorted list of points [x1, x2, . .
. , xn] on the real line with x1 < x2 < . . . < xn, returns the smallest
number of unit-length closed intervals (intervals of length 1 like [1.1,
2.1] that contain their endpoints) that contains all of the given points.
You can assume all numbers in the input will be greater than or
equal to zero. Your algorithm must be greedy.

Problem 3.b (10 points)


Explain why your algorithm makes locally optimal choices that result in
a glob- ally optimal solution. In other words, show that your algorithm
has the Greedy- choice property described on page 424 in the
textbook.

Problem 3.c (10 points)


Provide a time complexity for your algorithm from 3.a and prove the runtime
referencing line numbers.

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.

You might also like