liveBook · Manning

Inductive proof

As explained in Grokking Algorithms, Second Edition

An inductive proof is a proof method that establishes a statement for a base case and then proves that validity for one case implies validity for the next case.

An inductive proof shows that a statement holds in two steps: establish a base case, then show that one valid case implies the next. section 4.2

Climbing a ladder

To prove you can climb a ladder, start with the base case: your legs are on rung 1. Then use the inductive case: whenever you are on one rung, you can step onto the next. Repeating that step gets you to the top. section 4.2

An inductive proof follows this pattern:

  • Base case: prove the statement for the starting values. section 4.2
  • Inductive case: prove that validity for one size or case gives validity for the next. section 4.2

For quicksort, arrays of zero or one element are already sorted. If quicksort works for one array size, recursive calls can sort the smaller sub-arrays for the next size, so the method works for arrays of every size. section 4.2

def quicksort(array):
  if len(array) < 2:
    return array                                  #1
  else:
    pivot = array[0]                              #2
    less = [i for i in array[1:] if i <= pivot]   #3
    
    greater = [i for i in array[1:] if i > pivot] #4

    return quicksort(less) + [pivot] + quicksort(greater)

print(quicksort([10, 5, 2, 3]))
#1 Base case: arrays with 0 or 1 element are already sorted.
#2 Recursive case
#3 Sub-array of all the elements less than the pivot
#4 Sub-array of all the elements greater than the pivot

A quicksort implementation labels its base and recursive cases directly. section 4.2

How Grokking Algorithms, Second Edition covers it

How deep it goes
Explains the base and inductive cases with ladder and quicksort examples, then stops short of deeper proof theory.
Examples
Uses climbing a ladder and quicksort arrays of increasing sizes as examples.
What it assumes
Assumes a recursive quicksort context while building the proof pattern from a familiar ladder analogy.
What you can do after
Evaluate/understand recursive algorithm correctness by checking its base case and step from one case to the next.
How it connects
Connects inductive proofs to quicksort, recursion, and divide and conquer.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.