CSC165 ยท 2081 ยท 5 marks
Define subset and power set. How do you prove correctness of recursive algorithm using Induction? Illustrate with an example.
Define subset and power set. How do you prove correctness of recursive algorithm using Induction? Illustrate with an example. [5]
Quick answer
A set A is called a subset of set B if every element of A is also an element of B. This is denoted as: The empty set (โ ) is a subset of every set; Every set is a subset of itself; Base Case.
Subset, Power Set, and Proving Correctness of Recursive Algorithms by Induction
1. Subset
A set A is called a subset of set B if every element of A is also an element of B. This is denoted as:
A โ B if and only if โx (x โ A โ x โ B)
Example: If A = {a, b} and B = {a, b, c}, then A โ B.
- The empty set (โ ) is a subset of every set.
- Every set is a subset of itself.
2. Power Set
The power set of a set S is the set of all subsets of S, including the empty set. It is denoted as P(S).
If |S| = n, then |P(S)| = 2โฟ
Example (from notes): For S = {a, b, c, d}, the power set P(S) contains:
| Subset Size | Subsets |
|---|---|
| 0 elements | {โ } |
| 1 element | {a}, {b}, {c}, {d} |
| 2 elements | {a,b}, {a,c}, {a,d}, {b,c}, {b,d}, {c,d} |
| 3 elements | {a,b,c}, {a,b,d}, {a,c,d}, {b,c,d} |
| 4 elements | {a,b,c,d} |
Total subsets = 1 + 4 + 6 + 4 + 1 = 16 = 2โด โ
3. Proving Correctness of a Recursive Algorithm Using Induction
Mathematical induction is the standard method for proving that a recursive algorithm produces the correct result for all valid inputs.
Steps:
- Base Case: Show the algorithm gives the correct output for the smallest/simplest input.
- Inductive Hypothesis: Assume the algorithm works correctly for all inputs of size less than n (or for input n = k).
- Inductive Step: Show that if the algorithm works for smaller inputs (by the hypothesis), it also works correctly for input of size n (or n = k+1).
4. Illustration: Recursive Factorial Algorithm
Algorithm:
factorial(n):
if n = 0 then
return 1
else
return n * factorial(n - 1)
Claim: factorial(n) correctly computes n! for all n โฅ 0.
Proof by Mathematical Induction:
Base Case (n = 0):
The algorithm returns 1 when n = 0. By definition, 0! = 1. Therefore, the algorithm is correct for n = 0. โ
Inductive Hypothesis:
Assume factorial(k) correctly computes k! for some arbitrary k โฅ 0.
That is, assume factorial(k) returns k!.
Inductive Step (show it works for n = k + 1):
When the algorithm is called with input (k + 1):
- Since k + 1 > 0, it executes:
return (k+1) * factorial(k) - By the inductive hypothesis,
factorial(k)returns k! - Therefore, the algorithm returns:
$$ (k+1) \times k! = (k+1)! $$
This is exactly the correct value of (k+1)!. โ
Conclusion:
By the principle of mathematical induction, factorial(n) correctly computes n! for all n โฅ 0.
This demonstrates that induction mirrors the recursive structure of the algorithm: the base case handles the termination condition, and the inductive step handles the recursive call.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.