29.10.26

CS702 Midterm Past Papers: Solved Subjective Questions, Proofs, Recurrences, Dynamic Programming and Short Notes (Advanced Algorithm Analysis and Design)

Getting ready for the midterm of CS702 (Advanced Algorithm Analysis and Design)? Here is everything in one place: solved MCQs, subjective questions with answers and short notes and key terms, gathered from past papers and files shared by students. No duplicates and all the answers are checked and verified. Use the tabs above to switch between Objective, Subjective and Short Notes & Key Terms, and tap Show answer only after you have tried the question yourself. Best of luck with your exam!

Part 1: Objective (MCQs)

No items for this course in this section.

Part 2: Subjective Questions

Short Questions

Definitions, proofs and short derivations.

Q1. Prove that logab⋅logbc=logac\log_a b \cdot \log_b c = \log_a c.

Let logab=s\log_a b = s and logbc=t\log_b c = t. By the definition of a logarithm,

as=bandbt=c.a^{s} = b \qquad\text{and}\qquad b^{t} = c .

Substitute b=asb = a^{s} in the second equation:

c=bt=(as)t=ast⇒logac=st.c = b^{t} = (a^{s})^{t} = a^{st} \;\Rightarrow\; \log_a c = s\,t .

Therefore logab⋅logbc=st=logac\log_a b \cdot \log_b c = s\,t = \log_a c. ◼\blacksquare

Q2. Prove that 3log4n=nlog433^{\log_4 n} = n^{\log_4 3}.

Take log4\log_4 of both sides (the logarithm is one-to-one, so the two sides are equal exactly when their logarithms are equal).

log4(3log4n)=log4n⋅log43,log4(nlog43)=log43⋅log4n.\begin{aligned} \log_4\!\left(3^{\log_4 n}\right) &= \log_4 n \cdot \log_4 3,\\ \log_4\!\left(n^{\log_4 3}\right) &= \log_4 3 \cdot \log_4 n . \end{aligned}

Both are the same number, so 3log4n=nlog433^{\log_4 n} = n^{\log_4 3}. ◼\blacksquare

Q3. Prove that n2+n=O(n3)n^{2} + n = O(n^{3}).

Let f(n)=n2+nf(n) = n^{2}+n and g(n)=n3g(n) = n^{3}. We need constants c>0c>0 and n0n_0 with 0≤f(n)≤cg(n)0 \le f(n) \le c\,g(n) for all n≥n0n \ge n_0.

For n≥1n \ge 1 we have n≤n3n \le n^{3} and n2≤n3n^{2} \le n^{3} (in general, if a≤ba \le b then na≤nbn^{a} \le n^{b} whenever n≥1n \ge 1). Hence

n2+n≤n3+n3=2n3for all n≥1.n^{2} + n \;\le\; n^{3} + n^{3} \;=\; 2n^{3}\qquad\text{for all } n \ge 1 .

So the definition of Big-O holds with c=2c = 2 and n0=1n_0 = 1, i.e. n2+n=O(n3)n^{2}+n = O(n^{3}). ◼\blacksquare

Q4. Prove that n+n=O(n3)\sqrt{n} + n = O(n^{3}).

For n≥1n \ge 1: n≤n≤n3\sqrt{n} \le n \le n^{3} and n≤n3n \le n^{3}. Therefore

n+n≤n3+n3=2n3(n≥1),\sqrt{n} + n \le n^{3} + n^{3} = 2n^{3}\qquad (n \ge 1),

so n+n=O(n3)\sqrt{n}+n = O(n^{3}) with c=2c = 2, n0=1n_0 = 1. ◼\blacksquare

Q5. Prove that 2n2∈O(n3)2n^{2} \in O(n^{3}).

Let f(n)=2n2f(n) = 2n^{2} and g(n)=n3g(n) = n^{3}. We look for cc and n0n_0 such that f(n)≤cg(n)f(n) \le c\,g(n):

2n2≤cn3⇔2≤cn.2n^{2} \le c\,n^{3} \;\Longleftrightarrow\; 2 \le c\,n .

Take c=1c = 1 and n0=2n_0 = 2 (or c=2c = 2 and n0=1n_0 = 1). Then 2n2≤cn32n^{2} \le c\,n^{3} for all n≥n0n \ge n_0, so f(n)∈O(g(n))f(n) \in O(g(n)). ◼\blacksquare

Q6. Prove that f(n)=O(g(n))⇔g(n)=Ω(f(n))f(n) = O(g(n)) \iff g(n) = \Omega(f(n)) (transpose symmetry). (5 marks)

(⇒\Rightarrow) Since f(n)=O(g(n))f(n) = O(g(n)), there exist a constant c>0c > 0 and n0∈ℕn_0 \in \mathbb{N} such that

0≤f(n)≤cg(n)∀n≥n0.0 \le f(n) \le c\,g(n)\qquad \forall\, n \ge n_0 .

Divide both sides by cc:

0≤1cf(n)≤g(n)∀n≥n0.0 \le \tfrac{1}{c}\,f(n) \le g(n)\qquad \forall\, n \ge n_0 .

Put c′=1c>0c' = \tfrac{1}{c} > 0. Then 0≤c′f(n)≤g(n)0 \le c'\,f(n) \le g(n) for all n≥n0n \ge n_0, which is exactly the definition of g(n)=Ω(f(n))g(n) = \Omega(f(n)).

