* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download SG Questions and Answers
Survey
Document related concepts
Transcript
Discrete Mathematics and Computational Complexity1 Sheet 1: Sets and Relations 1. List the members of these sets. (a) {x|x is a real number such that x2 = 1}. (b) {x|x is a positive integer less than 12}. (c) {x|x is the square of an integer and x < 100}. (d) {x|x is an integer such that x2 = 2}. 2. What are the union and intersection of Z+ and {z ∈ Z|z is odd}? 3. Derive a formula for |A ∪ B| in terms of |A|, |B|, and |A ∩ B|. 4. How many elements does A × B have if A has m elements and B has n elements? 5. Suppose A × B = ∅, where A and B are sets. What can you conclude about A and B? 6. Which of these is a power-set of a set? (a) ∅. (b) {∅, {a}}. (c) {∅, {a}, {∅, a}}. (d) {∅, {a}, {b}, {a, b}}. 7. How many elements does each of these sets have? (a) P ({a, b, {a, b}}). (b) P ({∅, a, {a}, {{a}}}). (c) P (P (∅)). 8. Consider the relation R = {(a, b)|a divides b} on the set {1, 2, 3, 4, 5, 6}. (a) List all the ordered pairs in R. (b) Draw the digraph of R. 9. Which of these relations on the set of ISE2 students is (i) transitive, (ii) reflexive, (iii) symmetric? (a) R = {(a, b)|a is taller than b}. (b) R = {(a, b)|a was born on the same day as b}. (c) R = {(a, b)|a has the same name as b}. (d) R = {(a, b)|a has a common grandparent with b}. 10. Find the transitive closure of the relation R = {(1, 2), (2, 1), (2, 3), (3, 4), (4, 1)} on the set {1, 2, 3, 4}. 11. Let R be a reflexive relation on A. Show that Rn is reflexive for all n ∈ Z+ . 1 Questions adapted from Rosen. gac1. 1 Discrete Mathematics and Computational Complexity2 Sheet 2: Functions and Propositional Logic 1. Let f : Z → Z be the function f (n) = n2 + 1. What is this function’s domain, codomain, and range? 2. Give an example of a function f : Z+ → Z+ that: (a) is bijective (b) is injective but not surjective (c) is surjective but not injective (d) is neither injective nor surjective 3. Determine whether f : Z × Z → Z is surjective if: (a) f (m, n) = 2m − n. (b) f (m, n) = m + n + 1. (c) f (m, n) = |m| − |n|. (d) f (m, n) = m2 − 4. 4. Let f (x) = ax + b and g(x) = cx + d, where a, b, c, and d are constants. Determine for which constants a, b, c, and d it is true that f • g = g • f . 5. Let A = {1, 2} and B = {3, 4, 5}. (a) If possible, give a function from A to B which is (i) injective but not surjective, (ii) surjective but not injective, (iii) bijective. If it is not possible, explain why. (b) Repeat with functions from B to A. (c) List all possible functions from A to A, and classify them as (i) injective, (ii) surjective, (iii) bijective. 6. Show that p ↔ q and (p ∧ q) ∨ (¬p ∧ ¬q) are equivalent. 7. Let p be the proposition “you get first class honours”. Let q be the proposition “you do all the study group sheets”. Let r be the proposition “you get an A in this subject”. Write the following propositions using p, q, and r and the appropriate symbolic logical connectives. (a) You get an A in this subject but you do not do all the study group sheets. (b) You get first class honours, you do all the study group sheets, and you get an A in this subject. (c) To get first class honours, it is necessary that you get an A in this subject. (d) You get first class honours, but you don’t do all the study group sheets; nevertheless, you get an A in this subject. (e) Getting an A in this subject and doing all the study group sheets is sufficient to get first class honours. (f) You will get first class honours if and only if you either do all the study group sheets, or you get an A in this subject. 2 Questions adapted from Rosen. gac1. 2 Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate Logic and Inference 1. Let P (x) be the predicate “x spends more than ten hours private study time per week”. Let the universe of discourse consist of the set of all students. Express each of these quantifications in English. (a) ∃xP (x). (b) ∀xP (x). (c) ∃x¬P (x). (d) ∀x¬P (x). 2. Express each of these statements using appropriate predicates and quantifiers. The universe of discourse is the set of all living things. (a) All dogs have fleas. (b) There is a horse that has fleas. (c) Every koala can climb. (d) No monkey can speak French. (e) There is a pig that can climb but can’t speak French. 3. State the truth value of each of these statements, and provide a proof if you can. The universe of discourse is the set of all integers. (a) ∀n(n + 1 > n). (b) ∃n(2n = 3n). (c) ∃n(n = −n). (d) ∀n(n2 ≥ n). (e) ∀n∃m(n2 < m). (f) ∃n∀m(n < m2 ). (g) ∀n∃m(n + m = 0). (h) ∃n∀m(nm = m). (i) ∃n∀m(n2 + m2 = 5). (j) ∀n∀m∃p(p = (m + n)/2). 4. Which rules of inference have been used in each of these arguments? (a) Alice studies mathematics. Therefore Alice either studies mathematics or studies computer science. (b) Jerry studies mathematics and computer science. Therefore Jerry studies mathematics. (c) If it is rainy, then the pool will be closed. It is rainy. Therefore the pool is closed. (d) If it snows today, Imperial College will close. Imperial College is not closed today. Therefore, it did not snow today. 3 Questions adapted from Rosen. gac1. 3 (e) If I go swimming, then I will stay in the sun too long. If I stay in the sun too long, I will get sunburn. Therefore, if I go swimming, I will get sunburn. 5. Determine whether each of these arguments is valid. If it is valid, then state which rule of inference is being used. If it is invalid, explain which logical error has been made. (a) If n is a real number such that n > 1, then n2 > 1. Suppose n2 > 1. Then n > 1. (b) The number log2 3 is irrational if it is not the ratio of two integers. Therefore since log2 3 cannot be written in the form a/b, where a and b are integers, it is irrational. (c) If n is a real number with n > 3, then n2 > 9. Suppose n2 ≤ 9. Then n ≤ 3. (d) If n is a real number with n > 2, then n2 > 4. Suppose n ≤ 2. Then n2 ≤ 4. 6. Write each of the hypotheses and steps in these arguments using appropriate predicates and symbolic logical connectives. State which rule of inference is being used at each step. (a) Linda owns a red convertible. Everyone who owns a red convertible has got a speeding ticket. Therefore someone has got a speeding ticket. (b) All films directed by Ken Loach are wonderful. Ken Loach directed a film about the Spanish Civil War. Therefore there is a wonderful film about the Spanish Civil War. 4 Discrete Mathematics and Computational Complexity4 Sheet 4: Growth of Functions and Complexity of Algorithms 1. Show that: (a) 2x + 17 is O(3x ). (Hint: Do each term separately. For the first term, start with log2 2 < log2 3). (b) (c) x2 +1 x+1 is O(x). x3 +2x 2 2x+1 is O(x ). 2. Find the least integer n such that f (x) is O(xn ) for each of these functions. (a) f (x) = 2x3 + x2 ln x. (b) f (x) = (c) f (x) = x4 +x2 +1 x3 +1 . 4 x +5 ln x x4 +1 . 3. In the lectures, we showed that if f1 (x) is O(g1 (x)) and f2 (x) is O(g2 (x)), then f1 (x) + f2 (x) is O(max(|g1 (x)|, |g2 (x)|)). Often, especially when f1 (x) and f2 (x) represent algorithm execution time, it is known that f1 (x) and f2 (x) are never negative. In this case, prove that if f1 (x) is Ω(g1 (x)) and f2 (x) is Ω(g2 (x)), then f1 (x) + f2 (x) is Ω(max(|g1 (x)|, |g2 (x)|)). 4. Consider the code shown in Figure 1. This algorithm can be used to evaluate the polynomial an xn + an−1 xn−1 + . . . a1 x + a0 of degree n at the point x = c. (a) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm. (b) Derive a formula for the number of multiplication operations required to evaluate a polynomial of degree n. (c) Derive a formula for the number of addition operations required to evaluate a polynomial of degree n (excluding the additions required to increment i). (d) Derive a Big-O approximation for the execution time of this algorithm in terms of the polynomial degree. 5. Consider the code shown in Figure 2. This represents an alternative way of evaluating the polynomial, known as Horner’s scheme. (a) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm. (b) Derive a formula for the number of multiplication operations required to evaluate a polynomial of degree n. (c) Derive a formula for the number of addition operations required to evaluate a polynomial of degree n (excluding the additions required to increment i). (d) Derive a Big-O approximation for the execution time of this algorithm in terms of the polynomial degree. (e) Which algorithm has lower time complexity? 6. Consider the code shown in Figure 3. This algorithm calcuates the product of matrices a[] and b[], storing the result in matrix c[]. Obtain a big-O estimate for the execution time of this algorithm. 4 Questions adapted from Rosen. gac1. 5 procedure poly1( c : real, a[0 to n] : real ) begin power := 1 y := a[0] for i := 1 to n begin power := power*c y := y + a[i]*power end end Figure 1: An algorithm for polynomial evaluation. procedure poly2( c : real, a[0 to n] : real ) begin y := a[n] for i := 1 to n y := y*c + a[n − i] end Figure 2: Horner’s scheme for polynomial evaluation. procedure mmult( a[1 to n, 1 to n], b[1 to n, 1 to n] : real ) begin for i := 1 to n for j := 1 to n begin c[i, j] := 0 for k := 1 to n c[i, j] := c[i, j] + a[i, k] ∗ b[k, j] end end Figure 3: An algorithm for matrix multiplication. 6 Discrete Mathematics and Computational Complexity5 Sheet 5: Linear Recurrence Relations and Recursive Algorithms 1. Suppose that the number of bacteria in a colony triples every hour. (a) Write a recurrence relation for the number of bacteria after n hours have elapsed. (b) If 100 bacteria are used to begin a new colony, how many bacteria will be in the colony after 10 hours? 2. Show that the sequence {an } is a solution of the recurrence relation an = an−1 + 2an−2 + 2n − 9 if (a) an = −n + 2. (b) an = 5(−1)n − n + 2. (c) an = 3(−1)n + 2n − n + 2. (d) an = 7 · 2n − n + 2. 3. Solve each of these recurrence relations, given the stated initial conditions. (a) an = an−1 + 6an−2 for n ≥ 2, a0 = 3, a1 = 6. (b) an = 7an−1 − 10an−2 for n ≥ 2, a0 = 2, a1 = 1. (c) an = 6an−1 − 8an−2 for n ≥ 2, a0 = 4, a1 = 10. (d) an = 7an−1 − 10an−2 + 2 for n ≥ 2, a0 = 4, a1 = 1. (e) A challenge: an = an−2 + 1 for n ≥ 2, a0 = 5, a1 = −1. (f) an+2 = −4an+1 + 5an for n ≥ 0, a0 = 2, a1 = 8. 4. Consider the code shown in Fig. 4. Write, and solve, a linear recurrence relation for the number of multiplication operations performed, an . An optimizing compiler may recognise that the code in Fig. 4 can be transformed into the code shown in Fig. 5. Write, and solve, a linear recurrence relation for the number of multiplication operations in the transformed code, and compare the two results. 5. Consider the code shown in Fig. 6. Write, and solve, a linear recurrence relation for (a) the number of addition operations, an . (b) the number of multiplication operations, bn . (c) the number of comparison operations, cn . Hence find a big-O expression for the execution time of this code, in terms of n. 5 Most questions adapted from Rosen. gac1. 7 procedure linrec( n: integer ) begin if n = 0 then result := 1 else result := linrec( n − 1 ) * linrec( n − 1 ) + 5 end Figure 4: Linear recursion example. procedure linrec2( n: integer ) begin if n = 0 then result := 1 else begin temp := linrec2( n − 1 ) result := temp*temp + 5 end end Figure 5: Optimized linear recursion example. procedure linrec3( n: integer, a[0 to 1]: integer ) begin if n =0 then result := 3 + a[n] else if n = 1 then result := 5 + a[n] else result := 3 * linrec3( n − 1 ) * linrec3( n − 2 ) end Figure 6: Another linear recursion example. 8 Discrete Mathematics and Computational Complexity6 Sheet 6: Divide-and-Conquer Recurrence Relations and Recursive Algorithms 1. Suppose that there are n = 2k teams in a knock-out tournament, for some positive integer k. There are n/2 games in the first round, with the n/2 winners playing in the second round, and so on. (a) Develop a recurrence relation for the number of rounds in the tournament, f (n), and obtain a big-O estimate of f (n). (b) Develop a recurrence relation for the number of games in the tournament, g(n), and obtain a big-O estimate of g(n). 2. We looked at an iterative matrix multiplication in Sheet 4. We will now consider two recursive equivalents. (a) We may recognise that a 2n × 2n matrix multiplication C = AB can be divided into sections, as shown in (1), where Aij , Bij and Cij are n × n matrix multiplications. Expanding this, we obtain (2). This expansion forms the basis of the algorithm in Fig. 7, which works when n is an integer power of 2. A11 A12 B11 B12 C11 C12 = (1) C21 C22 A21 A22 B21 B22 C11 C12 C21 C22 = A11 B11 + A12 B21 = A11 B12 + A12 B22 = A21 B11 + A22 B21 = A21 B12 + A22 B22 (2) i. Write an expression for the number of addition operations, a(n), performed by a call to matrixsum. ii. Hence write a divide-and-conquer recurrence relation for the total number of addition and multiplication operations performed by a call to recmatmult1. iii. Obtain a big-O expression for this quantity, and compare it to the iterative algorithm from Sheet 4. (b) In the 1960s, Strassen recognised that (2) could be re-written as (3). P1 = (A11 + A22 )(B11 + B22 ) P2 = (A21 + A22 )B11 P3 = A11 (B12 − B22 ) P4 = A22 (B21 − B11 ) P5 = (A11 + A12 )B22 P6 = (A21 − A11 )(B11 + B12 ) P7 = (A12 − A22 )(B21 + B22 ) C11 = P1 + P4 − P5 + P7 C12 = P3 + P5 C21 = P2 + P4 C22 = P1 + P3 − P2 + P6 6 Some questions adapted from Rosen. gac1. 9 (3) procedure recmatmult1( A[1 to n], B[1 to n] ) begin if n = 1 then result[1,1] := A[1,1]*B[1,1] else begin result[1 to n/2, 1 to n/2] := matrixsum( recmatmult1( A[1 to n/2, 1 to n/2], B[1 to n/2, 1 to n/2] ), recmatmult1( A[1 to n/2, n/2 + 1 to n], B[n/2 + 1 to n, 1 to n/2]) ) result[1 to n/2, n/2 + 1 to n] := matrixsum( recmatmult1( A[1 to n/2, 1 to n/2], B[1 to n/2, n/2 + 1 to n] ), recmatmult1( A[1 to n/2, n/2 + 1 to n], B[n/2 + 1 to n, n/2 + 1 to n]) ) result[n/2 + 1 to n, 1 to n/2] := matrixsum( recmatmult1( A[n/2 + 1 to n, 1 to n/2], B[1 to n/2, 1 to n/2] ), recmatmult1( A[n/2 + 1 to n, n/2+1 to n], B[n/2 + 1 to n, 1 to n/2]) ) result[n/2 + 1 to n, n/2 + 1 to n] := matrixsum( recmatmult1( A[n/2 + 1 to n, 1 to n/2], B[1 to n/2, n/2 + 1 to n] ), recmatmult1( A[n/2 + 1 to n, n/2 + 1 to n], B[n/2 + 1 to n, n/2 + 1 to n]) ) end end procedure matrixsum( A[1 to n], B[1 to n] ) begin for i := 1 to n for j := 1 to n result[i,j] := A[i, j] + B[i, j] end Figure 7: A Recursive Matrix Multiplication i. Using the same procedure matrixsum, and a similar one matrixdiff, write a modified version of recmatmult1, called recmatmult2, that uses this alternative expansion. ii. Write a divide-and-conquer recurrence relation for the total number of additions, subtractions, and multiplication operations performed by a call to recmatmult2. iii. Obtain a big-O expression for this quantity, and compare it to that for recmatmult1. 10 Discrete Mathematics and Computational Complexity7 Sheet 7: Computability 1. If Π is a decision problem, then the complement of Π, written Πc , is defined to be the problem with the same parameters as Π, whose answer is ‘no’ whenever the answer to Π is ‘yes’, and vice-versa. Prove that Πc is solvable iff P i is solvable. (Hint: Write some pseudo code to solve each problem, given a function to solve the other). 2. We can imagine the relation R on the set of all decision problems P , defined by (Π2 , Π1 ) ∈ R iff Π1 is ‘at least as hard as’ problem Π2 , in the sense defined in Lecture 14. Does R have each of the following properties? Justify your answers. (a) symmetric (b) transitive (c) reflexive 3. This question concerns the properties of some graphs, and can be considered as extension material. (a) The complete graph with n nodes, Kn , is the graph with node set V and edge set E = {{v1 , v2 }|v1 ∈ V and v2 ∈ V and v1 = v2 }. Draw a graphical representation of K1 , K2 , K3 , and K4 . (b) A subgraph of a graph with node set V and edge set E is any graph with node set V and edge set E , where V ⊆ V and E ⊆ E. Partition the graphs in Figures 8(a) and (b) into the minimum number of complete subgraphs (no two subgraphs can share a node). (c) A Challenge: The sub-graph partitioning problem is: Given a graph with node set V and edge set E, is there a partition into no more than k complete subgraphs? Prove that this problem is solvable. (Hint: We can label each node with a partition number from 1 to k. There are a total of k |V | possible labellings, some of which will correspond to partitions into complete subgraphs, and some of which may not. You could try to write some pseudo-code to try each one of these labellings in turn, and check whether it corresponds to a complete subgraph.) (d) The complement of a graph with node set V and edge set E is the graph with the same node set, and with edge set E = {{v1 , v2 }|v1 ∈ V and v2 ∈ V and v1 = / E}. Draw the complements of the graphs in Figures 8(a) and v2 and {v1 , v2 } ∈ (b). (e) Another Challenge: Prove that if the sub-graph partitioning problem is tractable, then so is the k-colouring problem. (Hint: Think about the relationship between the sub-graph partitioning of a graph, and the k-colouring of its complement). 7 Some questions adapted from Rayward-Smith. gac1. 11 1 1 2 3 3 4 2 4 5 5 (b) (a) Figure 8: Some graphs 12 Discrete Mathematics and Computational Complexity8 Sheet 1: Solutions 1. (a) {−1, 1}. (b) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}. (c) {81, 64, 49, 36, 25, 16, 9, 4, 1, 0}. (d) ∅. 2. (a) Union: {z ∈ Z|z is odd or positive} = {. . . , −5, −3, −1, 1, 2, 3, 4, 5, . . .}. (b) Intersection: {z ∈ Z|z is odd and positive} = {1, 3, 5, . . .}. 3. If A ∩ B = ∅, then clearly |A ∪ B| = |A| + |B|. For the general case, let us write |A ∪ B| = |A − (A ∩ B)| + |B − (A ∩ B)| + |A ∩ B|. Since A ∩ B ⊆ A, and A ∩ B ⊆ B, we obtain |A ∪ B| = |A| − |A ∩ B| + |B| − |A ∩ B| + |A ∩ B| = |A| + |B| − |A ∩ B|. 4. |A × B| = nm. 5. Since A × B = {(a, b)|a ∈ A and b ∈ B}, we must conclude that there is no such pair of elements. Thus either A = ∅, or B = ∅, or both. 6. (a) The cardinality of a power-set |P (A)| = 2|A| . But |∅| = 0, so it cannot be the power-set of any set. (b) {∅, {a}} = P ({a}). (c) {∅, {a}, {∅, a}} cannot be a power set, as there is an element {∅, a}, but no element {∅}. (d) {∅, {a}, {b}, {a, b}} = P ({a, b}). 7. (a) |P ({a, b, {a, b}})| = 2|{a,b,{a,b}}| = 23 = 8. (b) |P ({∅, a, {a}, {{a}}}) = 2|{∅,a,{a},{{a}}}| = 24 = 16. |∅| (c) |P (P (∅))| = 22 {∅, {∅}}. 0 = 22 = 21 = 2. Note that here the elements are P (P (∅)) = 8. (a) R = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 4), (2, 6), (3, 3), (3, 6)}. (b) See Figure 9. 9. (a) Transitive (if a is taller than b, and b is taller than c, then a is taller than c). Not reflexive (a is not taller than a). Not symmetric (if a is taller than b, then b is not taller than a). (b) Transitive (if a was born on the same day as b, and b was born on the same day as c, then a was born on the same day as c). Reflexive (a was born on the same day as a). Symmetric (if a was born on the same day as b, then b was born on the same day as a). (c) Transitive (if a was has the same name as b, and b has the same name as c, then a has the same name as c). Reflexive (a has the same name as a). Symmetric (if a has the same name as b, then b has the same name as a). 8 gac1. 13 2 3 1 5 4 6 Figure 9: The digraph of R = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 4), (2, 6), (3, 3), (3, 6)}. 1 2 1 2 3 4 3 4 (b) its transitive closure R∗ (a) the original relation R Figure 10: The digraphs of R = {(1, 2), (2, 1), (2, 3), (3, 4), (4, 1)} and its transitive closure. (d) Not transitive (if a has a common grandparent with b, and b has a common grandparent with c, it does not necessarily follow that a has a common grandparent with c; they may be on different branches of the family tree. Reflexive (a has a common grandparent with a). Symmetric (if a has a common grandparent with b, then b has a common grandparent with a). 10. It is easiest to see this in graphical form. The digraph of the relation R is shown in Figure 10(a). We can construct the transitive closure by adding an edge between two nodes (a, b) if there is a path of any positive length between these nodes in the original graph. This results in the relation R∗ shown in Figure 10(b). In this case, R = {1, 2, 3, 4} × {1, 2, 3, 4}, i.e. the relation contains all possible ordered pairs. 11. This is trivially true for n = 1. We will prove for n > 1 by induction. Assume Rn is reflexive, and demonstrate that Rn+1 is reflexive. We have that (a, a) ∈ R, and (a, a) ∈ Rn for all a ∈ A. Now Rn+1 = R • Rn = {(a, c)| there is a b such that (a, b) ∈ R and (b, c) ∈ Rn }. Thus for any a ∈ A, if (a, a) ∈ Rn and (a, a) ∈ R, it follows that (a, a) ∈ Rn+1 . ✷ 14 Discrete Mathematics and Computational Complexity9 Sheet 2: Solutions 1. From the function definition, the domain is Z and the codomain is Z. The function’s range is the set f (Z) = {n2 + 1|n ∈ Z} = Z+ . x − 1, if x is even 2. (a) f (x) = is bijective. It is its own inverse: f −1 (x) = f (x). x + 1, if x is odd (b) f (x) = 2x is injective. If y = f (x), then x = y/2. However, it is not surjective, as f (Z+ ) = {x|x > 0 and x is even} = Z+ . x/2, if x is even (c) f (x) = is surjective. For any y ∈ Z+ , there is an x = 2y (x + 1)/2, if x is odd such that f (x) = y. However, it is not injective as, for example f (1) = f (2) = 1. (x + 2)/2, if x is even (d) f (x) = is not surjective, as there is no x such that (x + 3)/2, if x is odd f (x) = 1. It is also not injective as, for example f (1) = f (2) = 2. 3. (a) f (m, n) = 2m − n is surjective. For any y ∈ Z, f (0, −y) = y. (b) f (m, n) = m + n + 1 is surjective. For any y ∈ Z, f (0, y − 1) = y. (c) f (m, n) = |m| − |n| is surjective. For any y ∈ N, f (y, 0) = y. For any y ∈ Z− , f (0, y) = y. Since Z = N ∪ Z− , the entire codomain is covered. (d) f (m, n) = m2 − 4 is not surjective. For example, there is no pair (m, n) ∈ Z2 such that f (m, n) = −5. 4. f • g = a(cx + d) + b = acx + ad + b. g • f = c(ax + b) + d = acx + bc + d. For f • g = g • f , we require ad + b = bc + d. 5. (a) Since |A| < |B|, no function can be surjective. But we can answer part (i). One such function is f (1) = 3, f (2) = 4. (b) Since |B| > |A|, no function can be injective. But we can answer part (ii). One such function is f (3) = 1, f (4) = 2, f (5) = 1. (c) Here are the possible functions from A to A: i. f (1) = 1, f (2) = 1. This function is neither surjective nor injective. ii. f (1) = 1, f (2) = 2. This function is both surjective and injective; it is a bijection. iii. f (1) = 2, f (2) = 1. This function is both surjective and injective; it is a bijection. iv. f (1) = 2, f (2) = 2. This function is neither surjective nor injective. 6. Figure 11 shows the relevant truth tables. Examining the final column on each side demonstrates equivalence. Alternatively, p ↔ q ≡ (p → q) ∧ (q → p) ≡ (¬p ∨ q) ∧ (¬q ∨ p) ≡ ((¬p ∨ q) ∧ ¬q) ∨ ((¬p ∨ q) ∧ p) ≡ (¬p ∧ ¬q) ∨ (q ∧ p). 7. (a) r ∧ ¬q. Note that the English ‘but’ here means ‘and’ ! (b) p ∧ q ∧ r. This one is straight-forward, but note the missing word ‘and’ from the English version. 9 gac1. 15 p F F T T q F T F T p↔q T F F T p∧q F F F T ¬p ∧ ¬q T F F F (p ∧ q) ∨ (¬p ∧ ¬q) T F F T Figure 11: A truth table to demonstrate p ↔ q ≡ (p ∧ q) ∨ (¬p ∧ ¬q) (c) p → r. The key phrase here is ‘it is necessary’; this means an implication somewhere. Make sure your implication is the right way round: if you get first class honours, you get an A in this subject. (d) p ∧ ¬q ∧ r. Note that the English ‘but’ and ‘nevertheless’ have become ‘and’ in logic. (e) r ∧ q → p. The key phrase here is ‘is sufficient’; this means an implication somewhere. (f) p ↔ q ∨ r. 16 Discrete Mathematics and Computational Complexity10 Sheet 3: Solutions 1. (a) There is a student who spends more than ten hours private study time per week. (b) All students spend more than ten hours private study time per week. (c) There is a student who doesn’t spend more than ten hours private study time per week. (d) No student spends more than ten hours private study time per week. 2. Let P (x) be the predicate “x is a dog”. Let Q(x) be the predicate “x has fleas”. Let R(x) be the predicate “x is a horse”. Let S(x) be the predicate “x is a koala”. Let T (x) be the predicate “x can climb”. Let U (x) be the predicate “x is a monkey”. Let V (x) be the predicate “x can speak French”. Let W (x) be the predicate “x is a pig”. Then we have: (a) ∀x(P (x) → Q(x)). (For all x, if x is a dog, then x has fleas). (b) ∃x(R(x) ∧ Q(x)). (There is an x that is a horse and has fleas). (c) ∀x(S(x) → T (x). (For all x, if x is a koala, then x can climb). (d) ∀x(U (x) → ¬V (x)). (For all x, if x is a monkey, then x cannot speak French). Some people prefer to think of this as ¬∃x(U (x) ∧ V (x)). (There is no x that is both a monkey and can speak French). These two versions are logically equivalent - can you prove this? (e) ∃x(W (x) ∧ T (x) ∧ ¬V (x)). (There is an x such that x is a pig and x can climb and x can’t speak French). 3. (a) This is true. We know that 1 > 0. Adding n to both sides results in n + 1 > n, which will therefore be true for any integer n. Thus we have ∀n(n + 1 > n). (b) This is true. We can prove this by identifying one n satisfying the condition. For n = 0, we have 2n = 3n. Since 0 is an integer, we have established that ∃n(2n = 3n) (existential generalization). (c) This is true. We can prove this by identifying one n satisfying the condition. For n = 0, we have n = −n. Since 0 is an integer, we have established that ∃n(2n = 3n) (existential generalization). (d) This is true. We will treat the three cases n ≤ −1, n ≥ 1, and n = 0 separately. First consider n = 0. In this case, n2 ≥ n as 0 ≥ 0. Next consider n ≥ 1. Multiplying both sides by n gives n2 ≥ n. Finally, consider n ≤ −1. Multiplying both sides by n gives n2 ≥ n (the inequality is reversed, since n is negative). This covers all the cases of n, and so the proposition is true. (e) This is true. To prove this, we can identify an m (which may depend on n), satisfying the condition n2 < m. m = n2 + 1 is one such value. (f) This is true. To prove this, we can identify an n (which does not depend on m), satisfying the condition n < m2 , no matter what m we choose. One possibility is n = −1, since the square of an integer will always be non-negative. 10 gac1. 17 (g) This is true. To prove this, we can idenify an m (which may depend on n), satisfying the condition n + m = 0. One such m is the value m = −n, which is an integer. (Actually this is the only such value, but this is irrelevant to truth of the proposition). (h) This is true. To prove this, we need to identify an n (which does not depend on m), satisfying the condition nm = m, no matter what m we choose. One such value is n = 1. (As above, this is a unique value, but that is irrelevant to the truth of the proposition). (i) This is false. To prove this, let us assume it is true. Then, by universal instantiation, we can choose an abitrary m. Let us choose m = 3. Then n2 + 9 = 5, or n2 = −4. But there is no integer value of n for which this is true. This leads to a contradiction, and so the original proposition was false. (Alternatively, we could have chosen two different values of m, and demonstrated that the same n won’t hold for both). (j) This is false. We can apply universal instantiation with n = 0, m = 1, to give ∃p(p = 1/2). But there is no value 1/2 in the set of integers. Thus the original proposition was false. 4. (a) p → p ∨ q. This is addition. (b) p ∧ q → p. This is simplification. (c) (p → q) ∧ p → q. This is modus ponens. (d) (p → q) ∧ ¬q → ¬p. This is modus tollens. (e) (p → q) ∧ (q → r) → (p → r). This is a hypothetical syllogism. 5. (a) This is invalid. The hypotheses were p → q and q. The ‘conclusion’ was p. This is an example of affirming the conclusion. (b) This is valid. The hypotheses were p → q and p. The conclusion was q. This is an application of modus ponens. (c) This is valid. The hypotheses were p → q and ¬q. The conclusion was ¬p. This is an application of modus tollens. (d) This is invalid. The hypotheses were p → q and ¬p. The ‘conclusion’ was ¬q. This is an example of denying the hypothesis. 6. (a) Let P (x) be the predicate “x owns a red convertible”. Let Q(x) be the predicate “x has got a speeding ticket. We will use the set people as the universe of discourse. Then there are two hypotheses: (i) P (Linda), and (iii) ∀x(P (x) → Q(x)). The steps in the argument are: i. ∀x(P (x) → Q(x)). Therefore P (Linda) → Q(Linda). (Universal instantiation). ii. (P (Linda) → Q(Linda))∧P (Linda). Therefore Q(Linda). (Modus ponens). iii. Q(Linda). Therefore ∃xQ(x). (Existential generalisation). (b) Let P (x) be the predicate “x was directed by Ken Loach”. Let Q(x) be the predicate “x is wonderful”. Let R(x) be the predicate “x is about the Spanish Civil War”. Let the universe of discourse be the set of films. Then there are two hypotheses: (i) ∀x(P (x) → Q(x)), (ii) ∃x(P (x) ∧ R(x)). The steps in the argument are: 18 i. ∀y(P (y) → Q(y)) and ∃x(P (x) ∧ R(x)). Therefore ∃x(P (x) ∧ R(x) ∧ (P (x) → Q(x))). (Universal instantiation). Note that I re-labelled the variable x as y in the first hypothesis to avoid confusion: here y is the general variable used for universal quantification, and x is the specific instantiation. ii. ∃x(P (x) ∧ R(x) ∧ (P (x) → Q(x))). Therefore ∃x(Q(x) ∧ R(x)). (Modus ponens). 19 Discrete Mathematics and Computational Complexity11 Sheet 4: Solutions 1. A sketch of a proof for each case is given below. (a) Firstly, we will show that the function 2x is O(3x ). One way of doing this is to observe that log2 2 = 1 < log2 3. So for x > 0, we have x log 2 < x log 3, and so 2x < 3x . Thus using constants k = 0, c = 1, we have that 2x is O(3x ). Also, 17 is trivially O(3x ); for example, constants k = 0 and c = 17 demonstrate this, as 17 ≤ 17 · 3x for all x > 0. By using the formula for the sum of Big-O expressions, 2x + 17 is O(max(3x , 3x )) = O(3x ). 2 +1 2 2 (b) We may re-write xx+1 = x − 1 + x+1 . The term x+1 is O(x): choosing k = 1, 2 2 c = 1 gives x+1 < 1 for all x > 1, and so clearly x+1 < x for all x > 1. The term x − 1 is O(x) from our theorem on the big-O of polynomials. Thus, using 2 +1 is O(x). the formula for the sum of Big-O expressions, xx+1 3 +2x 9 9 = 12 x2 − 14 x + 98 − 8(2x+1) . Again, the term 8(2x+1) is (c) We may re-write x2x+1 9 2 O(x ): choosing k = 1, c = 1 gives 8(2x+1) < 1 for all x > 1, and so clearly 2 1 2 1 9 2 2 x+1 < x for all x > 1. The term 2 x − 4 x + 8 is O(x ) from our theorem on the big-O of polynomials. Thus, using the formula for the sum of Big-O expressions, x3 +2x 2 2x+1 is O(x ). 2. A sketch of a proof for each case is given below. (a) f (x) cannot be O(x2 ) or below, as there is an x3 term. 2x3 is O(x3 ), so the question we need to ask is whether x2 ln x is O(x3 ). To answer this, note that ln x < x (you could show this by finding the turning points of y = ln x − x). Thus using any k, and c = 1, we have that ln x is O(x). Using the formula for the product of big-O expressions, we obtain x2 ln x is O(x3 ). Therefore the least integer n = 3. (b) Performing a long-division on the expression for f (x) will result in a polynomial of degree 1, and possibly a remainder of the form xn(x) 3 +1 , where n(x) is a polynomial with maximum degree 2. From the first of these two terms, n must be at least 1. As with the previous question, we can choose for large enough x, the remainder will be less than 1. Thus the remainder term is also O(x2 ). Therefore the least integer n = 2. 4 (c) We may split this into f (x) = x4x+1 + x5 4ln+1x . The second term is always positive for x > 1, and by a similar argument to the previous example, the first term is O(1). Thus n must be at least 0. To check whether n = 0 is sufficient, we need to ensure that the second term is O(1). From above, ln x < x, so x54ln+1x < x45x +1 . But this expression is O(1), as we can choose, for example k = 2, c = 1 to ensure that numerator is always greater than the denominator and so x45x +1 < 1. Since the RHS of this inequality is O(1), the LHS must also be O(1), and so f (x) = is O(1). x4 +5 ln x x4 +1 3. If f1 (x) is Ω(g1 (x)) and f2 (x) is Ω(g2 (x)), then there exist constants k1 > 0, k2 > 0, c1 > 0, and c2 > 0 such that for all x > k1 , |f1 (x)| ≥ c1 |g1 (x)|, and for all x > k2 , 11 gac1. 20 |f2 (x)| ≥ c2 |g2 (x)|. We also know that for all x, f1 (x) ≥ 0 and f2 (x) ≥ 0. Consider the sum f1 (x) + f2 (x). We have |f1 (x) + f2 (x)| = f1 (x) + f2 (x) = |f1 (x)| + |f2 (x)|, as f1 and f2 are non-negative. Therefore for all x > max(k1 , k2 ), |f1 (x) + f2 (x)| ≥ c1 |g1 (x)| + c2 |g2 (x)|. But since c1 and c2 are positive, we also have |f1 (x) + f2 (x)| ≥ max(c1 |g1 (x)|, c2 |g2 (x)|) ≥ min(c1 , c2 ) max(|g1 (x)|, |g2 (x)|). Therefore f1 (x) + f2 (x) is Ω(max(|g1 (x)|, |g2 (x)|)). 4. (a) i. power = 1, y = 1. ii. {i = 1}: power = 2, y = 1 + 1 ∗ 2 = 3. iii. {i = 2}: power = 4, y = 3 + 3 ∗ 4 = 15. (b) Two multiplications per iteration of the for loop. So 2n multiplications. (c) One addition per iteration of the for loop. So n additions. (d) The number of additions is O(n), and the number of multiplications is O(n). The initialization of ‘power’, y, and of the loop is O(1), the loop variable updates are, in total, O(n), and we can assume that a single array indexing operation is O(1). So in total, the execution time is O(n). 5. (a) (b) (c) (d) (e) i. y = 3 ii. {i = 1}: y = 3 ∗ 2 + 1 = 7. iii. {i = 2}: y = 7 ∗ 2 + 1 = 15. This method requires one multiplication per iteration of the loop, giving a total of n multiplications. This method requires one addition per iteration of the loop, giving a total of n addition operations. Following a similar argment to the previous example, this algorithm is O(n). Algorithm poly2 has the lower computational complexity, as it contains fewer multiplications each iteration, while keeping the same number of additions and loop iterations. It also has one less initialization statement. Note that although poly2 has lower computational complexity, both algorithms are O(n), as they are only out by a multiplicative factor. The only reason we are even able to say that poly2 has lower computational complexity is that the number of every type of operation performed in poly2 is less than or equal to that in poly1. 6. The following operations are performed by the code: (a) (b) (c) (d) (e) (f) (g) (h) (i) Initializing i := 1. This contributes O(1). Initializing j := 1. This contributes O(n), as it is done n times in total. Initializing c[i, j] := 0. This contributes O(n2 ), as it is done n2 times in total. Initializing k := 1. This contributes O(n2 ), as it is done n2 times in total. Multiplying a[i, k] by b[k, j]. This contributes O(n3 ), as it is done n3 times in total. Adding c[i, j] to the result of the multiplication. This contributes O(n3 ) for the same reason. Updating k. This contributes O(n3 ). Updating j. This contributes O(n2 ). Updating i. This contributes O(n). The total execution time is thus O(n3 ) + O(n2 ) + O(n) + O(1) = O(n3 ). 21 Discrete Mathematics and Computational Complexity12 Sheet 5: Solutions 1. Let an denote the number of bacteria after n hours. (a) We have an = 3an−1 , i.e. the number triples every hour. (b) This is a straight-forward first-degree linear homogeneous recurrence relation with constant coefficient. We have an = a0 3n . (c) Thus a10 = 100 · 310 = 5, 904, 900. 2. (a) Assume solution holds for n − 1 and n − 2. Then an−1 + 2an−2 + 2n − 9 = −(n − 1) + 2 + 2(−(n − 2) + 2) + 2n − 9 = −n + 2 = an , so true for n. (b) Assume solution holds for n − 1 and n − 2. Then an−1 + 2an−2 + 2n − 9 = 5(−1)n−1 − (n − 1) + 2 + 10 · (−1)n−2 − 2(n − 2) + 4 + 2n − 9 = 5(−1)n−2 (2 + 1(−1)) − n = 5(−1)n−2 − n + 2 = 5(−1)n ((−1)−2 ) − n + 2 = 5(−1)n − n + 2, so true for n. (c) Assume solution holds for n − 1 and n − 2. Then an−1 + 2an−2 + 2n − 9 = 3(−1)n−1 + 2n−1 − (n − 1) + 2 + 6(−1)n−2 + 2n−1 − 2(n − 2) + 4 + 2n − 9 = 3(−1)n (−1 + 2(−1)−2 ) + 2n − n + 2 = 3(−1)n + 2n − n + 2, so true for n. (d) Assume solution holds for n−1 and n−2. Then an−1 +2an−2 +2n−9 = 7·2n−1 − (n−1)+2+14·2n−2 −2(n−2)+4+2n−9 = 7·2n (2−1 +2·2−2 )−n+2 = 7·2n −n+2, so true for n. 3. Each of the recurrence relations is a linear recurrence relation of degree 2 with constant coefficients. Each has the form an = c1 an−1 + c2 an−2 + F (n) (except for the final one, which can be brought into this form with the substitution n = n + 2). In each case, we will therefore consider the solutions to the equation r2 − c1 r − c2 = 0. (a) r2 − r − 6 = 0 has distinct solutions r1 = −2 and r2 = 3. Then a solution has the form an = α1 (−2)n + α2 3n . Substituting n = 0 gives 3 = α1 + α2 , and n = 1 gives 6 = −2α1 + 3α2 . So α2 = 12/5 and α1 = 3/5. (b) r2 − 7r + 10 = 0 also has distinct roots r1 = 2, and r2 = 5. The solution proceeds as above. (c) r2 − 6r + 8 = 0 also has distinct roots r1 = 2, and r2 = 4. The solution proceeds as above. (d) r2 − 7r + 10 = 0 has distinct roots r1 = 2, and r2 = 5. A solution therefore has the form an = α1 2n + α2 5n + 1. Substitute, as before, to find the values of α1 and α2 . (e) r2 − 1 = 0 has distinct roots r1 = −1, and r2 = 1. However, c1 + c2 = 1, and so the the theorem cannot be applied. But this is an interesting case, which we may be able to solve directly. We could consider splitting the sequence into two subsequences: ‘evens’ and ‘odds’, i.e. {a0 , a2 , a4 , . . .} and {a1 , a3 , a5 , . . .}. Note that the recurrence relation defines an in terms of an−2 and does not refer to an−1 . Thus we may consider the two subsequences separately. Define an = an−1 + 1, with a0 = 5 for the ‘even’ sequence, and an = an−1 + 1, with a0 = −1 for the ‘odds’. Then we can stitch together the two sequences, obtaining a2n = an , and 12 gac1. 22 a2n+1 = an . Each of the individual recurrences can be solved (they are both the c = 1 case of the degree-1 linear non-homogeneous recurrence relation with constant coefficients). (f) After applying the substitution referred to above, we obtain r2 + 4r − 5 = 0, which has two distinct roots r1 = −5 and r2 = 1. We may then proceed as with the previous questions. 4. For the original code, we have an = 1 + an−1 + an−1 = 1 + 2an−1 for n > 0, with a0 = 0. This is a non-homogeneous linear recurrence of degree 1. From our theorems, we obtain an = α2n + 1/(1 − 2) = α2n − 1. Substituting n = 0 gives α − 1 = 0, so we obtain an = 2n − 1. For the transformed code, we have an = 1 + an−1 multiplications, for n > 0, with a0 = 0. This is again a non-homogeneous linear recurrence of degree 1. From our theorems, we obtain an = α + n. Substituting n = 0 gives α = 0, so we obtain an = n. This is a very significant improvement in code quality: from exponential to linear run-time! For example, a call to linrec( 50 ) would require approximately 1.13×1015 multiplication operations, whereas linrec2( 50 ) would require 50 multiplication operations to achieve the same result. At a clock frequency of 1GHz, and with one multiplication per cycle, this would be the difference between 13 days run time and 50ns run time(!) 5. For addition, an = an−1 + an−2 for n > 1, with a0 = 1 and a1 = 1. For multiplication, mn = 2 + mn−1 + mn−2 for n > 1, with m0 = 0 and m1 = 0. For n = 0, there is just c0 = 1 comparison (if n = 0). For n = 1, there are c1 = 2 comparisons: if n = 0 and else if n = 1. For n > 1, there are cn = 2 + cn−1 + cn−2 comparisons. We shall now examine the solution of each of these recurrences in turn. (a) The recurrence an = an−1 + an−2 is linear homogeneous, degree 2. Examining r2 − r − 1 gives two distinct solutions, exactly the same as√the fibonacci sequence √ example in the lecture, i.e. r1 = (1 − 5)/2, r2 = (1 + 5)/2. Also the initial conditions the example in the lecture notes. Thus we obtain are√the n same as √ n 1 1+ 5 1− 5 . − an = √ 5 2 2 (b) The recurrence mn = 2 + mn−1 + mn−2 is linear non-homogeneous, degree 2. We have the same roots r1 and r2 as above, leading to the general form of solution mn = α1 r1n + α2 r2n − 2. Substituting n = 0 gives α1 + α2 = 2, and substituting 1 −1) √1 n = 1 gives α1 r1 + α2 r2 = 2. Thus α2 (r1 − r2 ) = 0, giving α2 = 2(r r1 −r2 = 1 + 5 , √ n 1− 5 and α2 = 1 − √15 . The overall solution is therefore an = 1 − √15 + 2 √ n 1+ 5 1 + √15 − 2. 2 (c) The recurrence cn = 2 + cn−1 + cn−2 is identical to the previous one, except for the initial conditions c0 = 1 and c1 = 2. For n = 0, we obtain α1 + α2 = 3. For n = 1, we obtain α√1 r1 + α2 r2 = 4. Following √ through in a similar manner gives a2 = 12 5 + 3 5 and α1 = 12 1 − 3 5 . We therefore obtain √ √ n √ √ n − 2. an = 12 1 − 3 5 1−2 5 + 5 + 3 5 1+2 5 (d) Let us simplify the discussion by keeping the values r1 and r2 as defined above. Then an is the sum of an O(r1n ) function and an O(r2n ) function, which is an O(r2n ) function. bn is similar, except there is also an O(1) component; it is an O(r2n ) function. cn is also an O(r2n ) function for the same reason. Thus no matter what the relative time taken by each of the operations, the total time will be a 23 weighted sum of these functions with constant resulting in an O(r2n ) weights, √ n 1+ 5 . function. The execution time thus scales as O 2 24 Discrete Mathematics and Computational Complexity13 Sheet 6: Solutions 1. (a) A tournament for n teams has one more round than a tournament for n/2 teams: f (n) = f (n/2) + 1, with f (1) = 0. Hence from the Master Theorem, a = bd , so f (n) is O(log n). (b) A tournament for n teams has n/2 more games than a tournament for n/2 teams: g(n) = g(n/2) + n/2, with g(1) = 0. Hence from the Master Theorem a < bd , so g(n) is O(n). 2. (a) i. The outer loop iterates n times. The inner loop iterates n times for each iteration of the outer loop. So a(n) = n2 . ii. For n > 1, the algorithm recursively calls itself 8 times, each on a size n/2 problem. It also calls matrixsum 4 times, each on a size n/2 problem, so executing 4a(n/2) = 4(n/2)2 = n2 addition operations. The total number of operations is therefore f (n) = 8f (n/2) + n2 , with f (1) = 1, a single multiplication for the base case. iii. Applying the Master Theorem, we have f (n) is O(nlog2 8 ) = O(n3 ). The iterative algorithm from Sheet 4 had n3 multiplication operations, and the same number of additions. They are both O(n3 ). (b) i. The code is a straight-forward translation of the equations, with the same base case for the recursion as recmatmult1. ii. This time, in order to perform an n×n matrix multiply, we need only 7 n/2× n/2 matrix multiplication operations, but many more (18) n/2 × n/2 matrix additions or subtractions, each of size n/2 and thus requiring 18(n/2)2 = 9n2 /2 scalar operations. The total number of operations is thus f (n) = 7f (n/2) + 9n2 /2, with f (1) = 1 (the same base case). iii. Applying the Master Theorem, we have f (n) is O(nlog2 7 ) or O(n2.81 ). This is a reduced bound on the number of operations required, compared to either the iterative version of Sheet 4 or the straight-forward recursive implementation. 13 gac1. 25 Discrete Mathematics and Computational Complexity14 Sheet 7: Solutions 1. We wish to prove that Πc is solvable iff P i is solvable. We can first prove that if Π is solvable, then Πc is solvable, and then prove that if Πc is solvable, then Π is solvable. This can be done by constructing algorithms solveΠc and solveΠ, as illustrated in Fig. 12. The first of these two algorithms proves that if P i is solvable, we can obtain a solution for Πc . The second illustrates a method for obtaining a solution for Π if a solution exists for Πc . 2. The definition of Π1 is ‘at least as hard as’ Π2 is that a general instance of Π2 can be transformed into an instance of Π1 . (a) If a general instance of Π2 can be transformed into an instance of Π1 , it doesn’t necessarily follow that a general instance of Π1 can be transformed into an instance of Π2 . So it does not follow that R is symmetric. (b) If a general instance of Π2 can be transformed into an instance of Π1 , and a general instance of Π1 can be transformed into an instance of Π3 , it follows that a general instance of Π2 can be transformed into an instance of Π3 . So if (Π2 , Π1 ) ∈ R and (Π1 , Π3 ) ∈ R, then it follows that (Π2 , Π3 ) ∈ R. Thus R is transitive. (c) A general instance of Π1 can be trivially ‘transformed’ into an instance of itself: if we have an algorithm solveΠ1 to solve Π1 , we can construct another one alsosolveΠ1 which just calls solveΠ1 with the same argument. So R is reflexive. 3. (a) The graphs are shown in Figure 13. (b) The graph in Figure 8(a) can be partitioned into two comple subgraphs. One possible solution is to have one subgraph with node set {1, 2, 3} and the other with node set {4, 5}. The alternative is to have node 3 in the other subgraph. The graph in Figure 8(b) can be partitioned into three complete subgraphs. One possible solution is to have node sets {1}, {2, 3}, and {4, 5}. Another is to have node sets {1}, {2}, and {3, 4, 5}. (c) Some possible pseudo-code is shown in Figure 14. (d) The graph complements are shown in Figure 15. (e) 14 gac1. 26 procedure solveΠc ( p ) begin if solveΠ( p ) = true then result := false else result := true end procedure solveΠ( p ) begin if solveΠc( p ) = true then result := false else result := true end Figure 12: Algorithms to prove that Πc is solvable iff Π is solvable. (a) (b) (c) (d) Figure 13: (a) K1 , (b) K2 , (c) K3 , and (d) K4 . 27 procedure graphpart( V , E, k ) begin // we will assume that V and E can be treated as arrays // label[] will store the label of each node result := graphlabel( V , E, k, label, 0 ) end procedure graphlabel( V , E, k, label, n ) begin // graphlabel takes in a graph that has already had V [0] to V [n] labelled if n = |V | then // already labelled all nodes result := checklabelling( V , E, k, label ) else begin // still more nodes to label trial := 1 success := false while trial ¡= k and not(success) // try each possible label for V [n] in turn label[V[n]] := trial success := graphlabel( V , E, k, label, n + 1 ) end result := success end end procedure checklabel( V , E, k, label ) begin // checklabel takes in a labelled graph and sees whether this label corresponds to // a partition into complete subgraphs result := true for i := 1 to |V | do // check that all nodes with this label are connected to all other nodes with this label for j := i + 1 to |V | do if label[V [i]] = label[V [j]] do if {V [i], V [j]} ∈ / E do result := false end Figure 14: A possible algorithm to solve the graph partitioning problem. 28 1 1 2 3 3 4 2 4 5 5 (b) (a) Figure 15: Complements of the graphs in Figure 8. 29