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 .
Let and . By the definition of a logarithm,
Substitute in the second equation:
Therefore .
Q2. Prove that .
Take of both sides (the logarithm is one-to-one, so the two sides are equal exactly when their logarithms are equal).
Both are the same number, so .
Q3. Prove that .
Let and . We need constants and with for all .
For we have and (in general, if then whenever ). Hence
So the definition of Big-O holds with and , i.e. .
Q4. Prove that .
For : and . Therefore
so with , .
Q5. Prove that .
Let and . We look for and such that :
Take and (or and ). Then for all , so .
Q6. Prove that (transpose symmetry). (5 marks)
() Since , there exist a constant and such that
Divide both sides by :
Put . Then for all , which is exactly the definition of .
() Since , there exist and with for all . Dividing by gives ; with this says .
Q7. Prove that . (5 marks)
Recall: means that for every constant there is an with for all . Similarly means that for every there is an with for all .
() Let be arbitrary. Apply the hypothesis with the constant : there is an such that for all . Multiplying by gives for all . As was arbitrary, .
() Let be arbitrary. Apply with the constant : there is an with for all , i.e. . Hence .
Q8. Prove that (little omega).
Let and . We must show that for any constant there is an such that for all .
So for any we may take . For example, for take ; the inequality then holds for every . Hence .
Q9. Prove that .
Let and . To show it is enough to find one constant for which no works.
Take . Then would mean
which is false for every positive integer . So no exists for , and therefore .
Q10. Prove that .
Let and . Choose the constant . Then the requirement becomes
which is false for every . So no exists for this , hence .
Note: the shared solution proved the related statement : assuming gives , which fails for and every . That statement is also true, and both proofs are shown here.
Q11. Prove that .
Let and . We need for all :
Take and . Then for all , so .
Q12. Prove that .
Let and . For we have , and . Therefore
By the definition of Big-Omega (with and ), .
Q13. Prove that .
Let and . Assume, for contradiction, that . Then there are constants and such that
Dividing by gives , and for this gives , i.e.
This cannot hold for arbitrarily large (take any ). Contradiction. Hence .
Q14. Prove that .
Let and . For we have and all other terms are positive, so
Thus with and .
Q15. Prove that .
Let and . We need with for all .
Upper bound. For , , so works.
Lower bound. For we have (because ), so
so works.
Hence for all , i.e. , , , and .
Q16. Prove that , where are constants and .
Let and . Choose
For we have , so , and , so . Then
So for all , and every degree-2 polynomial with is .
Q17. Prove that (i.e. ).
Note: the question is printed as "", but that claim is false; the correct statement to prove is that the function is not (it is ).
Let and . Suppose, on the contrary, that . Then there exist positive constants with
Dividing by gives
For the middle term is at most , so , i.e. for all . That is impossible because can be made as large as we like. Contradiction, hence .
Q18. Prove that .
Let and . Then means , i.e. . Take and . Hence .
Q19. Prove that .
Let and . Take :
which is true for all . So for all , and .
(Alternatively, and also works, because for .)
Q20. Prove by contradiction that .
Note: some sources print this as "prove ", but the true statement is .
Assume is . Then there are positive constants and such that
Dividing by (which is non-zero) gives for all . But this fails for every , a contradiction. Therefore .
Q21. Prove that . (5 marks)
Let and . We need with for all .
Method 1 (testing values). Try :
| ? | |||
|---|---|---|---|
| 1 | 15 | 1 | no |
| 2 | 32 | 16 | no |
| 3 | 73 | 81 | yes |
For the inequality keeps holding (the right side grows faster), so and work.
Method 2 (bounding each term). For : , and , so with and .
Hence .
Q22. Prove that .
Let and . Take and look for with :
Check: for , . So for all , i.e. , . Hence .
Q23. Prove that . (5 marks)
Let and . We must find positive constants such that
Lower bound. Take : holds for every (we only drop positive terms).
Upper bound. Take . Then
Testing values: for , (fails); for , (holds), and it keeps holding for larger . Direct check at :
Hence for all , so , , and .
Note: some shared solutions print the sum as ; the correct value is (the conclusion is unchanged).
Q24. Prove that . (5 marks)
For all we have and . Therefore
So , , satisfy the definition of , and .
Q25. Prove that . (5 marks)
Upper bound. For , , so .
Lower bound. Take . Then
which is true for every integer .
Hence for all , i.e. , , , and .
Q26. Show that . (5 marks)
Reading of the question: the printed text is garbled (" OR "); the bracketed form is the intended function.
For we have . Therefore
So , , , and .
Q27. Prove that . (5 marks)
Upper bound. For , , so ; take .
Lower bound. Take :
which is true for all (at it equals , and the left side is increasing for ).
Therefore for all ; , , .
Q28. Prove that (symmetry of ). (5 marks)
() Since , there exist constants and such that
From the right half of (1): , so . From the left half of (1): , so .
Hence, with and ,
which says .
() Apply the same argument with the roles of and exchanged.
Q29. Show that is . (5 marks)
For : and . So
Thus and work, and . (Equivalently .)
Q30. Show that is . (5 marks)
For we have . Hence
so and satisfy , and .
Q31. Show that is . (5 marks)
For we have , so
Therefore , , and .
Q32. Show that is . (5 marks)
The sum is . For ,
So and work, and . (In fact .)
Q33. Prove that the statement "" is incorrect. (5 marks)
Suppose, for contradiction, that . Then in particular : there are constants and with
But , so for any (taking ) we get — a contradiction. Hence . (It is but not .)
Q34. Prove that . (5 marks)
For every we have . Multiplying both sides by :
So , , and .
Q35. Prove that . (5 marks)
For : and . Hence
so , and .
Q36. Use logical equivalences to show that is a contradiction. (5 marks)
The formula is equivalent to , so it is false for every truth assignment, i.e. it is a contradiction.
Q37. Prove by contradiction that 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
From being true, is true and is true. A true implication with a true hypothesis forces its conclusion to be true, so is true. This contradicts " is false". Hence no such assignment exists and is a tautology.
Q38. Show that is a tautology (hypothetical syllogism). (5 marks)
By logical equivalences.
A shorter argument: suppose the hypothesis is true and is true. From we get ; from we get . So is true whenever the hypothesis is true, and the implication can never be false.
By truth table.
| 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.
Q39. Show that is a tautology (proof by cases). (5 marks)
Suppose the hypothesis is true. Because is true, at least one of is true.
- If is true, then gives .
- If is true, then gives .
In both cases is true, so the implication is never false. A truth table confirms this:
| 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.
Q40. Show that is not a tautology. (5 marks)
A formula is not a tautology if some assignment makes it false. Try , :
- and , so the hypothesis ;
- the conclusion .
A true hypothesis with a false conclusion makes the implication false.
| 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 gives , so the formula is not a tautology (this is the fallacy of denying the hypothesis).
Q41. Prove that for all integers and and any non-negative integer , . (5 marks)
If both sides are , so the statement is trivial.
Let . The gcd of two integers is the smallest positive element of the set of their integer linear combinations. Hence
The last step holds because is the smallest positive element of the combinations of and , which is .
Note: the shared solution ends with "", which is a typo for .
Q42. What is a linear congruence ? How is it solved? (5 marks)
A congruence of the form is called a linear congruence. To solve it we must find all integers that satisfy it.
- An inverse of modulo is an integer such that . It exists exactly when .
- If such exists, multiply both sides by it:
Example (added for illustration). Solve . Since , the inverse of is , so . Check: . ✓
Q43. Use mathematical induction to prove that for all . (5 marks)
Let be the statement .
Basis step (). , which is true.
Inductive step. Assume for some , i.e. . We must show . Using the hypothesis,
because . So . By induction holds for all .
Note: a shared solution concludes with "", which is not a valid conclusion; the correct step is as shown above.
Q44. Prove that every integer can be written as a product of primes. (5 marks)
We use strong induction on .
Basis. is prime, so it is a product of one prime.
Inductive step. Let and assume every integer with is a product of primes.
- If is prime, it is a product of one prime.
- If is composite, then with . By the inductive hypothesis and are each products of primes, so is a product of primes (the concatenation of the two products).
By strong induction every integer is a product of primes.
Q45. Prove the totient property , where and are distinct prime numbers. (5 marks)
For a prime , every number is relatively prime to , so (for example ). Likewise .
Now let . Consider the residues — there are of them. The ones not relatively prime to are the multiples of or of :
These three sets do not overlap (a common multiple of and below would be only). Hence
Q46. Prove: for any integer and any prime , if divides then does not divide . (5 marks)
Suppose, for contradiction, that and . Then divides their difference:
But the only positive divisor of is , and a prime is greater than . This is a contradiction. Hence if then .
Q47. Prove that the square of an odd integer has the form for some integer . (5 marks)
By the quotient–remainder theorem every integer has one of the forms . An odd integer must be or .
Case 1: .
Case 2: .
In both cases the square has the form .
Q48. Use mathematical induction to prove that for all . (5 marks)
Let : .
Basis. : , true.
Inductive step. Assume for some . Then
using . So holds, and for all positive integers .
Q49. Prove that any integer is divisible by a prime. (5 marks)
We use strong induction.
Basis. : and is prime.
Inductive step. Assume every integer with is divisible by some prime (inductive hypothesis). We show it for .
- Case 1: is prime. Then and itself is the prime divisor.
- Case 2: is composite. Then with and . By the hypothesis some prime divides (); also . By transitivity of divisibility, .
Hence is true for all by strong induction.
Q50. Prove Euclid's rule: for positive integers , . (Equivalently .) (5 marks)
It is enough to prove the simpler rule , because is obtained by subtracting from repeatedly.
- Any integer that divides both and also divides . So every common divisor of is a common divisor of , giving .
- Likewise, any integer that divides both and also divides , so it is a common divisor of , giving .
Therefore , and repeating the subtraction gives .
Q51. Find all sequences of the form () that satisfy for all . (5 marks)
Put into the relation: . Dividing by gives the characteristic equation
So the sequences
are both particular solutions of the recurrence.
Q52. Prove that any linear combination of solutions of the homogeneous linear recurrence is also a solution. (5 marks)
Let and be two solutions, so for every
For constants put . Then
So is again a solution. By repeating the argument, any finite linear combination of solutions is a solution.
Q53. Solve the first-order recurrence with . Hence solve with . (5 marks)
Expand repeatedly:
So the explicit formula is — a geometric sequence. A first-order homogeneous recurrence is simply a geometric sequence.
For and : (so ).
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 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 disks: move the top disks to the spare tower ( moves), move the largest disk (1 move), then move the disks on top of it ( moves):
Solution. Expanding, , so
(Equivalently, the characteristic polynomial gives , and give .)
Q55. Find a general formula for the Fibonacci sequence (with , ). (5 marks)
The Fibonacci sequence is defined by with , giving
Characteristic equation. , with roots
General solution . From : . From : . Since , we get , :
(Binet's formula.) For example . As , , the golden ratio.
Q56. Prove/solve (with given), solving the recurrence as printed. (10 marks)
Reading: the recurrence is solved exactly as printed, with constant .
At : . So decreases linearly: . This is the same as the previous recurrence with replaced by . Such a recurrence cannot describe the running time of an algorithm for large (the "time" would eventually become negative); with it gives .
Q57. Solve the recurrence . (10 marks)
Reading: the question is printed as "solve the recurrence " (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: , i.e. , roots and . Hence
With and given: , , so and . The sequence just alternates: .
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
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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;
- (the golden ratio) as ;
- is the number of ways dominoes can cover a board;
- ; ; 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 in three steps:
- Divide — split the problem into sub-problems that are smaller instances of the same problem (and independent of each other). Cost .
- Conquer — solve each sub-problem recursively; when a sub-problem is small enough (size ) solve it directly in constant time .
- Combine — merge the sub-solutions into the solution of the original problem. Cost .
Time complexity. If the problem is split into two halves,
where is the time for splitting, the time for merging, and are constants. For example, merge sort has and , so ; the maxima algorithm below has , 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 is maximal if no other point satisfies and (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 points, each comparison taking constant time: .
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 to " in the last loop; it should use the same bounds as the rest of the procedure.)
Analysis. MERGE takes linear time, so . By the recursion-tree or master method (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 , so each unordered pair is examined once ( 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.
(The unimproved version gives 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: .
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 .
| line | work | times executed |
|---|---|---|
| 2 | outer-loop test and increment (cost ) | times (the body runs times) |
| 3 | inner-loop test and increment | |
| 4 | comparison | |
| 5 | swap | at most (every comparison swaps in the worst case) |
The first pass through the inner loop makes comparisons and swaps, the next , …, and the last pass makes one. If is the time to do one comparison, one swap, one inner-loop test and one increment, and the constant time for declaring and initialising and :
Q65. How many ways can a chain of matrices be fully parenthesised? Why is brute force not practical? (5 marks)
Let be the number of full parenthesisations of matrices. A single matrix has one way. For the last multiplication can split the chain after any matrix , giving
This is the Catalan recurrence, so where
The sequence is and grows exponentially (about ). Trying every parenthesisation and computing its cost is therefore exponential, which is why we use dynamic programming ().
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 many times (overlapping sub-problems). Its running time satisfies , which gives . Storing each the first time it is computed (memoisation) reduces the work to the distinct sub-problems times each, i.e. .
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 gives two smaller polygons, but the same sub-polygons (sets of consecutive vertices ) 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 the minimum weight of triangulating the polygon :
which is the same recurrence as matrix-chain multiplication; there are only distinct sub-polygons, so the answer is found in time.
Q69. What are the steps of dynamic programming, and what are its limitations? (5 marks)
Steps
- Characterise the structure of an optimal solution (optimal substructure).
- Recursively define the value of an optimal solution.
- Compute the value bottom-up, storing results in a table.
- 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 or .
- The knapsack algorithm runs in , which is pseudo-polynomial: it is exponential in the number of bits needed to write .
- A table entry may depend on many others, so the order of filling must be chosen carefully.
Q70. Brute-force the knapsack instance: capacity , items (weight, value) = (4, 40), (10, 60), (20, 100), (10, 20). (6 marks)
Check every subset (weight, value):
| Subset | Weight | Value | Feasible ()? |
|---|---|---|---|
| ∅ | 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 taken. A greedy strategy is optimal: take items in decreasing order of value per unit weight .
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 (sorting). The greedy method is not optimal for 0-1 knapsack, which needs dynamic programming ().
Q72. What is the brute-force cost of the two-line assembly problem? (3 marks)
Each of the stations can be done on line 1 or on line 2, so there are possible routes. Evaluating one route takes additions, so brute force costs , compared with 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)
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 is the input size and are constants.
, , , , , , , , , , , , , , , , , , , (10 marks)
First simplify each function to a standard form:
| function | simplified form |
|---|---|
| (because ) | |
Growth classes, from fastest to slowest: factorial/super-exponential exponential (larger base wins) polynomial (larger exponent wins) constants.
| rank | function | reason |
|---|---|---|
| 1 | grows faster than (Stirling: ) | |
| 2 | factorial divided by a constant | |
| 3 | eventually , so dividing by gives a smaller value than dividing by | |
| 4 | grows faster than , so this is smaller than rank 3 | |
| 5 | exponential with the larger base | |
| 6 | base ; grows without bound | |
| 7 | base , with polynomial factor | |
| 8 | times | |
| 9 | alone | |
| 10 | polynomial of degree | |
| 11 | degree , smaller than because | |
| 12 | degree | |
| 13 | degree | |
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 | ||
| 19 | constant (larger) | |
| 20 | constant (smaller) |
Assumption: ranks 10–12 assume (that is, ), which is what makes larger than ; for the functions at ranks 11 and 12 would swap. The standard answer key uses the order shown above.
Q75. Prove that by (a) logical equivalence, (b) rules of inference, (c) contradiction. (10 marks)
(a) Logical equivalence.
(b) Rules of inference (contraposition + modus ponens). The contrapositive of the statement is
Suppose is true. Then is true and is true, so by modus ponens is true. Thus holds, and so does the original statement (a statement and its contrapositive are equivalent).
(c) Contradiction. Assume is true but is also true (the negation of the conclusion). Then
This is a contradiction, so must be true whenever is.
Q76. Prove that by (1) logical equivalences, (2) truth table, (3) rules of inference, (4) contradiction. (10 marks)
1. Logical equivalences.
2. Truth table.
| 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: . If is true, then and are true, so is true by modus ponens. Hence the contrapositive, and therefore the original statement, is true.
4. Contradiction. Suppose is true and is true. Then , a contradiction.
Q77. Show that is a tautology. (10 marks)
By logical equivalences.
This rule is called resolution.
By truth table.
| 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.
Q78. Prove, using propositional equivalences, that each pair is equivalent: (a) ; (b) . (10 marks)
(a)
(b)
Q79. Prove that each of the following is a tautology by showing it is equivalent to : (a) ; (b) ; (c) . (10 marks)
(a) (see also hypothetical syllogism above)
(b)
(c)
Q80. Construct truth tables for (a) and (b) . (10 marks)
(a)
| 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) ( is exclusive-or: true exactly when the two inputs differ.)
| 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 (both sides are false there); in every other row exactly one side is true.
Q81. Are the following compound propositions tautologies? (a) ; (b) ; (c) . In other words, are implication, conjunction and exclusive-or transitive? (10 marks)
(a) Yes — it is hypothetical syllogism (shown above), so is transitive.
(b) If is true, then , and are all true, so is true. Hence it is a tautology and is transitive.
(c) No. Take , , : then and , so the hypothesis is true, but . The implication is false.
| 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 are both true and false, or both false and true. Therefore and are transitive, but is not.
Q82. Let be primitive propositions. Determine whether and are logically equivalent. (10 marks)
Two formulas are logically equivalent when is a tautology (their columns are identical).
| 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:
Q83. Let be the set of natural numbers. The symbols (less than), (less than or equal) and (equal) are relations over . Prove or disprove: (1) is reflexive, symmetric and transitive; (2) is reflexive, symmetric and transitive; (3) is reflexive, symmetric and transitive. (10 marks)
Definitions. A relation on a set is
- reflexive if for all ;
- symmetric if for all ;
- transitive if and for all .
| relation | reflexive | symmetric | transitive |
|---|---|---|---|
| No, because is never true. | No: but is false. | Yes: and . | |
| Yes, because always holds. | No: but is false. | Yes: and . | |
| Yes, . | Yes: . | Yes: and . |
Conclusion.
- " is reflexive, symmetric and transitive" is false (it is only transitive).
- " is reflexive, symmetric and transitive" is false (it is reflexive and transitive but not symmetric; it is a partial order).
- " is reflexive, symmetric and transitive" is true — equality is an equivalence relation.
Q84. Prove that every positive integer has a unique binary representation
where is an integer, , and for . (10 marks)
Existence (strong induction on ).
Basis: (so , ).
Step: let and assume every positive integer smaller than has such a representation. Divide by : with and . By the hypothesis with , . Then
which is the required form.
Uniqueness. Suppose has two representations. Reduce both modulo : every term except is even, so — the last digit is determined by . Subtract and divide by ; both representations give representations of the same number , which are equal by the inductive hypothesis (uniqueness holds for smaller numbers). Therefore all digits agree and the representation is unique.
Q85. Use mathematical induction to prove that the sum of the first positive integers is . (10 marks)
Let denote .
Basis step. : , true.
Inductive step. Assume for some positive integer :
Then
This is . By induction, is true for all .
Q86. Show that in any base , the sum of any three single-digit numbers is at most two digits long. (10 marks)
Solution 1 (direct). The largest digit in base is , so the sum of three digits is at most . The largest two-digit number in base is . It is enough to show :
So the sum is never larger than , i.e. it has at most two digits.
Solution 2 (induction on the base ). For the largest sum is
a two-digit number whose leading digit is and whose last digit is .
Basis (): (two digits). (): (two digits).
Inductive step: assume the claim holds in base , i.e. is the two-digit number . In base the largest digit is , so the largest possible sum is
which is the two-digit number (its last digit because ). Hence the claim holds for base whenever it holds for base , and so for all bases ; base was checked directly.
Q87. Use mathematical induction to prove that the sum of the first odd positive integers is , i.e. . (10 marks)
Let : .
Basis step. : , true.
Inductive step. Assume : . Then
which is . By induction holds for all positive integers .
Q88. Use mathematical induction to prove that the sum of the first even positive integers is , i.e. . (10 marks)
Let : .
Basis step. : , true.
Inductive step. Assume : . Then
This is , so is true for all positive integers .
Q89. Deduce the result "sum of the first even integers is " from "sum of the first odd integers is ", and vice versa. (5 marks)
Even from odd. Adding to each of the first odd numbers gives the first even numbers:
Odd from even. Subtracting from each of the first even numbers gives the first odd numbers:
Q90. Show by mathematical induction that any amount of cents can be obtained using only 3-cent and 5-cent coins. (10 marks)
Let : " cents can be formed using only 3-cent and 5-cent coins".
Basis (three consecutive values).
Inductive step (strong induction). Let and assume . Since , we have , and , so holds: cents can be formed. Add one more 3-cent coin: is obtainable.
By strong induction holds for every .
Check: cents is the largest amount that cannot be formed (Frobenius number ).
Q91. Prove that postage of cents can be formed using only 4-cent and 5-cent stamps. (10 marks)
Let : " cents can be formed with 4-cent and 5-cent stamps".
Basis.
Inductive step (strong induction). Let and assume . Then and , so cents can be formed. Add one 4-cent stamp to get cents.
Hence is true for all .
Check: cents cannot be formed ( is the largest impossible amount).
Q92. Show by mathematical induction that any amount of cents can be obtained using only 4-cent and 7-cent coins. (10 marks)
Let : " for some integers ".
Basis (four consecutive values).
(Also , , .)
Inductive step (strong induction). Let and assume . Then and , so for some . Adding one 4-cent coin:
So is true, and by strong induction every can be formed.
Check: is the largest impossible amount ().
Q93. Show by mathematical induction that any amount of cents can be obtained using only 5-cent and 6-cent coins. (10 marks)
Let : " for some integers ".
Basis.
Inductive step (strong induction). Let and assume . Then and , so for some . Hence
and is true. By strong induction holds for all .
Check: the largest impossible amount is , so 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 and with , every amount can be formed. For this gives . (The largest impossible amount is .)
Claim. Every can be written as with .
Basis. , , .
Inductive step (strong induction). Let and assume the claim for all amounts from to . Then and , so ; therefore .
Q95. Use mathematical induction to show that when is an exact power of , the solution of the recurrence
is . (10 marks)
Basis (). and . ✓
Inductive step. Let , , and assume the formula for : . Then
This completes the inductive proof for exact powers of .
Q96. Use mathematical induction to prove that . (10 marks)
Let : .
Basis ( or ). For : left side ; right side . ✓
Inductive step. Assume : . Then
which is . By induction the formula holds for all .
Q97. Find a sequence satisfying for all together with (i) ; and (ii) . (10 marks)
The characteristic equation is , i.e. , with roots and . The general solution is
(i) .
Check: , , . ✓
(ii) .
Check: , , . ✓
Q98. Suppose satisfies for all with , . Find an explicit formula using the characteristic equation. (10 marks)
Characteristic equation. , so is a repeated root.
Both and satisfy the recurrence, so the general solution is
Initial conditions.
Check: , , ✓, ✓.
Q99. Suppose for all with and . Find an explicit formula using the characteristic equation. (10 marks)
Characteristic equation. , so is a repeated root. (Some shared solutions write ""; it is a typo — the root is .)
General solution: .
Terms: , , , . Check: ✓.
Q100.
(15 marks)
(a) The characteristic equation is , so and .
Check: ✓.
(b) Reading: the right side is (the "" in the source means ).
The method (handout, slide 33 of lecture 08): the left side gives the factor . A right-hand term with of degree contributes the factor .
- : , degree , factor ;
- : , degree , factor .
Characteristic polynomial: , so
Correction note: the handout answer lists only , i.e. . That would be right if the right side were the constant (instead of ); for the factor must be squared.
Determining the constants. Substituting a particular solution gives , , , so , , and the full solution of the recurrence is
with one free constant fixed by an initial value.
Q101. Consider the recurrence for and otherwise. Find the general solution. (10 marks)
Rewrite: . The characteristic equation is
Test : , so is a root and . The roots are , so
Initial conditions ():
Check: and the formula gives ✓.
Q102. Solve the recurrence (with ). (10 marks)
Rewrite as and compare with :
- (degree );
- (degree ).
The characteristic polynomial is , with roots and , each of multiplicity . Hence
Substituting into the recurrence gives , , (matching the right side ); then gives . Therefore
Check: ✓; ✓.
Q103. Find the solution of . (10 marks)
Here and (degree ). Reduce to a homogeneous recurrence: replace by and multiply by :
Subtract this from :
The characteristic equation has roots and , so
Remark. Not every choice of solves the original recurrence: substituting back shows that must equal , so the solutions of are , with fixed by an initial value.
Q104. Find the general solution of , . (10 marks)
Eliminating the right-hand side.
The term has and degree , so the factor is needed. Eliminate the right side with (the coefficients come from ):
since . This gives the homogeneous recurrence
Characteristic equation.
Correction note: the shared solution eliminates with the wrong multipliers and obtains the roots and the form ; that is incorrect. The roots must be (from the left side) and twice (from ). Substituting a particular solution gives , so the actual solutions of the recurrence are .
Q105. Consider the recurrence . Find its general solution. (10 marks)
Write the equation for , , (all rewritten with ):
Multiply (5) by and (6) by (the coefficients of ) and add to (4):
Characteristic equation.
roots . Hence
(Substituting a particular solution shows the actual solutions are .)
Q106. Find the solution of the recurrence with and . (10 marks)
Characteristic equation. , so
Two distinct complex roots , , so .
Initial conditions.
Solving: and . Hence
Because , this simplifies to
Remark. The sequence is obviously real, although the formula involves complex numbers. In real form, with :
Check: , and the formula agrees.
Correction note: the shared solution prints the exponent as in the last line; with as above the correct exponent is (at the printed form would give instead of ).
Q107. The sequence is defined by with . Show that can be expressed in terms of Fibonacci and Lucas numbers. (15 marks)
Recall the Fibonacci numbers and the Lucas numbers , with .
Claim.
Lemma. For all :
Proof of the lemma. First, two identities that follow from (or from the Binet formulas):
(For (3): . For (4): and , which are equal.)
Then
Proof of the claim (induction). Basis: . ✓
Step: assume . Then
using the lemma.
Check: , , , . ✓
Q108. Solve the recurrence using the substitution method (with ). (10 marks)
Repeated substitution: instantiate the recurrence with smaller and smaller arguments and look for a pattern.
The pattern: after substitutions the argument is and the added cost is . The process stops at , i.e. when , :
So , which is . (Note: the handout line "" is a typo for .)
Q109. Solve the recurrence using the substitution method. (10 marks)
Assume . Substitute repeatedly:
With we have and the geometric sum with :
(because ). So . Using and :
so . (Check: and the formula gives ✓.)
Q110. Solve the recurrence using the substitution method. (10 marks)
Let and substitute repeatedly:
With , and , :
Since , and :
hence . (Check: and ✓; ✓.)
Q111. Solve the recurrence using (i) the substitution method and (ii) the recursion-tree method. (15 marks)
(i) Substitution (assume ). Exactly as in the two examples above:
using . With :
(ii) Recursion tree.
- The cost at the top level is and four subtrees, each of cost , are expanded.
- Level has nodes, each of size , so the cost of level is .
- A subproblem reaches when , i.e. , so the tree has levels .
- The last level has nodes, each costing , contributing .
Since , this is in particular (the weaker bound printed in the handout), and the exact bound is .
Q112. Solve the recurrence using the recursion-tree method. (15 marks)
- The root costs and has three children . Each of those costs and has three children , and so on.
- The subproblem size at depth is ; it reaches when , so the tree has levels.
- Each level has three times as many nodes as the level above: depth has nodes, so its total cost is
- The last level (depth ) has nodes, each of cost , contributing .
Total cost:
The infinite geometric series equals , so
Since , the term dominates and . In fact the exact solution for is , so .
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 is immature until month ; from month 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.)
The number of pairs at month is: , , , , , , .
(2) Recursive model.
- Total pairs at level = total pairs at level + total pairs born at level . (1)
- Pairs born at level = (total pairs at level ), because every pair that exists at month is at least four months old at month and produces two pairs each month. (2)
From (1) and (2), with = total pairs at level :
(3) Pairs after one year (12 months).
Hence the number of pairs of rabbits after one year is 57.
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 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 = pairs at level + pairs born at level .
- is the number of pairs at level , so pairs were born at level , i.e. are exactly two months old at level and produce two pairs each.
- The pairs that are at least three months old produce one pair each.
- Pairs born at level .
(iii) Values.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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.
Q115. Below is a set of 9 points in 2-D. Compute the set of maximal points using the brute-force approach: . (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 | points that dominate it | maximal? |
|---|---|---|
| (4,16), (8,8), (8,20), (14,12), (16,16), (22,8) | no | |
| (8,20), (16,16) | no | |
| (8,20), (14,12), (16,16), (22,8) | no | |
| none | yes | |
| (14,12), (16,16), (22,8) | no | |
| (16,16) | no | |
| none | yes | |
| (22,8) | no | |
| none | yes |
Set of maximal points , , .
Q116. Compute the set of maximal points, using the brute-force approach, of the points . (10 marks)
Using the same MAXIMAL-POINTS procedure:
| point | points that dominate it | maximal? |
|---|---|---|
| (6,16), (8,8), (10,20), (16,14), (18,18), (24,8) | no | |
| (10,20), (18,18) | no | |
| (10,20), (16,14), (18,18), (24,8) | no | |
| none | yes | |
| (16,14), (18,18), (24,8) | no | |
| (18,18) | no | |
| none | yes | |
| (24,8) | no | |
| none | yes |
Set of maximal points , , .
Q117. Write the divide-and-conquer algorithm for the maxima-finding problem and find its time complexity. (10 marks)
Input: a set of 2-dimensional points. Output: the maximal set of .
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 and combining (steps 4–5) takes , so
Assume and expand ( stands for the two terms):
Q118. Write the pseudo-code of the brute-force maximal-points algorithm for -dimensional points. (10 marks)
For points with coordinates (dimension ), a point is dominated by when every coordinate of is the corresponding coordinate of .
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: — pairs, each compared in .
Q119. Write the pseudo-code of the plane-sweep algorithm for the maxima problem in 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 in all coordinates is removed (it has a smaller first coordinate because of the sorting), and then is pushed.
Note. For this is exactly the classic plane-sweep algorithm and runs in 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 -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 around the line, and after sorting the strip by each point need only be compared with its next neighbours.
Running time. Divide ; combine (strip selection and sorting) :
Each level of the recursion tree costs about , and summing over levels gives , so the total running time is . (If the points are also kept sorted by during the recursion, the combine step becomes and the total improves to .)
Q121. State the matrix-chain multiplication problem and explain how dynamic programming solves it. (10 marks)
Problem. Given a chain of matrices where has dimensions , fully parenthesise the product 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 matrix by a matrix needs scalar multiplications and gives a matrix.
Optimal substructure. Let be the minimum cost of computing . If the last multiplication splits the chain at (), the two sub-chains must themselves be parenthesised optimally, so
The table is filled by increasing chain length ; stores the giving the minimum. The answer is , and is used to print the parenthesisation. There are sub-problems and each takes to evaluate, so the running time is and the space is .
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 times, so ; in fact . The tables and need space.
Q123. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain : , : , : . (10 marks)
Using :
Dimensions , i.e. : , : , : .
Cost table (row , column ):
| 0 | 5000 | 7500 | |
| – | 0 | 25000 | |
| – | – | 0 |
Split table :
| – | 1 | 2 | |
| – | – | 2 | |
| – | – | – |
Minimum number of scalar multiplications: , with optimal parenthesisation .
Check: costs , whereas costs .
Q124. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain : , : , : , : . (10 marks)
Using :
Dimensions , i.e. : , : , : , : .
Cost table (row , column ):
| 0 | 6000 | 7500 | 13250 | |
| – | 0 | 4500 | 8250 | |
| – | – | 0 | 11250 | |
| – | – | – | 0 |
Split table :
| – | 1 | 1 | 1 | |
| – | – | 2 | 3 | |
| – | – | – | 3 | |
| – | – | – | – |
Minimum number of scalar multiplications: , with optimal parenthesisation .
Q125. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain : , : , : , : . (10 marks)
Using :
Dimensions , i.e. : , : , : , : .
Cost table (row , column ):
| 0 | 140 | 308 | 556 | |
| – | 0 | 120 | 352 | |
| – | – | 0 | 192 | |
| – | – | – | 0 |
Split table :
| – | 1 | 2 | 2 | |
| – | – | 2 | 2 | |
| – | – | – | 3 | |
| – | – | – | – |
Minimum number of scalar multiplications: , with optimal parenthesisation .
Q126. Find the optimal parenthesisation (minimum number of scalar multiplications) of the matrix chain : , : , : , : . (10 marks)
Using :
Dimensions , i.e. : , : , : , : .
Cost table (row , column ):
| 0 | 5000 | 7500 | 11000 | |
| – | 0 | 25000 | 15000 | |
| – | – | 0 | 5000 | |
| – | – | – | 0 |
Split table :
| – | 1 | 2 | 2 | |
| – | – | 2 | 2 | |
| – | – | – | 3 | |
| – | – | – | – |
Minimum number of scalar multiplications: , with optimal parenthesisation .
Q127. State the 0-1 knapsack problem and write a brute-force algorithm with its running time. (8 marks)
Problem. Given items, item with value and weight , and a knapsack of capacity , choose a subset of items with total weight and maximum total value. Each item is taken whole or not at all (0-1).
Brute force: examine all 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 subsets and each costs to evaluate, so .
Q128. Write the dynamic-programming algorithm for the 0-1 knapsack problem and give its running time. (8 marks)
Let be the maximum value using only the first items with capacity :
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 time and space. This is pseudo-polynomial because appears numerically, not as its length in bits.
Q129. Solve the 0-1 knapsack problem with capacity and items (weight, value) = (1,1), (2,6), (5,18), (6,22), (7,28) using dynamic programming. (10 marks)
Table computed with the recurrence above:
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |
| 0 | 1 | 6 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | 7 | |
| 0 | 1 | 6 | 7 | 7 | 18 | 19 | 24 | 25 | 25 | 25 | 25 | |
| 0 | 1 | 6 | 7 | 7 | 18 | 22 | 24 | 28 | 29 | 29 | 40 | |
| 0 | 1 | 6 | 7 | 7 | 18 | 22 | 28 | 29 | 34 | 35 | 40 |
The optimal value is . 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 and items (weight, value) = (4,40), (3,30), (5,40), (4,50), (5,70) using dynamic programming. (10 marks)
Table computed with the recurrence above:
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 0 | 0 | 0 | 40 | 40 | 40 | 40 | 40 | 40 | 40 | 40 | 40 | |
| 0 | 0 | 0 | 30 | 40 | 40 | 40 | 70 | 70 | 70 | 70 | 70 | 70 | |
| 0 | 0 | 0 | 30 | 40 | 40 | 40 | 70 | 70 | 80 | 80 | 80 | 110 | |
| 0 | 0 | 0 | 30 | 50 | 50 | 50 | 80 | 90 | 90 | 90 | 120 | 120 | |
| 0 | 0 | 0 | 30 | 50 | 70 | 70 | 80 | 100 | 120 | 120 | 120 | 150 |
The optimal value is . Tracing back with the keep table gives items {2, 4, 5} (total weight 12, total value 150).
Q131. Solve the 0-1 knapsack instance , values , weights 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 and abandoning any branch that would exceed it. Nodes are labelled chosen weights ; total value.
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 is obtained by deleting zero or more elements without changing the order of the rest. A common subsequence of and is a subsequence of both; the LCS is a longest one.
Let be the length of an LCS of the prefixes and :
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 are followed from to print the LCS. Time and space are .
Q133. Find the longest common subsequence of ABDADBAB and DABABDABA using dynamic programming. (10 marks)
Fill with the LCS recurrence (each cell shows the length followed by the direction arrow of ):
| 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 . 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 with the LCS recurrence (each cell shows the length followed by the direction arrow of ):
| 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 . 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 stations (). Station takes time (the stations in the same position do the same job). A chassis enters line with entry time , leaves with exit time , and moving from line after station to the other line costs . Choose the stations on each line so that the total time through the factory is minimum.
Optimal substructure. The fastest way to reach comes either straight from or from the other line's plus the transfer. Let be the fastest time to complete station :
and the optimum is . Let record the line used at station and the line used at station .
Brute force would try all station choices (, or with evaluation); DP fills the table in .
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 .
Q137. Find the fastest way through the factory: , , , , , . (10 marks)
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 8 | 11 | 14 | 20 | 28 | 33 | |
| 4 | 8 | 16 | 21 | 25 | 32 | |
| – | 2 | 1 | 1 | 1 | 2 | |
| – | 2 | 2 | 1 | 2 | 2 |
Final: and , so and .
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: .
The fastest time is 36.
Q138. Extend the assembly-line scheduling idea to lines with stations each. Give the recurrence and the algorithm. (10 marks)
Let be the fastest time to finish station , with the transfer time from line to line after station (it is when ):
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 . Brute force tries every choice of line at every station: routes of length , i.e. . (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 vertices. Prove that every triangulation has triangles and chords, and relate it to the dual tree. (8 marks)
A convex polygon with vertices has interior angle sum . Each triangle of a triangulation contributes , 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 .
Counting sides: the triangles have sides in total. Each of the polygon sides belongs to one triangle and each chord to two, so , giving 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 nodes and 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 polygon sides hang off as external edges, one of which is the root side, so sides appear as leaves.
Part 3: Short Notes & Key Terms
- Big-O notation O(g(n))
- if there are constants and such that for all (an asymptotic upper bound).
- Big-Omega notation Ω(g(n))
- if there are constants and such that for all (an asymptotic lower bound).
- Big-Theta notation Θ(g(n))
- if there are constants and such that for all ; equivalently and (a tight bound).
- Little-o and little-omega
- means ; means . Also .
- Transitivity and symmetry
- are transitive; is symmetric (); and are transpose-symmetric ().
- Logarithm identities
- , , , .
- 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 for all : (1) basis, prove ; (2) induction step, assume and prove .
- Strong induction
- Assume all hold and prove ; useful when depends on several earlier cases (e.g. every integer has a prime factor).
- Recurrence relation
- An equation that defines a sequence by earlier terms, e.g. . Solved by iteration, recursion tree, substitution or the characteristic equation.
- Characteristic equation
- For solve . Distinct roots : ; repeated root : .
- Master-type results used
- ; ; Tower of Hanoi .
- Divide and conquer
- Divide the problem into independent sub-problems, conquer them recursively, then combine: . Examples: merge sort, maximal points, closest pair.
- Maximal point
- A point 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
- ; time , space . Number of parenthesisations is the Catalan number .
- 0-1 knapsack
- ; time (pseudo-polynomial). Brute force is .
- Longest common subsequence
- if , otherwise ; time .
- Assembly-line scheduling
- ; time versus routes for brute force.
- Convex polygon triangulation
- Every triangulation of a convex -gon has triangles and chords; the dual graph is a tree.
- Fibonacci numbers
- ; closed form with .
- Euclid's algorithm
- , with ; runs in steps.
No comments:
Post a Comment
Please give us your feedback & help us to improve this site.