Midterm Test on Software Engineering Theory: SE211 There are ten questions each worth 3 points. Try to solve all. Show your work. 1. Write down your full name: 2. Write down your student ID number: Problem 1. Consider the statement: “The sum of two rational numbers is a rational number.” 1. Write down the hypothesis of the statement: Given two rational numbers p and q. 2. Write down the conclusion of the statement: The sum p + q is a rational number. 3. Prove the statement (You must keep your proof short, within at most 4-5 sentences). Proof. Since p and q are rational numbers there must exist integers n, m and s, t such that p= n m and s q= , t where m 6= 0 and t 6= 0. Add these numbers: p+q = s n·t+s·m n + = . m t m·t The number n · t + s · m is an integer and so is m · t. Moreover, m · t 6= 0 since m 6= 0 and t 6= 0. Thus, p + q is a fraction of two integers with non-zero denominator. We have proved that p + q is a rational number. Problem 2. 1. Prove by induction the following theorem. Every positive integer can be written as a product of prime numbers. In your proof you have to clearly state the basis and the inductive hypothesis. We want to show, by induction that every positive integer n is a product of prime numbers. Base Case. Clearly, when n = 1 or n = 2 or n = 3 the statement is true. Inductive hypothesis is this. All the number less than a number k are products of prime numbers. Now we want to show that k is also a product of prime numbers. If k is a prime number then the product of k is k itself. Assume that k is not prime. Then it is a product of smaller numbers s and t, that is k = s · t. By inductive assumption s and t are products of prime numbers. Hence, k must be a product of prime numbers as well. 2. Consider the set A = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}. Write down the numbers in A congruent to 1 modulo 3. These numbers are 1, 4, 7, 10, 13. 3. Apply the Euclidian algorithm to the numbers 564 and 255 to find the gcd of 564 and 255. Write down each step of the algorithm. 564 = 2 · 255 + 54, 255 = 4 · 54 + 39, 54 = 1 · 39 + 15, 39 = 2 · 15 + 9, 15 = 1 · 9 + 6, 9 = 1 · 6 + 6, 6 = 2 · 3. Hence the desired gcd is 3. Problem 3 Consider the following P athExistence(G, s, t) algorithm: 1. Mark s as visited. 2. Repeat the following until no more vertices can be marked. (a) Run through all edges. If an edge (v, u) is found such that v is marked visited and u is not then mark u as visited. 3. If t is marked visited then report yes and stop. Otherwise, report no and stop. Write down two statements that are needed to be proved to show that the algorithm is correct. Outline the proofs of each of your statements. Each proof must NOT exceed the space provided and should be NOT more than 6-7 simple sentences. The first statement: If node t is marked visited then there is a path from s to t. Proof. Before the first iteration s is marked visited and there is a path from s to s. Suppose that after the kth iteration we know that for each marked x there is a path from s to x. Assume that y is marked after the (k + 1)th iteration. There must exist an x and an edge (x, y) such that x is marked visited. Hence taking a path from s to x and the the edge (x, y), we see that there is a path from s to all the nodes marked after the (k + 1)th iteration. Thus, if s is marked then it is marked after some some iteration t. After the iteration s must be marked visited as we have shown. The second statement: If there is a path from S to t then t is marked visited. Proof. Since there is a path from s to t, let us take such a path: v1 , v2 , . . . , vk−1 , vk , where v1 = s and vk = t, and each (vi , vi=1 ) is an edge. Note that v1 is marked visited. By the algorithm v2 must be marked visited after some iteration of the while loop. Since v1 is marked v2 must also be marked visited according to the algorithm. Eventually, vk−1 must be marked visited since all the vertices v1 , . . . , vk−2 are marked. Therefore, the according to the algorithm vk must be marked visited. Problem 4. Consider the directed graph G given as follows: The set V of vertices is {0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 15}; The set of of edges is E = {(x, y) | x + 1 = y} ∪ {(3, 0), (7, 5), (8, 7), (10, 12), (3, 5)} 1. Draw the graph. This will be shown in one of the lectures. 2. Write down the adjacency matrix representation of the graph. This will be shown in one of the lectures. 3. Write down all the strongly connected components of the graph. Here are the strongly connected components: {0, 1, 2, 3}, {5, 6, 7, 8}, {10}, {11}, {12}, {15}. 4. Suppose that all the edges in the graph are made undirected. Write down all the components of the graph. Here are the connected components: {0, 1, 2, 3}, {5, 6, 7, 8}, {10, 11, 12}, {15}. Problem 5. Suppose you are given a connected graph G such that the degree of every vertex in the graph is congruent to 2 modulo 4. 1. Does this graph have an Euler circuit? (your answer must be ”yes” or ”no” for this question) yes. 2. Explain your answer above within 4-5 sentences at most (you may refer to one of the theorems from the lecture notes). One of the theorems states that if a graph is connected and the degree of each vertex in the graph is even then the graphs has an Euler circuit. Since the degree of each vertex equals 2 modulo 4 then the degrees of all the vertices must be even. Hence, by the theorem, the graph has an Euler circuit. 3. Suppose for the graph G of this problem you remove the condition that G is connected. Does G have an Euler circuit? Explain your answer in at most 2-3 sentences. No. The degree of each vertex of G can be equal to 2 mod 4 but G may have two components. Such graph does not have an Euler circuit. Problem 6. 1. Is it true that every undirected graph has a spanning tree? (Say yes or no). yes; but this question is not marked. 2. Is (((2 + 3) ? (v/x)) − 7) an arithmetic expression? Explain your answer in 2-4 sentences at most. This follows from the definition of arithmetic expressions. 2, 3, 7 are arithmetic expressions by the base case. Now by induction each of the following is (2 + 3), (v/x), ((2 + 3) ? (v/x)), and (((2 + 3) ? (v/x)) − 7) an arithmetic expression. 3. Draw an expression tree for the following arithmetical expression: ((x ? ((v + x) − 7)) − z). This will be shown in the lecture. Problem 7. Let T be a tree. Let n be the number of nodes of the tree and m be the number of edges in the tree. Prove, by induction, that n − 1 = m. In your proof state clearly your base case and inductive hypothesis. Proof. Base case. If the tree has one node then it has no edges. Therefore n = 1 and m = 0. So, the statement is true for the base case. Assume that the statement is true for all trees of height at most h (This is inductive assumption). Let T be tree of height h + 1, and let n be the number of nodes, m edges of T . Consider all the leaves v1 ,. . ., vk in T that are at level h + 1. The tree T1 without these leaves has height h. Let n1 be the nodes on T1 and m1 be the edges of T1 . By the induction assumption n1 − 1 = m1 . Thus, there are n1 = k nodes in T . Each vi contributes an edge to the set of edges of T1 . Therefore there are mk + k edges. Hence T has n = n1 + k nodes and m = mk + k edges. Clearly, n − 1 = m. We proved the statement. Problem 8. Let A = {a, b}, B = {a, x, y} and C = ∅. Write down the following sets: 1. A ∪ B = {a, b, x, y}. 2. A ∩ B = {a}. 3. A × C = {a, b}. 4. B 2 = {(a, a), (a, x), (a, y), (x, a), (x, x), (x, y), (y, a), (y, x), (y, y)}. 5. P (C) = {∅} 6. A × B = {(a, a), (a, x), (a, y), (b, a), (b, x), (b, y)}. Problem 9. Let X and Y be sets. Consider the Cartesian products X × Y and Y × X. Is it true that X × Y = Y × X? If your answer is “yes” then explain your answer in one sentence. If your answer is “no” then write down, in one sentence, when the equality occurs. Not always true. The equality occurs when either X = Y or X = ∅ or Y = ∅. Problem 10. Consider the following relational structure: (Z; R1 , R2 ), where Z is the set of integers, R1 = {(x, y) | x ≤ y}, and R2 = {(x, y) | |x| = |y|}. Write down the following sets: 1. (R1 ∪ R2 ) ∩ {(x, y) | |x| < 2, |y| < 2}. The relation is {(−1, −1), (−1, 0), (−1, 1), (0, 0), (0, 1), (1, 1), (1, −1)}. 2. ∃x(R1 (x, y) ∩ {(x, y) | x 6= y}). This relation equals to the set Z itself. Indeed, if y ∈ Z then there is an x such that (x, y) ∈ R1 . For example, (y, y) ∈ R1 . 3. Inst(R2 , 5, 1). This set is obtained by putting 5 in the first coordinate of the relation and finding all x such that R2 (5, x)) is true. This set is {−5, 5}. 4. Inst(R1 , 5, 1). This set is obtained by putting 5 in the first coordinate of the relation and finding all x such that R1 (5, x)) is true. This set is {5, 6, 7, 8, 9, 10, 11, . . .}. 5. What is the value of the following expressions: 1. 2. 3. 4. ∀x∃yR1 (x, y). The value is T rue ∀x∃yR2 (x, y). The value here is also T rue. ∃yInst(R2 , 5, 1). The value is T rue. ∀yInst(R2 , 5, 1). The value is F alse. Problem 11. On the set N of natural numbers consider the following relation: {(y, x) | y = 2n x for some n}. Do the following: 1. Prove that R is a partial order. We need to show that R is reflexive, antisymmetric, and transitive. For each x ∈ N we have x = 20 · x. Hence (x, x) ∈ R. If (x, y) and (y, x) are in R then y = 2n x and x = 2m y for some n, m ∈ N . Therefore y = 2n+m y. This occurs only when n = m = 0. Hence it must be the case that x = y. This shows that R is antisymmetric. To show that R is transitive assume that (y, x) ∈ R and (x, z) ∈ R. Then y = 2n x and x = 2m z for some n, m ∈ N . Hence y = 2n+m z. Therefore (y, z) ∈ R. 2. Are there maximal elements of the partially ordered set (N, R)? If your answer is no then explain your answer. If your answer is yes then explain what these numbers are. Yes, there are maximal elements. These are 0, 1, 3, 5, 7, 9, 11, . . . all the odd numbers including 0.