(⇐\Leftarrow) Since g(n)=Ω(f(n))g(n) = \Omega(f(n)), there exist c′>0c' > 0 and n0n_0 with 0≤c′f(n)≤g(n)0 \le c'\,f(n) \le g(n) for all n≥n0n \ge n_0. Dividing by c′c' gives 0≤f(n)≤1c′g(n)0 \le f(n) \le \tfrac{1}{c'}\,g(n); with c=1c′c = \tfrac{1}{c'} this says f(n)=O(g(n))f(n) = O(g(n)). ◼\blacksquare

Q7. Prove that f(n)=o(g(n))⇔g(n)=ω(f(n))f(n) = o(g(n)) \iff g(n) = \omega(f(n)). (5 marks)

Recall: f(n)=o(g(n))f(n) = o(g(n)) means that for every constant c>0c > 0 there is an n0n_0 with 0≤f(n)<cg(n)0 \le f(n) < c\,g(n) for all n≥n0n \ge n_0. Similarly g(n)=ω(f(n))g(n) = \omega(f(n)) means that for every c>0c > 0 there is an n0n_0 with 0≤cf(n)<g(n)0 \le c\,f(n) < g(n) for all n≥n0n \ge n_0.

(⇒\Rightarrow) Let c′>0c' > 0 be arbitrary. Apply the hypothesis f=o(g)f = o(g) with the constant c=1c′c = \tfrac{1}{c'}: there is an n0n_0 such that f(n)<1c′g(n)f(n) < \tfrac{1}{c'}\,g(n) for all n≥n0n \ge n_0. Multiplying by c′c' gives c′f(n)<g(n)c'\,f(n) < g(n) for all n≥n0n \ge n_0. As c′c' was arbitrary, g(n)=ω(f(n))g(n) = \omega(f(n)).

(⇐\Leftarrow) Let c>0c > 0 be arbitrary. Apply g=ω(f)g = \omega(f) with the constant 1c\tfrac{1}{c}: there is an n0n_0 with 1cf(n)<g(n)\tfrac{1}{c}\,f(n) < g(n) for all n≥n0n \ge n_0, i.e. f(n)<cg(n)f(n) < c\,g(n). Hence f(n)=o(g(n))f(n) = o(g(n)). ◼\blacksquare

Q8. Prove that 5n2∈ω(n)5n^{2} \in \omega(n) (little omega).

Let f(n)=5n2f(n) = 5n^{2} and g(n)=ng(n) = n. We must show that for any constant c>0c > 0 there is an n0n_0 such that cg(n)<f(n)c\,g(n) < f(n) for all n≥n0n \ge n_0.

cn<5n2⇔c<5n⇔n>c5.c\,n < 5n^{2} \;\Longleftrightarrow\; c < 5n \;\Longleftrightarrow\; n > \tfrac{c}{5}.

So for any cc we may take n0=⌊c/5⌋+1n_0 = \lfloor c/5 \rfloor + 1. For example, for c=1000000c = 1\,000\,000 take n0=200001n_0 = 200\,001; the inequality then holds for every n≥n0n \ge n_0. Hence 5n2∈ω(n)5n^{2} \in \omega(n). ◼\blacksquare

Q9. Prove that 5n+10∉ω(n)5n + 10 \notin \omega(n).

Let f(n)=5n+10f(n) = 5n + 10 and g(n)=ng(n) = n. To show f(n)∉ω(g(n))f(n) \notin \omega(g(n)) it is enough to find one constant cc for which no n0n_0 works.

Take c=16c = 16. Then cg(n)<f(n)c\,g(n) < f(n) would mean

16n<5n+10⇔11n<10,16n < 5n + 10 \;\Longleftrightarrow\; 11n < 10,

which is false for every positive integer nn. So no n0n_0 exists for c=16c = 16, and therefore 5n+10∉ω(n)5n + 10 \notin \omega(n). ◼\blacksquare

Q10. Prove that 100n∉ω(n)100n \notin \omega(n).

Let f(n)=100nf(n) = 100n and g(n)=ng(n) = n. Choose the constant c=100c = 100. Then the requirement cg(n)<f(n)c\,g(n) < f(n) becomes

100n<100n,100n < 100n,

which is false for every nn. So no n0n_0 exists for this cc, hence 100n∉ω(n)100n \notin \omega(n). ◼\blacksquare

Note: the shared solution proved the related statement 100n∉ω(n2)100n \notin \omega(n^{2}): assuming cn2<100nc\,n^{2} < 100n gives cn<100c\,n < 100, which fails for c=100c = 100 and every n≥1n \ge 1. That statement is also true, and both proofs are shown here.

Q11. Prove that 5n2∈Ω(n)5n^{2} \in \Omega(n).

Let f(n)=5n2f(n) = 5n^{2} and g(n)=ng(n) = n. We need cg(n)≤f(n)c\,g(n) \le f(n) for all n≥n0n \ge n_0:

cn≤5n2⇔c≤5n.c\,n \le 5n^{2} \;\Longleftrightarrow\; c \le 5n .

Take c=5c = 5 and n0=1n_0 = 1. Then 5n≤5n25n \le 5n^{2} for all n≥1n \ge 1, so f(n)∈Ω(g(n))f(n) \in \Omega(g(n)). ◼\blacksquare

Q12. Prove that n3+4n2=Ω(n2)n^{3} + 4n^{2} = \Omega(n^{2}).

Let f(n)=n3+4n2f(n) = n^{3} + 4n^{2} and g(n)=n2g(n) = n^{2}. For n≥1n \ge 1 we have n2≤n3n^{2} \le n^{3}, and n3≤n3+4n2n^{3} \le n^{3} + 4n^{2}. Therefore

1⋅n2≤n3≤n3+4n2for all n≥1.1\cdot n^{2} \;\le\; n^{3} \;\le\; n^{3} + 4n^{2}\qquad\text{for all } n \ge 1 .

By the definition of Big-Omega (with c=1c = 1 and n0=1n_0 = 1), n3+4n2=Ω(n2)n^{3} + 4n^{2} = \Omega(n^{2}). ◼\blacksquare

Q13. Prove that 100n+5∉Ω(n2)100n + 5 \notin \Omega(n^{2}).

Let f(n)=100n+5f(n) = 100n + 5 and g(n)=n2g(n) = n^{2}. Assume, for contradiction, that f(n)∈Ω(g(n))f(n) \in \Omega(g(n)). Then there are constants c>0c > 0 and n0n_0 such that

cn2≤100n+5∀n≥n0.c\,n^{2} \le 100n + 5\qquad \forall\, n \ge n_0 .

Dividing by nn gives cn≤100+5nc\,n \le 100 + \tfrac{5}{n}, and for n≥1n \ge 1 this gives cn≤105c\,n \le 105, i.e.

n≤105c.n \le \frac{105}{c}.

This cannot hold for arbitrarily large nn (take any n>105/cn > 105/c). Contradiction. Hence 100n+5∉Ω(n2)100n + 5 \notin \Omega(n^{2}). ◼\blacksquare

Q14. Prove that nlogn+3n+2=Ω(nlogn)n\log n + 3n + 2 = \Omega(n\log n).

Let f(n)=nlogn+3n+2f(n) = n\log n + 3n + 2 and g(n)=nlogng(n) = n\log n. For n≥1n \ge 1 we have logn≥0\log n \ge 0 and all other terms are positive, so

1⋅nlogn≤nlogn+3n+2for all n≥1.1\cdot n\log n \;\le\; n\log n + 3n + 2 \qquad\text{for all } n \ge 1 .

Thus f(n)∈Ω(g(n))f(n) \in \Omega(g(n)) with c=1c = 1 and n0=1n_0 = 1. ◼\blacksquare

Q15. Prove that 12n2−12n=Θ(n2)\tfrac{1}{2}n^{2} - \tfrac{1}{2}n = \Theta(n^{2}).

Let f(n)=12n2−12nf(n) = \tfrac12 n^{2} - \tfrac12 n and g(n)=n2g(n) = n^{2}. We need c1,c2,n0c_1, c_2, n_0 with c1g(n)≤f(n)≤c2g(n)c_1 g(n) \le f(n) \le c_2 g(n) for all n≥n0n \ge n_0.

Upper bound. For n≥0n \ge 0, 12n2−12n≤12n2\tfrac12 n^{2} - \tfrac12 n \le \tfrac12 n^{2}, so c2=12c_2 = \tfrac12 works.

Lower bound. For n≥2n \ge 2 we have 12n≤14n2\tfrac12 n \le \tfrac14 n^{2} (because n≥2n \ge 2), so

12n2−12n≥12n2−14n2=14n2,\tfrac12 n^{2} - \tfrac12 n \;\ge\; \tfrac12 n^{2} - \tfrac14 n^{2} \;=\; \tfrac14 n^{2},

so c1=14c_1 = \tfrac14 works.

Hence 14n2≤12n2−12n≤12n2\tfrac14 n^{2} \le \tfrac12 n^{2} - \tfrac12 n \le \tfrac12 n^{2} for all n≥2n \ge 2, i.e. c1=14c_1 = \tfrac14, c2=12c_2 = \tfrac12, n0=2n_0 = 2, and f(n)=Θ(n2)f(n) = \Theta(n^{2}). ◼\blacksquare

Q16. Prove that an2+bn+c=Θ(n2)a n^{2} + b n + c = \Theta(n^{2}), where a,b,ca, b, c are constants and a>0a > 0.

Let f(n)=an2+bn+cf(n) = a n^{2} + b n + c and g(n)=n2g(n) = n^{2}. Choose

c1=a4,c2=7a4,n0=2max(|b|a,|c|a).c_1 = \frac{a}{4},\qquad c_2 = \frac{7a}{4},\qquad n_0 = 2\max\!\left(\frac{|b|}{a},\ \sqrt{\frac{|c|}{a}}\right).

For n≥n0n \ge n_0 we have n≥2|b|an \ge \tfrac{2|b|}{a}, so |b|n≤a2n2|b|\,n \le \tfrac{a}{2}n^{2}, and n≥2|c|/an \ge 2\sqrt{|c|/a}, so |c|≤a4n2|c| \le \tfrac{a}{4}n^{2}. Then

f(n)≤an2+|b|n+|c|≤an2+a2n2+a4n2=7a4n2,f(n) \le a n^{2} + |b| n + |c| \le a n^{2} + \tfrac{a}{2}n^{2} + \tfrac{a}{4}n^{2} = \tfrac{7a}{4}\,n^{2},

f(n)≥an2−|b|n−|c|≥an2−a2n2−a4n2=a4n2.f(n) \ge a n^{2} - |b| n - |c| \ge a n^{2} - \tfrac{a}{2}n^{2} - \tfrac{a}{4}n^{2} = \tfrac{a}{4}\,n^{2}.

So 0≤c1g(n)≤f(n)≤c2g(n)0 \le c_1 g(n) \le f(n) \le c_2 g(n) for all n≥n0n \ge n_0, and every degree-2 polynomial with a>0a>0 is Θ(n2)\Theta(n^{2}). ◼\blacksquare

Q17. Prove that 2n2+3n+6≠Θ(n3)2n^{2} + 3n + 6 \ne \Theta(n^{3}) (i.e. 2n2+3n+6∉Θ(n3)2n^{2} + 3n + 6 \notin \Theta(n^{3})).

Note: the question is printed as "=Θ(n3)=\Theta(n^{3})", but that claim is false; the correct statement to prove is that the function is not Θ(n3)\Theta(n^{3}) (it is Θ(n2)\Theta(n^{2})).

Let f(n)=2n2+3n+6f(n) = 2n^{2} + 3n + 6 and g(n)=n3g(n) = n^{3}. Suppose, on the contrary, that f(n)∈Θ(g(n))f(n) \in \Theta(g(n)). Then there exist positive constants c1,c2,n0c_1, c_2, n_0 with

c1n3≤2n2+3n+6≤c2n3∀n≥n0.c_1 n^{3} \le 2n^{2} + 3n + 6 \le c_2 n^{3}\qquad \forall\, n \ge n_0 .

Dividing by n2n^{2} gives

c1n≤2+3n+6n2≤c2n.c_1\, n \;\le\; 2 + \frac{3}{n} + \frac{6}{n^{2}}\;\le\; c_2\, n .

For n≥1n \ge 1 the middle term is at most 1111, so c1n≤11c_1 n \le 11, i.e. n≤11c1n \le \tfrac{11}{c_1} for all n≥n0n \ge n_0. That is impossible because nn can be made as large as we like. Contradiction, hence 2n2+3n+6∉Θ(n3)2n^{2}+3n+6 \notin \Theta(n^{3}). ◼\blacksquare

Q18. Prove that n2∈O(n2)n^{2} \in O(n^{2}).

Let f(n)=n2f(n) = n^{2} and g(n)=n2g(n) = n^{2}. Then f(n)≤cg(n)f(n) \le c\,g(n) means n2≤cn2n^{2} \le c\,n^{2}, i.e. 1≤c1 \le c. Take c=1c = 1 and n0=1n_0 = 1. Hence n2∈O(n2)n^{2} \in O(n^{2}). ◼\blacksquare

Q19. Prove that 1000n2+1000n∈O(n2)1000n^{2} + 1000n \in O(n^{2}).

Let f(n)=1000n2+1000nf(n) = 1000n^{2} + 1000n and g(n)=n2g(n) = n^{2}. Take c=1001c = 1001:

1000n2+1000n≤1001n2⇔1000n≤n2⇔n(n−1000)≥0,1000n^{2} + 1000n \le 1001\,n^{2} \;\Longleftrightarrow\; 1000n \le n^{2} \;\Longleftrightarrow\; n(n - 1000) \ge 0,

which is true for all n≥1000n \ge 1000. So f(n)≤1001g(n)f(n) \le 1001\,g(n) for all n≥n0=1000n \ge n_0 = 1000, and f(n)∈O(n2)f(n) \in O(n^{2}). ◼\blacksquare

(Alternatively, c=2000c = 2000 and n0=1n_0 = 1 also works, because n≤n2n \le n^{2} for n≥1n \ge 1.)

Q20. Prove by contradiction that n3∉O(n2)n^{3} \notin O(n^{2}).

Note: some sources print this as "prove n3∈O(n2)n^{3} \in O(n^{2})", but the true statement is n3∉O(n2)n^{3} \notin O(n^{2}).

Assume g(n)=n3g(n) = n^{3} is O(n2)O(n^{2}). Then there are positive constants cc and kk such that

n3≤cn2for all n≥k.n^{3} \le c\,n^{2}\qquad\text{for all } n \ge k .

Dividing by n2n^{2} (which is non-zero) gives n≤cn \le c for all n≥kn \ge k. But this fails for every n>cn > c, a contradiction. Therefore n3∉O(n2)n^{3} \notin O(n^{2}). ◼\blacksquare

Q21. Prove that 2n3+3n+10∈O(n4)2n^{3} + 3n + 10 \in O(n^{4}). (5 marks)

Let f(n)=2n3+3n+10f(n) = 2n^{3} + 3n + 10 and g(n)=n4g(n) = n^{4}. We need c,n0c, n_0 with 0≤f(n)≤cg(n)0 \le f(n) \le c\,g(n) for all n≥n0n \ge n_0.

Method 1 (testing values). Try c=1c = 1:

nn f(n)=2n3+3n+10f(n) = 2n^3+3n+10 g(n)=n4g(n)=n^4 f(n)≤g(n)f(n)\le g(n)?
1 15 1 no
2 32 16 no
3 73 81 yes

For n≥3n \ge 3 the inequality keeps holding (the right side grows faster), so c=1c = 1 and n0=3n_0 = 3 work.

Method 2 (bounding each term). For n≥1n \ge 1: 2n3≤2n42n^{3} \le 2n^{4}, 3n≤3n43n \le 3n^{4} and 10≤10n410 \le 10n^{4}, so f(n)≤15n4f(n) \le 15\,n^{4} with c=15c = 15 and n0=1n_0 = 1.

Hence 2n3+3n+10∈O(n4)2n^{3} + 3n + 10 \in O(n^{4}). ◼\blacksquare

Q22. Prove that 5n2+7n∈O(n2)5n^{2} + 7n \in O(n^{2}).

Let f(n)=5n2+7nf(n) = 5n^{2} + 7n and g(n)=n2g(n) = n^{2}. Take c=6c = 6 and look for n0n_0 with 5n2+7n≤6n25n^{2} + 7n \le 6n^{2}:

5n2+7n≤6n2⇔7n≤n2⇔n≥7.5n^{2} + 7n \le 6n^{2} \;\Longleftrightarrow\; 7n \le n^{2} \;\Longleftrightarrow\; n \ge 7 .

Check: for n=7n = 7, 5(49)+7(7)=245+49=294=6(49)5(49) + 7(7) = 245 + 49 = 294 = 6(49). So f(n)≤6g(n)f(n) \le 6\,g(n) for all n≥7n \ge 7, i.e. c=6c = 6, n0=7n_0 = 7. Hence 5n2+7n∈O(n2)5n^{2} + 7n \in O(n^{2}). ◼\blacksquare

Q23. Prove that 5n2+10n+16∈Θ(n2)5n^{2} + 10n + 16 \in \Theta(n^{2}). (5 marks)

Let f(n)=5n2+10n+16f(n) = 5n^{2}+10n+16 and g(n)=n2g(n) = n^{2}. We must find positive constants c1,c2,n0c_1, c_2, n_0 such that

0≤c1n2≤5n2+10n+16≤c2n2∀n≥n0.0 \le c_1 n^{2} \le 5n^{2}+10n+16 \le c_2 n^{2}\qquad \forall\, n \ge n_0 .

Lower bound. Take c1=5c_1 = 5: 5n2≤5n2+10n+165n^{2} \le 5n^{2}+10n+16 holds for every n≥1n \ge 1 (we only drop positive terms).

Upper bound. Take c2=6c_2 = 6. Then

5n2+10n+16≤6n2⇔n2≥10n+16.5n^{2}+10n+16 \le 6n^{2} \;\Longleftrightarrow\; n^{2} \ge 10n+16 .

Testing values: for n=11n = 11, 121<126121 < 126 (fails); for n=12n = 12, 144≥136144 \ge 136 (holds), and it keeps holding for larger nn. Direct check at n=12n = 12:

5(144)+10(12)+16=856≤864=6(144).5(144)+10(12)+16 = 856 \le 864 = 6(144).

Hence 5n2≤5n2+10n+16≤6n25n^{2} \le 5n^{2}+10n+16 \le 6n^{2} for all n≥12n \ge 12, so c1=5c_1 = 5, c2=6c_2 = 6, n0=12n_0 = 12 and f(n)∈Θ(n2)f(n) \in \Theta(n^{2}). ◼\blacksquare

Note: some shared solutions print the sum as 857857; the correct value is 856856 (the conclusion is unchanged).

Q24. Prove that n2+5n+7=Θ(n2)n^{2} + 5n + 7 = \Theta(n^{2}). (5 marks)

For all n≥1n \ge 1 we have 5n≤5n25n \le 5n^{2} and 7≤7n27 \le 7n^{2}. Therefore

1⋅n2≤n2+5n+7≤n2+5n2+7n2=13n2(n≥1).1\cdot n^{2} \;\le\; n^{2}+5n+7 \;\le\; n^{2}+5n^{2}+7n^{2} = 13\,n^{2}\qquad (n \ge 1).

So c1=1c_1 = 1, c2=13c_2 = 13, n0=1n_0 = 1 satisfy the definition of Θ\Theta, and n2+5n+7=Θ(n2)n^{2}+5n+7 = \Theta(n^{2}). ◼\blacksquare

Q25. Prove that 5n2−6n=Θ(n2)5n^{2} - 6n = \Theta(n^{2}). (5 marks)

Upper bound. For n≥1n \ge 1, 5n2−6n≤5n25n^{2}-6n \le 5n^{2}, so c2=5c_2 = 5.

Lower bound. Take c1=1c_1 = 1. Then

5n2−6n≥n2⇔4n2≥6n⇔n≥32,5n^{2}-6n \ge n^{2} \;\Longleftrightarrow\; 4n^{2} \ge 6n \;\Longleftrightarrow\; n \ge \tfrac{3}{2},

which is true for every integer n≥2n \ge 2.

Hence 1⋅n2≤5n2−6n≤5n21\cdot n^{2} \le 5n^{2}-6n \le 5\,n^{2} for all n≥2n \ge 2, i.e. c1=1c_1 = 1, c2=5c_2 = 5, n0=2n_0 = 2, and 5n2−6n=Θ(n2)5n^{2}-6n = \Theta(n^{2}). ◼\blacksquare

Q26. Show that 12n2+3n=Θ(n2)\tfrac{1}{2}n^{2} + 3n = \Theta(n^{2}). (5 marks)

Reading of the question: the printed text is garbled ("2n2+3n2^{n^2+3n} OR [12n2+3n][\tfrac12 n^{2}+3n]"); the bracketed form 12n2+3n\tfrac12 n^{2}+3n is the intended function.

For n≥1n \ge 1 we have 3n≤3n23n \le 3n^{2}. Therefore

12n2≤12n2+3n≤12n2+3n2=72n2(n≥1).\tfrac12\,n^{2} \;\le\; \tfrac12 n^{2}+3n \;\le\; \tfrac12 n^{2}+3n^{2} = \tfrac72\,n^{2}\qquad (n \ge 1).

So c1=12c_1 = \tfrac12, c2=72c_2 = \tfrac72, n0=1n_0 = 1, and 12n2+3n=Θ(n2)\tfrac12 n^{2}+3n = \Theta(n^{2}). ◼\blacksquare

Q27. Prove that 3n2−4n+1=Θ(n2)3n^{2} - 4n + 1 = \Theta(n^{2}). (5 marks)

Upper bound. For n≥1n \ge 1, −4n+1<0-4n+1 < 0, so 3n2−4n+1≤3n23n^{2}-4n+1 \le 3n^{2}; take c2=3c_2 = 3.

Lower bound. Take c1=1c_1 = 1:

3n2−4n+1≥n2⇔2n2−4n+1≥0,3n^{2}-4n+1 \ge n^{2} \;\Longleftrightarrow\; 2n^{2}-4n+1 \ge 0,

which is true for all n≥2n \ge 2 (at n=2n = 2 it equals 11, and the left side is increasing for n≥1n \ge 1).

Therefore n2≤3n2−4n+1≤3n2n^{2} \le 3n^{2}-4n+1 \le 3n^{2} for all n≥2n \ge 2; c1=1c_1=1, c2=3c_2=3, n0=2n_0=2. ◼\blacksquare

Q28. Prove that f(n)=Θ(g(n))⇔g(n)=Θ(f(n))f(n) = \Theta(g(n)) \iff g(n) = \Theta(f(n)) (symmetry of Θ\Theta). (5 marks)

(⇒\Rightarrow) Since f(n)=Θ(g(n))f(n) = \Theta(g(n)), there exist constants c1,c2>0c_1, c_2 > 0 and n0n_0 such that

0≤c1g(n)≤f(n)≤c2g(n)∀n≥n0.0 \le c_1\,g(n) \le f(n) \le c_2\,g(n)\qquad \forall\, n \ge n_0. \tag{1}

From the right half of (1): f(n)≤c2g(n)f(n) \le c_2 g(n), so g(n)≥1c2f(n)g(n) \ge \dfrac{1}{c_2}\,f(n). From the left half of (1): c1g(n)≤f(n)c_1 g(n) \le f(n), so g(n)≤1c1f(n)g(n) \le \dfrac{1}{c_1}\,f(n).

Hence, with c3=1c2c_3 = \dfrac{1}{c_2} and c4=1c1c_4 = \dfrac{1}{c_1},

0≤c3f(n)≤g(n)≤c4f(n)∀n≥n0,0 \le c_3\, f(n) \le g(n) \le c_4\, f(n)\qquad \forall\, n \ge n_0,

which says g(n)=Θ(f(n))g(n) = \Theta(f(n)).

(⇐\Leftarrow) Apply the same argument with the roles of ff and gg exchanged. ◼\blacksquare

Q29. Show that f(n)=n2+2n+1f(n) = n^{2}+2n+1 is O(n2)O(n^{2}). (5 marks)

For n≥1n \ge 1: 2n≤2n22n \le 2n^{2} and 1≤n21 \le n^{2}. So

n2+2n+1≤n2+2n2+n2=4n2(n≥1).n^{2}+2n+1 \le n^{2}+2n^{2}+n^{2} = 4n^{2}\qquad (n \ge 1).

Thus c=4c = 4 and n0=1n_0 = 1 work, and f(n)=O(n2)f(n) = O(n^{2}). (Equivalently f(n)=(n+1)2≤(2n)2=4n2f(n) = (n+1)^{2} \le (2n)^{2} = 4n^{2}.) ◼\blacksquare

Q30. Show that f(n)=3n+7f(n) = 3n + 7 is O(n)O(n). (5 marks)

For n≥1n \ge 1 we have 7≤7n7 \le 7n. Hence

3n+7≤3n+7n=10n(n≥1),3n + 7 \le 3n + 7n = 10\,n\qquad (n \ge 1),

so c=10c = 10 and n0=1n_0 = 1 satisfy 0≤f(n)≤cn0 \le f(n) \le c\,n, and f(n)=O(n)f(n) = O(n). ◼\blacksquare

Q31. Show that f(n)=(n+1)3f(n) = (n+1)^{3} is O(n3)O(n^{3}). (5 marks)

For n≥1n \ge 1 we have n+1≤2nn + 1 \le 2n, so

(n+1)3≤(2n)3=8n3(n≥1).(n+1)^{3} \le (2n)^{3} = 8\,n^{3}\qquad (n \ge 1).

Therefore c=8c = 8, n0=1n_0 = 1, and (n+1)3=O(n3)(n+1)^{3} = O(n^{3}). ◼\blacksquare

Q32. Show that f(n)=∑i=1nif(n) = \displaystyle\sum_{i=1}^{n} i is O(n2)O(n^{2}). (5 marks)

The sum is ∑i=1ni=n(n+1)2\displaystyle\sum_{i=1}^{n} i = \frac{n(n+1)}{2}. For n≥1n \ge 1,

n(n+1)2=n2+n2≤n2+n22=n2.\frac{n(n+1)}{2} = \frac{n^{2}+n}{2} \le \frac{n^{2}+n^{2}}{2} = n^{2}.

So c=1c = 1 and n0=1n_0 = 1 work, and f(n)=O(n2)f(n) = O(n^{2}). (In fact f(n)=Θ(n2)f(n) = \Theta(n^{2}).) ◼\blacksquare

Q33. Prove that the statement "n2logn=Θ(n2)n^{2}\log n = \Theta(n^{2})" is incorrect. (5 marks)

Suppose, for contradiction, that n2logn∈Θ(n2)n^{2}\log n \in \Theta(n^{2}). Then in particular n2logn∈O(n2)n^{2}\log n \in O(n^{2}): there are constants c>0c > 0 and n0n_0 with

n2logn≤cn2∀n≥n0⇒logn≤c∀n≥n0.n^{2}\log n \le c\,n^{2}\qquad \forall\, n \ge n_0 \;\Longrightarrow\; \log n \le c\qquad \forall\, n \ge n_0 .

But logn→∞\log n \to \infty, so for any n>2cn > 2^{c} (taking log=log2\log = \log_2) we get logn>c\log n > c — a contradiction. Hence n2logn≠Θ(n2)n^{2}\log n \ne \Theta(n^{2}). (It is Ω(n2)\Omega(n^{2}) but not O(n2)O(n^{2}).) ◼\blacksquare

Q34. Prove that nlogn∈O(n2)n\log n \in O(n^{2}). (5 marks)

For every n≥1n \ge 1 we have log2n≤n\log_2 n \le n. Multiplying both sides by nn:

nlogn≤n⋅n=n2(n≥1).n\log n \le n\cdot n = n^{2}\qquad (n \ge 1).

So c=1c = 1, n0=1n_0 = 1, and nlogn∈O(n2)n\log n \in O(n^{2}). ◼\blacksquare

Q35. Prove that n2+42n+7=O(n2)n^{2}+42n+7 = O(n^{2}). (5 marks)

For n≥1n \ge 1: 42n≤42n242n \le 42n^{2} and 7≤7n27 \le 7n^{2}. Hence

n2+42n+7≤n2+42n2+7n2=50n2(n≥1),n^{2}+42n+7 \le n^{2}+42n^{2}+7n^{2} = 50\,n^{2}\qquad (n \ge 1),

so c=50c = 50, n0=1n_0 = 1 and n2+42n+7=O(n2)n^{2}+42n+7 = O(n^{2}). ◼\blacksquare

Q36. Use logical equivalences to show that ¬(p∨¬(p∧q))\neg\big(p \lor \neg(p \land q)\big) is a contradiction. (5 marks)

¬(p∨¬(p∧q))≡¬p∧¬¬(p∧q)De Morgan’s law≡¬p∧(p∧q)double negation≡(¬p∧p)∧qassociative law≡𝐅∧qnegation law (¬p∧p≡𝐅)≡𝐅domination law\begin{aligned} \neg\big(p \lor \neg(p \land q)\big) &\equiv \neg p \land \neg\neg(p \land q) &&\text{De Morgan's law}\\ &\equiv \neg p \land (p \land q) &&\text{double negation}\\ &\equiv (\neg p \land p) \land q &&\text{associative law}\\ &\equiv \mathbf{F} \land q &&\text{negation law } (\neg p\land p\equiv \mathbf F)\\ &\equiv \mathbf{F} &&\text{domination law} \end{aligned}

The formula is equivalent to 𝐅\mathbf F, so it is false for every truth assignment, i.e. it is a contradiction. ◼\blacksquare

Q37. Prove by contradiction that [B∧(B→C)]→C[B \land (B \to C)] \to C is always true (modus ponens). (5 marks)

Assume the statement is false for some assignment. An implication is false only when its hypothesis is true and its conclusion is false, so

B∧(B→C)is trueandCis false.B \land (B\to C)\ \text{is true}\quad\text{and}\quad C\ \text{is false}.

From B∧(B→C)B \land (B\to C) being true, BB is true and B→CB \to C is true. A true implication with a true hypothesis forces its conclusion to be true, so CC is true. This contradicts "CC is false". Hence no such assignment exists and [B∧(B→C)]→C[B \land (B\to C)] \to C is a tautology. ◼\blacksquare

Q38. Show that [(p→q)∧(q→r)]→(p→r)[(p \to q)\land(q \to r)] \to (p \to r) is a tautology (hypothetical syllogism). (5 marks)

By logical equivalences.

[(p→q)∧(q→r)]→(p→r)≡¬[(¬p∨q)∧(¬q∨r)]∨(¬p∨r)≡(p∧¬q)∨(q∧¬r)∨¬p∨rDe Morgan≡[(p∧¬q)∨¬p]∨[(q∧¬r)∨r]regroup≡(¬p∨¬q)∨(q∨r)since (x∧y)∨¬x≡¬x∨y≡¬p∨(¬q∨q)∨r≡𝐓negation law\begin{aligned} [(p\to q)\land(q\to r)]\to(p\to r) &\equiv \neg[(\neg p\lor q)\land(\neg q\lor r)]\lor(\neg p\lor r)\\ &\equiv (p\land\neg q)\lor(q\land\neg r)\lor\neg p\lor r &&\text{De Morgan}\\ &\equiv \big[(p\land\neg q)\lor\neg p\big]\lor\big[(q\land\neg r)\lor r\big] &&\text{regroup}\\ &\equiv (\neg p\lor\neg q)\lor(q\lor r) &&\text{since }(x\land y)\lor\neg x\equiv\neg x\lor y\\ &\equiv \neg p\lor(\neg q\lor q)\lor r\equiv\mathbf T &&\text{negation law} \end{aligned}

A shorter argument: suppose the hypothesis (p→q)∧(q→r)(p\to q)\land(q\to r) is true and pp is true. From p→qp\to q we get qq; from q→rq\to r we get rr. So p→rp\to r is true whenever the hypothesis is true, and the implication can never be false.

By truth table.

pp qq rr p→qp\to q q→rq\to r (p→q)∧(q→r)(p\to q)\land(q\to r) p→rp\to r whole formula\text{whole formula}
T T T T T T T T
T T F T F F F T
T F T F T F T T
T F F F T F F T
F T T T T T T T
F T F T F F T T
F F T T T T T T
F F F T T T T T

All entries of the last column are true, so it is a tautology. ◼\blacksquare

Q39. Show that [(p∨q)∧(p→r)∧(q→r)]→r[(p \lor q)\land(p \to r)\land(q \to r)] \to r is a tautology (proof by cases). (5 marks)

Suppose the hypothesis (p∨q)∧(p→r)∧(q→r)(p\lor q)\land(p\to r)\land(q\to r) is true. Because p∨qp\lor q is true, at least one of p,qp,q is true.

  • If pp is true, then p→rp\to r gives rr.
  • If qq is true, then q→rq\to r gives rr.

In both cases rr is true, so the implication is never false. A truth table confirms this:

pp qq rr p∨qp\lor q p→rp\to r q→rq\to r (p∨q)∧(p→r)∧(q→r)(p\lor q)\land(p\to r)\land(q\to r) whole formula\text{whole formula}
T T T T T T T T
T T F T F F F T
T F T T T T T T
T F F T F T F T
F T T T T T T T
F T F T T F F T
F F T F T T F T
F F F F T T F T

The last column is all true, so it is a tautology. ◼\blacksquare

Q40. Show that [¬p∧(p→q)]→¬q[\neg p \land (p \to q)] \to \neg q is not a tautology. (5 marks)

A formula is not a tautology if some assignment makes it false. Try p=𝐅p=\mathbf F, q=𝐓q=\mathbf T:

  • ¬p=𝐓\neg p = \mathbf T and p→q=𝐓p\to q = \mathbf T, so the hypothesis ¬p∧(p→q)=𝐓\neg p\land(p\to q)=\mathbf T;
  • the conclusion ¬q=𝐅\neg q = \mathbf F.

A true hypothesis with a false conclusion makes the implication false.

pp qq ¬p\neg p p→qp\to q ¬p∧(p→q)\neg p\land(p\to q) ¬q\neg q [¬p∧(p→q)]→¬q[\neg p\land(p\to q)]\to\neg q
T T F T F F T
T F F F F T T
F T T T T F F
F F T T T T T

The row p=𝐅,q=𝐓p=\mathbf F,\ q=\mathbf T gives 𝐅\mathbf F, so the formula is not a tautology (this is the fallacy of denying the hypothesis). ◼\blacksquare

Q41. Prove that for all integers aa and bb and any non-negative integer nn, gcd(an,bn)=ngcd(a,b)\gcd(an,\,bn) = n\gcd(a,b). (5 marks)

If n=0n = 0 both sides are 00, so the statement is trivial.

Let n>0n > 0. The gcd of two integers is the smallest positive element of the set of their integer linear combinations. Hence

gcd(an,bn)=min{anx+bny:x,y∈ℤ,anx+bny>0}=min{n(ax+by)}=n⋅min{ax+by>0}=n⋅gcd(a,b).\gcd(an,bn) = \min\{\,anx + bny : x,y\in\mathbb Z,\ anx+bny>0\,\} = \min\{\,n\,(ax+by)\,\}= n\cdot\min\{\,ax+by>0\,\}= n\cdot\gcd(a,b).

The last step holds because min{ax+by>0}\min\{ax+by>0\} is the smallest positive element of the combinations of aa and bb, which is gcd(a,b)\gcd(a,b). ◼\blacksquare

Note: the shared solution ends with "ngcd(x,y)n\gcd(x,y)", which is a typo for ngcd(a,b)n\gcd(a,b).

Q42. What is a linear congruence ax≡b(modm)ax \equiv b \pmod m? How is it solved? (5 marks)

A congruence of the form ax≡b(modm)ax \equiv b \pmod m is called a linear congruence. To solve it we must find all integers xx that satisfy it.

  • An inverse of aa modulo mm is an integer a′a' such that a′a≡1(modm)a'a \equiv 1 \pmod m. It exists exactly when gcd(a,m)=1\gcd(a,m) = 1.
  • If such a′a' exists, multiply both sides by it:

a′ax≡a′b⇒1⋅x≡a′b⇒x≡a′b(modm).a'ax \equiv a'b \;\Longrightarrow\; 1\cdot x \equiv a'b \;\Longrightarrow\; x \equiv a'b \pmod m .

Example (added for illustration). Solve 3x≡2(mod7)3x \equiv 2 \pmod 7. Since 3⋅5=15≡1(mod7)3\cdot 5 = 15 \equiv 1 \pmod 7, the inverse of 33 is a′=5a' = 5, so x≡5⋅2=10≡3(mod7)x \equiv 5\cdot 2 = 10 \equiv 3 \pmod 7. Check: 3⋅3=9≡2(mod7)3\cdot 3 = 9 \equiv 2 \pmod 7. ✓

Q43. Use mathematical induction to prove that n2≥n+100n^{2} \ge n + 100 for all n≥11n \ge 11. (5 marks)

Let P(n)P(n) be the statement n2≥n+100n^{2} \ge n + 100.

Basis step (n=11n = 11). 112=121≥111=11+10011^{2} = 121 \ge 111 = 11 + 100, which is true.

Inductive step. Assume P(k)P(k) for some k≥11k \ge 11, i.e. k2≥k+100k^{2} \ge k + 100. We must show (k+1)2≥(k+1)+100(k+1)^{2} \ge (k+1) + 100. Using the hypothesis,

(k+1)2=k2+2k+1≥(k+100)+2k+1=(k+1)+100+2k≥(k+1)+100,(k+1)^{2} = k^{2} + 2k + 1 \;\ge\; (k+100) + 2k + 1 = (k+1) + 100 + 2k \;\ge\; (k+1)+100,

because 2k>02k > 0. So P(k)⇒P(k+1)P(k) \Rightarrow P(k+1). By induction P(n)P(n) holds for all n≥11n \ge 11. ◼\blacksquare

Note: a shared solution concludes with "P(k)≥P(k+1)P(k) \ge P(k+1)", which is not a valid conclusion; the correct step is P(k)⇒P(k+1)P(k)\Rightarrow P(k+1) as shown above.

Q44. Prove that every integer n>1n > 1 can be written as a product of primes. (5 marks)

We use strong induction on nn.

Basis. n=2n = 2 is prime, so it is a product of one prime.

Inductive step. Let n>2n > 2 and assume every integer mm with 2≤m<n2 \le m < n is a product of primes.

  • If nn is prime, it is a product of one prime.
  • If nn is composite, then n=a⋅bn = a\cdot b with 2≤a,b<n2 \le a,b < n. By the inductive hypothesis aa and bb are each products of primes, so n=a⋅bn = a\cdot b is a product of primes (the concatenation of the two products).

By strong induction every integer n>1n>1 is a product of primes. ◼\blacksquare

Q45. Prove the totient property φ(p⋅q)=(p−1)(q−1)\varphi(p\cdot q) = (p-1)(q-1), where pp and qq are distinct prime numbers. (5 marks)

For a prime pp, every number 1,2,…,p−11,2,\ldots,p-1 is relatively prime to pp, so φ(p)=p−1\varphi(p) = p-1 (for example φ(7)=6\varphi(7) = 6). Likewise φ(q)=q−1\varphi(q) = q-1.

Now let n=pqn = pq. Consider the residues S1={0,1,2,…,pq−1}S_1 = \{0,1,2,\ldots,pq-1\} — there are pqpq of them. The ones not relatively prime to nn are the multiples of pp or of qq:

S2={p,2p,…,(q−1)p}(q−1 elements),S3={q,2q,…,(p−1)q}(p−1 elements),S4={0}(1 element).S_2=\{p,2p,\ldots,(q-1)p\}\ \ (q-1\text{ elements}),\qquad S_3=\{q,2q,\ldots,(p-1)q\}\ \ (p-1\text{ elements}),\qquad S_4=\{0\}\ \ (1\text{ element}).

These three sets do not overlap (a common multiple of pp and qq below pqpq would be 00 only). Hence

φ(n)=pq−[(q−1)+(p−1)+1]=pq−p−q+1=(p−1)(q−1)=φ(p)φ(q).◼\varphi(n) = pq - \big[(q-1)+(p-1)+1\big] = pq - p - q + 1 = (p-1)(q-1) = \varphi(p)\,\varphi(q). \ \blacksquare

Q46. Prove: for any integer nn and any prime pp, if pp divides nn then pp does not divide n+1n+1. (5 marks)

Suppose, for contradiction, that p∣np \mid n and p∣(n+1)p \mid (n+1). Then pp divides their difference:

p∣(n+1)−n=1.p \mid (n+1) - n = 1 .

But the only positive divisor of 11 is 11, and a prime is greater than 11. This is a contradiction. Hence if p∣np \mid n then p∤n+1p \nmid n+1. ◼\blacksquare

Q47. Prove that the square of an odd integer has the form 8m+18m+1 for some integer mm. (5 marks)

By the quotient–remainder theorem every integer has one of the forms 4m,4m+1,4m+2,4m+34m,\ 4m+1,\ 4m+2,\ 4m+3. An odd integer must be 4m+14m+1 or 4m+34m+3.

Case 1: n=4m+1n = 4m+1.

(4m+1)2=16m2+8m+1=8(2m2+m)+1=8m′+1,m′=2m2+m.(4m+1)^{2} = 16m^{2}+8m+1 = 8(2m^{2}+m)+1 = 8m'+1,\qquad m' = 2m^{2}+m .

Case 2: n=4m+3n = 4m+3.

(4m+3)2=16m2+24m+9=8(2m2+3m+1)+1=8m″+1,m″=2m2+3m+1.(4m+3)^{2} = 16m^{2}+24m+9 = 8(2m^{2}+3m+1)+1 = 8m''+1,\qquad m''=2m^{2}+3m+1 .

In both cases the square has the form 8k+18k+1. ◼\blacksquare

Q48. Use mathematical induction to prove that n<2nn < 2^{n} for all n∈ℤ+n \in \mathbb Z^{+}. (5 marks)

Let P(n)P(n): n<2nn < 2^{n}.

Basis. P(1)P(1): 1<21=21 < 2^{1}=2, true.

Inductive step. Assume k<2kk < 2^{k} for some k≥1k \ge 1. Then

k+1<2k+1≤2k+2k=2⋅2k=2k+1,k+1 < 2^{k}+1 \le 2^{k}+2^{k} = 2\cdot 2^{k} = 2^{k+1},

using 1≤2k1 \le 2^{k}. So P(k+1)P(k+1) holds, and n<2nn<2^{n} for all positive integers nn. ◼\blacksquare

Q49. Prove that any integer n≥2n \ge 2 is divisible by a prime. (5 marks)

We use strong induction.

Basis. n=2n=2: 2∣22\mid 2 and 22 is prime.

Inductive step. Assume every integer ii with 2≤i<k2\le i<k is divisible by some prime (inductive hypothesis). We show it for kk.

  • Case 1: kk is prime. Then k∣kk\mid k and kk itself is the prime divisor.
  • Case 2: kk is composite. Then k=a⋅bk=a\cdot b with 2≤a<k2\le a<k and 2≤b<k2\le b<k. By the hypothesis some prime pp divides aa (p∣ap\mid a); also a∣ka\mid k. By transitivity of divisibility, p∣kp\mid k.

Hence P(n)P(n) is true for all n≥2n\ge 2 by strong induction. ◼\blacksquare

Q50. Prove Euclid's rule: for positive integers x≥yx \ge y, gcd(x,y)=gcd(xmody,y)\gcd(x,y) = \gcd(x \bmod y,\ y). (Equivalently gcd(a,b)=gcd(b,amodb)\gcd(a,b)=\gcd(b,\ a \bmod b).) (5 marks)

It is enough to prove the simpler rule gcd(x,y)=gcd(x−y,y)\gcd(x,y) = \gcd(x-y,\,y), because xmodyx \bmod y is obtained by subtracting yy from xx repeatedly.

  • Any integer that divides both xx and yy also divides x−yx-y. So every common divisor of (x,y)(x,y) is a common divisor of (x−y,y)(x-y,\,y), giving gcd(x,y)≤gcd(x−y,y)\gcd(x,y)\le\gcd(x-y,y).
  • Likewise, any integer that divides both x−yx-y and yy also divides (x−y)+y=x(x-y)+y = x, so it is a common divisor of (x,y)(x,y), giving gcd(x,y)≥gcd(x−y,y)\gcd(x,y)\ge\gcd(x-y,y).

Therefore gcd(x,y)=gcd(x−y,y)\gcd(x,y) = \gcd(x-y,\,y), and repeating the subtraction gives gcd(x,y)=gcd(xmody,y)\gcd(x,y)=\gcd(x\bmod y,\ y). ◼\blacksquare

Q51. Find all sequences of the form 1,t,t2,…,tn,…1, t, t^{2}, \ldots, t^{n}, \ldots (t≠0t\neq 0) that satisfy ak=ak−1+2ak−2a_k = a_{k-1} + 2a_{k-2} for all k≥2k \ge 2. (5 marks)

Put an=tna_n = t^{n} into the relation: tn=tn−1+2tn−2t^{n} = t^{n-1} + 2t^{n-2}. Dividing by tn−2(t≠0)t^{n-2}\ (t\ne0) gives the characteristic equation

t2=t+2⇔t2−t−2=0⇔(t−2)(t+1)=0⇒t=2ort=−1.t^{2} = t + 2 \;\Longleftrightarrow\; t^{2}-t-2 = 0 \;\Longleftrightarrow\; (t-2)(t+1) = 0 \;\Longrightarrow\; t = 2 \ \text{or}\ t = -1 .

So the sequences

1,2,4,8,…,2n,…and1,−1,1,−1,…,(−1)n,…1,\,2,\,4,\,8,\ldots,2^{n},\ldots\qquad\text{and}\qquad 1,\,-1,\,1,\,-1,\ldots,(-1)^{n},\ldots

are both particular solutions of the recurrence. ◼\blacksquare

Q52. Prove that any linear combination of solutions of the homogeneous linear recurrence a0tn+a1tn−1+⋯+aktn−k=0a_0 t_n + a_1 t_{n-1} + \cdots + a_k t_{n-k} = 0 is also a solution. (5 marks)

Let (sn)(s_n) and (un)(u_n) be two solutions, so for every nn

∑i=0kaisn−i=0and∑i=0kaiun−i=0.\sum_{i=0}^{k} a_i s_{n-i} = 0 \qquad\text{and}\qquad \sum_{i=0}^{k} a_i u_{n-i} = 0 .

For constants c,dc,d put wn=csn+dunw_n = c\,s_n + d\,u_n. Then

∑i=0kaiwn−i=c∑i=0kaisn−i+d∑i=0kaiun−i=c⋅0+d⋅0=0.\sum_{i=0}^{k} a_i w_{n-i} = c\sum_{i=0}^{k} a_i s_{n-i} + d\sum_{i=0}^{k} a_i u_{n-i} = c\cdot 0 + d\cdot 0 = 0 .

So (wn)(w_n) is again a solution. By repeating the argument, any finite linear combination ∑jcjsn(j)\sum_j c_j s^{(j)}_n of solutions is a solution. ◼\blacksquare

Q53. Solve the first-order recurrence bk=abk−1b_k = a\,b_{k-1} with b0=cb_0 = c. Hence solve bk=5bk−1b_k = 5b_{k-1} with b0=3b_0 = 3. (5 marks)

Expand repeatedly:

bk=abk−1=a2bk−2=a3bk−3=⋯=akb0=cak.b_k = a\,b_{k-1} = a^{2}b_{k-2} = a^{3}b_{k-3} = \cdots = a^{k}b_{0} = c\,a^{k}.

So the explicit formula is bk=c⋅akb_k = c\cdot a^{k} — a geometric sequence. A first-order homogeneous recurrence is simply a geometric sequence.

For a=5a = 5 and b0=c=3b_0 = c = 3: bk=3⋅5k\boxed{b_k = 3\cdot 5^{k}} (so 3,15,75,375,…3, 15, 75, 375,\ldots). ◼\blacksquare

Q54. What is the Tower of Hanoi? Write its recurrence and solve it. (5 marks)

The Tower of Hanoi is a puzzle with three towers and nn disks of different sizes, initially stacked on one tower in order of size (smallest on top). The goal is to move the whole stack to another tower under these rules:

  • only one disk may be moved at a time;
  • a move takes the top disk of one tower and slides it onto another tower;
  • no disk may be placed on top of a smaller disk.

Recurrence. To move nn disks: move the top n−1n-1 disks to the spare tower (T(n−1)T(n-1) moves), move the largest disk (1 move), then move the n−1n-1 disks on top of it (T(n−1)T(n-1) moves):

T(n)=2T(n−1)+1,T(1)=1.T(n) = 2\,T(n-1) + 1,\qquad T(1) = 1 .

Solution. Expanding, T(n)=2T(n−1)+1=22T(n−2)+2+1=⋯=2n−1T(1)+(2n−2+⋯+2+1)=2n−1+2n−1−1T(n) = 2T(n-1)+1 = 2^{2}T(n-2)+2+1 = \cdots = 2^{n-1}T(1) + (2^{n-2}+\cdots+2+1) = 2^{n-1} + 2^{n-1}-1, so

T(n)=2n−1\boxed{T(n) = 2^{n}-1}

(Equivalently, the characteristic polynomial (x−2)(x−1)(x-2)(x-1) gives T(n)=c12n+c2T(n) = c_1 2^{n}+c_2, and T(0)=0,T(1)=1T(0)=0,\ T(1)=1 give c1=1,c2=−1c_1 = 1,\ c_2=-1.) ◼\blacksquare

Q55. Find a general formula for the Fibonacci sequence fn=fn−1+fn−2f_n = f_{n-1} + f_{n-2} (with f0=0f_0 = 0, f1=1f_1 = 1). (5 marks)

The Fibonacci sequence is defined by Fk=Fk−1+Fk−2F_k = F_{k-1}+F_{k-2} with F0=0,F1=1F_0=0,\ F_1=1, giving

0,1,1,2,3,5,8,13,21,34,55,89,144,…0,\ 1,\ 1,\ 2,\ 3,\ 5,\ 8,\ 13,\ 21,\ 34,\ 55,\ 89,\ 144,\ldots

Characteristic equation. r2−r−1=0r^{2} - r - 1 = 0, with roots

r1=φ=1+52,r2=ψ=1−52.r_1 = \varphi = \frac{1+\sqrt5}{2},\qquad r_2 = \psi = \frac{1-\sqrt5}{2}.

General solution Fn=c1φn+c2ψnF_n = c_1\varphi^{n}+c_2\psi^{n}. From F0=0F_0 = 0: c1+c2=0c_1+c_2=0. From F1=1F_1=1: c1φ+c2ψ=1c_1\varphi + c_2\psi = 1. Since φ−ψ=5\varphi-\psi=\sqrt5, we get c1=15c_1 = \dfrac1{\sqrt5}, c2=−15c_2=-\dfrac1{\sqrt5}:

Fn=15[(1+52)n−(1−52)n]\boxed{F_n = \frac{1}{\sqrt5}\left[\left(\frac{1+\sqrt5}{2}\right)^{n} - \left(\frac{1-\sqrt5}{2}\right)^{n}\right]}

(Binet's formula.) For example F12=144F_{12} = 144. As n→∞n\to\infty, Fn+1/Fn→φ≈1.6180339887F_{n+1}/F_n \to \varphi \approx 1.6180339887, the golden ratio. ◼\blacksquare

Q56. Prove/solve T(n)=T(n−1)−CT(n) = T(n-1) - C (with T(1)T(1) given), solving the recurrence as printed. (10 marks)

Reading: the recurrence is solved exactly as printed, T(n)=T(n−1)−CT(n)=T(n-1)-C with constant C>0C>0.

T(n)=T(n−1)−C=T(n−2)−2C=T(n−3)−3C=⋯=T(n−k)−kC.T(n) = T(n-1)-C = T(n-2)-2C = T(n-3)-3C = \cdots = T(n-k)-kC .

At k=n−1k = n-1: T(n)=T(1)−(n−1)CT(n) = T(1)-(n-1)C. So T(n)T(n) decreases linearly: T(n)=T(1)−C(n−1)T(n) = T(1) - C(n-1). This is the same as the previous recurrence with cc replaced by −C-C. Such a recurrence cannot describe the running time of an algorithm for large nn (the "time" would eventually become negative); with +C+C it gives Θ(n)\Theta(n). ◼\blacksquare

Q57. Solve the recurrence jn=jn−2j_n = j_{n-2}. (10 marks)

Reading: the question is printed as "solve the recurrence jn=jn−2j_n = j_{n-2}" (some copies say "prove"). It is solved here exactly as written; if the intended recurrence was a different one, the same method applies.

Characteristic equation: x2=1x^{2} = 1, i.e. x2−1=0x^{2}-1=0, roots x=1x=1 and x=−1x=-1. Hence

jn=A⋅1n+B⋅(−1)n=A+B(−1)n.j_n = A\cdot 1^{n} + B\cdot(-1)^{n} = A + B(-1)^{n}.

With j0j_0 and j1j_1 given: A+B=j0A+B=j_0, A−B=j1A-B=j_1, so A=j0+j12A=\tfrac{j_0+j_1}{2} and B=j0−j12B=\tfrac{j_0-j_1}{2}. The sequence just alternates: j0,j1,j0,j1,…j_0, j_1, j_0, j_1,\ldots. ◼\blacksquare

Q58. What is the Fibonacci sequence? Write its recursive formula, compute its first values, and list some applications. (5 marks)

The Fibonacci sequence is defined by

Fk=Fk−1+Fk−2,F0=0,F1=1.F_k = F_{k-1} + F_{k-2},\qquad F_0 = 0,\ F_1 = 1 .

kk 0 1 2 3 4 5 6 7 8 9 10 11 12
FkF_k 0 1 1 2 3 5 8 13 21 34 55 89 144

Applications and properties (from the lecture):

  • trend analysis and some pseudo-random number generators;
  • the number of petals of many flowers is a Fibonacci number; leaves around stems and seeds on flower heads follow Fibonacci arrangements;
  • consecutive Fibonacci numbers give the worst-case behaviour of Euclid's gcd algorithm;
  • Fn+1Fn→φ=1.6180339887…\dfrac{F_{n+1}}{F_n} \to \varphi = 1.6180339887\ldots (the golden ratio) as n→∞n\to\infty;
  • Fn+1F_{n+1} is the number of ways 2×12\times1 dominoes can cover a 2×n2\times n board;
  • ∑i=1nFi=Fn+2−1\displaystyle\sum_{i=1}^{n}F_i = F_{n+2}-1; gcd(Fn,Fm)=Fgcd(n,m)\ \gcd(F_n,F_m)=F_{\gcd(n,m)}; the shallow diagonals of Pascal's triangle sum to Fibonacci numbers.

Q59. Write down the steps of the divide-and-conquer approach and describe its running time. (5 marks)

Divide and conquer solves a problem of size nn in three steps:

  1. Divide — split the problem into sub-problems that are smaller instances of the same problem (and independent of each other). Cost S(n)S(n).
  2. Conquer — solve each sub-problem recursively; when a sub-problem is small enough (size <c<c) solve it directly in constant time bb.
  3. Combine — merge the sub-solutions into the solution of the original problem. Cost M(n)M(n).

Time complexity. If the problem is split into two halves,

T(n)={2T(n/2)+S(n)+M(n)n≥c,bn<c,T(n)=\begin{cases} 2\,T(n/2) + S(n) + M(n) & n\ge c,\\ b & n<c,\end{cases}

where S(n)S(n) is the time for splitting, M(n)M(n) the time for merging, and b,cb,c are constants. For example, merge sort has S(n)=O(1)S(n)=O(1) and M(n)=Θ(n)M(n)=\Theta(n), so T(n)=2T(n/2)+Θ(n)=Θ(nlogn)T(n) = 2T(n/2)+\Theta(n) = \Theta(n\log n); the maxima algorithm below has S(n)=O(n)S(n)=O(n), M(n)=O(n)M(n)=O(n) and the same solution.

Q60. Write the algorithm of the brute-force approach for finding the maximal points of a set of 2-dimensional points. (5 marks)

A point P[i]P[i] is maximal if no other point P[j]P[j] satisfies P[i].x≤P[j].xP[i].x \le P[j].x and P[i].y≤P[j].yP[i].y \le P[j].y (no other point dominates it). Brute force compares every point with every other point, testing both coordinates:

MAXIMAL-POINTS(Point P[1..m])
0   A = ∅
1   for i ← 1 to m                  // m = number of points
2       maximal ← true
3       for j ← 1 to m
4           if (i ≠ j) and (P[i].x ≤ P[j].x) and (P[i].y ≤ P[j].y)
5               maximal ← false
6               break
7       if maximal
8           A ← A ∪ {P[i]}
9   return A

Time complexity. Two nested loops over mm points, each comparison taking constant time: T(m)=∑i=1m∑j=1mc=cm2=Θ(m2)T(m)=\sum_{i=1}^{m}\sum_{j=1}^{m}c = c\,m^{2} = \Theta(m^{2}).

Q61. Write the Merge-sort algorithm and analyse its running time. (10 marks)

Merge sort is a divide-and-conquer algorithm: divide the array into two halves; recursively sort each half; merge the two sorted halves.

MERGE-SORT(A, f, l)
1.  if f < l
2.      then m ← ⌊(f + l) / 2⌋
3.           MERGE-SORT(A, f, m)
4.           MERGE-SORT(A, m + 1, l)
5.           MERGE(A, f, m, l)

MERGE(A, f, m, l)
1.  T[f..l]                          // temporary array of the same size
2.  i ← f;  k ← f;  j ← m + 1
3.  while (i ≤ m) and (j ≤ l)
4.      do if A[i] ≤ A[j]
5.             then T[k++] ← A[i++]
6.             else T[k++] ← A[j++]
7.  while (i ≤ m)   do T[k++] ← A[i++]       // copy rest of left half
9.  while (j ≤ l)   do T[k++] ← A[j++]       // copy rest of right half
11. for i ← f to l  do A[i] ← T[i]           // copy back from T to A

(The printed handout writes "for i←pi\leftarrow p to rr" in the last loop; it should use the same bounds f..lf..l as the rest of the procedure.)

Analysis. MERGE takes linear time, so T(n)=T(n/2)+T(n/2)+Θ(n)=2T(n/2)+Θ(n)T(n)=T(n/2)+T(n/2)+\Theta(n)=2T(n/2)+\Theta(n). By the recursion-tree or master method T(n)=Θ(nlogn)T(n)=\Theta(n\log n) (see the maxima analysis above for the substitution).

Q62. Write an algorithm for the closest pair of points in 2-D using the brute-force approach, and give its time complexity. (5 marks)

Compute the distance between every pair of points and keep the smallest. (Squared distances can be compared, so the square root is not needed.)

ClosestPairBF(P[1..n])
1   mind ← ∞
2   for i ← 1 to n - 1
3       for j ← i + 1 to n
4           d ← (x_i - x_j)² + (y_i - y_j)²
5           if d < mind
6               mind ← d
7               mini ← i
8               minj ← j
9   return mind, P[mini], P[minj]

Improved version: the inner loop starts at j=i+1j=i+1, so each unordered pair is examined once (n(n−1)/2n(n-1)/2 pairs). The first (non-improved) version uses for i ← 1 to n and for j ← 1 to n with the test if i ≠ j, which examines every pair twice.

Time complexity.

T(n)=∑i=1n−1∑j=i+1nc=∑i=1n−1c(n−i)=c(n(n−1)−(n−1)n2)=cn(n−1)2=Θ(n2).T(n)=\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}c=\sum_{i=1}^{n-1}c\,(n-i)=c\left(n(n-1)-\frac{(n-1)n}{2}\right)=c\,\frac{n(n-1)}{2}=\Theta(n^{2}).

(The unimproved version gives ∑i=1n∑j=1nc=cn2=Θ(n2)\sum_{i=1}^{n}\sum_{j=1}^{n}c=cn^{2}=\Theta(n^{2}) as well, with a larger constant.) The printed handout uses "minn" in line 8 as a typo for "mind".

Q63. Write the brute-force algorithm for the closest pair of points in 3-D. (5 marks)

Only the distance formula changes: a third coordinate is added.

ClosestPairBF(P[1..n])
1   mind ← ∞
2   for i ← 1 to n - 1
3       for j ← i + 1 to n
4           d ← (x_i - x_j)² + (y_i - y_j)² + (z_i - z_j)²
5           if d < mind
6               mind ← d
7               mini ← i
8               minj ← j
9   return mind, P[mini], P[minj]

Time complexity. Same double loop: ∑i=1n−1c(n−i)=cn(n−1)2=Θ(n2)\sum_{i=1}^{n-1}c\,(n-i)=c\,\dfrac{n(n-1)}{2}=\Theta(n^{2}).

Q64. Find the time complexity of each line of the following algorithm.

1. Procedure sort(A[1..n])
2.   for i := 1 to n-1 do
3.     for j := 1 to n-i do
4.       if A[j] > A[j+1] then
5.         swap A[j] with A[j+1]
(10 marks)

This is bubble sort. Analyse the worst case (array in reverse order) for an array of size nn.

line work times executed
2 outer-loop test and increment (cost c1c_1) nn times (the body runs n−1n-1 times)
3 inner-loop test and increment ∑i=1n−1(n−i+1)=n(n−1)2+(n−1)\displaystyle\sum_{i=1}^{n-1}(n-i+1)=\frac{n(n-1)}{2}+(n-1)
4 comparison A[j]>A[j+1]A[j]>A[j+1] ∑i=1n−1(n−i)=(n−1)+(n−2)+⋯+1=n(n−1)2\displaystyle\sum_{i=1}^{n-1}(n-i)=(n-1)+(n-2)+\cdots+1=\frac{n(n-1)}{2}
5 swap at most n(n−1)2\dfrac{n(n-1)}{2} (every comparison swaps in the worst case)

The first pass through the inner loop makes n−1n-1 comparisons and swaps, the next n−2n-2, …, and the last pass makes one. If cc is the time to do one comparison, one swap, one inner-loop test and one increment, and kk the constant time for declaring and initialising ii and jj:

T(n)=c[(n−1)+(n−2)+⋯+1]+c1(n−1)+k=c2n(n−1)+c1(n−1)+k=Θ(n2).T(n) = c\big[(n-1)+(n-2)+\cdots+1\big] + c_1(n-1) + k = \frac{c}{2}\,n(n-1)+c_1(n-1)+k=\Theta(n^{2}).

Q65. How many ways can a chain of nn matrices be fully parenthesised? Why is brute force not practical? (5 marks)

Let P(n)P(n) be the number of full parenthesisations of nn matrices. A single matrix has one way. For n≥2n\ge2 the last multiplication can split the chain after any matrix k=1,…,n−1k=1,\dots,n-1, giving

P(n)={1n=1,∑k=1n−1P(k)P(n−k)n≥2.P(n)=\begin{cases}1 & n=1,\\[4pt]\displaystyle\sum_{k=1}^{n-1}P(k)\,P(n-k) & n\ge2.\end{cases}

This is the Catalan recurrence, so P(n)=C(n−1)P(n)=C(n-1) where

C(m)=1m+1(2mm)=Ω(4mm3/2).C(m)=\frac{1}{m+1}\binom{2m}{m}=\Omega\!\left(\frac{4^{m}}{m^{3/2}}\right).

The sequence is 1,1,2,5,14,42,132,…1,1,2,5,14,42,132,\dots and grows exponentially (about 4n/n3/24^n/n^{3/2}). Trying every parenthesisation and computing its cost is therefore exponential, which is why we use dynamic programming (Θ(n3)\Theta(n^3)).

Q66. Write the recursive (top-down) version of the matrix-chain algorithm. Why is it inefficient without memoisation? (6 marks)

RECURSIVE-MATRIX-CHAIN(p, i, j)
1   if i = j
2       return 0
3   m[i,j] ← ∞
4   for k ← i to j − 1
5       q ← RECURSIVE-MATRIX-CHAIN(p,i,k) + RECURSIVE-MATRIX-CHAIN(p,k+1,j) + p[i−1]·p[k]·p[j]
6       if q < m[i,j]
7           m[i,j] ← q
8   return m[i,j]

The recursion recomputes the same sub-chains Ai⋯AjA_i\cdots A_j many times (overlapping sub-problems). Its running time satisfies T(n)≥1+∑k=1n−1(T(k)+T(n−k)+1)T(n)\ge 1+\sum_{k=1}^{n-1}\bigl(T(k)+T(n-k)+1\bigr), which gives T(n)=Ω(2n)T(n)=\Omega(2^{n}). Storing each m[i,j]m[i,j] the first time it is computed (memoisation) reduces the work to the Θ(n2)\Theta(n^2) distinct sub-problems times O(n)O(n) each, i.e. O(n3)O(n^3).

Q67. Compare divide-and-conquer with dynamic programming. (5 marks)

Divide and conquer Dynamic programming
Sub-problems Independent (do not overlap) Overlapping (the same sub-problem occurs many times)
Work Each sub-problem is solved separately, possibly repeatedly Each sub-problem is solved once and its answer stored in a table
Direction Top-down recursion Usually bottom-up (table filling) or memoised top-down
Typical use Merge sort, quick sort, maximal points, closest pair Matrix chain, knapsack, LCS, assembly line
Needs Combine step Optimal substructure + overlapping sub-problems

Q68. Why can the minimum-weight triangulation of a convex polygon not be solved by plain divide and conquer, and what makes dynamic programming suitable? (5 marks)

Splitting the polygon along a chord vivjv_iv_j gives two smaller polygons, but the same sub-polygons (sets of consecutive vertices vi,…,vjv_i,\dots,v_j) arise from many different choices of the first chord. The sub-problems therefore overlap, and a divide-and-conquer solution would recompute them exponentially often.

With t[i,j]t[i,j] the minimum weight of triangulating the polygon vi−1,vi,…,vjv_{i-1},v_i,\dots,v_j:

t[i,j]={0i=j,mini≤k<j{t[i,k]+t[k+1,j]+w(vi−1vkvj)}i<j,t[i,j]=\begin{cases}0 & i=j,\\[4pt]\displaystyle\min_{i\le k<j}\bigl\{t[i,k]+t[k+1,j]+w(v_{i-1}v_kv_j)\bigr\} & i<j,\end{cases}

which is the same recurrence as matrix-chain multiplication; there are only Θ(n2)\Theta(n^2) distinct sub-polygons, so the answer is found in Θ(n3)\Theta(n^3) time.

Q69. What are the steps of dynamic programming, and what are its limitations? (5 marks)

Steps

  1. Characterise the structure of an optimal solution (optimal substructure).
  2. Recursively define the value of an optimal solution.
  3. Compute the value bottom-up, storing results in a table.
  4. Construct the optimal solution from the stored information.

Limitations

  • The problem must have optimal substructure and overlapping sub-problems; otherwise DP gives no gain.
  • It needs extra memory for the table, often Θ(n2)\Theta(n^2) or Θ(nW)\Theta(nW).
  • The knapsack algorithm runs in O(nW)O(nW), which is pseudo-polynomial: it is exponential in the number of bits needed to write WW.
  • A table entry may depend on many others, so the order of filling must be chosen carefully.

Q70. Brute-force the knapsack instance: capacity W=32W=32, items (weight, value) = (4, 40), (10, 60), (20, 100), (10, 20). (6 marks)

Check every subset (weight, value):

Subset Weight Value Feasible (≤32\le32)?
∅ 0 0 yes
{1} 4 40 yes
{2} 10 60 yes
{3} 20 100 yes
{4} 10 20 yes
{1,2} 14 100 yes
{1,3} 24 140 yes
{1,4} 14 60 yes
{2,3} 30 160 yes
{2,4} 20 80 yes
{3,4} 30 120 yes
{1,2,3} 34 200 no
{1,2,4} 24 120 yes
{1,3,4} 34 160 no
{2,3,4} 40 180 no
{1,2,3,4} 44 220 no

The best feasible subset is {2, 3} with weight 30 and value 160.

Q71. Write the algorithm for the fractional knapsack problem. How does it differ from 0-1 knapsack? (6 marks)

In the fractional knapsack an item may be split and any fraction xi∈[0,1]x_i\in[0,1] taken. A greedy strategy is optimal: take items in decreasing order of value per unit weight vi/wiv_i/w_i.

FRACTIONAL-KNAPSACK(v[1..n], w[1..n], W)
1   sort items so that v[1]/w[1] ≥ v[2]/w[2] ≥ ... ≥ v[n]/w[n]
2   for i ← 1 to n: x[i] ← 0
3   weight ← 0
4   for i ← 1 to n
5       if weight + w[i] ≤ W
6           x[i] ← 1 ; weight ← weight + w[i]
7       else
8           x[i] ← (W − weight)/w[i]
9           break
10  return x

Time O(nlogn)O(n\log n) (sorting). The greedy method is not optimal for 0-1 knapsack, which needs dynamic programming (O(nW)O(nW)).

Q72. What is the brute-force cost of the two-line assembly problem? (3 marks)

Each of the nn stations can be done on line 1 or on line 2, so there are 2n2^{n} possible routes. Evaluating one route takes Θ(n)\Theta(n) additions, so brute force costs Θ(n2n)\Theta(n\,2^{n}), compared with Θ(n)\Theta(n) for dynamic programming.

Q73. For the given undirected graph with vertices 1–5 and edges 1–4, 2–4, 2–5, 3–5, 4–5 and a self-loop at 5, write the adjacency matrix and adjacency lists. (5 marks)

12345Edges: 1–4, 2–4, 2–5, 3–5, 4–5, and a self-loop at vertex 5

Adjacency matrix (rows and columns 1–5; a self-loop counts as 1 on the diagonal):

1 2 3 4 5
1 0 0 0 1 0
2 0 0 0 1 1
3 0 0 0 0 1
4 1 1 0 0 1
5 0 1 1 1 1

Adjacency lists:

  • 1 → 4
  • 2 → 4, 5
  • 3 → 5
  • 4 → 1, 2, 5
  • 5 → 2, 3, 4, 5

Long Questions

Algorithms, tables, trees and multi-part problems.

Q74. Arrange the following 20 functions from most complex to least complex (fastest growing first). Here nn is the input size and j>k>2j > k > 2 are constants.

nlogn10000\dfrac{n\log n}{10000}, 1000n6k/2\;1000\,n^{6k/2}, n24j/4\;n^{24j/4}, 10000lgn\;\sqrt{10000}\,\lg n, nn−1\;n^{\,n-1}, 52000000000\;52\,000\,000\,000, 2n\;2^{n}, n8lgn\;\sqrt[8]{n}\,\lg n, n!100\;\dfrac{n!}{100}, 2nnnn2\;\dfrac{2^{n}\,n\sqrt{n}}{\sqrt{n^{2}}}, n!n\;\dfrac{n!}{\sqrt{n}}, 2nnlogn\;2^{n}\,n\log n, n!logn\;\dfrac{n!}{\log n}, 5000\;5000, n4n4\;\dfrac{n^{4}}{\sqrt[4]{n}}, nlognn5\;n\log n\,\sqrt[5]{n}, n7n11\;\dfrac{n^{7}}{\sqrt[11]{n}}, nlognn3\;n\log n\,\sqrt[3]{n}, kn−2\;k^{\,n-2}, jn−4\;j^{\,n-4} (10 marks)

First simplify each function to a standard form:

function simplified form
2nnn/n22^{n}n\sqrt{n}\,/\sqrt{n^{2}} 2nn2^{n}\sqrt{n} (because nn/n=nn\sqrt n / n = \sqrt n)
n24j/4n^{24j/4} n6jn^{6j}
1000n6k/21000\,n^{6k/2} Θ(n3k)\Theta(n^{3k})
n7/n11n^{7}/\sqrt[11]{n} n7−1/11≈n6.91n^{7-1/11}\approx n^{6.91}
n4/n4n^{4}/\sqrt[4]{n} n4−1/4=n3.75n^{4-1/4}=n^{3.75}
nlognn3n\log n\,\sqrt[3]{n} n4/3lognn^{4/3}\log n
nlognn5n\log n\,\sqrt[5]{n} n6/5lognn^{6/5}\log n
10000lgn\sqrt{10000}\,\lg n 100lgn100\lg n

Growth classes, from fastest to slowest: factorial/super-exponential >> exponential (larger base wins) >> polynomial (larger exponent wins) >> nϵlognn^{\epsilon}\log n >> logn\log n >> constants.

rank function reason
1 nn−1n^{\,n-1} nn/nn^{n}/n grows faster than n!n! (Stirling: n!≈(n/e)n2πnn!\approx (n/e)^{n}\sqrt{2\pi n})
2 n!/100n!/100 factorial divided by a constant
3 n!/lognn!/\log n eventually logn>100\log n > 100, so dividing by logn\log n gives a smaller value than dividing by 100100
4 n!/nn!/\sqrt{n} n\sqrt n grows faster than logn\log n, so this is smaller than rank 3
5 jn−4j^{\,n-4} exponential with the larger base jj
6 kn−2k^{\,n-2} base k<jk<j; (j/k)n(j/k)^{n} grows without bound
7 2nnlogn2^{n}\,n\log n base 2<k2<k, with polynomial factor nlognn\log n
8 2nn2^{n}\sqrt{n} 2n2^{n} times n1/2n^{1/2}
9 2n2^{n} 2n2^{n} alone
10 n24j/4=n6jn^{24j/4} = n^{6j} polynomial of degree 6j>126j > 12
11 1000n6k/2=1000n3k1000\,n^{6k/2} = 1000\,n^{3k} degree 3k3k, smaller than 6j6j because j>kj>k
12 n7/n11n^{7}/\sqrt[11]{n} degree ≈6.91\approx 6.91
13 n4/n4n^{4}/\sqrt[4]{n} degree 3.753.75
14 nlognn3n\log n\,\sqrt[3]{n} n4/3lognn^{4/3}\log n
15 nlognn5n\log n\,\sqrt[5]{n} n6/5lognn^{6/5}\log n
16 nlogn/10000n\log n/10000 Θ(nlogn)\Theta(n\log n)
17 n8lgn\sqrt[8]{n}\,\lg n n1/8lognn^{1/8}\log n
18 10000lgn\sqrt{10000}\,\lg n 100lgn100\lg n
19 5200000000052\,000\,000\,000 constant (larger)
20 50005000 constant (smaller)

Assumption: ranks 10–12 assume 3k≥73k \ge 7 (that is, k≥7/3k \ge 7/3), which is what makes n3kn^{3k} larger than n7−1/11n^{7-1/11}; for 2<k<2.312<k<2.31 the functions at ranks 11 and 12 would swap. The standard answer key uses the order shown above.

Q75. Prove that ¬B→¬(A∧(A→B))\neg B \to \neg\big(A \land (A \to B)\big) by (a) logical equivalence, (b) rules of inference, (c) contradiction. (10 marks)

(a) Logical equivalence.

¬B→¬(A∧(A→B))≡¬B→¬(A∧(¬A∨B))implication law≡¬B→¬((A∧¬A)∨(A∧B))distributive law≡¬B→¬(𝐅∨(A∧B))negation law≡¬B→¬(A∧B)identity law≡¬¬B∨¬(A∧B)implication law≡B∨¬A∨¬Bdouble negation, De Morgan≡(B∨¬B)∨¬Acommutative/associative≡𝐓∨¬A≡𝐓negation and domination laws\begin{aligned} \neg B \to \neg\big(A \land (A\to B)\big) &\equiv \neg B \to \neg\big(A \land (\neg A \lor B)\big) &&\text{implication law}\\ &\equiv \neg B \to \neg\big((A\land\neg A) \lor (A \land B)\big) &&\text{distributive law}\\ &\equiv \neg B \to \neg\big(\mathbf F \lor (A \land B)\big) &&\text{negation law}\\ &\equiv \neg B \to \neg(A \land B) &&\text{identity law}\\ &\equiv \neg\neg B \lor \neg(A \land B) &&\text{implication law}\\ &\equiv B \lor \neg A \lor \neg B &&\text{double negation, De Morgan}\\ &\equiv (B \lor \neg B) \lor \neg A &&\text{commutative/associative}\\ &\equiv \mathbf T \lor \neg A \equiv \mathbf T &&\text{negation and domination laws} \end{aligned}

(b) Rules of inference (contraposition + modus ponens). The contrapositive of the statement is

¬¬(A∧(A→B))→¬¬B≡(A∧(A→B))→B.\neg\neg\big(A \land (A\to B)\big) \to \neg\neg B \;\equiv\; \big(A \land (A \to B)\big) \to B .

Suppose A∧(A→B)A \land (A\to B) is true. Then AA is true and A→BA\to B is true, so by modus ponens BB is true. Thus (A∧(A→B))→B(A \land (A\to B)) \to B holds, and so does the original statement (a statement and its contrapositive are equivalent).

(c) Contradiction. Assume ¬B\neg B is true but A∧(A→B)A \land (A\to B) is also true (the negation of the conclusion). Then

(A∧(A→B))∧¬B≡(A∧(¬A∨B))∧¬B≡((A∧¬A)∨(A∧B))∧¬B≡(A∧B)∧¬B≡A∧(B∧¬B)≡A∧𝐅≡𝐅.\begin{aligned} \big(A \land (A\to B)\big)\land\neg B &\equiv \big(A \land (\neg A\lor B)\big)\land\neg B\\ &\equiv \big((A\land\neg A)\lor(A\land B)\big)\land\neg B\\ &\equiv (A \land B)\land\neg B \equiv A \land (B\land\neg B) \equiv A\land\mathbf F\equiv\mathbf F . \end{aligned}

This is a contradiction, so ¬(A∧(A→B))\neg\big(A\land(A\to B)\big) must be true whenever ¬B\neg B is. ◼\blacksquare

Q76. Prove that ¬C→¬(B∧(B→C))\neg C \to \neg\big(B \land (B \to C)\big) by (1) logical equivalences, (2) truth table, (3) rules of inference, (4) contradiction. (10 marks)

1. Logical equivalences.

¬C→¬(B∧(B→C))≡C∨¬(B∧(¬B∨C))≡C∨¬((B∧¬B)∨(B∧C))≡C∨¬(B∧C)≡C∨¬B∨¬C≡(C∨¬C)∨¬B≡𝐓.\begin{aligned} \neg C \to \neg\big(B \land (B\to C)\big) &\equiv C \lor \neg\big(B \land (\neg B \lor C)\big)\\ &\equiv C \lor \neg\big((B\land\neg B)\lor(B\land C)\big)\\ &\equiv C \lor \neg(B \land C)\\ &\equiv C \lor \neg B \lor \neg C\\ &\equiv (C\lor\neg C)\lor\neg B \equiv \mathbf T . \end{aligned}

2. Truth table.

BB CC B→CB\to C B∧(B→C)B\land(B\to C) ¬(B∧(B→C))\neg(B\land(B\to C)) ¬C\neg C ¬C→¬(B∧(B→C))\neg C\to\neg(B\land(B\to C))
T T T T F F T
T F F F T T T
F T T F T F T
F F T F T T T

The last column is true in every row, so the statement is a tautology.

3. Rules of inference. Take the contrapositive: (B∧(B→C))→C\big(B \land (B\to C)\big) \to C. If B∧(B→C)B\land(B\to C) is true, then BB and B→CB\to C are true, so CC is true by modus ponens. Hence the contrapositive, and therefore the original statement, is true.

4. Contradiction. Suppose ¬C\neg C is true and B∧(B→C)B\land(B\to C) is true. Then (B∧(¬B∨C))∧¬C≡((B∧¬B)∨(B∧C))∧¬C≡(B∧C)∧¬C≡B∧𝐅≡𝐅(B\land(\neg B\lor C))\land\neg C \equiv ((B\land\neg B)\lor(B\land C))\land\neg C \equiv (B\land C)\land\neg C \equiv B\land\mathbf F\equiv\mathbf F, a contradiction. ◼\blacksquare

Q77. Show that (p∨q)∧(¬p∨r)→(q∨r)(p \lor q)\land(\neg p \lor r)\to(q \lor r) is a tautology. (10 marks)

By logical equivalences.

(p∨q)∧(¬p∨r)→(q∨r)≡¬[(p∨q)∧(¬p∨r)]∨(q∨r)≡[(¬p∧¬q)∨(p∧¬r)]∨q∨rDe Morgan≡[(¬p∧¬q)∨q]∨[(p∧¬r)∨r]regroup≡(¬p∨q)∨(p∨r)since (x∧¬q)∨q≡x∨q≡(¬p∨p)∨q∨r≡𝐓negation law.\begin{aligned} (p\lor q)\land(\neg p\lor r)\to(q\lor r) &\equiv \neg\big[(p\lor q)\land(\neg p\lor r)\big]\lor(q\lor r)\\ &\equiv \big[(\neg p\land\neg q)\lor(p\land\neg r)\big]\lor q\lor r &&\text{De Morgan}\\ &\equiv \big[(\neg p\land\neg q)\lor q\big]\lor\big[(p\land\neg r)\lor r\big] &&\text{regroup}\\ &\equiv (\neg p\lor q)\lor(p\lor r) &&\text{since }(x\land\neg q)\lor q\equiv x\lor q\\ &\equiv (\neg p\lor p)\lor q\lor r\equiv\mathbf T &&\text{negation law.} \end{aligned}

This rule is called resolution.

By truth table.

pp qq rr p∨qp\lor q ¬p∨r\neg p\lor r (p∨q)∧(¬p∨r)(p\lor q)\land(\neg p\lor r) q∨rq\lor r whole formula\text{whole formula}
T T T T T T T T
T T F T F F T T
T F T T T T T T
T F F T F F F T
F T T T T T T T
F T F T T T T T
F F T F T F T T
F F F F T F F T

Every row of the final column is true, so the formula is a tautology. ◼\blacksquare

Q78. Prove, using propositional equivalences, that each pair is equivalent: (a) p↔q≡(p∧q)∨(¬p∧¬q)p \leftrightarrow q \equiv (p\land q)\lor(\neg p\land\neg q); (b) ¬p→(q→r)≡q→(p∨r)\neg p \to (q \to r) \equiv q \to (p \lor r). (10 marks)

(a)

p↔q≡(p→q)∧(q→p)definition of ↔≡(¬p∨q)∧(¬q∨p)implication law≡[(¬p∨q)∧¬q]∨[(¬p∨q)∧p]distributive law≡(¬p∧¬q)∨(q∧¬q)∨(¬p∧p)∨(q∧p)distributive law≡(¬p∧¬q)∨𝐅∨𝐅∨(p∧q)negation law≡(p∧q)∨(¬p∧¬q)identity, commutative\begin{aligned} p\leftrightarrow q &\equiv (p\to q)\land(q\to p) &&\text{definition of }\leftrightarrow\\ &\equiv (\neg p\lor q)\land(\neg q\lor p) &&\text{implication law}\\ &\equiv \big[(\neg p\lor q)\land\neg q\big]\lor\big[(\neg p\lor q)\land p\big] &&\text{distributive law}\\ &\equiv (\neg p\land\neg q)\lor(q\land\neg q)\lor(\neg p\land p)\lor(q\land p) &&\text{distributive law}\\ &\equiv (\neg p\land\neg q)\lor\mathbf F\lor\mathbf F\lor(p\land q) &&\text{negation law}\\ &\equiv (p\land q)\lor(\neg p\land\neg q) &&\text{identity, commutative} \end{aligned}

(b)

¬p→(q→r)≡¬¬p∨(¬q∨r)implication law (twice)≡p∨¬q∨rdouble negation≡¬q∨(p∨r)commutative/associative≡q→(p∨r)implication law.◼\begin{aligned} \neg p\to(q\to r) &\equiv \neg\neg p\lor(\neg q\lor r) &&\text{implication law (twice)}\\ &\equiv p\lor\neg q\lor r &&\text{double negation}\\ &\equiv \neg q\lor(p\lor r) &&\text{commutative/associative}\\ &\equiv q\to(p\lor r) &&\text{implication law.}\ \blacksquare \end{aligned}

Q79. Prove that each of the following is a tautology by showing it is equivalent to 𝐓\mathbf T: (a) ((p→q)∧(q→r))→(p→r)((p\to q)\land(q\to r))\to(p\to r); (b) ((p∧q)∨(p∧r))→(q∨r)((p\land q)\lor(p\land r))\to(q\lor r); (c) (p∧q)∨(¬p∧q)∨¬q(p\land q)\lor(\neg p\land q)\lor\neg q. (10 marks)

(a) (see also hypothetical syllogism above)

((p→q)∧(q→r))→(p→r)≡¬((¬p∨q)∧(¬q∨r))∨¬p∨r≡(p∧¬q)∨(q∧¬r)∨¬p∨r≡(¬p∨¬q)∨(q∨r)≡𝐓.\begin{aligned} ((p\to q)\land(q\to r))\to(p\to r) &\equiv \neg\big((\neg p\lor q)\land(\neg q\lor r)\big)\lor\neg p\lor r\\ &\equiv (p\land\neg q)\lor(q\land\neg r)\lor\neg p\lor r\\ &\equiv (\neg p\lor\neg q)\lor(q\lor r)\equiv\mathbf T . \end{aligned}

(b)

((p∧q)∨(p∧r))→(q∨r)≡¬(p∧(q∨r))∨(q∨r)distributive law≡¬p∨¬(q∨r)∨(q∨r)De Morgan≡¬p∨𝐓≡𝐓negation, domination.\begin{aligned} ((p\land q)\lor(p\land r))\to(q\lor r) &\equiv \neg\big(p\land(q\lor r)\big)\lor(q\lor r) &&\text{distributive law}\\ &\equiv \neg p\lor\neg(q\lor r)\lor(q\lor r) &&\text{De Morgan}\\ &\equiv \neg p\lor\mathbf T\equiv\mathbf T &&\text{negation, domination.} \end{aligned}

(c)

(p∧q)∨(¬p∧q)∨¬q≡((p∨¬p)∧q)∨¬qdistributive law≡(𝐓∧q)∨¬q≡q∨¬q≡𝐓.◼\begin{aligned} (p\land q)\lor(\neg p\land q)\lor\neg q &\equiv \big((p\lor\neg p)\land q\big)\lor\neg q &&\text{distributive law}\\ &\equiv (\mathbf T\land q)\lor\neg q\equiv q\lor\neg q\equiv\mathbf T . \ \blacksquare \end{aligned}

Q80. Construct truth tables for (a) (p∧q)∨(p∧r)(p\land q)\lor(p\land r) and (b) (q∧p)↔(q⊕p)(q\land p)\leftrightarrow(q\oplus p). (10 marks)

(a)

pp qq rr p∧qp\land q p∧rp\land r (p∧q)∨(p∧r)(p\land q)\lor(p\land r)
T T T T T T
T T F T F T
T F T F T T
T F F F F F
F T T F F F
F T F F F F
F F T F F F
F F F F F F

(b) (⊕\oplus is exclusive-or: true exactly when the two inputs differ.)

pp qq q∧pq\land p q⊕pq\oplus p (q∧p)↔(q⊕p)(q\land p)\leftrightarrow(q\oplus p)
T T T F F
T F F T F
F T F T F
F F F F T

In (b) the formula is true only in the row p=q=𝐅p=q=\mathbf F (both sides are false there); in every other row exactly one side is true.

Q81. Are the following compound propositions tautologies? (a) ((p→q)∧(q→r))→(p→r)((p\to q)\land(q\to r))\to(p\to r); (b) ((p∧q)∧(q∧r))→(p∧r)((p\land q)\land(q\land r))\to(p\land r); (c) ((p⊕q)∧(q⊕r))→(p⊕r)((p\oplus q)\land(q\oplus r))\to(p\oplus r). In other words, are implication, conjunction and exclusive-or transitive? (10 marks)

(a) Yes — it is hypothetical syllogism (shown above), so →\to is transitive.

(b) If (p∧q)∧(q∧r)(p\land q)\land(q\land r) is true, then pp, qq and rr are all true, so p∧rp\land r is true. Hence it is a tautology and ∧\land is transitive.

(c) No. Take p=𝐓p=\mathbf T, q=𝐅q=\mathbf F, r=𝐓r=\mathbf T: then p⊕q=𝐓p\oplus q=\mathbf T and q⊕r=𝐓q\oplus r=\mathbf T, so the hypothesis is true, but p⊕r=𝐅p\oplus r=\mathbf F. The implication is false.

pp qq rr p⊕qp\oplus q q⊕rq\oplus r p⊕rp\oplus r (c) whole formula\text{(c) whole formula}
T T T F F F T
T T F F T T T
T F T T T F F
T F F T F T T
F T T T F T T
F T F T T F F
F F T F T T T
F F F F F F T

It fails when p,rp,r are both true and qq false, or p,rp,r both false and qq true. Therefore →\to and ∧\land are transitive, but ⊕\oplus is not. ◼\blacksquare

Q82. Let p,q,rp,q,r be primitive propositions. Determine whether (p∨q)→r(p\lor q)\to r and (p→r)∧(q→r)(p\to r)\land(q\to r) are logically equivalent. (10 marks)

Two formulas are logically equivalent when A↔BA\leftrightarrow B is a tautology (their columns are identical).

pp qq rr p∨qp\lor q (p∨q)→r(p\lor q)\to r p→rp\to r q→rq\to r (p→r)∧(q→r)(p\to r)\land(q\to r) equivalent?\text{equivalent?}
T T T T T T T T T
T T F T F F F F T
T F T T T T T T T
T F F T F F T F T
F T T T T T T T T
F T F T F T F F T
F F T F T T T T T
F F F F T T T T T

Columns 2 and 5 agree in all eight rows, so the two statements are logically equivalent. Algebraically:

(p∨q)→r≡¬(p∨q)∨r≡(¬p∧¬q)∨r≡(¬p∨r)∧(¬q∨r)≡(p→r)∧(q→r).◼(p\lor q)\to r\equiv\neg(p\lor q)\lor r\equiv(\neg p\land\neg q)\lor r\equiv(\neg p\lor r)\land(\neg q\lor r)\equiv(p\to r)\land(q\to r). \ \blacksquare

Q83. Let NN be the set of natural numbers. The symbols << (less than), ≤\le (less than or equal) and == (equal) are relations over NN. Prove or disprove: (1) << is reflexive, symmetric and transitive; (2) ≤\le is reflexive, symmetric and transitive; (3) == is reflexive, symmetric and transitive. (10 marks)

Definitions. A relation RR on a set AA is

  • reflexive if xRxx\,R\,x for all x∈Ax\in A;
  • symmetric if xRy⇒yRxx\,R\,y \Rightarrow y\,R\,x for all x,y∈Ax,y\in A;
  • transitive if xRyx\,R\,y and yRz⇒xRzy\,R\,z \Rightarrow x\,R\,z for all x,y,z∈Ax,y,z\in A.
relation reflexive symmetric transitive
<< No, because x<xx<x is never true. No: 1<21<2 but 2<12<1 is false. Yes: x<yx<y and y<z⇒x<zy<z\Rightarrow x<z.
≤\le Yes, because x≤xx\le x always holds. No: 1≤21\le 2 but 2≤12\le 1 is false. Yes: x≤yx\le y and y≤z⇒x≤zy\le z\Rightarrow x\le z.
== Yes, x=xx=x. Yes: x=y⇒y=xx=y\Rightarrow y=x. Yes: x=yx=y and y=z⇒x=zy=z\Rightarrow x=z.

Conclusion.

  1. "<< is reflexive, symmetric and transitive" is false (it is only transitive).
  2. "≤\le is reflexive, symmetric and transitive" is false (it is reflexive and transitive but not symmetric; it is a partial order).
  3. "== is reflexive, symmetric and transitive" is true — equality is an equivalence relation. ◼\blacksquare

Q84. Prove that every positive integer nn has a unique binary representation

n=cr⋅2r+cr−1⋅2r−1+⋯+c1⋅21+c0,n = c_r\cdot 2^{r} + c_{r-1}\cdot 2^{r-1} + \cdots + c_1\cdot 2^{1} + c_0,

where r≥0r \ge 0 is an integer, cr=1c_r = 1, and cj∈{0,1}c_j \in \{0,1\} for j=0,1,…,r−1j = 0,1,\ldots,r-1. (10 marks)

Existence (strong induction on nn).

Basis: n=1=1⋅20n = 1 = 1\cdot 2^{0} (so r=0r = 0, c0=1c_0 = 1).

Step: let n≥2n \ge 2 and assume every positive integer smaller than nn has such a representation. Divide by 22: n=2q+c0n = 2q + c_0 with c0∈{0,1}c_0 \in \{0,1\} and 1≤q<n1 \le q < n. By the hypothesis q=cr2r−1+⋯+c2⋅2+c1q = c_r 2^{r-1} + \cdots + c_2\cdot 2 + c_1 with cr=1c_r=1, cj∈{0,1}c_j\in\{0,1\}. Then

n=2q+c0=cr2r+⋯+c1⋅2+c0,n = 2q + c_0 = c_r 2^{r} + \cdots + c_1\cdot 2 + c_0 ,

which is the required form.

Uniqueness. Suppose nn has two representations. Reduce both modulo 22: every term except c0c_0 is even, so c0≡n(mod2)c_0 \equiv n \pmod 2 — the last digit is determined by nn. Subtract c0c_0 and divide by 22; both representations give representations of the same number (n−c0)/2<n(n-c_0)/2 < n, which are equal by the inductive hypothesis (uniqueness holds for smaller numbers). Therefore all digits agree and the representation is unique. ◼\blacksquare

Q85. Use mathematical induction to prove that the sum of the first mm positive integers is m(m+1)2\dfrac{m(m+1)}{2}. (10 marks)

Let P(m)P(m) denote 1+2+3+⋯+m=m(m+1)21+2+3+\cdots+m = \dfrac{m(m+1)}{2}.

Basis step. P(1)P(1): 1=1⋅(1+1)21 = \dfrac{1\cdot(1+1)}{2}, true.

Inductive step. Assume P(k)P(k) for some positive integer kk:

1+2+⋯+k=k(k+1)2.1+2+\cdots+k = \frac{k(k+1)}{2}.

Then

1+2+⋯+k+(k+1)=k(k+1)2+(k+1)=k(k+1)+2(k+1)2=(k+1)(k+2)2.1+2+\cdots+k+(k+1) = \frac{k(k+1)}{2}+(k+1) = \frac{k(k+1)+2(k+1)}{2} = \frac{(k+1)(k+2)}{2}.

This is P(k+1)P(k+1). By induction, P(m)P(m) is true for all m∈ℤ+m \in \mathbb Z^{+}. ◼\blacksquare

Q86. Show that in any base b≥2b \ge 2, the sum of any three single-digit numbers is at most two digits long. (10 marks)

Solution 1 (direct). The largest digit in base bb is b−1b-1, so the sum of three digits is at most 3(b−1)=3b−33(b-1) = 3b-3. The largest two-digit number in base bb is b2−1b^{2}-1. It is enough to show b2−1≥3b−3b^{2}-1 \ge 3b-3:

b2−1−3b+3=b2−3b+2=(b−1)(b−2)≥0for b≥2.b^{2}-1-3b+3 = b^{2}-3b+2 = (b-1)(b-2) \ge 0\qquad\text{for } b \ge 2 .

So the sum is never larger than b2−1b^{2}-1, i.e. it has at most two digits. ◼\blacksquare

Solution 2 (induction on the base bb). For b≥3b \ge 3 the largest sum is

(b−1)b+(b−1)b+(b−1)b=3b−3=2⋅b+(b−3)=(2(b−3))b,(b-1)_b + (b-1)_b + (b-1)_b = 3b-3 = 2\cdot b + (b-3) = \big(2\ (b-3)\big)_b ,

a two-digit number whose leading digit is 22 and whose last digit is b−3b-3.

Basis (b=2b = 2): 12+12+12=1121_2+1_2+1_2 = 11_2 (two digits). (b=3b=3): 23+23+23=(20)32_3+2_3+2_3 = (20)_3 (two digits).

Inductive step: assume the claim holds in base kk, i.e. 3(k−1)=2k+(k−3)3(k-1) = 2k+(k-3) is the two-digit number (2(k−3))k(2\ (k-3))_k. In base k+1k+1 the largest digit is kk, so the largest possible sum is

3k=2(k+1)+(k−2)=2(k+1)+((k+1)−3),3k = 2(k+1) + (k-2) = 2(k+1) + \big((k+1)-3\big),

which is the two-digit number (2((k+1)−3))k+1\big(2\ ((k+1)-3)\big)_{k+1} (its last digit k−2≥0k-2\ge 0 because k≥3k\ge 3). Hence the claim holds for base k+1k+1 whenever it holds for base kk, and so for all bases b≥3b\ge 3; base 22 was checked directly. ◼\blacksquare

Q87. Use mathematical induction to prove that the sum of the first mm odd positive integers is m2m^{2}, i.e. ∑i=1m(2i−1)=m2\displaystyle\sum_{i=1}^{m}(2i-1) = m^{2}. (10 marks)

Let P(m)P(m): 1+3+5+⋯+(2m−1)=m21+3+5+\cdots+(2m-1) = m^{2}.

Basis step. P(1)P(1): 2(1)−1=1=122(1)-1 = 1 = 1^{2}, true.

Inductive step. Assume P(k)P(k): 1+3+5+⋯+(2k−1)=k21+3+5+\cdots+(2k-1) = k^{2}. Then

1+3+⋯+(2k−1)+(2k+1)=k2+2k+1=(k+1)2,1+3+\cdots+(2k-1)+(2k+1) = k^{2}+2k+1 = (k+1)^{2},

which is P(k+1)P(k+1). By induction P(m)P(m) holds for all positive integers mm. ◼\blacksquare

Q88. Use mathematical induction to prove that the sum of the first mm even positive integers is m(m+1)m(m+1), i.e. ∑i=1m2i=m(m+1)\displaystyle\sum_{i=1}^{m}2i = m(m+1). (10 marks)

Let P(m)P(m): 2+4+6+⋯+2m=m(m+1)2+4+6+\cdots+2m = m(m+1).

Basis step. P(1)P(1): 2=1⋅22 = 1\cdot 2, true.

Inductive step. Assume P(k)P(k): 2+4+⋯+2k=k(k+1)2+4+\cdots+2k = k(k+1). Then

2+4+⋯+2k+(2k+2)=k(k+1)+2(k+1)=(k+1)(k+2)=(k+1)((k+1)+1).2+4+\cdots+2k+(2k+2) = k(k+1)+2(k+1) = (k+1)(k+2) = (k+1)\big((k+1)+1\big).

This is P(k+1)P(k+1), so P(m)P(m) is true for all positive integers mm. ◼\blacksquare

Q89. Deduce the result "sum of the first mm even integers is m(m+1)m(m+1)" from "sum of the first mm odd integers is m2m^{2}", and vice versa. (5 marks)

Even from odd. Adding 11 to each of the first mm odd numbers gives the first mm even numbers:

∑i=1m((2i−1)+1)=∑i=1m(2i−1)+m=m2+m⇒∑i=1m2i=m(m+1).\sum_{i=1}^{m}\big((2i-1)+1\big) = \sum_{i=1}^{m}(2i-1) + m = m^{2}+m \;\Longrightarrow\; \sum_{i=1}^{m}2i = m(m+1).

Odd from even. Subtracting 11 from each of the first mm even numbers gives the first mm odd numbers:

∑i=1m(2i−1)=∑i=1m2i−m=m(m+1)−m=m2.◼\sum_{i=1}^{m}(2i-1) = \sum_{i=1}^{m}2i - m = m(m+1)-m = m^{2}. \ \blacksquare

Q90. Show by mathematical induction that any amount of n≥8n \ge 8 cents can be obtained using only 3-cent and 5-cent coins. (10 marks)

Let P(n)P(n): "nn cents can be formed using only 3-cent and 5-cent coins".

Basis (three consecutive values).

8=3+5,9=3⋅3,10=5⋅2.8 = 3+5,\qquad 9 = 3\cdot 3,\qquad 10 = 5\cdot 2 .

Inductive step (strong induction). Let k≥10k \ge 10 and assume P(8),…,P(k)P(8),\ldots,P(k). Since k+1≥11k+1 \ge 11, we have k+1−3=k−2≥8k+1-3 = k-2 \ge 8, and k−2≤kk-2 \le k, so P(k−2)P(k-2) holds: k−2k-2 cents can be formed. Add one more 3-cent coin: k+1=(k−2)+3k+1 = (k-2)+3 is obtainable.

By strong induction P(n)P(n) holds for every n≥8n \ge 8. ◼\blacksquare

Check: 77 cents is the largest amount that cannot be formed (Frobenius number 3⋅5−3−5=73\cdot5-3-5=7).

Q91. Prove that postage of n≥12n \ge 12 cents can be formed using only 4-cent and 5-cent stamps. (10 marks)

Let P(n)P(n): "nn cents can be formed with 4-cent and 5-cent stamps".

Basis.

12=4⋅3,13=4⋅2+5,14=4+5⋅2,15=5⋅3.12 = 4\cdot 3,\qquad 13 = 4\cdot 2+5,\qquad 14 = 4+5\cdot 2,\qquad 15 = 5\cdot 3 .

Inductive step (strong induction). Let k≥15k \ge 15 and assume P(12),…,P(k)P(12),\ldots,P(k). Then k+1−4=k−3≥12k+1-4 = k-3 \ge 12 and k−3≤kk-3\le k, so k−3k-3 cents can be formed. Add one 4-cent stamp to get k+1k+1 cents.

Hence P(n)P(n) is true for all n≥12n \ge 12. ◼\blacksquare

Check: 1111 cents cannot be formed (4⋅5−4−5=114\cdot5-4-5=11 is the largest impossible amount).

Q92. Show by mathematical induction that any amount of n≥18n \ge 18 cents can be obtained using only 4-cent and 7-cent coins. (10 marks)

Let P(n)P(n): "n=4a+7bn = 4a + 7b for some integers a,b≥0a,b \ge 0".

Basis (four consecutive values).

18=4⋅1+7⋅2,19=4⋅3+7⋅1,20=4⋅5,21=7⋅3.18 = 4\cdot 1+7\cdot 2,\quad 19 = 4\cdot 3+7\cdot 1,\quad 20 = 4\cdot 5,\quad 21 = 7\cdot 3 .

(Also 22=4⋅2+7⋅222 = 4\cdot 2+7\cdot 2, 23=4⋅4+723 = 4\cdot 4+7, 24=4⋅624=4\cdot 6.)

Inductive step (strong induction). Let k≥21k \ge 21 and assume P(18),…,P(k)P(18),\ldots,P(k). Then k+1−4=k−3≥18k+1-4 = k-3 \ge 18 and k−3≤kk-3 \le k, so k−3=4a+7bk-3 = 4a+7b for some a,b≥0a,b\ge0. Adding one 4-cent coin:

k+1=4(a+1)+7b.k+1 = 4(a+1)+7b .

So P(k+1)P(k+1) is true, and by strong induction every n≥18n\ge 18 can be formed. ◼\blacksquare

Check: 1717 is the largest impossible amount (4⋅7−4−7=174\cdot7-4-7=17).

Q93. Show by mathematical induction that any amount of n≥20n \ge 20 cents can be obtained using only 5-cent and 6-cent coins. (10 marks)

Let P(n)P(n): "n=5s+6tn = 5s + 6t for some integers s,t≥0s,t \ge 0".

Basis.

20=5⋅4,21=5⋅3+6,22=5⋅2+6⋅2,23=5+6⋅3,24=6⋅4.20 = 5\cdot 4,\quad 21 = 5\cdot 3+6,\quad 22 = 5\cdot 2+6\cdot 2,\quad 23 = 5+6\cdot 3,\quad 24 = 6\cdot 4 .

Inductive step (strong induction). Let k≥24k \ge 24 and assume P(20),…,P(k)P(20),\ldots,P(k). Then k+1−5=k−4≥20k+1-5 = k-4 \ge 20 and k−4≤kk-4\le k, so k−4=5s+6tk-4 = 5s+6t for some s,t≥0s,t\ge0. Hence

k+1=5(s+1)+6t,k+1 = 5(s+1)+6t,

and P(k+1)P(k+1) is true. By strong induction P(n)P(n) holds for all n≥20n \ge 20. ◼\blacksquare

Check: the largest impossible amount is 5⋅6−5−6=195\cdot 6-5-6 = 19, so 2020 is the right starting point.

Q94. Using 3-cent and 7-cent coins, make a general formula: from which amount onward can every amount be obtained? Prove it by induction. (10 marks)

Reading of the question: the source only says "3 cents and 7 cents coins, make a general formula for it".

For two coins aa and bb with gcd(a,b)=1\gcd(a,b)=1, every amount n≥(a−1)(b−1)n \ge (a-1)(b-1) can be formed. For a=3,b=7a=3,\ b=7 this gives (3−1)(7−1)=12(3-1)(7-1) = 12. (The largest impossible amount is ab−a−b=11ab-a-b = 11.)

Claim. Every n≥12n \ge 12 can be written as n=3a+7bn = 3a+7b with a,b≥0a,b\ge0.

Basis. 12=3⋅412 = 3\cdot 4, 13=3⋅2+713 = 3\cdot 2+7, 14=7⋅214 = 7\cdot 2.

Inductive step (strong induction). Let k≥14k \ge 14 and assume the claim for all amounts from 1212 to kk. Then k+1−3=k−2≥12k+1-3 = k-2 \ge 12 and k−2≤kk-2\le k, so k−2=3a+7bk-2 = 3a+7b; therefore k+1=3(a+1)+7bk+1 = 3(a+1)+7b. ◼\blacksquare

Q95. Use mathematical induction to show that when nn is an exact power of 22, the solution of the recurrence

T(n)={2if n=2,2T(n/2)+nif n=2k,k>1,T(n) = \begin{cases} 2 & \text{if } n = 2,\\ 2\,T(n/2) + n & \text{if } n = 2^{k},\ k>1,\end{cases}

is T(n)=nlgnT(n) = n\lg n. (10 marks)

Basis (n=2n = 2). T(2)=2T(2) = 2 and 2lg2=2⋅1=22\lg 2 = 2\cdot 1 = 2. ✓

Inductive step. Let n=2kn = 2^{k}, k>1k>1, and assume the formula for n/2n/2: T(n/2)=n2lgn2T(n/2) = \dfrac n2\,\lg\dfrac n2. Then

T(n)=2T(n/2)+n=2⋅n2lgn2+n=n(lgn−1)+n=nlgn−n+n=nlgn.\begin{aligned} T(n) &= 2\,T(n/2) + n\\ &= 2\cdot\frac n2\lg\frac n2 + n\\ &= n(\lg n - 1) + n\\ &= n\lg n - n + n = n\lg n . \end{aligned}

This completes the inductive proof for exact powers of 22. ◼\blacksquare

Q96. Use mathematical induction to prove that ∑i=0ni2=n(n+1)(2n+1)6\displaystyle\sum_{i=0}^{n} i^{2} = \dfrac{n(n+1)(2n+1)}{6}. (10 marks)

Let P(n)P(n): 02+12+⋯+n2=n(n+1)(2n+1)60^{2}+1^{2}+\cdots+n^{2} = \dfrac{n(n+1)(2n+1)}{6}.

Basis (n=0n=0 or n=1n=1). For n=1n=1: left side =1=1; right side =1⋅2⋅36=1=\dfrac{1\cdot2\cdot3}{6}=1. ✓

Inductive step. Assume P(k)P(k): ∑i=0ki2=k(k+1)(2k+1)6\displaystyle\sum_{i=0}^{k} i^{2}=\frac{k(k+1)(2k+1)}{6}. Then

∑i=0k+1i2=k(k+1)(2k+1)6+(k+1)2=(k+1)[k(2k+1)+6(k+1)]6=(k+1)(2k2+7k+6)6=(k+1)(k+2)(2k+3)6=(k+1)((k+1)+1)(2(k+1)+1)6,\begin{aligned} \sum_{i=0}^{k+1} i^{2} &= \frac{k(k+1)(2k+1)}{6} + (k+1)^{2} = \frac{(k+1)\big[k(2k+1)+6(k+1)\big]}{6}\\ &= \frac{(k+1)(2k^{2}+7k+6)}{6} = \frac{(k+1)(k+2)(2k+3)}{6}\\ &= \frac{(k+1)\big((k+1)+1\big)\big(2(k+1)+1\big)}{6}, \end{aligned}

which is P(k+1)P(k+1). By induction the formula holds for all n≥0n\ge 0. ◼\blacksquare

Q97. Find a sequence satisfying ak=ak−1+2ak−2a_k = a_{k-1}+2a_{k-2} for all k≥2k\ge2 together with (i) a0=1,a1=8a_0 = 1,\ a_1 = 8; and (ii) a0=2,a1=7a_0 = 2,\ a_1 = 7. (10 marks)

The characteristic equation is t2−t−2=0t^{2}-t-2=0, i.e. (t−2)(t+1)=0(t-2)(t+1)=0, with roots t=2t=2 and t=−1t=-1. The general solution is

an=C⋅2n+D⋅(−1)n.a_n = C\cdot 2^{n} + D\cdot(-1)^{n}.

(i) a0=1,a1=8a_0=1,\ a_1=8.

{C+D=12C−D=8⇒3C=9,C=3,D=−2⇒an=3⋅2n−2(−1)n\begin{cases} C + D = 1\\ 2C - D = 8\end{cases}\;\Longrightarrow\; 3C = 9,\ C=3,\ D=-2 \quad\Longrightarrow\quad \boxed{a_n = 3\cdot 2^{n} - 2\,(-1)^{n}}

Check: a0=3−2=1a_0 = 3-2 = 1, a1=6+2=8a_1 = 6+2 = 8, a2=12−2=10=a1+2a0=8+2a_2 = 12-2 = 10 = a_1+2a_0 = 8+2. ✓

(ii) a0=2,a1=7a_0=2,\ a_1=7.

{C+D=22C−D=7⇒3C=9,C=3,D=−1⇒an=3⋅2n−(−1)n\begin{cases} C + D = 2\\ 2C - D = 7\end{cases}\;\Longrightarrow\; 3C = 9,\ C=3,\ D=-1 \quad\Longrightarrow\quad \boxed{a_n = 3\cdot 2^{n} - (-1)^{n}}

Check: a0=3−1=2a_0 = 3-1 = 2, a1=6+1=7a_1 = 6+1 = 7, a2=12−1=11=7+2⋅2a_2 = 12-1 = 11 = 7+2\cdot 2. ✓

Q98. Suppose b0,b1,b2,…b_0,b_1,b_2,\ldots satisfies bk=4bk−1−4bk−2b_k = 4b_{k-1} - 4b_{k-2} for all k≥2k\ge 2 with b0=1b_0=1, b1=3b_1=3. Find an explicit formula using the characteristic equation. (10 marks)

Characteristic equation. t2−4t+4=0⇒(t−2)2=0t^{2}-4t+4 = 0 \Rightarrow (t-2)^{2}=0, so t=2t = 2 is a repeated root.

Both 2n2^{n} and n⋅2nn\cdot 2^{n} satisfy the recurrence, so the general solution is

bn=C⋅2n+D⋅n⋅2n.b_n = C\cdot 2^{n} + D\cdot n\cdot 2^{n}.

Initial conditions.

n=0:C=1,n=1:2C+2D=3⇒2+2D=3⇒D=12.n=0:\ C = 1,\qquad n=1:\ 2C + 2D = 3 \Rightarrow 2 + 2D = 3 \Rightarrow D = \tfrac12 .

bn=2n+12n2n=(1+n2)2n\boxed{b_n = 2^{n} + \tfrac12\,n\,2^{n} = \left(1+\frac n2\right)2^{n}}

Check: b0=1b_0 = 1, b1=32⋅2=3b_1 = \tfrac32\cdot 2 = 3, b2=2⋅4=8=4(3)−4(1)b_2 = 2\cdot 4 = 8 = 4(3)-4(1) ✓, b3=52⋅8=20=4(8)−4(3)b_3 = \tfrac52\cdot 8 = 20 = 4(8)-4(3) ✓. ◼\blacksquare

Q99. Suppose bk=6bk−1−9bk−2b_k = 6b_{k-1} - 9b_{k-2} for all k≥2k \ge 2 with b0=2b_0 = 2 and b1=6b_1 = 6. Find an explicit formula using the characteristic equation. (10 marks)

Characteristic equation. t2−6t+9=0⇒(t−3)2=0t^{2}-6t+9=0 \Rightarrow (t-3)^{2}=0, so t=3t=3 is a repeated root. (Some shared solutions write "t=2t=2"; it is a typo — the root is 33.)

General solution: bn=C⋅3n+D⋅n⋅3nb_n = C\cdot 3^{n} + D\cdot n\cdot 3^{n}.

n=0:C=2;n=1:3C+3D=6⇒6+3D=6⇒D=0.n=0:\ C = 2;\qquad n=1:\ 3C + 3D = 6 \Rightarrow 6+3D = 6 \Rightarrow D = 0 .

bn=2⋅3n\boxed{b_n = 2\cdot 3^{n}}

Terms: b0=2b_0 = 2, b1=6b_1 = 6, b2=18b_2 = 18, b3=54b_3 = 54. Check: b2=6(6)−9(2)=18b_2 = 6(6)-9(2) = 18 ✓. ◼\blacksquare

Q100.

  1. Suppose bk=5bk−1−6bk−2b_k = 5b_{k-1} - 6b_{k-2} for all k≥2k \ge 2 with b0=2b_0 = 2, b1=5b_1 = 5. Find an explicit formula using the characteristic equation.
  2. Find the general solution of tn+3tn−1=2n+3⋅5nt_n + 3t_{n-1} = 2n + 3\cdot 5^{n}.
(15 marks)

(a) The characteristic equation is t2−5t+6=0⇒(t−2)(t−3)=0t^{2}-5t+6=0 \Rightarrow (t-2)(t-3)=0, so t=2,3t=2,3 and bn=C2n+D3nb_n = C\,2^{n}+D\,3^{n}.

{C+D=22C+3D=5⇒D=1,C=1⇒bn=2n+3n\begin{cases}C + D = 2\\ 2C+3D = 5\end{cases}\Longrightarrow D = 1,\ C = 1 \quad\Longrightarrow\quad \boxed{b_n = 2^{n}+3^{n}}

Check: b2=4+9=13=5(5)−6(2)b_2 = 4+9 = 13 = 5(5)-6(2) ✓.

(b) Reading: the right side is 2n+3⋅5n2n + 3\cdot 5^{n} (the "3.5n3.5^{n}" in the source means 3⋅5n3\cdot5^{n}).

The method (handout, slide 33 of lecture 08): the left side tn+3tn−1t_n + 3t_{n-1} gives the factor (x+3)(x+3). A right-hand term bnp(n)b^{n}p(n) with pp of degree dd contributes the factor (x−b)d+1(x-b)^{d+1}.

  • 2n=1n⋅(2n)2n = 1^{n}\cdot(2n): b=1b=1, degree d=1d=1, factor (x−1)2(x-1)^{2};
  • 3⋅5n3\cdot 5^{n}: b=5b=5, degree 00, factor (x−5)(x-5).

Characteristic polynomial: (x+3)(x−1)2(x−5)(x+3)(x-1)^{2}(x-5), so

tn=c1(−3)n+c2⋅1n+c3n⋅1n+c45n.t_n = c_1(-3)^{n} + c_2\cdot 1^{n} + c_3\,n\cdot 1^{n} + c_4\,5^{n} .

Correction note: the handout answer lists only (x+3)(x−1)(x−5)(x+3)(x-1)(x-5), i.e. tn=c1(−3)n+c2+c35nt_n=c_1(-3)^n+c_2+c_3 5^n. That would be right if the right side were the constant 22 (instead of 2n2n); for 2n2n the factor (x−1)(x-1) must be squared.

Determining the constants. Substituting a particular solution tn=αn+β+γ5nt_n = \alpha n + \beta + \gamma 5^{n} gives 4α=24\alpha = 2, 4β−3α=04\beta - 3\alpha = 0, 8γ=158\gamma = 15, so α=12\alpha = \tfrac12, β=38\beta = \tfrac38, γ=158\gamma = \tfrac{15}{8} and the full solution of the recurrence is

tn=c(−3)n+1585n+n2+38,t_n = c\,(-3)^{n} + \frac{15}{8}\,5^{n} + \frac n2 + \frac38 ,

with one free constant cc fixed by an initial value. ◼\blacksquare

Q101. Consider the recurrence tn=nt_n = n for n=0,1,2n = 0,1,2 and tn=6tn−1−11tn−2+6tn−3t_n = 6t_{n-1} - 11t_{n-2} + 6t_{n-3} otherwise. Find the general solution. (10 marks)

Rewrite: tn−6tn−1+11tn−2−6tn−3=0t_n - 6t_{n-1} + 11t_{n-2} - 6t_{n-3} = 0. The characteristic equation is

x3−6x2+11x−6=0.x^{3}-6x^{2}+11x-6 = 0 .

Test x=1x = 1: 1−6+11−6=01-6+11-6 = 0, so x=1x=1 is a root and x3−6x2+11x−6=(x−1)(x2−5x+6)=(x−1)(x−2)(x−3)x^{3}-6x^{2}+11x-6 = (x-1)(x^{2}-5x+6) = (x-1)(x-2)(x-3). The roots are r1=1,r2=2,r3=3r_1=1,\ r_2=2,\ r_3=3, so

tn=c11n+c22n+c33n.t_n = c_1\,1^{n} + c_2\,2^{n} + c_3\,3^{n}.

Initial conditions (t0=0,t1=1,t2=2t_0=0,\ t_1=1,\ t_2=2):

{c1+c2+c3=0c1+2c2+3c3=1c1+4c2+9c3=2⇒c1=−32,c2=2,c3=−12.\begin{cases} c_1+c_2+c_3 = 0\\ c_1+2c_2+3c_3 = 1\\ c_1+4c_2+9c_3 = 2\end{cases}\Longrightarrow c_1=-\tfrac32,\ c_2 = 2,\ c_3=-\tfrac12 .

tn=−32+2⋅2n−12⋅3n\boxed{t_n = -\tfrac32 + 2\cdot 2^{n} - \tfrac12\cdot 3^{n}}

Check: t3=6(2)−11(1)+6(0)=1t_3 = 6(2)-11(1)+6(0) = 1 and the formula gives −32+16−272=1-\tfrac32+16-\tfrac{27}2 = 1 ✓. ◼\blacksquare

Q102. Solve the recurrence tn=2tn−1+n+2nt_n = 2t_{n-1} + n + 2^{n} (with t0=0t_0 = 0). (10 marks)

Rewrite as tn−2tn−1=n+2nt_n - 2t_{n-1} = n + 2^{n} and compare with ∑aitn−i=b1np1(n)+b2np2(n)\sum a_i t_{n-i} = b_1^{n}p_1(n) + b_2^{n}p_2(n):

  • b1=1,p1(n)=nb_1 = 1,\ p_1(n) = n (degree d1=1d_1 = 1);
  • b2=2,p2(n)=1b_2 = 2,\ p_2(n) = 1 (degree d2=0d_2 = 0).

The characteristic polynomial is (x−2)(x−1)2(x−2)=(x−2)2(x−1)2(x-2)\,(x-1)^{2}\,(x-2) = (x-2)^{2}(x-1)^{2}, with roots x=1x = 1 and x=2x = 2, each of multiplicity 22. Hence

tn=c11n+c2n1n+c32n+c4n2n.t_n = c_1\,1^{n} + c_2\,n\,1^{n} + c_3\,2^{n} + c_4\,n\,2^{n}.

Substituting into the recurrence gives c2=−1c_2 = -1, c1=−2c_1 = -2, c4=1c_4 = 1 (matching the right side n+2nn+2^n); t0=0t_0 = 0 then gives c3=2c_3 = 2. Therefore

tn=n2n+2n+1−n−2\boxed{t_n = n\,2^{n} + 2^{n+1} - n - 2}

Check: t1=2+4−1−2=3=2(0)+1+2t_1 = 2+4-1-2 = 3 = 2(0)+1+2 ✓; t2=8+8−2−2=12=2(3)+2+4t_2 = 8+8-2-2 = 12 = 2(3)+2+4 ✓. ◼\blacksquare

Q103. Find the solution of tn−2tn−1=3nt_n - 2t_{n-1} = 3^{n}. (10 marks)

Here b=3b=3 and p(n)=1p(n)=1 (degree 00). Reduce to a homogeneous recurrence: replace nn by n−1n-1 and multiply by 33:

3tn−1−6tn−2=3n.3t_{n-1} - 6t_{n-2} = 3^{n}.

Subtract this from tn−2tn−1=3nt_n - 2t_{n-1} = 3^{n}:

tn−5tn−1+6tn−2=0.t_n - 5t_{n-1} + 6t_{n-2} = 0 .

The characteristic equation x2−5x+6=0⇒(x−2)(x−3)=0x^{2}-5x+6 = 0 \Rightarrow (x-2)(x-3)=0 has roots 22 and 33, so

tn=c12n+c23n.t_n = c_1\,2^{n} + c_2\,3^{n}.

Remark. Not every choice of c1,c2c_1,c_2 solves the original recurrence: substituting back shows that c2c_2 must equal 33, so the solutions of tn−2tn−1=3nt_n-2t_{n-1}=3^n are tn=c12n+3n+1t_n = c_1 2^{n} + 3^{n+1}, with c1c_1 fixed by an initial value. ◼\blacksquare

Q104. Find the general solution of tn−4tn−1=(n+5)3nt_n - 4t_{n-1} = (n+5)\,3^{n}, n≥1n \ge 1. (10 marks)

Eliminating the right-hand side.

tn−4tn−1=(n+5)3n=9(n+5)3n−2(1)tn−1−4tn−2=(n+4)3n−1=3(n+4)3n−2(2)tn−2−4tn−3=(n+3)3n−2(3)\begin{aligned} t_n - 4t_{n-1} &= (n+5)3^{n} = 9(n+5)\,3^{n-2} &&(1)\\ t_{n-1} - 4t_{n-2} &= (n+4)3^{n-1} = 3(n+4)\,3^{n-2} &&(2)\\ t_{n-2} - 4t_{n-3} &= (n+3)3^{n-2} &&(3) \end{aligned}

The term (n+5)3n(n+5)3^{n} has b=3b=3 and degree d=1d=1, so the factor (x−3)2(x-3)^{2} is needed. Eliminate the right side with (1)−6⋅(2)+9⋅(3)(1) - 6\cdot(2) + 9\cdot(3) (the coefficients 1,−6,91,-6,9 come from (x−3)2=x2−6x+9(x-3)^{2}=x^{2}-6x+9):

[tn−4tn−1]−6[tn−1−4tn−2]+9[tn−2−4tn−3]=[9(n+5)−18(n+4)+9(n+3)]3n−2=0,\big[t_n - 4t_{n-1}\big] - 6\big[t_{n-1}-4t_{n-2}\big] + 9\big[t_{n-2}-4t_{n-3}\big] = \big[9(n+5)-18(n+4)+9(n+3)\big]3^{n-2} = 0 ,

since 9n+45−18n−72+9n+27=09n+45-18n-72+9n+27 = 0. This gives the homogeneous recurrence

tn−10tn−1+33tn−2−36tn−3=0.t_n - 10t_{n-1} + 33t_{n-2} - 36t_{n-3} = 0 .

Characteristic equation.

x3−10x2+33x−36=(x−4)(x−3)2=0⇒x=4,3,3.x^{3}-10x^{2}+33x-36 = (x-4)(x-3)^{2} = 0 \Rightarrow x = 4,\,3,\,3 .

tn=c14n+c23n+c3n3n\boxed{t_n = c_1\,4^{n} + c_2\,3^{n} + c_3\,n\,3^{n}}

Correction note: the shared solution eliminates with the wrong multipliers and obtains the roots 3,−4,43,-4,4 and the form c13n−c24n+c3n4nc_1 3^n - c_2 4^n + c_3 n4^n; that is incorrect. The roots must be 44 (from the left side) and 33 twice (from (n+5)3n(n+5)3^n). Substituting a particular solution (αn+β)3n(\alpha n+\beta)3^{n} gives α=−3,β=−27\alpha=-3,\ \beta=-27, so the actual solutions of the recurrence are tn=c4n−(3n+27)3nt_n = c\,4^{n} - (3n+27)\,3^{n}. ◼\blacksquare

Q105. Consider the recurrence tn−4tn−1=(n+3)5nt_n - 4t_{n-1} = (n+3)\,5^{n}. Find its general solution. (10 marks)

Write the equation for nn, n−1n-1, n−2n-2 (all rewritten with 5n−25^{n-2}):

tn−4tn−1=25(n+3)5n−2(4)tn−1−4tn−2=5(n+2)5n−2(5)tn−2−4tn−3=(n+1)5n−2(6)\begin{aligned} t_n - 4t_{n-1} &= 25(n+3)\,5^{n-2} &&(4)\\ t_{n-1} - 4t_{n-2} &= 5(n+2)\,5^{n-2} &&(5)\\ t_{n-2} - 4t_{n-3} &= (n+1)\,5^{n-2} &&(6) \end{aligned}

Multiply (5) by −10-10 and (6) by 2525 (the coefficients of (x−5)2=x2−10x+25(x-5)^{2} = x^{2}-10x+25) and add to (4):

tn−14tn−1+65tn−2−100tn−3=25(n+3)−50(n+2)+25(n+1)=0.t_n - 14t_{n-1} + 65t_{n-2} - 100t_{n-3} = 25(n+3)-50(n+2)+25(n+1) = 0 .

Characteristic equation.

x3−14x2+65x−100=(x−4)(x2−10x+25)=(x−4)(x−5)2=0,x^{3}-14x^{2}+65x-100 = (x-4)(x^{2}-10x+25) = (x-4)(x-5)^{2} = 0,

roots x=4,5,5x = 4,\,5,\,5. Hence

tn=c14n+c25n+c3n5n\boxed{t_n = c_1\,4^{n} + c_2\,5^{n} + c_3\,n\,5^{n}}

(Substituting a particular solution shows the actual solutions are tn=c4n+5(n−1)5nt_n = c\,4^{n} + 5(n-1)5^{n}.) ◼\blacksquare

Q106. Find the solution of the recurrence xn=2xn−1−5xn−2x_n = 2x_{n-1} - 5x_{n-2} with x0=1x_0 = 1 and x1=5x_1 = 5. (10 marks)

Characteristic equation. r2−2r+5=0r^{2}-2r+5=0, so

r=2±4−202=1±2i.r = \frac{2\pm\sqrt{4-20}}{2} = 1\pm 2i .

Two distinct complex roots r1=1+2ir_1 = 1+2i, r2=1−2ir_2 = 1-2i, so xn=c1(1+2i)n+c2(1−2i)nx_n = c_1(1+2i)^{n} + c_2(1-2i)^{n}.

Initial conditions.

c1+c2=1,c1(1+2i)+c2(1−2i)=5.c_1+c_2 = 1,\qquad c_1(1+2i)+c_2(1-2i) = 5 .

Solving: c1=1−2i2c_1 = \dfrac{1-2i}{2} and c2=1+2i2c_2 = \dfrac{1+2i}{2}. Hence

xn=1−2i2(1+2i)n+1+2i2(1−2i)n.x_n = \frac{1-2i}{2}(1+2i)^{n} + \frac{1+2i}{2}(1-2i)^{n}.

Because (1−2i)(1+2i)=5(1-2i)(1+2i) = 5, this simplifies to

xn=52[(1+2i)n−1+(1−2i)n−1],n≥0\boxed{x_n = \frac52\Big[(1+2i)^{\,n-1} + (1-2i)^{\,n-1}\Big],\qquad n\ge 0}

Remark. The sequence is obviously real, although the formula involves complex numbers. In real form, with θ=arctan2\theta=\arctan 2:

xn=5(n+1)/2cos((n−1)θ).x_n = 5^{(n+1)/2}\cos\big((n-1)\theta\big).

Check: x0=1,x1=5,x2=2(5)−5(1)=5,x3=2(5)−5(5)=−15,x4=−55x_0 = 1,\ x_1 = 5,\ x_2 = 2(5)-5(1) = 5,\ x_3 = 2(5)-5(5) = -15,\ x_4 = -55, and the formula agrees.

Correction note: the shared solution prints the exponent as n+1n+1 in the last line; with c1,c2c_1,c_2 as above the correct exponent is n−1n-1 (at n=0n=0 the printed form would give 55 instead of x0=1x_0=1). ◼\blacksquare

Q107. The sequence ⟨un⟩\langle u_n\rangle is defined by un+1=3un+15un+3u_{n+1} = \dfrac{3u_n+1}{5u_n+3} with u1=1u_1 = 1. Show that unu_n can be expressed in terms of Fibonacci and Lucas numbers. (15 marks)

Recall the Fibonacci numbers F0=0,F1=1,Fm+1=Fm+Fm−1F_0=0,\ F_1=1,\ F_{m+1}=F_m+F_{m-1} and the Lucas numbers L0=2,L1=1,Lm+1=Lm+Lm−1L_0=2,\ L_1=1,\ L_{m+1}=L_m+L_{m-1}, with Lm=Fm−1+Fm+1L_m = F_{m-1}+F_{m+1}.

Claim.

un=F2n−1L2n−1(n≥1).u_n = \frac{F_{2n-1}}{L_{2n-1}}\qquad (n \ge 1).

Lemma. For all n≥1n\ge 1:

3F2n−1+L2n−1=2F2n+1(1),5F2n−1+3L2n−1=2L2n+1(2).3F_{2n-1} + L_{2n-1} = 2F_{2n+1}\quad(1),\qquad 5F_{2n-1} + 3L_{2n-1} = 2L_{2n+1}\quad(2).

Proof of the lemma. First, two identities that follow from Lm=Fm−1+Fm+1L_m=F_{m-1}+F_{m+1} (or from the Binet formulas):

Fm+Lm=2Fm+1(3),5Fm+Lm=2Lm+1(4).F_m + L_m = 2F_{m+1}\quad(3),\qquad 5F_m + L_m = 2L_{m+1}\quad(4).

(For (3): Fm+Fm−1+Fm+1=Fm+1+Fm+1F_m + F_{m-1}+F_{m+1} = F_{m+1}+F_{m+1}. For (4): 5Fm+Lm=4Fm+2Fm+15F_m+L_m = 4F_m + 2F_{m+1} and 2Lm+1=2Fm+2Fm+2=2Fm+2(Fm+1+Fm)2L_{m+1} = 2F_m+2F_{m+2} = 2F_m+2(F_{m+1}+F_m), which are equal.)

Then

3F2n−1+L2n−1=2F2n−1+(F2n−1+L2n−1)=2F2n−1+2F2n=2F2n+1by (3),3F_{2n-1}+L_{2n-1} = 2F_{2n-1} + (F_{2n-1}+L_{2n-1}) = 2F_{2n-1}+2F_{2n} = 2F_{2n+1}\quad\text{by (3)},

5F2n−1+3L2n−1=(5F2n−1+L2n−1)+2L2n−1=2L2n+2L2n−1=2L2n+1by (4).▫5F_{2n-1}+3L_{2n-1} = (5F_{2n-1}+L_{2n-1}) + 2L_{2n-1} = 2L_{2n}+2L_{2n-1} = 2L_{2n+1}\quad\text{by (4)}.\ \square

Proof of the claim (induction). Basis: u1=1=F1/L1u_1 = 1 = F_1/L_1. ✓

Step: assume un=F2n−1/L2n−1u_n = F_{2n-1}/L_{2n-1}. Then

un+1=3un+15un+3=3F2n−1+L2n−15F2n−1+3L2n−1=2F2n+12L2n+1=F2(n+1)−1L2(n+1)−1,u_{n+1} = \frac{3u_n+1}{5u_n+3} = \frac{3F_{2n-1}+L_{2n-1}}{5F_{2n-1}+3L_{2n-1}} = \frac{2F_{2n+1}}{2L_{2n+1}} = \frac{F_{2(n+1)-1}}{L_{2(n+1)-1}} ,

using the lemma. ◼\blacksquare

Check: u1=1=11u_1=1=\tfrac11, u2=12=F3L3=24u_2=\tfrac12=\tfrac{F_3}{L_3}=\tfrac24, u3=511=F5L5u_3=\tfrac5{11}=\tfrac{F_5}{L_5}, u4=1329=F7L7u_4=\tfrac{13}{29}=\tfrac{F_7}{L_7}. ✓

Q108. Solve the recurrence T(n)=T(n−1)+cT(n) = T(n-1) + c using the substitution method (with T(1)=dT(1)=d). (10 marks)

Repeated substitution: instantiate the recurrence with smaller and smaller arguments and look for a pattern.

T(n)=T(n−1)+c=(T(n−2)+c)+c=T(n−2)+2c=(T(n−3)+c)+2c=T(n−3)+3c⋮=T(n−k)+kc.\begin{aligned} T(n) &= T(n-1) + c\\ &= \big(T(n-2)+c\big)+c = T(n-2)+2c\\ &= \big(T(n-3)+c\big)+2c = T(n-3)+3c\\ &\;\;\vdots\\ &= T(n-k) + k\,c . \end{aligned}

The pattern: after kk substitutions the argument is n−kn-k and the added cost is kck\,c. The process stops at T(1)T(1), i.e. when n−k=1n-k = 1, k=n−1k = n-1:

T(n)=T(1)+(n−1)c=d+(n−1)c=cn+(d−c).T(n) = T(1) + (n-1)c = d + (n-1)c = c\,n + (d - c).

So T(n)=cn+(d−c)\boxed{T(n) = c\,n + (d-c)}, which is Θ(n)\Theta(n). (Note: the handout line "T(n−c)+3cT(n-c)+3c" is a typo for T(n−3)+3cT(n-3)+3c.) ◼\blacksquare

Q109. Solve the recurrence T(n)={1n=13T(n/4)+notherwiseT(n) = \begin{cases}1 & n=1\\ 3T(n/4)+n & \text{otherwise}\end{cases} using the substitution method. (10 marks)

Assume n=4kn = 4^{k}. Substitute repeatedly:

T(n)=3T(n/4)+n=3[3T(n/42)+n/4]+n=32T(n/42)+34n+n=33T(n/43)+(34)2n+34n+n⋮=3kT(n/4k)+n[1+34+(34)2+⋯+(34)k−1].\begin{aligned} T(n) &= 3T(n/4)+n\\ &= 3\big[3T(n/4^{2}) + n/4\big]+n = 3^{2}T(n/4^{2}) + \tfrac34 n + n\\ &= 3^{3}T(n/4^{3}) + \big(\tfrac34\big)^{2}n+\tfrac34 n+n\\ &\;\;\vdots\\ &= 3^{k}\,T(n/4^{k}) + n\Big[1+\tfrac34+\big(\tfrac34\big)^{2}+\cdots+\big(\tfrac34\big)^{k-1}\Big]. \end{aligned}

With n=4kn=4^{k} we have T(n/4k)=T(1)=1T(n/4^{k}) = T(1) = 1 and the geometric sum ∑i=0k−1xi=1−xk1−x\sum_{i=0}^{k-1}x^{i} = \dfrac{1-x^{k}}{1-x} with x=34x=\tfrac34:

T(n)=3k+n⋅1−(3/4)k1−3/4=3k+4n(1−3k4k)=3k+4n−4⋅3k.T(n) = 3^{k} + n\cdot\frac{1-(3/4)^{k}}{1-3/4} = 3^{k} + 4n\left(1-\frac{3^{k}}{4^{k}}\right) = 3^{k} + 4n - 4\cdot 3^{k} .

(because 4k=n4^{k}=n). So T(n)=4n−3⋅3kT(n) = 4n - 3\cdot 3^{k}. Using k=log4nk = \log_4 n and 3log4n=nlog433^{\log_4 n} = n^{\log_4 3}:

T(n)=4n−3nlog43with α=log43≈0.79<1,\boxed{T(n) = 4n - 3\,n^{\log_4 3}}\qquad\text{with }\alpha=\log_4 3\approx 0.79<1,

so T(n)=Θ(n)T(n) = \Theta(n). (Check: T(4)=3+4=7T(4)=3+4=7 and the formula gives 16−9=716-9=7 ✓.) ◼\blacksquare

Q110. Solve the recurrence T(n)={1n=14T(n/5)+notherwiseT(n) = \begin{cases}1 & n=1\\ 4\,T(n/5)+n & \text{otherwise}\end{cases} using the substitution method. (10 marks)

Let n=5kn = 5^{k} and substitute repeatedly:

T(n)=4T(n/5)+n=4[4T(n/52)+n/5]+n=42T(n/52)+45n+n=43T(n/53)+(45)2n+45n+n⋮=4kT(n/5k)+n[1+45+(45)2+⋯+(45)k−1].\begin{aligned} T(n) &= 4T(n/5)+n\\ &= 4\big[4T(n/5^{2})+n/5\big]+n = 4^{2}T(n/5^{2})+\tfrac45 n+n\\ &= 4^{3}T(n/5^{3})+\big(\tfrac45\big)^{2}n+\tfrac45 n+n\\ &\;\;\vdots\\ &= 4^{k}\,T(n/5^{k}) + n\Big[1+\tfrac45+\big(\tfrac45\big)^{2}+\cdots+\big(\tfrac45\big)^{k-1}\Big]. \end{aligned}

With n=5kn=5^{k}, T(n/5k)=T(1)=1T(n/5^{k})=T(1)=1 and ∑i=0k−1xi=1−xk1−x\sum_{i=0}^{k-1}x^{i}=\dfrac{1-x^{k}}{1-x}, x=45x=\tfrac45:

T(n)=4k+n⋅5(1−4k5k)=4k+5n(1−4kn)=4k+5n−5⋅4k=5n−4⋅4k.T(n) = 4^{k} + n\cdot 5\left(1-\frac{4^{k}}{5^{k}}\right) = 4^{k} + 5n\left(1-\frac{4^{k}}{n}\right) = 4^{k}+5n-5\cdot 4^{k} = 5n - 4\cdot 4^{k}.

Since 5k=n5^{k}=n, k=log5nk=\log_5 n and 4log5n=nlog544^{\log_5 n} = n^{\log_5 4}:

T(n)=5n−4nlog54=5n−4nα,α=log54≈0.86∈[0,1],\boxed{T(n) = 5n - 4\,n^{\log_5 4}} = 5n - 4n^{\alpha},\qquad \alpha = \log_5 4\approx 0.86\in[0,1],

hence T(n)∈Θ(n)T(n) \in \Theta(n). (Check: T(5)=4+5=9T(5)=4+5=9 and 25−4⋅4=925-4\cdot 4=9 ✓; T(25)=4⋅9+25=61=125−64T(25)=4\cdot9+25=61 = 125-64 ✓.) ◼\blacksquare

Q111. Solve the recurrence T(n)={1n=14T(⌊n/3⌋)+notherwiseT(n) = \begin{cases}1 & n=1\\ 4\,T(\lfloor n/3\rfloor)+n & \text{otherwise}\end{cases} using (i) the substitution method and (ii) the recursion-tree method. (15 marks)

(i) Substitution (assume n=3kn = 3^{k}). Exactly as in the two examples above:

T(n)=4kT(1)+n[1+43+(43)2+⋯+(43)k−1]=4k+n⋅(4/3)k−14/3−1=4k+3(4k−n)=4k+1−3n,T(n) = 4^{k}T(1) + n\Big[1+\tfrac43+\big(\tfrac43\big)^{2}+\cdots+\big(\tfrac43\big)^{k-1}\Big] = 4^{k} + n\cdot\frac{(4/3)^{k}-1}{4/3-1} = 4^{k}+3\big(4^{k}-n\big)=4^{k+1}-3n,

using n(4/3)k=3k(4/3)k=4kn(4/3)^{k} = 3^{k}(4/3)^{k}=4^{k}. With 4k=4log3n=nlog344^{k} = 4^{\log_3 n} = n^{\log_3 4}:

T(n)=4nlog34−3n.T(n) = 4\,n^{\log_3 4} - 3n .

(ii) Recursion tree.

nn/3n/3n/3n/3n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9n/9……… and so on, down to depth log₃ n (leaves T(1))level 0: nlevel 1: 4·(n/3) = (4/3)·nlevel 2: 16·(n/9) = (4/3)²·nlevel i: (4/3)^i · nleaves: 4^(log₃n) = n^(log₃4)
  • The cost at the top level is nn and four subtrees, each of cost T(n/3)T(n/3), are expanded.
  • Level ii has 4i4^{i} nodes, each of size n/3in/3^{i}, so the cost of level ii is 4i⋅n3i=(43)in4^{i}\cdot\dfrac{n}{3^{i}} = \left(\dfrac43\right)^{i}n.
  • A subproblem reaches T(1)T(1) when n/3i=1n/3^{i}=1, i.e. i=log3ni=\log_3 n, so the tree has log3n+1\log_3 n + 1 levels (0,1,…,log3n)(0,1,\ldots,\log_3 n).
  • The last level has 4log3n=nlog344^{\log_3 n} = n^{\log_3 4} nodes, each costing T(1)=1T(1)=1, contributing Θ(nlog34)\Theta\big(n^{\log_3 4}\big).

T(n)=∑i=0log3n−1(43)in+Θ(nlog34)=(4/3)log3n−1(4/3)−1n+Θ(nlog34)=3(nlog34−n)+Θ(nlog34)=Θ(nlog34).T(n) = \sum_{i=0}^{\log_3 n-1}\left(\frac43\right)^{i}n + \Theta\big(n^{\log_3 4}\big) = \frac{(4/3)^{\log_3 n}-1}{(4/3)-1}\,n+\Theta\big(n^{\log_3 4}\big) = 3\big(n^{\log_3 4}-n\big)+\Theta\big(n^{\log_3 4}\big)=\Theta\big(n^{\log_3 4}\big).

Since log34≈1.26<2\log_3 4\approx 1.26<2, this is in particular O(n2)O(n^{2}) (the weaker bound printed in the handout), and the exact bound is Θ(nlog34)\Theta(n^{\log_3 4}). ◼\blacksquare

Q112. Solve the recurrence T(n)={1n=13T(n/2)+n2otherwiseT(n) = \begin{cases}1 & n=1\\ 3\,T(n/2)+n^{2} & \text{otherwise}\end{cases} using the recursion-tree method. (15 marks)

n²(n/2)²(n/2)²(n/2)²(n/4)²(n/4)²(n/4)²(n/4)²(n/4)²(n/4)²(n/4)²(n/4)²(n/4)²……… and so on, down to depth log₂ n (leaves T(1))level 0: n²level 1: 3·(n/2)² = (3/4)·n²level 2: 9·(n/4)² = (3/4)²·n²level i: (3/4)^i · n²leaves: n^(log₂3) · T(1)
  • The root costs n2n^{2} and has three children T(n/2)T(n/2). Each of those costs (n/2)2(n/2)^{2} and has three children T(n/4)T(n/4), and so on.
  • The subproblem size at depth ii is n/2in/2^{i}; it reaches 11 when i=log2ni=\log_2 n, so the tree has log2n+1\log_2 n+1 levels.
  • Each level has three times as many nodes as the level above: depth ii has 3i3^{i} nodes, so its total cost is

3i(n2i)2=(34)in2.3^{i}\left(\frac n{2^{i}}\right)^{2} = \left(\frac34\right)^{i} n^{2}.

  • The last level (depth log2n\log_2 n) has 3log2n=nlog233^{\log_2 n} = n^{\log_2 3} nodes, each of cost T(1)=1T(1)=1, contributing Θ(nlog23)\Theta\big(n^{\log_2 3}\big).

Total cost:

T(n)=Θ(nlog23)+[(34)0+(34)1+⋯+(34)log2n−1]n2≤Θ(nlog23)+n2∑i=0∞(34)i.T(n) = \Theta\big(n^{\log_2 3}\big) + \left[\left(\tfrac34\right)^{0}+\left(\tfrac34\right)^{1}+\cdots+\left(\tfrac34\right)^{\log_2 n-1}\right]n^{2} \;\le\; \Theta\big(n^{\log_2 3}\big) + n^{2}\sum_{i=0}^{\infty}\left(\tfrac34\right)^{i}.

The infinite geometric series equals 11−3/4=4\dfrac{1}{1-3/4}=4, so

T(n)≤Θ(nlog23)+4n2.T(n) \le \Theta\big(n^{\log_2 3}\big) + 4n^{2} .

Since log23≈1.585<2\log_2 3\approx 1.585<2, the term 4n24n^{2} dominates and T(n)=O(n2)\boxed{T(n) = O(n^{2})}. In fact the exact solution for n=2kn=2^{k} is T(n)=4n2−3nlog23T(n) = 4n^{2}-3\,n^{\log_2 3}, so T(n)=Θ(n2)T(n)=\Theta(n^{2}). ◼\blacksquare

Q113. Start with a special kind of pair of rabbits, one male and one female, born on January 1. Assume all months are of equal length, that rabbits begin to produce four months after their own birth, that after reaching the age of four months each pair produces two mixed pairs (two males and two females), and then two more mixed pairs each month, and that no rabbit dies. (1) Develop a model using a tree diagram. (2) Describe a recursive mathematical model. (3) Compute the number of pairs of rabbits after one year. (15 marks)

(1) Tree diagram. Each node is a pair. A pair born in month bb is immature until month b+4b+4; from month b+4b+4 onward, it produces two new pairs every month. (Red = pairs that are still young and cannot yet produce; the tree is drawn to month 9.)

level (month) → numbers on the right = total pairs F(k)F1 = 11F2 = 12F3 = 13F4 = 14F5 = 35F6 = 56F7 = 77F8 = 98F9 = 159

The number of pairs at month kk is: F0=0F_0=0, F1=F2=F3=F4=1F_1=F_2=F_3=F_4=1, F5=3F_5=3, F6=5F_6=5, F7=7F_7=7, F8=9F_8=9, F9=15F_9=15.

(2) Recursive model.

  • Total pairs at level kk = total pairs at level k−1k-1 + total pairs born at level kk. (1)
  • Pairs born at level kk = 2×2\times (total pairs at level k−4k-4), because every pair that exists at month k−4k-4 is at least four months old at month kk and produces two pairs each month. (2)

From (1) and (2), with FkF_k = total pairs at level kk:

Fk=Fk−1+2Fk−4,F0=0,F1=F2=F3=1\boxed{F_k = F_{k-1} + 2F_{k-4},\qquad F_0=0,\ F_1=F_2=F_3=1}

(3) Pairs after one year (12 months).

F4=F3+2F0=1+0=1F5=F4+2F1=1+2=3F6=F5+2F2=3+2=5F7=F6+2F3=5+2=7F8=F7+2F4=7+2=9F9=F8+2F5=9+6=15F10=F9+2F6=15+10=25F11=F10+2F7=25+14=39F12=F11+2F8=39+18=57\begin{aligned} F_4 &= F_3+2F_0 = 1+0 = 1 & F_5 &= F_4+2F_1 = 1+2 = 3\\ F_6 &= F_5+2F_2 = 3+2 = 5 & F_7 &= F_6+2F_3 = 5+2 = 7\\ F_8 &= F_7+2F_4 = 7+2 = 9 & F_9 &= F_8+2F_5 = 9+6 = 15\\ F_{10} &= F_9+2F_6 = 15+10 = 25 & F_{11} &= F_{10}+2F_7 = 25+14 = 39\\ F_{12} &= F_{11}+2F_8 = 39+18 = 57 \end{aligned}

Hence the number of pairs of rabbits after one year is 57. ◼\blacksquare

Q114. Start with a pair of rabbits (one male, one female) born on January 1. All months have equal length, no rabbit dies, and the production rule of the handout is used: a pair produces two mixed pairs at the age of two months, and from the age of three months onward it produces one mixed pair every month. (i) Develop a tree-diagram model. (ii) Describe a recursive mathematical model. (iii) Compute the number of pairs after one year. (15 marks)

Reading of the question: the printed statement is garbled ("two mixed pairs two months after its own birth and after that produces three mixed pairs after each three months"). The recursive model printed in the solution — and the values F0=0,F1=1,F2=1,F3=3,F4=4F_0=0,\ F_1=1,\ F_2=1,\ F_3=3,\ F_4=4 given with it — correspond to the rule stated above, so that rule is used here. This interpretation could not be verified against the original handout.

(ii) Recursive model.

  • Pairs at level kk = pairs at level k−1k-1 + pairs born at level kk.
  • Fk−1F_{k-1} is the number of pairs at level k−1k-1, so Fk−2−Fk−3F_{k-2}-F_{k-3} pairs were born at level k−2k-2, i.e. are exactly two months old at level kk and produce two pairs each.
  • The Fk−3F_{k-3} pairs that are at least three months old produce one pair each.
  • Pairs born at level kk =2(Fk−2−Fk−3)+Fk−3= 2\,(F_{k-2}-F_{k-3}) + F_{k-3}.

Fk=Fk−1+2(Fk−2−Fk−3)+Fk−3,F0=0,F1=1,F2=1\boxed{F_k = F_{k-1} + 2\,(F_{k-2}-F_{k-3}) + F_{k-3},\qquad F_0=0,\ F_1=1,\ F_2=1}

(iii) Values.

kk 0 1 2 3 4 5 6 7 8 9 10 11 12
FkF_k 0 1 1 3 4 9 14 28 47 89 155 286 507

So after one year (12 months) there are 507 pairs under this model.

(i) Tree diagram. Draw the initial pair at level 1; at level 3 it produces two pairs; from level 4 it produces one pair every month; every new pair follows the same rule (two pairs at age two months, then one pair per month from age three). The node counts per level are exactly the numbers in the table. ◼\blacksquare

Q115. Below is a set of 9 points in 2-D. Compute the set of maximal points using the brute-force approach: (2,6),(4,16),(8,8),(8,20),(10,4),(14,12),(16,16),(18,2),(22,8)(2,6),\ (4,16),\ (8,8),\ (8,20),\ (10,4),\ (14,12),\ (16,16),\ (18,2),\ (22,8). (10 marks)

Using the procedure MAXIMAL-POINTS above, every point is compared with every other point (both coordinates). The points that dominate each point are:

point P[i]P[i] points that dominate it maximal?
(2,6)(2,6) (4,16), (8,8), (8,20), (14,12), (16,16), (22,8) no
(4,16)(4,16) (8,20), (16,16) no
(8,8)(8,8) (8,20), (14,12), (16,16), (22,8) no
(8,20)(8,20) none yes
(10,4)(10,4) (14,12), (16,16), (22,8) no
(14,12)(14,12) (16,16) no
(16,16)(16,16) none yes
(18,2)(18,2) (22,8) no
(22,8)(22,8) none yes

Set of maximal points == (8,20)(8,20), (16,16)(16,16), (22,8)(22,8).

Q116. Compute the set of maximal points, using the brute-force approach, of the points (2,8),(6,16),(8,8),(10,20),(12,6),(16,14),(18,18),(20,2),(24,8)(2,8),\ (6,16),\ (8,8),\ (10,20),\ (12,6),\ (16,14),\ (18,18),\ (20,2),\ (24,8). (10 marks)

Using the same MAXIMAL-POINTS procedure:

point P[i]P[i] points that dominate it maximal?
(2,8)(2,8) (6,16), (8,8), (10,20), (16,14), (18,18), (24,8) no
(6,16)(6,16) (10,20), (18,18) no
(8,8)(8,8) (10,20), (16,14), (18,18), (24,8) no
(10,20)(10,20) none yes
(12,6)(12,6) (16,14), (18,18), (24,8) no
(16,14)(16,14) (18,18) no
(18,18)(18,18) none yes
(20,2)(20,2) (24,8) no
(24,8)(24,8) none yes

Set of maximal points == (10,20)(10,20), (18,18)(18,18), (24,8)(24,8).

Q117. Write the divide-and-conquer algorithm for the maxima-finding problem and find its time complexity. (10 marks)

Input: a set SS of 2-dimensional points. Output: the maximal set of SS.

MAXIMA(P[1..n])
1. Sort the points in ascending order with respect to the x-axis.
2. If |S| = 1 then return it; else
   find a line L perpendicular to the x-axis that separates S into S_L and S_R,
   each consisting of n/2 points.
3. Recursively find the maxima of S_L and S_R.
4. Project the maxima of S_L and S_R onto L and sort these points by their y-values.
5. Conduct a linear scan on the projections and discard each maximum of S_L
   whose y-value is less than the y-value of some maximum of S_R.

Time complexity. Dividing takes O(n)O(n) and combining (steps 4–5) takes O(n)O(n), so

T(n)={2T(n/2)+O(n)+O(n)n≥2,1n<2.T(n)=\begin{cases}2\,T(n/2)+O(n)+O(n) & n\ge 2,\\ 1 & n<2.\end{cases}

Assume n=2kn=2^{k} and expand (2n2n stands for the two O(n)O(n) terms):

T(n)=2T(n/2)+2n=22T(n/22)+4n=⋯=2kT(n/2k)+2kn=nT(1)+2nlog2n.T(n)=2T(n/2)+2n = 2^{2}T(n/2^{2})+4n=\cdots=2^{k}\,T(n/2^{k})+2kn = n\,T(1)+2n\log_2 n .

T(n)=n+2nlog2n=Θ(nlogn)\boxed{T(n)=n+2n\log_2 n=\Theta(n\log n)}

Q118. Write the pseudo-code of the brute-force maximal-points algorithm for nn-dimensional points. (10 marks)

For points with dd coordinates (dimension dd), a point P[i]P[i] is dominated by P[j]P[j] when every coordinate of P[i]P[i] is ≤\le the corresponding coordinate of P[j]P[j].

MAXIMAL-POINTS(int m, int d, Point P[1..m])      // m points, d dimensions
0   A ← ∅
1   for i ← 1 to m
2       maximal ← true
3       for j ← 1 to m
4           if i ≠ j
5               dominated ← true
6               for k ← 1 to d
7                   if P[i].x[k] > P[j].x[k]
8                       dominated ← false
9                       break
10              if dominated
11                  maximal ← false
12                  break
13      if maximal
14          A ← A ∪ {P[i]}
15  return A

Complexity: Θ(m2d)\Theta(m^{2}\,d) — m2m^{2} pairs, each compared in O(d)O(d).

Q119. Write the pseudo-code of the plane-sweep algorithm for the maxima problem in nn dimensions. (10 marks)

The plane-sweep (stack) method first sorts the points by the first coordinate, then sweeps from left to right keeping a stack of candidate maximal points.

MAXIMAL-POINTS(int m, int n, Point P[1..m])      // m points, n = dimension
1   sort P in increasing order by first component
2   stack s
3   for i ← 1 to m
4       while (s is not empty) and
5             (for all j ← 2 to n:  s.top().x[j] ≤ P[i].x[j])
6           s.pop()
7       s.push(P[i])
8   output the contents of s

Any point on top of the stack that is dominated by the new point P[i]P[i] in all coordinates 2,…,n2,\ldots,n is removed (it has a smaller first coordinate because of the sorting), and then P[i]P[i] is pushed.

Note. For n=2n=2 this is exactly the classic plane-sweep algorithm and runs in O(mlogm)O(m\log m) time (sorting dominates; each point is pushed and popped at most once). The handout applies the same stack idea to higher dimensions.

Q120. Write the divide-and-conquer algorithm for the closest pair of points in 2-D and find its running time. (10 marks)

Sort the points by xx-coordinate once. Then:

Closest-Pair(P, l, r)
01  if r - l < 3 then return ClosestPairBF(P[l..r])
02  q ← ⌈(l + r) / 2⌉
03  d_l ← Closest-Pair(P, l, q - 1)
04  d_r ← Closest-Pair(P, q, r)
05  d ← min(d_l, d_r)
06  S ← empty list
07  for i ← l to r do
08      if P[q].x - d ≤ P[i].x ≤ P[q].x + d then
09          append P[i] to S
10  sort S on y-coordinate
11  for j ← 1 to size_of(S) - 1 do
12      check whether any of d(S[j], S[j+1]), ..., d(S[j], S[j+7]) is smaller than d;
        if so, set d to the smallest of them
13  return d

Idea. The closest pair is either entirely in the left half, entirely in the right half, or it straddles the dividing line. In the last case both points lie in the strip of width 2d2d around the line, and after sorting the strip by yy each point need only be compared with its next 77 neighbours.

Running time. Divide =O(1)=O(1); combine (strip selection and sorting) =O(nlogn)=O(n\log n):

T(n)={nn≤3,2T(n/2)+nlognotherwise.T(n)=\begin{cases} n & n\le 3,\\ 2\,T(n/2) + n\log n & \text{otherwise.}\end{cases}

Each level of the recursion tree costs about nlog(n/2i)n\log(n/2^{i}), and summing over logn\log n levels gives Θ(nlog2n)\Theta(n\log^{2}n), so the total running time is O(nlog2n)O(n\log^{2}n). (If the points are also kept sorted by yy during the recursion, the combine step becomes O(n)O(n) and the total improves to O(nlogn)O(n\log n).)

Q121. State the matrix-chain multiplication problem and explain how dynamic programming solves it. (10 marks)

Problem. Given a chain ⟨A1,A2,…,An⟩\langle A_1,A_2,\dots,A_n\rangle of matrices where AiA_i has dimensions pi−1×pip_{i-1}\times p_i, fully parenthesise the product A1A2⋯AnA_1A_2\cdots A_n so that the number of scalar multiplications is minimum. Matrix multiplication is associative, so every parenthesisation gives the same matrix, but the cost differs greatly.

Cost of one product. Multiplying a p×qp\times q matrix by a q×rq\times r matrix needs pqrp\,q\,r scalar multiplications and gives a p×rp\times r matrix.

Optimal substructure. Let m[i,j]m[i,j] be the minimum cost of computing Ai⋯AjA_i\cdots A_j. If the last multiplication splits the chain at kk (i≤k<ji\le k<j), the two sub-chains must themselves be parenthesised optimally, so

m[i,j]={0i=j,mini≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}i<j.m[i,j]=\begin{cases}0 & i=j,\\[4pt] \displaystyle\min_{i\le k<j}\bigl\{\,m[i,k]+m[k+1,j]+p_{i-1}\,p_k\,p_j\,\bigr\} & i<j.\end{cases}

The table is filled by increasing chain length ℓ=j−i+1\ell=j-i+1; s[i,j]s[i,j] stores the kk giving the minimum. The answer is m[1,n]m[1,n], and ss is used to print the parenthesisation. There are Θ(n2)\Theta(n^2) sub-problems and each takes O(n)O(n) to evaluate, so the running time is Θ(n3)\Theta(n^3) and the space is Θ(n2)\Theta(n^2).

Q122. Write the algorithm MATRIX-CHAIN-ORDER and PRINT-OPTIMAL-PARENS and give its running time. (8 marks)

MATRIX-CHAIN-ORDER(p[0..n])
1   for i ← 1 to n
2       m[i,i] ← 0
3   for ℓ ← 2 to n                     // ℓ = chain length
4       for i ← 1 to n − ℓ + 1
5           j ← i + ℓ − 1
6           m[i,j] ← ∞
7           for k ← i to j − 1
8               q ← m[i,k] + m[k+1,j] + p[i−1]·p[k]·p[j]
9               if q < m[i,j]
10                  m[i,j] ← q
11                  s[i,j] ← k
12  return m, s

PRINT-OPTIMAL-PARENS(s, i, j)
1   if i = j
2       print "A" i
3   else
4       print "("
5       PRINT-OPTIMAL-PARENS(s, i, s[i,j])
6       PRINT-OPTIMAL-PARENS(s, s[i,j]+1, j)
7       print ")"

Three nested loops each run at most nn times, so T(n)=O(n3)T(n)=O(n^3); in fact Θ(n3)\Theta(n^3). The tables mm and ss need Θ(n2)\Theta(n^2) space.

Q123. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain A1A_1: 10×10010\times100, A2A_2: 100×5100\times5, A3A_3: 5×505\times50. (10 marks)

Using m[i,j]=mini≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j]=\min_{i\le k<j}\{m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\}:

Dimensions p=10,100,5,50p=10,100,5,50, i.e. A1A_1: 10×10010\times100, A2A_2: 100×5100\times5, A3A_3: 5×505\times50.

Cost table m[i,j]m[i,j] (row ii, column jj):

j=1j=1 j=2j=2 j=3j=3
i=1i=1 0 5000 7500
i=2i=2 – 0 25000
i=3i=3 – – 0

Split table s[i,j]s[i,j]:

j=1j=1 j=2j=2 j=3j=3
i=1i=1 – 1 2
i=2i=2 – – 2
i=3i=3 – – –

Minimum number of scalar multiplications: m[1,3]=7500m[1,3]=7500, with optimal parenthesisation ((A1A2)A3)((A_1A_2)A_3).

Check: (A1A2)A3(A_1A_2)A_3 costs 10⋅100⋅5+10⋅5⋅50=5000+2500=750010\cdot100\cdot5+10\cdot5\cdot50=5000+2500=7500, whereas A1(A2A3)A_1(A_2A_3) costs 100⋅5⋅50+10⋅100⋅50=25000+50000=75000100\cdot5\cdot50+10\cdot100\cdot50=25000+50000=75000.

Q124. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain A1A_1: 20×1020\times10, A2A_2: 10×3010\times30, A3A_3: 30×1530\times15, A4A_4: 15×2515\times25. (10 marks)

Using m[i,j]=mini≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j]=\min_{i\le k<j}\{m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\}:

Dimensions p=20,10,30,15,25p=20,10,30,15,25, i.e. A1A_1: 20×1020\times10, A2A_2: 10×3010\times30, A3A_3: 30×1530\times15, A4A_4: 15×2515\times25.

Cost table m[i,j]m[i,j] (row ii, column jj):

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 0 6000 7500 13250
i=2i=2 – 0 4500 8250
i=3i=3 – – 0 11250
i=4i=4 – – – 0

Split table s[i,j]s[i,j]:

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 – 1 1 1
i=2i=2 – – 2 3
i=3i=3 – – – 3
i=4i=4 – – – –

Minimum number of scalar multiplications: m[1,4]=13250m[1,4]=13250, with optimal parenthesisation (A1((A2A3)A4))(A_1((A_2A_3)A_4)).

Q125. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain A1A_1: 7×57\times5, A2A_2: 5×45\times4, A3A_3: 4×64\times6, A4A_4: 6×86\times8. (10 marks)

Using m[i,j]=mini≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j]=\min_{i\le k<j}\{m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\}:

Dimensions p=7,5,4,6,8p=7,5,4,6,8, i.e. A1A_1: 7×57\times5, A2A_2: 5×45\times4, A3A_3: 4×64\times6, A4A_4: 6×86\times8.

Cost table m[i,j]m[i,j] (row ii, column jj):

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 0 140 308 556
i=2i=2 – 0 120 352
i=3i=3 – – 0 192
i=4i=4 – – – 0

Split table s[i,j]s[i,j]:

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 – 1 2 2
i=2i=2 – – 2 2
i=3i=3 – – – 3
i=4i=4 – – – –

Minimum number of scalar multiplications: m[1,4]=556m[1,4]=556, with optimal parenthesisation ((A1A2)(A3A4))((A_1A_2)(A_3A_4)).

Q126. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain A1A_1: 10×10010\times100, A2A_2: 100×5100\times5, A3A_3: 5×505\times50, A4A_4: 50×2050\times20. (10 marks)

Using m[i,j]=mini≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j]=\min_{i\le k<j}\{m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\}:

Dimensions p=10,100,5,50,20p=10,100,5,50,20, i.e. A1A_1: 10×10010\times100, A2A_2: 100×5100\times5, A3A_3: 5×505\times50, A4A_4: 50×2050\times20.

Cost table m[i,j]m[i,j] (row ii, column jj):

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 0 5000 7500 11000
i=2i=2 – 0 25000 15000
i=3i=3 – – 0 5000
i=4i=4 – – – 0

Split table s[i,j]s[i,j]:

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 – 1 2 2
i=2i=2 – – 2 2
i=3i=3 – – – 3
i=4i=4 – – – –

Minimum number of scalar multiplications: m[1,4]=11000m[1,4]=11000, with optimal parenthesisation ((A1A2)(A3A4))((A_1A_2)(A_3A_4)).

Q127. State the 0-1 knapsack problem and write a brute-force algorithm with its running time. (8 marks)

Problem. Given nn items, item ii with value viv_i and weight wiw_i, and a knapsack of capacity WW, choose a subset of items with total weight ≤W\le W and maximum total value. Each item is taken whole or not at all (0-1).

max∑i∈Svisubject to∑i∈Swi≤W,S⊆{1,…,n}.\max\sum_{i\in S}v_i\quad\text{subject to}\quad\sum_{i\in S}w_i\le W,\ S\subseteq\{1,\dots,n\}.

Brute force: examine all 2n2^n subsets.

KNAPSACK-BRUTE(v[1..n], w[1..n], W)
1   best ← 0
2   for each subset S of {1,...,n}
3       weight ← sum of w[i] for i in S
4       value  ← sum of v[i] for i in S
5       if weight ≤ W and value > best
6           best ← value ; bestSet ← S
7   return best, bestSet

There are 2n2^n subsets and each costs O(n)O(n) to evaluate, so T(n)=O(n2n)T(n)=O(n\,2^n).

Q128. Write the dynamic-programming algorithm for the 0-1 knapsack problem and give its running time. (8 marks)

Let V[i,w]V[i,w] be the maximum value using only the first ii items with capacity ww:

V[i,w]={0i=0orw=0,V[i−1,w]wi>w,max{V[i−1,w],vi+V[i−1,w−wi]}wi≤w.V[i,w]=\begin{cases}0 & i=0\ \text{or}\ w=0,\\[4pt]V[i-1,w] & w_i>w,\\[4pt]\max\{\,V[i-1,w],\ v_i+V[i-1,w-w_i]\,\} & w_i\le w.\end{cases}

KNAPSACK-DP(v[1..n], w[1..n], W)
1   for w ← 0 to W
2       V[0,w] ← 0
3   for i ← 1 to n
4       V[i,0] ← 0
5       for w ← 1 to W
6           if w[i] ≤ w and v[i] + V[i−1, w−w[i]] > V[i−1, w]
7               V[i,w] ← v[i] + V[i−1, w−w[i]]
8               keep[i,w] ← 1
9           else
10              V[i,w] ← V[i−1,w]
11              keep[i,w] ← 0
12  return V[n,W]

PRINT-ITEMS(keep, n, W)
1   w ← W
2   for i ← n downto 1
3       if keep[i,w] = 1
4           print i ; w ← w − w[i]

Two nested loops give Θ(nW)\Theta(nW) time and Θ(nW)\Theta(nW) space. This is pseudo-polynomial because WW appears numerically, not as its length in bits.

Q129. Solve the 0-1 knapsack problem with capacity W=11W=11 and items (weight, value) = (1,1), (2,6), (5,18), (6,22), (7,28) using dynamic programming. (10 marks)

Table V[i,w]V[i,w] computed with the recurrence above:

w=0w=0 w=1w=1 w=2w=2 w=3w=3 w=4w=4 w=5w=5 w=6w=6 w=7w=7 w=8w=8 w=9w=9 w=10w=10 w=11w=11
i=0i=0 0 0 0 0 0 0 0 0 0 0 0 0
i=1i=1 0 1 1 1 1 1 1 1 1 1 1 1
i=2i=2 0 1 6 7 7 7 7 7 7 7 7 7
i=3i=3 0 1 6 7 7 18 19 24 25 25 25 25
i=4i=4 0 1 6 7 7 18 22 24 28 29 29 40
i=5i=5 0 1 6 7 7 18 22 28 29 34 35 40

The optimal value is V[5,11]=40V[5,11]=40. Tracing back with the keep table gives items {3, 4} (total weight 11, total value 40).

Q130. Solve the 0-1 knapsack problem with capacity W=12W=12 and items (weight, value) = (4,40), (3,30), (5,40), (4,50), (5,70) using dynamic programming. (10 marks)

Table V[i,w]V[i,w] computed with the recurrence above:

w=0w=0 w=1w=1 w=2w=2 w=3w=3 w=4w=4 w=5w=5 w=6w=6 w=7w=7 w=8w=8 w=9w=9 w=10w=10 w=11w=11 w=12w=12
i=0i=0 0 0 0 0 0 0 0 0 0 0 0 0 0
i=1i=1 0 0 0 0 40 40 40 40 40 40 40 40 40
i=2i=2 0 0 0 30 40 40 40 70 70 70 70 70 70
i=3i=3 0 0 0 30 40 40 40 70 70 80 80 80 110
i=4i=4 0 0 0 30 50 50 50 80 90 90 90 120 120
i=5i=5 0 0 0 30 50 70 70 80 100 120 120 120 150

The optimal value is V[5,12]=150V[5,12]=150. Tracing back with the keep table gives items {2, 4, 5} (total weight 12, total value 150).

Q131. Solve the 0-1 knapsack instance W=8W=8, values v=(3,5,6,10)v=(3,5,6,10), weights w=(2,3,4,5)w=(2,3,4,5) by backtracking and show the search tree. (8 marks)

Backtracking builds the selection item by item, extending a partial solution only while the weight stays within W=8W=8 and abandoning any branch that would exceed it. Nodes are labelled chosen weights ; total value.

;02;32,2;62,2,2;92,2,2,2;122,2,3;112,2,4;122,3;82,3,3;132,4;92,5;133;53,3;103,4;113,5;154;64,4;125;10Node a,b,…; V = chosen weights; total value. Capacity 8. Best node (red): weights 3 and 5, value 15.

The best node is "3,5 ; 15": take the items of weight 3 and 5 (values 5 and 10) for a total value of 15 and weight 8. Other full nodes are worse, e.g. "2,2,2,2 ; 12" and "2,3,3 ; 13".

Q132. Define the longest common subsequence (LCS) problem and give its dynamic-programming recurrence and algorithm. (8 marks)

A subsequence of X=⟨x1,…,xm⟩X=\langle x_1,\dots,x_m\rangle is obtained by deleting zero or more elements without changing the order of the rest. A common subsequence of XX and Y=⟨y1,…,yn⟩Y=\langle y_1,\dots,y_n\rangle is a subsequence of both; the LCS is a longest one.

Let c[i,j]c[i,j] be the length of an LCS of the prefixes XiX_i and YjY_j:

c[i,j]={0i=0orj=0,c[i−1,j−1]+1i,j>0andxi=yj,max{c[i−1,j],c[i,j−1]}i,j>0andxi≠yj.c[i,j]=\begin{cases}0 & i=0\ \text{or}\ j=0,\\[4pt]c[i-1,j-1]+1 & i,j>0\ \text{and}\ x_i=y_j,\\[4pt]\max\{c[i-1,j],\,c[i,j-1]\} & i,j>0\ \text{and}\ x_i\ne y_j.\end{cases}

LCS-LENGTH(X[1..m], Y[1..n])
1   for i ← 0 to m: c[i,0] ← 0
2   for j ← 0 to n: c[0,j] ← 0
3   for i ← 1 to m
4       for j ← 1 to n
5           if X[i] = Y[j]
6               c[i,j] ← c[i−1,j−1] + 1 ; b[i,j] ← "↖"
7           else if c[i−1,j] ≥ c[i,j−1]
8               c[i,j] ← c[i−1,j] ; b[i,j] ← "↑"
9           else
10              c[i,j] ← c[i,j−1] ; b[i,j] ← "←"
11  return c, b

The arrows in bb are followed from (m,n)(m,n) to print the LCS. Time and space are Θ(mn)\Theta(mn).

Q133. Find the longest common subsequence of ABDADBAB and DABABDABA using dynamic programming. (10 marks)

Fill c[i,j]c[i,j] with the LCS recurrence (each cell shows the length followed by the direction arrow of b[i,j]b[i,j]):

xi∖yjx_i\backslash y_j 0 1 (D) 2 (A) 3 (B) 4 (A) 5 (B) 6 (D) 7 (A) 8 (B) 9 (A)
0 0 0 0 0 0 0 0 0 0 0
1 (A) 0 0↑ 1↖ 1← 1↖ 1← 1← 1↖ 1← 1↖
2 (B) 0 0↑ 1↑ 2↖ 2← 2↖ 2← 2← 2↖ 2←
3 (D) 0 1↖ 1↑ 2↑ 2↑ 2↑ 3↖ 3← 3← 3←
4 (A) 0 1↑ 2↖ 2↑ 3↖ 3← 3↑ 4↖ 4← 4↖
5 (D) 0 1↖ 2↑ 2↑ 3↑ 3↑ 4↖ 4↑ 4↑ 4↑
6 (B) 0 1↑ 2↑ 3↖ 3↑ 4↖ 4↑ 4↑ 5↖ 5←
7 (A) 0 1↑ 2↖ 3↑ 4↖ 4↑ 4↑ 5↖ 5↑ 6↖
8 (B) 0 1↑ 2↑ 3↖ 4↑ 5↖ 5← 5↑ 6↖ 6↑

The length of the LCS is c[8,9]=6c[8,9]=6. Following the arrows back from the last cell gives the LCS ABDABA.

Note: the printed table in the source file has mistakes in the rows for D (5) and B (6); the table above is recomputed.

Q134. Find the longest common subsequence of ⟨1,0,0,1,0,1,0,1⟩ and ⟨0,1,0,1,1,0,1,1,0⟩ using dynamic programming. (10 marks)

Fill c[i,j]c[i,j] with the LCS recurrence (each cell shows the length followed by the direction arrow of b[i,j]b[i,j]):

xi∖yjx_i\backslash y_j 0 1 (0) 2 (1) 3 (0) 4 (1) 5 (1) 6 (0) 7 (1) 8 (1) 9 (0)
0 0 0 0 0 0 0 0 0 0 0
1 (1) 0 0↑ 1↖ 1← 1↖ 1↖ 1← 1↖ 1↖ 1←
2 (0) 0 1↖ 1↑ 2↖ 2← 2← 2↖ 2← 2← 2↖
3 (0) 0 1↖ 1↑ 2↖ 2↑ 2↑ 3↖ 3← 3← 3↖
4 (1) 0 1↑ 2↖ 2↑ 3↖ 3↖ 3↑ 4↖ 4↖ 4←
5 (0) 0 1↖ 2↑ 3↖ 3↑ 3↑ 4↖ 4↑ 4↑ 5↖
6 (1) 0 1↑ 2↖ 3↑ 4↖ 4↖ 4↑ 5↖ 5↖ 5↑
7 (0) 0 1↖ 2↑ 3↖ 4↑ 4↑ 5↖ 5↑ 5↑ 6↖
8 (1) 0 1↑ 2↖ 3↑ 4↖ 5↖ 5↑ 6↖ 6↖ 6↑

The length of the LCS is c[8,9]=6c[8,9]=6. Following the arrows back from the last cell gives the LCS 100110.

Q135. Explain the assembly-line scheduling problem for two lines and derive the dynamic-programming recurrence. (10 marks)

A factory has two assembly lines, each with nn stations Si,1,…,Si,nS_{i,1},\dots,S_{i,n} (i=1,2i=1,2). Station Si,jS_{i,j} takes time ai,ja_{i,j} (the stations in the same position do the same job). A chassis enters line ii with entry time eie_i, leaves with exit time xix_i, and moving from line ii after station jj to the other line costs ti,jt_{i,j}. Choose the stations on each line so that the total time through the factory is minimum.

Optimal substructure. The fastest way to reach Si,jS_{i,j} comes either straight from Si,j−1S_{i,j-1} or from the other line's Si′,j−1S_{i',j-1} plus the transfer. Let fi[j]f_i[j] be the fastest time to complete station Si,jS_{i,j}:

f1[1]=e1+a1,1,f2[1]=e2+a2,1,f_1[1]=e_1+a_{1,1},\qquad f_2[1]=e_2+a_{2,1},

f1[j]=min{f1[j−1]+a1,j,f2[j−1]+t2,j−1+a1,j},f_1[j]=\min\{\,f_1[j-1]+a_{1,j},\ f_2[j-1]+t_{2,j-1}+a_{1,j}\,\},

f2[j]=min{f2[j−1]+a2,j,f1[j−1]+t1,j−1+a2,j}(j≥2),f_2[j]=\min\{\,f_2[j-1]+a_{2,j},\ f_1[j-1]+t_{1,j-1}+a_{2,j}\,\}\qquad(j\ge2),

and the optimum is f*=min{f1[n]+x1,f2[n]+x2}f^{*}=\min\{f_1[n]+x_1,\ f_2[n]+x_2\}. Let li[j]l_i[j] record the line used at station j−1j-1 and l*l^{*} the line used at station nn.

Brute force would try all 2n2^{n} station choices (Θ(2n)\Theta(2^n), or n⋅2nn\cdot2^n with evaluation); DP fills the table in Θ(n)\Theta(n).

Q136. Write the algorithm FASTEST-WAY and PRINT-STATIONS for the two-line assembly problem. (8 marks)

FASTEST-WAY(a, t, e, x, n)
1   f1[1] ← e1 + a1,1
2   f2[1] ← e2 + a2,1
3   for j ← 2 to n
4       if f1[j−1] + a1,j ≤ f2[j−1] + t2,j−1 + a1,j
5           f1[j] ← f1[j−1] + a1,j ;  l1[j] ← 1
6       else
7           f1[j] ← f2[j−1] + t2,j−1 + a1,j ;  l1[j] ← 2
8       if f2[j−1] + a2,j ≤ f1[j−1] + t1,j−1 + a2,j
9           f2[j] ← f2[j−1] + a2,j ;  l2[j] ← 2
10      else
11          f2[j] ← f1[j−1] + t1,j−1 + a2,j ;  l2[j] ← 1
12  if f1[n] + x1 ≤ f2[n] + x2
13      f* ← f1[n] + x1 ;  l* ← 1
14  else
15      f* ← f2[n] + x2 ;  l* ← 2

PRINT-STATIONS(l, l*, n)
1   i ← l*
2   print "line" i ", station" n
3   for j ← n downto 2
4       i ← l_i[j]
5       print "line" i ", station" j−1

Each loop iteration takes constant time, so the running time is Θ(n)\Theta(n).

Q137. Find the fastest way through the factory: a1=(5,5,3,6,8,7)a_1=(5,5,3,6,8,7), a2=(3,4,8,6,4,7)a_2=(3,4,8,6,4,7), t1=(5,4,1,6,3)t_1=(5,4,1,6,3), t2=(2,4,5,3,1)t_2=(2,4,5,3,1), e1=3,e2=1e_1=3,\ e_2=1, x1=3,x2=6x_1=3,\ x_2=6. (10 marks)

jj 1 2 3 4 5 6
f1[j]f_1[j] 8 11 14 20 28 33
f2[j]f_2[j] 4 8 16 21 25 32
l1[j]l_1[j] – 2 1 1 1 2
l2[j]l_2[j] – 2 2 1 2 2

Final: f1[6]+x1=33+3=36f_1[6]+x_1=33+3=36 and f2[6]+x2=32+6=38f_2[6]+x_2=32+6=38, so f*=36f^{*}=36 and l*=1l^{*}=1.

Tracing back: line 1 station 6 ← line 2 station 5 ← line 2 station 4 ← line 1 station 3 ← line 1 station 2 ← line 2 station 1. In the order of travel: S2,1→S1,2→S1,3→S2,4→S2,5→S1,6S_{2,1}\to S_{1,2}\to S_{1,3}\to S_{2,4}\to S_{2,5}\to S_{1,6}.

52441563315f1[1]=85f1[2]=113f1[3]=146f1[4]=208f1[5]=287f1[6]=333f2[1]=44f2[2]=88f2[3]=166f2[4]=214f2[5]=257f2[6]=32e1=3x1=3e2=1x2=6InOutLine 1Line 2Numbers in circles = assembly times; blue = transfer times; green = fastest times fRed path = optimal route, f* = 36

The fastest time is 36.

Q138. Extend the assembly-line scheduling idea to nn lines with mm stations each. Give the recurrence and the algorithm. (10 marks)

S1,1S1,2S1,3S1,jS1,5S1,mS2,1S2,2S2,3S2,jS2,5S2,mSi,1Si,2Si,3Si,jSi,5Si,mSn,1Sn,2Sn,3Sn,jSn,5Sn,me1x1e2x2eixienxnInOutt(k, j−1)n lines, m stations per line. S(i,j) can be reached from station j−1 of any line k; blue arrows show the transfers t(k, j−1).

Let f[i,j]f[i,j] be the fastest time to finish station Si,jS_{i,j}, with t[k,i,j−1]t[k,i,j-1] the transfer time from line kk to line ii after station j−1j-1 (it is 00 when k=ik=i):

f[i,1]=ei+a[i,1],f[i,j]=a[i,j]+min1≤k≤n{f[k,j−1]+t[k,i,j−1]}(j≥2),f[i,1]=e_i+a[i,1],\qquad f[i,j]=a[i,j]+\min_{1\le k\le n}\bigl\{f[k,j-1]+t[k,i,j-1]\bigr\}\quad(j\ge2),

f*=min1≤i≤n{f[i,m]+xi}.f^{*}=\min_{1\le i\le n}\bigl\{f[i,m]+x_i\bigr\}.

FASTEST-WAY-N(a, t, e, x, n, m)
1   for i ← 1 to n
2       f[i,1] ← e[i] + a[i,1]
3   for j ← 2 to m
4       for i ← 1 to n
5           f[i,j] ← ∞
6           for k ← 1 to n
7               q ← f[k,j−1] + t[k,i,j−1] + a[i,j]
8               if q < f[i,j]
9                   f[i,j] ← q ; L[i,j] ← k
10  f* ← ∞
11  for i ← 1 to n
12      if f[i,m] + x[i] < f*
13          f* ← f[i,m] + x[i] ; L* ← i

The three nested loops make the DP time Θ(mn2)\Theta(m\,n^{2}). Brute force tries every choice of line at every station: nmn^{m} routes of length mm, i.e. Θ(mnm)\Theta(m\,n^{m}). (Note: the source pseudocode wrote L[1,j]=1 and t[2,j−1] where L[i,j] and t[k,j−1] are meant; corrected here.)

Q139. A convex polygon has nn vertices. Prove that every triangulation has n−2n-2 triangles and n−3n-3 chords, and relate it to the dual tree. (8 marks)

A convex polygon with nn vertices has interior angle sum (n−2)π(n-2)\pi. Each triangle of a triangulation contributes π\pi, and the triangle angles together fill exactly the polygon's angles (chords do not cross, so no extra vertices appear), hence the number of triangles is (n−2)ππ=n−2\dfrac{(n-2)\pi}{\pi}=n-2.

Counting sides: the n−2n-2 triangles have 3(n−2)3(n-2) sides in total. Each of the nn polygon sides belongs to one triangle and each chord to two, so 3(n−2)=n+2c3(n-2)=n+2c, giving c=2n−62=n−3c=\dfrac{2n-6}{2}=n-3 chords.

Dual tree. Take one node per triangle and join two nodes when the triangles share a chord. The dual graph is a tree with n−2n-2 nodes and n−3n-3 edges (one per chord). Choosing the triangle on one polygon side as the root gives a binary tree in which each node has at most two children; the nn polygon sides hang off as external edges, one of which is the root side, so n−1n-1 sides appear as leaves.

v0v1v2v3v4v5v6convex polygon, n = 7: 5 triangles, 4 chordsdual tree (root = triangle on side v0v6)T1T2T3T4T5v0v1v5v6v1v2v4v5v2v3,v3v4

Part 3: Short Notes & Key Terms

Big-O notation O(g(n))
f(n)=O(g(n))f(n)=O(g(n)) if there are constants c>0c>0 and n0n_0 such that 0≤f(n)≤cg(n)0\le f(n)\le c\,g(n) for all n≥n0n\ge n_0 (an asymptotic upper bound).
Big-Omega notation Ω(g(n))
f(n)=Ω(g(n))f(n)=\Omega(g(n)) if there are constants c>0c>0 and n0n_0 such that 0≤cg(n)≤f(n)0\le c\,g(n)\le f(n) for all n≥n0n\ge n_0 (an asymptotic lower bound).
Big-Theta notation Θ(g(n))
f(n)=Θ(g(n))f(n)=\Theta(g(n)) if there are constants c1,c2>0c_1,c_2>0 and n0n_0 such that c1g(n)≤f(n)≤c2g(n)c_1g(n)\le f(n)\le c_2g(n) for all n≥n0n\ge n_0; equivalently f=O(g)f=O(g) and f=Ω(g)f=\Omega(g) (a tight bound).
Little-o and little-omega
f=o(g)f=o(g) means limn→∞f(n)/g(n)=0\lim_{n\to\infty}f(n)/g(n)=0; f=ω(g)f=\omega(g) means limn→∞f(n)/g(n)=∞\lim_{n\to\infty}f(n)/g(n)=\infty. Also f=o(g)⇔g=ω(f)f=o(g)\iff g=\omega(f).
Transitivity and symmetry
O,Ω,Θ,o,ωO,\Omega,\Theta,o,\omega are transitive; Θ\Theta is symmetric (f=Θ(g)⇔g=Θ(f)f=\Theta(g)\iff g=\Theta(f)); OO and Ω\Omega are transpose-symmetric (f=O(g)⇔g=Ω(f)f=O(g)\iff g=\Omega(f)).
Logarithm identities
loga(xy)=logax+logay\log_a(xy)=\log_ax+\log_ay, logaxk=klogax\log_ax^{k}=k\log_ax, logab=logcblogca\log_ab=\dfrac{\log_cb}{\log_ca}, alogbc=clogbaa^{\log_bc}=c^{\log_ba}.
Factors affecting algorithm design
Correctness, running time (time complexity), memory (space complexity), simplicity, and the nature of the input (size, order, distribution).
Recursion
Merits: short, natural for recursive structures (trees, divide and conquer). Demerits: extra stack space and call overhead, possible repeated computation of the same sub-problem.
Principle of mathematical induction
To prove P(n)P(n) for all n≥n0n\ge n_0: (1) basis, prove P(n0)P(n_0); (2) induction step, assume P(k)P(k) and prove P(k+1)P(k+1).
Strong induction
Assume P(n0),…,P(k)P(n_0),\dots,P(k) all hold and prove P(k+1)P(k+1); useful when P(k+1)P(k+1) depends on several earlier cases (e.g. every integer >1>1 has a prime factor).
Recurrence relation
An equation that defines a sequence by earlier terms, e.g. T(n)=2T(n/2)+nT(n)=2T(n/2)+n. Solved by iteration, recursion tree, substitution or the characteristic equation.
Characteristic equation
For an=c1an−1+c2an−2a_n=c_1a_{n-1}+c_2a_{n-2} solve x2=c1x+c2x^2=c_1x+c_2. Distinct roots r1,r2r_1,r_2: an=αr1n+βr2na_n=\alpha r_1^{n}+\beta r_2^{n}; repeated root rr: an=(α+βn)rna_n=(\alpha+\beta n)r^{n}.
Master-type results used
T(n)=2T(n/2)+n=Θ(nlogn)T(n)=2T(n/2)+n=\Theta(n\log n); T(n)=T(n−1)+c=Θ(n)T(n)=T(n-1)+c=\Theta(n); Tower of Hanoi T(n)=2T(n−1)+1=2n−1T(n)=2T(n-1)+1=2^{n}-1.
Divide and conquer
Divide the problem into independent sub-problems, conquer them recursively, then combine: T(n)=2T(n/2)+S(n)+M(n)T(n)=2T(n/2)+S(n)+M(n). Examples: merge sort, maximal points, closest pair.
Maximal point
A point PiP_i is maximal if no other point has both coordinates greater than or equal to it (is not dominated).
Dynamic programming
Technique for problems with optimal substructure and overlapping sub-problems: solve each sub-problem once, store it in a table, build the answer bottom-up.
Matrix-chain multiplication
m[i,j]=mini≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j]=\min_{i\le k<j}\{m[i,k]+m[k+1,j]+p_{i-1}p_kp_j\}; time Θ(n3)\Theta(n^3), space Θ(n2)\Theta(n^2). Number of parenthesisations is the Catalan number C(n−1)C(n-1).
0-1 knapsack
V[i,w]=max{V[i−1,w],vi+V[i−1,w−wi]}V[i,w]=\max\{V[i-1,w],\,v_i+V[i-1,w-w_i]\}; time Θ(nW)\Theta(nW) (pseudo-polynomial). Brute force is O(n2n)O(n2^{n}).
Longest common subsequence
c[i,j]=c[i−1,j−1]+1c[i,j]=c[i-1,j-1]+1 if xi=yjx_i=y_j, otherwise max{c[i−1,j],c[i,j−1]}\max\{c[i-1,j],c[i,j-1]\}; time Θ(mn)\Theta(mn).
Assembly-line scheduling
fi[j]=ai,j+min{fi[j−1],fi′[j−1]+ti′,j−1}f_i[j]=a_{i,j}+\min\{f_i[j-1],\,f_{i'}[j-1]+t_{i',j-1}\}; time Θ(n)\Theta(n) versus 2n2^{n} routes for brute force.
Convex polygon triangulation
Every triangulation of a convex nn-gon has n−2n-2 triangles and n−3n-3 chords; the dual graph is a tree.
Fibonacci numbers
F0=0,F1=1,Fn=Fn−1+Fn−2F_0=0,\ F_1=1,\ F_n=F_{n-1}+F_{n-2}; closed form Fn=φn−φ̂n5F_n=\dfrac{\varphi^{n}-\hat\varphi^{n}}{\sqrt5} with φ=1+52\varphi=\dfrac{1+\sqrt5}{2}.
Euclid's algorithm
gcd(a,b)=gcd(b,amodb)\gcd(a,b)=\gcd(b,\,a\bmod b), with gcd(a,0)=a\gcd(a,0)=a; runs in O(logmin(a,b))O(\log\min(a,b)) steps.

No comments:

Post a Comment

Please give us your feedback & help us to improve this site.