=========================================================================== CSC 236 Sample Solutions for Week 10 Tutorial Fall 2007 =========================================================================== A. This is the example in section 2.5 of the textbook, rephrased to use the same notation I use in lecture. Show that the following algorithm terminates: # Pre: x, y in N while x != 0 or y != 0: if x != 0: x -= 1 else: x = 16 y -= 1 Q: Standard technique? A: Find expression E such that E_k in N and E_{k+1} < E_k for all k in N such that there is an iteration number k+1. Q: What now? A: Idea: try to work out number of iterations of loop. Loop stops when x = 0 and y = 0 and decrements x as long as x != 0. So loop iterates at least x many times to start with. Afterward, behaviour depends only on value of y: x set to 16 then decremented to 0 before y decremented, i.e., loop performs 17 iterations (one for each value of x) to decrement y by 1. So loop should iterate 17y many times more before it stops. Q: So what expression do we use? A: Try E = 17y + x. Q: What do we check now? A: E_0 = 17 y_0 + x_0 in N (since y_0, x_0 in N). Q: Next? A: Let k >= 0 and suppose E_k = 17 y_k + x_k in N and there is an iteration number k+1. Value of E_{k+1} depends on y_k and x_k. Case 1 (x_k = 0): Then, x_{k+1} = 16, y_{k+1} = y_k - 1, so E_{k+1} = 17 y_{k+1} + x_{k+1} = 17 (y_k - 1) + 16 = 17 y_k - 1 < 17 y_k + 0 = 17 y_k + x_k = E_k. Moreover, iteration number k+1 happens only if y_k > 0 (since x_k = 0) so E_{k+1} in N. Case 2 (x_k > 0): Then, x_{k+1} = x_k - 1 and y_{k+1} = y_k so E_{k+1} = 17 y_{k+1} + x_{k+1} = 17 y_k + x_k - 1 < 17 y_k + x_k = E_k. Moreover, x_k > 0 means E_k > 0 so E_{k+1} in N. Q: Conclusion? A: E_0 > E_1 > ... is a decreasing sequence of natural numbers and must be finite, i.e., the loop terminates. B. Show that the following algorithm terminates: # Pre: x in N, x >= 1 while x != 1: if x % 2 == 0: x /= 2 else: x = 3 * x + 1 WARNING: This is not as easy as it looks! Trace it for some values of x (or code it up to display the sequence of values generated) to get a feeling for what's going on. >> In fact, nobody knows how to prove this: it's an open question! C. (a) List the elements of {a,ab}*. Find a way to describe strings in this language, i.e., find a predicate P(s) such that P(s) is true iff s in {a,ab}*, for all strings s over alphabet {a,b,c}. Q: What's {a,ab}*? A: {a,ab}* = {e} U {a,ab} U {aa,aab,aba,abab} U {aaa,...} U ... = {e,a,ab,aa,aab,aba,abab,aaa,aaab,aaba,aabab,...} Q: How can we describe strings in {a,ab}*? A: This is almost like {a,b}* (all strings of a's and b's), except each 'b' comes with an 'a' in front, i.e., P(s) = s is a string of a's and b's where each b is immediately preceded by an a. Q: We're done, right? A: Almost: we should check that s in {a,ab}* iff P(s), by arguing each direction: => It is obvious that each string in {a,ab}* has property P() (every b immediately preceded by an a). <= Moreover, each string where each b is immediately preceded by an a belongs to {a,ab}*: the string can be broken up into pieces 'ab' (one for each b in the string) with every other symbol being 'a'. (b) Find three different examples of a language L over alphabet {a,b,c} such that L = L.L { Make sure everyone is clear on why each of these have the required property. } - L = {}: L.L = {} = L - L = {e}: L.L = {e} = L - L = {e,a,aa,aaa,aaaa,...}: L.L = {e,a,aa,aaa,aaaa,...} U {a,aa,aaa,aaaa,...} U {aa,aaa,aaaa,...} U ... = L (c) Find three different examples of a language L over alphabet {a,b,c} such that L = L* { Make sure everyone is clear on why each of these have the required property. } - L = {e}: L* = {e} = L - L = {e,a,aa,aaa,...}: as above, L* = L - L = {a,b,c}* = {e,a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,...}: . s in L* iff s = s_1.s_2...s_k for some s_1,...,s_k in L, but then, s is a sequence of a's, b's, c's, so s in L; . s in L iff s = s_1.s_2...s_k for some s_1...s_k in {a,b,c}, but then, s in L* because {a,b,c} subset of L*. (d) Find three different examples of languages L_1, L_2 over alphabet {a,b,c} such that L_1.L_2 = L_2.L_1 { Make sure everyone is clear on why each of these have the required property. } - L_1 = {} (then, L_1.L_2 = {} = L_2.L_1 for all L_2) - L_1 = {e} (then, L_1.L_2 = L_2 = L_2.L_1 for all L_2) - L_1 = {a,aaa}, L_2 = {e,aa,aaaa}; then: L_1.L_2 = {a,aaa,aaaaa} U {aaa,aaaaa,aaaaaaa} = {a,aaa,aaaaa,aaaaaaa} = {a,aaa} U {aaa,aaaaa} U {aaaaa,aaaaaaa} = L_2.L_1