2081 12

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]

Discrete StructuresChapter 4: Induction and RecursionProving Correctness of Recursive Algorithms

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 SizeSubsets
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:

  1. Base Case: Show the algorithm gives the correct output for the smallest/simplest input.
  2. Inductive Hypothesis: Assume the algorithm works correctly for all inputs of size less than n (or for input n = k).
  3. 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.