29
Discrete Mathematics and Computational Complexity 1 Sheet 1: Sets and Relations 1. List the members of these sets. (a) {x|x is a real number such that x 2 =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 x 2 =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 R n is reflexive for all n Z + . 1 Questions adapted from Rosen. gac1. 1

Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

  • Upload
    dinhdat

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 theset 1, 2, 3, 4.

11. Let R be a reflexive relation on A. Show that Rn is reflexive for all n ∈ Z+.

1Questions adapted from Rosen. gac1.

1

Page 2: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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. Determinefor 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 “youdo 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 logicalconnectives.

(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; never-

theless, 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.

2Questions adapted from Rosen. gac1.

2

Page 3: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 thesequantifications 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. Theuniverse 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. Theuniverse 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 studiescomputer science.

(b) Jerry studies mathematics and computer science. Therefore Jerry studies math-ematics.

(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.

3Questions adapted from Rosen. gac1.

3

Page 4: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

(e) If I go swimming, then I will stay in the sun too long. If I stay in the sun toolong, 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 whichrule of inference is being used. If it is invalid, explain which logical error has beenmade.

(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. Thereforesince log2 3 cannot be written in the form a/b, where a and b are integers, it isirrational.

(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 predicatesand symbolic logical connectives. State which rule of inference is being used at eachstep.

(a) Linda owns a red convertible. Everyone who owns a red convertible has got aspeeding ticket. Therefore someone has got a speeding ticket.

(b) All films directed by Ken Loach are wonderful. Ken Loach directed a film aboutthe Spanish Civil War. Therefore there is a wonderful film about the SpanishCivil War.

4

Page 5: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

Discrete Mathematics and Computational Complexity4

Sheet 4: Growth of Functions and Complexityof Algorithms

1. Show that:

(a) 2x + 17 is O(3x). (Hint: Do each term separately. For the first term, start withlog2 2 < log2 3).

(b) x2+1x+1 is O(x).

(c) x3+2x2x+1 is O(x2).

2. Find the least integer n such that f(x) is O(xn) for each of these functions.

(a) f(x) = 2x3 + x2 lnx.

(b) f(x) = x4+x2+1x3+1 .

(c) f(x) = x4+5 ln xx4+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) representalgorithm execution time, it is known that f1(x) and f2(x) are never negative. Inthis 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 thepolynomial anx

n + an−1xn−1 + . . . a1x+ 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 evaluatingthe 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 matricesa[] and b[], storing the result in matrix c[]. Obtain a big-O estimate for the executiontime of this algorithm.

4Questions adapted from Rosen. gac1.

5

Page 6: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

procedure poly1( c : real, a[0 to n] : real )beginpower := 1y := a[0]for i := 1 to nbeginpower := power*cy := y + a[i]*power

endend

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 nfor j := 1 to nbegin

c[i, j] := 0for 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

Page 7: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

Discrete Mathematics and Computational Complexity5

Sheet 5: Linear Recurrence Relations andRecursive 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 thecolony 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 relationfor the number of multiplication operations performed, an. An optimizing compilermay recognise that the code in Fig. 4 can be transformed into the code shown inFig. 5. Write, and solve, a linear recurrence relation for the number of multiplicationoperations 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.

5Most questions adapted from Rosen. gac1.

7

Page 8: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

procedure linrec( n: integer )begin

if n = 0 thenresult := 1

elseresult := linrec( n− 1 ) * linrec( n− 1 ) + 5

end

Figure 4: Linear recursion example.

procedure linrec2( n: integer )begin

if n = 0 thenresult := 1

elsebegintemp := linrec2( n− 1 )result := temp*temp + 5

endend

Figure 5: Optimized linear recursion example.

procedure linrec3( n: integer, a[0 to 1]: integer )begin

if n =0 thenresult := 3 + a[n]

else if n = 1 thenresult := 5 + a[n]

elseresult := 3 * linrec3( n− 1 ) * linrec3( n− 2 )

end

Figure 6: Another linear recursion example.

8

Page 9: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

Discrete Mathematics and Computational Complexity6

Sheet 6: Divide-and-Conquer RecurrenceRelations and Recursive Algorithms

1. Suppose that there are n = 2k teams in a knock-out tournament, for some positiveinteger k. There are n/2 games in the first round, with the n/2 winners playing in thesecond 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 tworecursive equivalents.

(a) We may recognise that a 2n× 2n matrix multiplication C = AB can be dividedinto sections, as shown in (1), where Aij , Bij and Cij are n × n matrix multi-plications. Expanding this, we obtain (2). This expansion forms the basis of thealgorithm in Fig. 7, which works when n is an integer power of 2.

[C11 C12

C21 C22

]=

[A11 A12

A21 A22

] [B11 B12

B21 B22

](1)

C11 = A11B11 +A12B21

C12 = A11B12 +A12B22

C21 = A21B11 +A22B21

C22 = A21B12 +A22B22

(2)

i. Write an expression for the number of addition operations, a(n), performedby a call to matrixsum.

ii. Hence write a divide-and-conquer recurrence relation for the total number ofaddition and multiplication operations performed by a call to recmatmult1.

iii. Obtain a big-O expression for this quantity, and compare it to the iterativealgorithm 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

(3)

6Some questions adapted from Rosen. gac1.

9

Page 10: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

procedure recmatmult1( A[1 to n], B[1 to n] )begin

if n = 1 thenresult[1,1] := A[1,1]*B[1,1]

elsebeginresult[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]) )

endend

procedure matrixsum( A[1 to n], B[1 to n] )begin

for i := 1 to nfor j := 1 to nresult[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, writea modified version of recmatmult1, called recmatmult2, that uses this alter-native expansion.

ii. Write a divide-and-conquer recurrence relation for the total number of ad-ditions, subtractions, and multiplication operations performed by a call torecmatmult2.

iii. Obtain a big-O expression for this quantity, and compare it to that forrecmatmult1.

10

Page 11: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

Discrete Mathematics and Computational Complexity7

Sheet 7: Computability

1. If Π is a decision problem, then the complement of Π, written Πc, is defined to be theproblem with the same parameters as Π, whose answer is ‘no’ whenever the answer toΠ is ‘yes’, and vice-versa. Prove that Πc is solvable iff Pi is solvable. (Hint: Writesome 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 Lecture14. 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 exten-sion material.

(a) The complete graph with n nodes, Kn, is the graph with node set V and edge setE = v1, v2|v1 ∈ V and v2 ∈ V and v1 = v2. Draw a graphical representationof K1, K2, K3, and K4.

(b) A subgraph of a graph with node set V and edge set E is any graph with nodeset V ′ and edge set E′, where V ′ ⊆ V and E′ ⊆ E. Partition the graphs inFigures 8(a) and (b) into the minimum number of complete subgraphs (no twosubgraphs can share a node).

(c) A Challenge: The sub-graph partitioning problem is: Given a graph with nodeset V and edge set E, is there a partition into no more than k complete sub-graphs? Prove that this problem is solvable. (Hint: We can label each nodewith 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 ofwhich may not. You could try to write some pseudo-code to try each one of theselabellings 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 withthe same node set, and with edge set E′ = v1, v2|v1 ∈ V and v2 ∈ V and v1 =v2 and v1, v2 /∈ E. Draw the complements of the graphs in Figures 8(a) and(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 betweenthe sub-graph partitioning of a graph, and the k-colouring of its complement).

7Some questions adapted from Rayward-Smith. gac1.

11

Page 12: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

1 2

4

3

5

1 2

4

3

5

(a) (b)Figure 8: Some graphs

12

Page 13: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 pairof 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 thepower-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|∅|= 22

0= 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). Notreflexive (a is not taller than a). Not symmetric (if a is taller than b, then b isnot taller than a).

(b) Transitive (if a was born on the same day as b, and b was born on the same dayas c, then a was born on the same day as c). Reflexive (a was born on the sameday as a). Symmetric (if a was born on the same day as b, then b was born onthe same day as a).

(c) Transitive (if a was has the same name as b, and b has the same name as c, thena has the same name as c). Reflexive (a has the same name as a). Symmetric (ifa has the same name as b, then b has the same name as a).

8gac1.

13

Page 14: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

1

2 3

45

6

Figure 9: The digraph ofR = (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 4), (2, 6), (3, 3), (3, 6).

1 2

43

1 2

43

(a) the original relation R (b) its transitive closure 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 grand-parent with c, it does not necessarily follow that a has a common grandparentwith c; they may be on different branches of the family tree. Reflexive (a has acommon grandparent with a). Symmetric (if a has a common grandparent withb, 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 shownin Figure 10(a). We can construct the transitive closure by adding an edge betweentwo nodes (a, b) if there is a path of any positive length between these nodes in theoriginal 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

Page 15: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

Discrete Mathematics and Computational Complexity9

Sheet 2: Solutions

1. From the function definition, the domain is Z and the codomain is Z. The function’srange is the set f(Z) = n2 + 1|n ∈ Z = Z+.

2. (a) f(x) =

x− 1, if x is evenx+ 1, if x is odd is bijective. It is its own inverse: f−1(x) = f(x).

(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+.

(c) f(x) =

x/2, if x is even(x+ 1)/2, if x is odd is surjective. For any y ∈ Z+, there is an x = 2y

such that f(x) = y. However, it is not injective as, for example f(1) = f(2) = 1.

(d) f(x) =(x+ 2)/2, if x is even(x+ 3)/2, if x is odd is not surjective, as there is no x such that

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). Onesuch function is f(1) = 3, f(2) = 4.

(b) Since |B| > |A|, no function can be injective. But we can answer part (ii). Onesuch 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 abijection.

iii. f(1) = 2, f(2) = 1. This function is both surjective and injective; it is abijection.

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 sidedemonstrates 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.9gac1.

15

Page 16: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

p q p ↔ q p ∧ q ¬p ∧ ¬q (p ∧ q) ∨ (¬p ∧ ¬q)F F T F T TF T F F F FT F F F F FT T T T 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 some-where. Make sure your implication is the right way round: if you get first classhonours, you get an A in this subject.

(d) p ∧ ¬q ∧ r. Note that the English ‘but’ and ‘nevertheless’ have become ‘and’ inlogic.

(e) r ∧ q → p. The key phrase here is ‘is sufficient’; this means an implicationsomewhere.

(f) p ↔ q ∨ r.

16

Page 17: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 timeper 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”. LetR(x) be the predicate “x is a horse”. Let S(x) be the predicate “x is a koala”. LetT (x) be the predicate “x can climb”. Let U(x) be the predicate “x is a monkey”. LetV (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 isboth 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 climband 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. Multiplyingboth sides by n gives n2 ≥ n (the inequality is reversed, since n is negative). Thiscovers 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 isn = −1, since the square of an integer will always be non-negative.

10gac1.

17

Page 18: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

(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 isan integer. (Actually this is the only such value, but this is irrelevant to truth ofthe proposition).

(h) This is true. To prove this, we need to identify an n (which does not depend onm), satisfying the condition nm = m, no matter what m we choose. One suchvalue is n = 1. (As above, this is a unique value, but that is irrelevant to thetruth of the proposition).

(i) This is false. To prove this, let us assume it is true. Then, by universal instanti-ation, we can choose an abitrary m. Let us choose m = 3. Then n2 + 9 = 5, orn2 = −4. But there is no integer value of n for which this is true. This leads to acontradiction, and so the original proposition was false. (Alternatively, we couldhave chosen two different values of m, and demonstrated that the same n won’thold 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 originalproposition 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. Thisis an example of affirming the conclusion.

(b) This is valid. The hypotheses were p → q and p. The conclusion was q. This isan application of modus ponens.

(c) This is valid. The hypotheses were p → q and ¬q. The conclusion was ¬p. Thisis 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 ofdiscourse. 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 instantia-tion).

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 thepredicate “x is wonderful”. Let R(x) be the predicate “x is about the SpanishCivil War”. Let the universe of discourse be the set of films. Then there aretwo hypotheses: (i) ∀x(P (x) → Q(x)), (ii) ∃x(P (x) ∧ R(x)). The steps in theargument are:

18

Page 19: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 yin the first hypothesis to avoid confusion: here y is the general variable usedfor universal quantification, and x is the specific instantiation.

ii. ∃x(P (x) ∧ R(x) ∧ (P (x) → Q(x))). Therefore ∃x(Q(x) ∧ R(x)). (Modusponens).

19

Page 20: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 toobserve that log2 2 = 1 < log2 3. So for x > 0, we have x log 2 < x log 3, and so2x < 3x. Thus using constants k = 0, c = 1, we have that 2x is O(3x). Also, 17is trivially O(3x); for example, constants k = 0 and c = 17 demonstrate this, as17 ≤ 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).

(b) We may re-write x2+1x+1 = x − 1 + 2

x+1 . The term2

x+1 is O(x): choosing k = 1,c = 1 gives 2

x+1 < 1 for all x > 1, and so clearly 2x+1 < x for all x > 1. The

term x − 1 is O(x) from our theorem on the big-O of polynomials. Thus, usingthe formula for the sum of Big-O expressions, x2+1

x+1 is O(x).

(c) We may re-write x3+2x2x+1 = 1

2x2 − 1

4x +98 − 9

8(2x+1) . Again, the term9

8(2x+1) isO(x2): choosing k = 1, c = 1 gives 9

8(2x+1) < 1 for all x > 1, and so clearly2

x+1 < x2 for all x > 1. The term 12x

2− 14x+

98 is O(x

2) from our theorem on thebig-O of polynomials. Thus, using the formula for the sum of Big-O expressions,x3+2x2x+1 is O(x2).

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 thequestion we need to ask is whether x2 lnx is O(x3). To answer this, note thatlnx < x (you could show this by finding the turning points of y = lnx − x).Thus using any k, and c = 1, we have that lnx is O(x). Using the formula forthe product of big-O expressions, we obtain x2 lnx is O(x3). Therefore the leastinteger n = 3.

(b) Performing a long-division on the expression for f(x) will result in a polynomial ofdegree 1, and possibly a remainder of the form n(x)

x3+1 , where n(x) is a polynomialwith 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 remainderwill be less than 1. Thus the remainder term is also O(x2). Therefore the leastinteger n = 2.

(c) We may split this into f(x) = x4

x4+1 +5 lnxx4+1 . The second term is always positive

for x > 1, and by a similar argument to the previous example, the first term isO(1). Thus n must be at least 0. To check whether n = 0 is sufficient, we need toensure that the second term is O(1). From above, lnx < x, so 5 ln x

x4+1 < 5xx4+1 . But

this expression is O(1), as we can choose, for example k = 2, c = 1 to ensure thatnumerator is always greater than the denominator and so 5x

x4+1 < 1. Since the

RHS of this inequality is O(1), the LHS must also be O(1), and so f(x) = x4+5 ln xx4+1

is O(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,

11gac1.

20

Page 21: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

|f2(x)| ≥ c2|g2(x)|. We also know that for all x, f1(x) ≥ 0 and f2(x) ≥ 0. Considerthe 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) i. y = 3ii. i = 1: y = 3 ∗ 2 + 1 = 7.iii. i = 2: y = 7 ∗ 2 + 1 = 15.

(b) This method requires one multiplication per iteration of the loop, giving a totalof n multiplications.

(c) This method requires one addition per iteration of the loop, giving a total of naddition operations.

(d) Following a similar argment to the previous example, this algorithm is O(n).(e) Algorithm poly2 has the lower computational complexity, as it contains fewer

multiplications each iteration, while keeping the same number of additions andloop iterations. It also has one less initialization statement. Note that althoughpoly2 has lower computational complexity, both algorithms are O(n), as theyare only out by a multiplicative factor. The only reason we are even able to saythat poly2 has lower computational complexity is that the number of every typeof operation performed in poly2 is less than or equal to that in poly1.

6. The following operations are performed by the code:

(a) Initializing i := 1. This contributes O(1).(b) Initializing j := 1. This contributes O(n), as it is done n times in total.(c) Initializing c[i, j] := 0. This contributes O(n2), as it is done n2 times in total.(d) Initializing k := 1. This contributes O(n2), as it is done n2 times in total.(e) Multiplying a[i, k] by b[k, j]. This contributes O(n3), as it is done n3 times in

total.(f) Adding c[i, j] to the result of the multiplication. This contributes O(n3) for the

same reason.(g) Updating k. This contributes O(n3).(h) Updating j. This contributes O(n2).(i) Updating i. This contributes O(n).

The total execution time is thus O(n3) +O(n2) +O(n) +O(1) = O(n3).

21

Page 22: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 withconstant coefficient. We have an = a03n.

(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, sotrue 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 constantcoefficients. Each has the form an = c1an−1+ c2an−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 − c1r − c2 = 0.

(a) r2 − r − 6 = 0 has distinct solutions r1 = −2 and r2 = 3. Then a solution hasthe form an = α1(−2)n+α23n. Substituting n = 0 gives 3 = α1 +α2, and n = 1gives 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 proceedsas above.

(c) r2 − 6r+ 8 = 0 also has distinct roots r1 = 2, and r2 = 4. The solution proceedsas above.

(d) r2 − 7r + 10 = 0 has distinct roots r1 = 2, and r2 = 5. A solution therefore hasthe form an = α12n + α25n + 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 sothe the theorem cannot be applied. But this is an interesting case, which we maybe able to solve directly. We could consider splitting the sequence into two sub-sequences: ‘evens’ and ‘odds’, i.e. a0, a2, a4, . . . and a1, a3, a5, . . .. Note thatthe 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 a′n = an−1 + 1,with a′0 = 5 for the ‘even’ sequence, and a′′n = an−1 + 1, with a′′0 = −1 for the‘odds’. Then we can stitch together the two sequences, obtaining a2n = a′n, and

12gac1.

22

Page 23: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

a2n+1 = a′′n. Each of the individual recurrences can be solved (they are boththe c = 1 case of the degree-1 linear non-homogeneous recurrence relation withconstant 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 withthe previous questions.

4. For the original code, we have an = 1 + an−1 + an−1 = 1 + 2an−1 for n > 0, witha0 = 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 weobtain 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 degree1. 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: fromexponential to linear run-time! For example, a call to linrec( 50 ) would requireapproximately 1.13×1015multiplication operations, whereas linrec2( 50 ) would require50 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 daysrun 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 justc0 = 1 comparison (if n = 0). For n = 1, there are c1 = 2 comparisons: if n = 0 andelse if n = 1. For n > 1, there are cn = 2 + cn−1 + cn−2 comparisons. We shall nowexamine the solution of each of these recurrences in turn.

(a) The recurrence an = an−1 + an−2 is linear homogeneous, degree 2. Examiningr2 − r− 1 gives two distinct solutions, exactly the same as the fibonacci sequenceexample in the lecture, i.e. r1 = (1 −

√5)/2, r2 = (1 +

√5)/2. Also the initial

conditions are the same as the example in the lecture notes. Thus we obtainan = 1√

5

[(1+

√5

2

)n

−(

1−√5

2

)n].

(b) The recurrence mn = 2+mn−1+mn−2 is linear non-homogeneous, degree 2. Wehave the same roots r1 and r2 as above, leading to the general form of solutionmn = α1r

n1 + α2r

n2 − 2. Substituting n = 0 gives α1 + α2 = 2, and substituting

n = 1 gives α1r1+α2r2 = 2. Thus α2(r1− r2) = 0, giving α2 =2(r1−1)r1−r2

= 1+ 1√5,

and α2 = 1 − 1√5. The overall solution is therefore an =

(1− 1√

5

) (1−√

52

)n

+(1 + 1√

5

)(1+

√5

2

)n

− 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 α1r1 + α2r2 = 4. Following through in a similarmanner gives a2 = 1

2

(5 + 3

√5)and α1 = 1

2

(1− 3√5). We therefore obtain

an = 12

[(1− 3√5) (

1−√5

2

)n

+(5 + 3

√5) (

1+√

52

)n]− 2.

(d) Let us simplify the discussion by keeping the values r1 and r2 as defined above.Then an is the sum of an O(rn

1 ) function and an O(rn2 ) function, which is an

O(rn2 ) function. bn is similar, except there is also an O(1) component; it is an

O(rn2 ) function. cn is also an O(rn

2 ) function for the same reason. Thus no matterwhat the relative time taken by each of the operations, the total time will be a

23

Page 24: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

weighted sum of these functions with constant weights, resulting in an O(rn2 )

function. The execution time thus scales as O((

1+√

52

)n).

24

Page 25: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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, sof(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, sog(n) is O(n).

2. (a) i. The outer loop iterates n times. The inner loop iterates n times for eachiteration of the outer loop. So a(n) = n2.

ii. For n > 1, the algorithm recursively calls itself 8 times, each on a size n/2problem. It also calls matrixsum 4 times, each on a size n/2 problem, soexecuting 4a(n/2) = 4(n/2)2 = n2 addition operations. The total numberof operations is therefore f(n) = 8f(n/2) + n2, with f(1) = 1, a singlemultiplication for the base case.

iii. Applying the Master Theorem, we have f(n) is O(nlog2 8) = O(n3). Theiterative algorithm from Sheet 4 had n3 multiplication operations, and thesame number of additions. They are both O(n3).

(b) i. The code is a straight-forward translation of the equations, with the samebase case for the recursion as recmatmult1.

ii. This time, in order to perform an n×nmatrix multiply, we need only 7 n/2×n/2 matrix multiplication operations, but many more (18) n/2×n/2 matrixadditions 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). Thisis a reduced bound on the number of operations required, compared to eitherthe iterative version of Sheet 4 or the straight-forward recursive implemen-tation.

13gac1.

25

Page 26: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

Discrete Mathematics and Computational Complexity14

Sheet 7: Solutions

1. We wish to prove that Πc is solvable iff Pi is solvable. We can first prove that if Π issolvable, 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 inFig. 12. The first of these two algorithms proves that if Pi is solvable, we can obtaina solution for Πc. The second illustrates a method for obtaining a solution for Π if asolution exists for Πc.

2. The definition of Π1 is ‘at least as hard as’ Π2 is that a general instance of Π2 can betransformed into an instance of Π1.

(a) If a general instance of Π2 can be transformed into an instance of Π1, it doesn’tnecessarily follow that a general instance of Π1 can be transformed into an in-stance 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 generalinstance of Π1 can be transformed into an instance of Π3, it follows that a generalinstance 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 onealsosolveΠ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. Onepossible solution is to have one subgraph with node set 1, 2, 3 and the otherwith 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. Onepossible solution is to have node sets 1, 2, 3, and 4, 5. Another is to havenode 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)

14gac1.

26

Page 27: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

procedure solveΠc( p )begin

if solveΠ( p ) = true thenresult := false

elseresult := true

end

procedure solveΠ( p )begin

if solveΠc( p ) = true thenresult := false

elseresult := true

end

Figure 12: Algorithms to prove that Πc is solvable iff Π is solvable.

(c)

(a) (b)

(d)

Figure 13: (a) K1, (b) K2, (c) K3, and (d) K4.

27

Page 28: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

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 noderesult := 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] labelledif n = |V | then// already labelled all nodesresult := checklabelling( V , E, k, label )

else begin// still more nodes to labeltrial := 1success := falsewhile trial ¡= k and not(success)// try each possible label for V [n] in turnlabel[V[n]] := trialsuccess := graphlabel( V , E, k, label, n+ 1 )

endresult := success

endend

procedure checklabel( V , E, k, label )begin// checklabel takes in a labelled graph and sees whether this label corresponds to// a partition into complete subgraphsresult := truefor i := 1 to |V | do// check that all nodes with this label are connected to all other nodes with this labelfor j := i+ 1 to |V | do

if label[V [i]] = label[V [j]] doif V [i], V [j] /∈ E doresult := false

end

Figure 14: A possible algorithm to solve the graph partitioning problem.

28

Page 29: Discrete Mathematics and Computational Complexity1 Sheet …cas.ee.ic.ac.uk/people/gac1/Complexity/SGs.pdf · Discrete Mathematics and Computational Complexity3 Sheet 3: Predicate

1 2

4

3

5

1 2

4

3

5

(a) (b)Figure 15: Complements of the graphs in Figure 8.

29