Starting from:

$30

Assignment #2: induction

CSC236 
Assignment #2: induction

The aim of this assignment is to give you some practice with proving facts about recurrences, with the time
complexity of recursive algorithms, and proving the correctness of algorithms.
Your assignment must be typed to produce a PDF document a2.pdf (hand-written submissions are not
acceptable). Also submit Python source for two functions in sorting.py. You may work on the assignment
in groups of 1 or 2, and submit a single assignment for the entire group on MarkUs
1. De?ne T as the smallest such such that:
(a) Symbol 2 T .
(b) If t1; t2 2 T , then (t1t2) 2 T
Some examples of elements of T are , (), and (()).
(a) How many elements of T have (a) 0 left parentheses (b) 1 left parentheses (c) 2 left parentheses,
(d) 3 left parentheses, and (d) 4 left parentheses?
(b) Devise a recurrence c(n) such that c(n) is the number of di?erent elements of T with n left
parentheses. Explain why your c(n) is correct.
2. Devise a function p(n) such that p(n) is the number of di?erent ways to create postage of n cents using
3-, 4-, and 5-cent stamps.
(a) Carefully explain why your p(n) is correct.
(b) Prove that p(n) is monotonic nondecreasing on N
+
3. Consider the recurrence T that we derived for the worst-case time complexity of recBinSearch:
T(n) = (
c
0
if n = 1
1 + T(dn=2e) if n 1
(a) Emulate Lemma 3.6 from the course notes to prove that T is nondecreasing.
(b) Use simple induction on k to prove that 8k; n 2 N; n = 2k ) T(n) = lg(n) + c
0
.
(c) Combine the previous two steps to prove that T 2 (lg). Do not use induction.
4. Suppose you take the trouble to legally change your name to a string from the alphabet fA; C; T; Gg,
for example T AGAC might make a ?ne name. Then you (discreetly) collect DNA samples from your
friends, e.g. nail clippings, hair, used co?ee cups. Your idea is to count the number of times your name
occurs as a subsequence of their DNA (you may need some help sequencing that), and then invoice
1
them for that many licenses of your intellectual property. If your friend's DNA contained the string
AT AGGACCA they'd owe you for at least four licenses!
Clearly you'll need some computational help counting sequences. Read over the code below and either
(a) prove that its precondition plus execution implies its postcondition, or (b) provide a counterexample
that shows it is incorrect.
def count_subsequences(s1: str, s2: str,
i: int, j: int) - int:
""" Return the number of times s1[: i] occurs as a
subsequence of s2[: j].
Precondition: 0 <= i <= len(s1), 0 <= j <= len(s2)
count_subsequences("", "Danny", 0, 5)
1
count_subsequences("Danny", "", 5, 0)
0
count_subsequences("AAA", "AAAAA", 3, 5)
10
Postcondition: returns number of times s1[: i] occurs as a
subsequence of s2[: j]
"""
if i == 0:
return 1
elif i j:
return 0
elif s1[i-1] != s2[j-1]:
return count_subsequences(s1, s2, i, j-1)
else:
return (count_subsequences(s1, s2, i, j-1)
+ count_subsequences(s1, s2, i-1, j-1))
You may also ?nd the above code, plus an e?cient memoized version, in sequences.py
2
5. Read over unimplemented function shade sort below:
def shade_sort(colour_list: List[str]) - None:
""" Put colour_list in order "b" < "g" < "r".
precondition: colour_list is a List[str] from {"b", "g", "r"}
list_ = ["r", "b", "g"]
shade_sort(list_)
list_ == ["b", "g", "r"]
True
postcondition: colour_list has same strings as before, ordered "b" < "g" < "r"
"""
# TODO: initialize blue, green, red to be consistent with loop invariants.
# Hint: blue, green may increase while red decreases.
#
# loop invariants:
#
# 0 <= blue <= green <= red <= len(colour_list)
# colour_list[0 : green] + colour_list[red :] same colours as before
# and all([c == "b" for c in colour_list[0 : blue]])
# and all([c == "g" for c in colour_list[blue : green]])
# and all([c == "r" for c in colour_list[red :]])
#
# TODO: implement loop using invariants above!
implement: Use the invariants as a guide to implement function shade sort.
prove: Use those same invariants to prove that your function shade sort is correct: ?rst partial correctness, then termination.
extend: Modify the invariants of shade sort so they specify function four shade sort which adds a
fourth string "y" (meaning yellow) that must end up following the other three when colour list
is sorted. Then implement function four shade sort.
Submit both implementations (including invariants) as sorting.py, along with a2.pdf. You will ?nd the
above code in sorting.py
3

More products