You are on page 1of 115

Logic for Computer Science

Induction
Counting
Induction
• Sums
• Axiom
• Special
• Proof of Soundness
Loop Invariants
• Max in Array
• Differential Equations
• König’s Lemma
Recursion
• Merge Sort
• Planar Graph Jeff Edmonds
Lecture 6 Strong Induction York University
Induction
Counting
Counting i=0
Thanks! loop
Now count forever. print i
i=i+1
0,1,2,3,4,5,…… end loop

Do I ever reach ∞? No.


You only reaches positive integers.
(Natural Numbers)
N = { 0, 1, 2, 3, 4, 5, 6, 7, …. }
∞ is not one.
Counting i=0
Thanks! loop
Now count forever. print i
i=i+1
0,1,2,3,4,5,…… end loop

Do I ever reach all the


$ time t "iN (i is reached in time No
positive integers?
t)

I give you some time t. t is finite.


Not all of N is reached in this time.
I give you some iN that has not.
Counting i=0
Thanks! loop
Now count forever. print i
i=i+1
0,1,2,3,4,5,…… end loop

Do I ever reach all the


$ time t "iN (i is reached in time No
positive integers?
t)
Is each positive integer "iN $ time t (i is reached in time t) Yes
reached?
I give you some positive integer i.
Now some strange thing like ∞ are never reached.
But if i is a positive integer then it is.
I give you the time t during which this occurs.

This does tell us something deep about the positive integers.


(That is not true about strangers sets) Eg the reals.
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”
• S(i) ≡ "j≤i “If j has a proof of length j, then j is a valid”

n
1. 1 A Hilbert Style Proof is a sequence of formulas.
n
2. 2
n
A proof system is sound iff
3. 3
 has a proof  it is valid
n

n 4. 4
n
n+1 n+1 n+1 n+1 n+1
5. 5
6. ….
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”
• S(i) ≡ "j≤i “If j has a proof of length j, then j is a valid”
• S(i) ≡ “My iterative algorithm maintains its loop invariants
for the first i iterations.”
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

preCond
codeA
loop
loop-invariant
exit when exit Cond
codeB
endloop
codeC
postCond
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”
• S(i) ≡ "j≤i “If j has a proof of length j, then j is a valid”
• S(i) ≡ “My iterative algorithm maintains its loop invariants
for the first i iterations.”
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”
• S(i) ≡ “Every planar graph with i nodes
can be bichromatically coloured with at most 6 colours.”
• S(T) ≡ “My differential equation has been plotted from 0 to T.”

T T
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
My goal is to prove "i S(i).

S is complicated.
There are an infinite number of different i.
How can you prove S(i) for all of them???
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
My goal is to prove "i S(i).
My first step is to prove S(0).
Then I prove "i [S(i-1)S(i)].

To prove this using the game,


I first give you an arbitrary i.
I help you by assuring you of S(i-1).

I must prove S(i).


Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
My goal is to prove "i S(i).
My first step is to prove S(0).
Then I prove "i [S(i-1)S(i)].
Then I use modus ponens to get "i S(i).

Why can I then conclude "i S(i)???


Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)

My goal is to prove "i S(i).

S(0) S(1) S(2) S(3) S(4)



We will colour S(i) by S(i) when we know it is true.
S(i) when false.
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Assume the following:
S(0) & "i S(i-1)S(i)]
I can help you with these.
My goal is to prove "i S(i).

Telling me that S(0) is true is a good start.

S(0) S(1) S(2) S(3) S(4)



Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Assume the following:
S(0) & "i S(i-1)S(i)]
I can help you with these.
My goal is to prove "i S(i).
342
How do I prove S(1)?
342 231 342
I will set i=1 and assure you that S(0)S(1).
231 342
Excellent. We know S(0) and this gives us S(1).
S(0) S(1) S(2) S(3) S(4)

Each positive integer is eventually reached!
"i $ time t (i is reached in time t)
Sum
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”

n
n+1 n+1 n+1 n+1 n+1
Sum
1 + 2 + 3 + ... + n-1 + n = S = number of white dots

1 2........n
Sum
1 + 2 + 3 + ... + n-1 + n = S = number of white dots
n + n-1 + n-2 + ... + 2 + 1 = S = number of yellow dots

n ....... 2 1 1 2........n
Sum
1 + 2 + 3 + ... + n-1 + n = S = number of white dots
n + n-1 + n-2 + ... + 2 + 1 = S = number of yellow dots
n+1 n+1 n+1 n+1 n+1 2S

n (n+1) n

2S dots n
=
n
n(n+1) dots in the grid
n

n
1+2+3+…+n = ½ n(n+1)
n+1 n+1 n+1 n+1 n+1
Sum
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”
My goal is to prove "i S(i).
My first step is to prove S(0).

Left Hand Side Right Hand Side


1+2+3+…+0 ½ i(i+1)
0 ½ 0(0+1)
= 0
Sum
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”
Then I prove "i [S(i-1)S(i)].

To prove this using the game,


I first give you an arbitrary i.
I must prove S(i). Left Hand Side
1+2+3+…+i
= [1+2+3+…+(i-1)]+i
= [½ (i-1)i] +i
= ½ [(i-1)i +2i]
= ½ [(i+1)i]
I assure you S(i-1),
= Right Hand Side
ie1+2+3+…+(i-1) = ½ (i-1)i
Sum
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “1+2+3+…+i = ½ i(i+1)”
Hence S(0) & "i [S(i-1)S(i)].
And hence
“By way of induction, "i S(i)”.
Sum
I’m still confused by induction.
How do you prove "i S(i) ?

S(i) ≡ “1+2+3+…+i = ½ i(i+1)”


Here is a program that inputs n Left Hand Side Right Hand Side
and outputs a finite proof of S(n). 0 = ½ (0)(1)=0
Proof(n) Proof of S(4)S(5):
Give a proof of S(0). Left Hand Side
loop i = 1..n = 1+2+3+4+5
Give a proof of S(i-1)S(i). = [1+2+3+4]+5
end loop = [½ (4)(5)] +5
Use modus ponens = ½ [(4)(5)+2(5)]
n times to get S(n). = ½ [(6)(5)]
= Right Hand Side
Sum
I’m still confused by induction.
How do you prove "i S(i) ?

S(i) ≡ “1+2+3+…+i = ½ i(i+1)”


Here is a program that inputs n
and outputs a finite proof of S(n).
Proof(n)
Give a proof of S(0).
loop i = 1..n
Give a proof of S(i-1)S(i).
end loop S(i) can be proved
Use modus ponens for each positive integer i!
n times to get S(n).
"i $ time t (i is proved in time t)
Axiom
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
If induction is valid,
why does it need to be added as an axiom?

It is valid for the model of the positive integers,


at least the way we think about them,
but how do we ensure that this is our model?

But you could define


• U = {Devil, God, you}
• + ≡ marriage
• > ≡ “is better”
• 1 = Devil
Axiom
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
If induction is valid,
why does it need to be added as an axiom?

It is valid for the model of the positive integers,


at least the way we think about them,
but how do we ensure that this is our model?
S(0) S(1) S(2) S(3) S(4) S(∞+1) S(∞+2)

S(∞)
S(∞+3)
Do we KNOW that this process reaches all i? S(∞+5) S(∞+4)

Effectively what the Induction Axiom says is:


"i $ time t (i is proved in time t)
It says something about the nature of the positive integers.
Axiom
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)

It is useful to look at things in different ways.


Let’s see what we get when applying:
AB iff BA

or
S(0) S(1) S(2) S(3) S(4)

Predicate logic statement:
[S(0) & "i S(i-1)S(i)]  "i S(i)
["i S(i)]  [S(0) & "i S(i-1)S(i)]
$i S(0)
[S(i-1)S(i)]
or
$i S(i-1) & S(i)]
Axiom
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)

It is useful to look at things in different ways.


Let’s see what we get when applying:
AB iff BA

S(0) S(1) S(2) S(3) S(4)




If they are not all true,
then either the first is not true S(0)
or some i´ is the last to be true. $i S(i-1) & S(i)]
Special
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)

Claim: Every positive integer is special.


• Proof:
• Clearly 0 is special.
• By way of contradiction assume they are not all special.

S(0) S(1) S(2) S(3) S(4)



i

• Let i be the smallest integer that is not special.


• Well having that distinction would make i very special.
• This contradicts i not being special.
• Hence, every positive integer must be special.
Special
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)

Claim: Every positive integer is special.


• Proof:
• Clearly 0 is special.
• By way of contradiction assume they are not all special.

S(0) S(1) S(2) S(3) S(4)



i

• Let i be the smallest integer that is not special.


• Well having that distinction would make i very special.
• This contradicts i not being special.
• Hence, every positive integer must be special.
Proof of Soundness
A Hilbert Style Proof is a sequence of formulas.

1. 1
j = 2. 2 Where each such i is either:
• in Axioms
3. 3
j´= 4.  • or follows from some rule
4 “If j and j´ are line in your proof, then you can add line i”
5. 5
6. ….
i =8. 8

The last line n is our theorem .


n. 
Proof of Soundness
A Hilbert Style Proof is a sequence of formulas.
A proof system is sound iff
1. 1  has a proof  it is valid
j = 2. 2
Proof by induction
3. 3 • Induction Hypothesis:
j´= 4. 
4 Let S(i) be the statement that
5. 5 "j≤i “If j has a proof of length j, then j is a valid”
6. …. • S(0): Empty Proof
i =8. 8
• S(i-1)S(i) ie If the first i-1 lines are valid,
then the ith is.

n. 
Proof of Soundness
A Hilbert Style Proof is a sequence of formulas.
A proof system is sound iff
1. 1  has a proof  it is valid
j = 2. 2
3. 3
j´= 4. 4 S(i-1): by way of induction assume "j≤i-1 j is a
5. 5 valid.
6. ….

If iAxioms, then it is a valid.


i-1. i-1
• Proof System Requires:
i.  i
Rule “If j and j´ are line in your proof, then you can add line i”
requires “If j and j´ are valid, then so is i”
 i is valid  S(i-1)S(i)  "i S(i).
 "j “If j has a proof of length j, then j is a valid”
Loop Invariants
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.

• S(i) ≡ “My iterative algorithm maintains its loop invariants


for the first i iterations.”

preCond • Iterative algorithms is a key


codeA algorithmic paradigm.
loop
loop-invariant • Store what you currently know
exit when exit Cond
codeB
in some data structure.
endloop • Each iteration take a step forward
codeC
postCond towards destination.

34
Loop Invariants
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.

• S(i) ≡ “My iterative algorithm maintains its loop invariants


for the first i iterations.”

preCond I implored you to not worry


codeA
loop
about the entire computation.
loop-invariant
exit when exit Cond
codeB
endloop
codeC
postCond
next

35
Loop Invariants
Max( A ) Preconditions:
“preCond: Any assumptions that must be true
Input is array A[1..n] about the input instance.
of n values.”

What do you want to be true in


loop the middle of the computation?
“loop-invariant:
Loop Invariant:
m is max in {A[1]..A[i]}”
any assumptions (picture) that
It would be great to know the must be true at the top of the loop.
maximum of some prefix of the input.
endloop
Postconditions:
“postCond: The statement of what must be true
when the algorithm/program returns.
return max in {A[1]..A[n]}” 36
Loop Invariants
Max( A )
Establishing the loop invariant:
“preCond:
Input is array A[1..n] preCond
loop-invariant
of n values.” code
i=1
m = A[1] Exit
loop Maintaining the loop invariant:
“loop-invariant: loop-invariantt-1
m is max in {A[1]..A[i]}” ¬exit Cond loop-invariantt
exit when (i=n) codeB
m = max(m,A[i+1])
i=i+1 Exit Obtaining Postcondition:
endloop loop-invariant 
return(m) exit Cond postCond
“postCond: code
return max in {A[1]..A[n]}” 37
Loop Invariants
preCond: Input is array A[1..n] of n values.”
loop-invariant: m is max in {A[1]..A[i]}”
Establishing the loop invariant:
" Situations preCond
loop-invariant
code
Input: 4 5 3 7 1

Being an assumption, I can assure you that the


precondition is true for the input,
ie Input is array A[1..n] of n values.

But he does not know anything else.


The implied " means I choose.

38
Loop Invariants
preCond: Input is array A[1..n] of n values.”
loop-invariant: m is max in {A[1]..A[i]}”
Establishing the loop invariant:
preCond
loop-invariant
code
Input: 4 5 3 7 1
loop-invariant0 Max of 4 is 4
i=1 & m=4

My goal is to prove that the code before the loop


• Establishes the loop invariant.
Though I get to write this code,
I will instead, first choose what the data structure will look like.

39
Loop Invariants
preCond: Input is array A[1..n] of n values.”
loop-invariant: m is max in {A[1]..A[i]}”
Establishing the loop invariant:
preCond
loop-invariant
code
Input: 4 5 3 7 1
loop-invariant0 Max of 4 is 4
i = last index considered
index = 1

I now must write code to go from the one state to the other.
i=1
m = A[1]

40
Loop Invariants
loop-invariant: m is max in {A[1]..A[i]}”
Exit
Maintaining the loop invariant:
loop-invariantt-1
¬exit Cond loop-invariantt
codeB
Input: 4 5 3 7 1

Being an assumption, I can assure you that we are at


some arbitrary point in the computation and
the loop invariant is true.
m is max in {A[1]..A[i]}

This tells me that we have two variables m and i and


m is the maximum of the first i input values.

41
Loop Invariants
loop-invariant: m is max in {A[1]..A[i]}”
Exit
Maintaining the loop invariant:
" Situations loop-invariantt-1
¬exit Cond loop-invariantt
codeB
Input: 4 5 3 7 1
loop-invariantt-1 Max of 4 5 3 is 5

But he does not know anything else.


Eg He does not know # of iterations t-1.
The implied " means I choose.

42
Loop Invariants
loop-invariant: m is max in {A[1]..A[i]}”
Exit
Maintaining the loop invariant:
loop-invariantt-1
¬exit Cond loop-invariantt
codeB
Input: 4 5 3 7 1
loop-invariantt-1 Max of 4 5 3 is 5
loop-invariantt Max of 4 5 3 7 is 7

My goal is to prove that after the code goes around the loop
• Progress has been made
• The loop invariant is true again.
Though I get to write the code for this loop,
I will instead, first choose what the data structure will look like.
43
Loop Invariants
loop-invariant: m is max in {A[1]..A[i]}”
Exit
Maintaining the loop invariant:
loop-invariantt-1
¬exit Cond loop-invariantt
codeB
Input: 4 5 3 7 1
loop-invariantt-1 Max of 4 5 3 is 5
loop-invariantt Max of 4 5 3 7 is 7

I now must write code to go from the one state to the other.
• m is max in {A[1]..A[i]}
• The next input value to look at is A[i+1].
• The new value of m should be the max of these,
ie m = max(m,A[i+1])
• Then we change i, ie i = i + 1
44
Loop Invariants
loop-invariant: m is max in {A[1]..A[i]}”
postCond: return max in {A[1]..A[n]}”
Obtaining Postcondition:
Exit loop-invariant 
exit Cond postCond
code
Input: 4 5 3 7 1
loop-invariant Max of 4 5 3 is 5
My goal is to prove that in the end, the post condition is true,
ie return max in {A[1]..A[n]}

I can assure you that the loop invariant is true,


ie m is max in {A[1]..A[i]}

If i=n, then the post condition and the loop invariant are the same!
Let’s make i=n the exit condition. 45
Loop Invariants
loop-invariant: m is max in {A[1]..A[i]}”
postCond: return max in {A[1]..A[n]}”
Obtaining Postcondition:
Exit loop-invariant 
exit Cond postCond
code
Input: 4 5 3 7 1
loop-invariant Max of 4 5 3 is 5

I can assure you that the loop invariant is true,


ie m is max in {A[1]..A[i]}

exit Cond: i=n. code: Return m.

My goal is to prove that in the end, the post condition is true,


ie return max in {A[1]..A[n]}
46
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1
Step 0
m = A[1]
loop <preCond>
“loop-invariant: <loop-inv>
codeA
m is max in {A[1]..A[i]}”
exit when (i=n)
m = max(m,A[i+1])
array A[1..n]
i=i+1 m is max
endloop in {A[1]..A[i]}
i=1
return(m)
m = A[1]
“postCond:
return max in {A[1]..A[n]}”

47
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop
Exit
“loop-invariant:
Step 1
m is max in {A[1]..A[i]}”
exit when (i=n) loop-invariantt-1
m = max(m,A[i+1]) ¬exit Cond loop-invariantt
i=i+1 codeB
endloop
return(m) m is max m is max
“postCond: in {A[1]} in {A[1],A[2]}
return max in {A[1]..A[n]}” m = max(m,A[2])

48
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop
Exit
“loop-invariant:
Step 2
m is max in {A[1]..A[i]}”
exit when (i=n) loop-invariantt-1
m = max(m,A[i+1]) ¬exit Cond loop-invariantt
i=i+1 codeB
endloop
return(m) m is max m is max
“postCond: in {A[1],A[2]} in {A[1]..A[3]}
return max in {A[1]..A[n]}” m = max(m,A[3])

49
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop
Exit
“loop-invariant:
m is max in {A[1]..A[i]}” Step ?
exit when (i=n) loop-invariantt-1
m = max(m,A[i+1]) ¬exit Cond loop-invariantt
i=i+1 codeB
endloop
return(m) How many times to we have
“postCond: to prove the loop invariant
return max in {A[1]..A[n]}” has been maintained?
Only ONCE!
50
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop
Exit
“loop-invariant:
Step i
m is max in {A[1]..A[i]}”
exit when (i=n) loop-invariantt-1
m = max(m,A[i+1]) ¬exit Cond loop-invariantt
i=i+1 codeB
endloop m is max m is max
return(m) in {A[1]..A[i]} in {A[1]..A[i+1]}
“postCond:
m = max(m,A[i+1])
return max in {A[1]..A[n]}”
i = i+1
How do we differentiate between these to m and i?51
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1 Induction
m = A[1]
loop
Exit
“loop-invariant:
Step i
m is max in {A[1]..A[i]}”
exit when (i=n) loop-invariantt-1
m = max(m,A[i+1]) ¬exit Cond loop-invariantt
i=i+1 codeB
endloop
return(m) mt is max mt+1 is max
“postCond: in {A[1]..A[it]} in {A[1]..A[it+1]}
return max in {A[1]..A[n]}” mt+1 = max(mt,A[it+1])
it+1 = it + 1
Value of i at the top of the loop. and after going around.52
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop Exit Last Step
“loop-invariant:
m is max in {A[1]..A[i]}” <loop-invariant>
exit when (i=n) <exit Cond> <postCond>
m = max(m,A[i+1]) codeC
i=i+1
endloop m is max
return(m) in {A[1]..A[i]} return max
“postCond: i=n in {A[1]..A[n]}
return max in {A[1]..A[n]}” return(m)

53
Max( A ) Loop Invariants
“preCond:
Input is array A[1..n]
of n values.”
i=1 Definition of Correctness
m = A[1]
loop
“loop-invariant:
m is max in {A[1]..A[i]}”
exit when (i=n)
m = max(m,A[i+1]) array A[1..n] return is max
i=i+1 in {A[1]..A[n]}
endloop code
return(m)
“postCond:
return max in {A[1]..A[n]}”

54
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

T T

Let x(t) denote its distance from rest.


It will bounce from x(t)=+xm to -xm.

55
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

T T

It is at top peak,
has stopped moving,
and is accelerating down.

56
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

T T

It is in the center,
is moving fast down,
and is not accelerating.

57
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

T T

It is at bottom peak,
has stopped moving,
and is accelerating up.

58
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

T T

It is in the center,
is moving fast up,
and is not accelerating.

59
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

T T

It is at top peak,
it has stopped moving,
and is accelerating down.

60
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.

Acceleration(x) = -x(t)

T T

Let x(t) denote its distance from rest. Newton: F=MA


It will bounce from x(t)=+xm to -xm.
Acceleration(x) of block on spring Pull the string farther,
= Force / Mass it pulls back harder
= (-k x(t))/ Mass in the opposite
direction. 61
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T

Dude! You can’t do induction on the reals!


They are not countable.
Lets do induction just on multiples of some infinitesimal ε.
T = 0ε, 1ε, 2ε, 3ε, …These are countable.
Then we will pretend that it is true for the reals in between.
62
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T

My goal is to plot x(t) for t≥0.


It is sufficient to prove "T S(T),
ie for every T, we have plotted up to T.
If we use induction, it is sufficient to prove
S(0) & "i [S(i-1)S(i)] 63
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T

S(0) simply states that we know


the initial location and velocity of the block.
x(0)=+xm
Pull the block back
velocity(0)=0 and let go.
Establishing the loop invariant.
64
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T

My goal now is to prove


"T [S(T)S(T+ε)]
To prove this using the game,
I first give you an arbitrary T.

65
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T
T +ε

I help you by assuming S(T).


• x(t) has been plotted up to T. Exit

Then I prove S(T+ε). Maintainin


•I need only need to plot from T to T+ε. g
• loop inv 66
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

I help you by assuming S(T).


• x(t) has been plotted up to T. Exit
• We know x(T) and velocity(T).
Then I prove S(T+ε). Maintainin
•I need only need to plot from T to T+ε. g
• loop inv 67
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

•x(T+ε) = x(T) + velocity(T)  ε


(cm) = (cm) + (cm/sec)  (sec)

It is so important to balance units.

68
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

•x(T+ε) = x(T) + velocity(T)  ε


(cm) = (cm) + (cm/sec)  (sec)

If I travel this fast for this amount of time


then this is how far I travel.
69
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

•x(T+ε) = x(T) + velocity(T)  ε


(cm) = (cm) + (cm/sec)  (sec)

If I am here and travel this far


then this is where I will end up. 70
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

•x(T+ε) = x(T) + velocity(T)  ε Hey! You are


(cm) = (cm) + (cm/sec)  (sec) assuming that
during [T,T+ε]
that the velocity
Yes, but is an infinitesimal. is a constant.
71
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

•x(T+ε) = x(T) + velocity(T)  ε


(cm) = (cm) + (cm/sec)  (sec)
• acceleration(T) = - x(T)
• velocity(T+ε) = velocity(T) + acceleration(T)  ε
(cm/sec) = (cm/sec) + (cm/sec2)  (sec)
72
Differential Equations
Induction Axiom:
[S(0) & "T [S(T)S(T+ε)]]  "T S(T)
Let S(T) be a yes/no statement, for each positive “real” T.
loop S(T) ≡ “We have plotted distance x(t) from 0 to T
inv: and know x(T) and velocity(T).”

Acceleration(x) = -x(t)

T T, T +ε
T +ε

This completes the proof.

Thanks for your help.


But we just solved it on multiples of ε.
True.
But we can now take the limit as ε goes to zero. 73
König’s Lemma
König’s Lemma: Let T be a rooted tree with an infinite number of nodes.
It either has
• a node with infinite degree
• or a path of infinite length.


74
König’s Lemma
König’s Lemma: Let T be a tree with infinite nodes ∞ i
It either has a node with infinite degree
or a path of infinite length. ∞

Proof: We will build an infinite path one node at a time.


loop S(i) ≡ “We have either found a node with infinite degree
inv: or a path of length i to a node
whose subtree contains and infinite number of nodes.”

75
König’s Lemma
König’s Lemma: Let T be a tree with infinite nodes
It either has a node with infinite degree
or a path of infinite length.
Proof: We will build an infinite path one node at a time.
loop S(i) ≡ “We have either found a node with infinite degree
inv: or a path of length i to a node
whose subtree contains and infinite number of nodes.”
Proving S(0): Start at the root, with a path of length 0.

76
König’s Lemma
König’s Lemma: Let T be a tree with infinite nodes ∞
It either has a node with infinite degree
or a path of infinite length.
Proof: We will build an infinite path one node at a time.
loop S(i) ≡ “We have either found a node with infinite degree
inv: or a path of length i to a node
whose subtree contains and infinite number of nodes.”
Assume S(i) and proving S(i+1):
If we have found a node with infinite degree already
or the current node has infinite degree we are done.

77
König’s Lemma
König’s Lemma: Let T be a tree with infinite nodes i
It either has a node with infinite degree r

or a path of infinite length. ∞

Proof: We will build an infinite path one node at a time.


loop S(i) ≡ “We have either found a node with infinite degree
inv: or a path of length i to a node
whose subtree contains and infinite number of nodes.”
Assume S(i) and proving S(i+1):
Else we are at node with finite degree
whose subtree has an infinite number of nodes

78
König’s Lemma
König’s Lemma: Let T be a tree with infinite nodes i
It either has a node with infinite degree r

or a path of infinite length. n1 n2 n∞3 nr

Proof: We will build an infinite path one node at a time.


loop S(i) ≡ “We have either found a node with infinite degree
inv: or a path of length i to a node
whose subtree contains and infinite number of nodes.”
Assume S(i) and proving S(i+1):
Else we are at node with finite degree
whose subtree has an infinite number of nodes
Let n and n1, n2, n3, …, nr, denote the number of nodes in the subtrees
of our current node and its children.
Note n = n1 + n2 + n3 + … + nr + 1.
By our assumptions n is infinite and r is finite.
Hence one of the children must have infinite nj.
79
König’s Lemma
König’s Lemma: Let T be a tree with infinite nodes
It either has a node with infinite degree i+1

or a path of infinite length. ∞

Proof: We will build an infinite path one node at a time.


loop S(i) ≡ “We have either found a node with infinite degree
inv: or a path of length i to a node
whose subtree contains and infinite number of nodes.”
Assume S(i) and proving S(i+1):
Else we are at node with finite degree
whose subtree has an infinite number of nodes
Let n and n1, n2, n3, …, nr, denote the number of nodes in the subtrees
of our current node and its children.
Note n = n1 + n2 + n3 + … + nr + 1.
By our assumptions n is infinite and r is finite.
Hence one of the children must have infinite nj.
Move to a child, giving S(i+1). This completes the proof.80
Recursion
X = 33
Y = 12
ac = 31 = 3
I implored you to not worry bd = 32 = 6
(a+b)(c+d) =63
about the entire computation. =18
XY = 396

X=3 X=3 X=6


Y=1 Y=2 Y=3
XY = 3 XY = 6 XY = 18

81
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

Crazy! This algorithm is calling itself. 82


Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

My goal is to prove that my recursive


algorithm works on every input.

It is sufficient to prove "i S(i),


ie for every size i, it works for every
input of that size (or smaller).

If we use induction,
it is sufficient to prove
S(0) & "i [S(i-1)S(i)]
83
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

S(0) simply states that the algorithm


works for every input of size 0.

How many inputs can there be of size 0?

I like to expand this to proving that


the algorithm works on every base case.
These are inputs that are small enough that
I can solve them on my own.

84
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

S(0) simply states that the algorithm


works for every input of size 0.

How many inputs can there be of size 0?

I like to expand this to proving that


the algorithm works on every base case.
These are inputs that are small enough that
I can solve them on my own.

85
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

My goal now is to prove


"i [S(i-1)S(i)].

To prove this using the game,


I first give you an arbitrary i.

I help you by assuring you of S(i-1).

I must prove S(i).

86
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”
Then I prove S(i).

To prove this using the game,


I first give you an arbitrary input
whose size is at most i.

I can assure you of S(i-1),


ie, the algorithm works for on every
input is size at most i-1.
Then the input I give you will have size i.
87
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

I must prove that the algorithm works


on his input of size i.

What about those crazy calls to the same algorithm?


Do they give the right answer?

88
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

I must prove that the algorithm works


on his input of size i.
I can assure of S(i-1),
i.e. “My recursive algorithm gives the correct answer
on every input is size at most i-1.”

89
Recursion
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
Let S(i) be a yes/no statement, for each positive integer i.
• S(i) ≡ “My recursive algorithm gives the correct answer
on every input is size at most i.”

I must prove that the algorithm works


on his input of size i.
I can assure you that these calls are to friends.
As long as you give them inputs that are smaller than
yours, they will magically give you the answer.
Trust them & don’t micro manage them,
ie don’t trace out what they do and their friend’s friends 90
Merge Sort
Consider your 88 52
14
input instance. 31
25 98 30
62 23
You are assured 79
that it meets the
precondition.

Technique:
Divide and Conquer

Your job is to
produce an output 14,23,25,30,31,52,62,79,88,98

that meets the


post condition.
91
Merge Sort Split Set into Two
• You can have as many 88 52
14
friends as you want. 31
25 98 30
• You give each a sub instance. 62 23
79
– Meets the precondition
– Smaller
• Trust them to give you
the output for their instances.

3 1 Trust your friends


2
Don’t micro manage them 1,2,3
79 cm

By magic! 92
Merge Sort Split Set into Two
• You can have as many 88 52
14
friends as you want. 31
25 98 30
• You give each a sub instance. 62 23
79
– Meets the precondition
– Smaller
• Trust them to give you
the output for their instances.
Get one friend to
sort the first half.
Trust your friend
Don’t micro manage him. 25,31,52,88,98

79 cm
93
Merge Sort Split Set into Two
• You can have as many 14
25,31,52,88,98
friends as you want. 30
• You give each a sub instance. 62 23
79
– Meets the precondition
– Smaller
• Trust them to give you
the output for their instances.
Get another friend to
sort the second half.
Trust your friend
Don’t micro manage him. 14,23,30,62,79

79 cm
94
Merge Sort Split Set into Two
Use this help to
solve your own 25,31,52,88,98

instance. 14,23,30,62,79

Merge two sorted lists into one

95
Merge Sort Split Set into Two
Use this help to
solve your own
instance.

Merge two sorted lists into one

25,31,52,88,98

14,23,30, 62,79,

96
Merge Sort Split Set into Two
Use this help to
solve your own
instance.

Merge two sorted lists into one

14,23,25,30,31,52,62,79,88,98

97
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
Planar Graph: A simple undirected graph G is said to be planar
if its nodes u and edges u,v can be placed/embedded in the
2-dimensional plane in a way such that no edges cross.

Minimum Degree: A fact about every planar graph is that there


is always a node u whose degree is at most five.
" planar graph G, $ node u, degree(G,u) ≤ 5
Six Colouring: A six colouring C of a graph assigns one of six
colours to each nodes u such that each edge bichromatic.
" edge u,v, C(u) ≠ C(v).

98
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”

My goal is to prove that every planar graph


can be coloured with at most 6 colours.

It is sufficient to prove "i S(i),


ie for every i, it is true for every graph with i nodes.
If we use induction, it is sufficient to prove
S(0) & "i [S(i-1)S(i)]

99
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
S(0) simply states that graphs with zero
nodes can be coloured bichromatically.

I every elephant in the room pink?


There are no elephants in the room.
Hence, there is not one that is not pink.
As there is not an edge
that is not bichromatic.
Hence the empty colouring is
a biochromatic colouring. 100
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”

My goal now is to prove


"i [S(i-1)S(i)].

To prove this using the game,


I first give you an arbitrary i.

I help you by assuring you of S(i-1).

I must prove S(i).

101
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”

Then I prove S(i).

To prove this using the game,


I first give you an arbitrary planar graph with i
nodes.
My goal is to colour this graph with 6 colours.

For a general graph, there is no algorithm known


faster than trying all 6n colourings.

102
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
My goal is to colour this graph with 6 colours.

I can assure you of S(i-1),


ie, I can 6 colour any planar graph with one fewer nodes.

Hey, if I remove one node from the i node graph,


it will still be planer,
and my oracle could colour it for me.
I can choose which node to delete.

Minimum Degree: A fact about every planar graph is that there


is always a node u whose degree is at most five.
" planar graph G, $ node u, degree(G,u) ≤ 5 103
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
My goal is to colour this graph with 6 colours.

I will delete one node of degree at most 5.


I will 6 colour the rest.
I will give the deleted node one of the 6 colours
that its 5 neighbors does not have.
This competes the colouring of my adversaries graph.

This completes the proof that all planer


graphs have 6 colourings.

104
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
In this recursive algorithm
•The adversary gave you a 12 node graph.
•You give friend 11, a 11 node graph.
•He gives friend 10, a 10 node graph.
•He gives friend 9, a 9 node graph.
•…
•He gives friend 0, a 0 node graph.
•Friend 0 is the base case and has returns the empty colouring.
•Friend 1 colours his node and returns the 1 colouring.
•Friend 2 colours his node and returns the 2 colouring.
•…
•Friend 12 colours his node and returns the 12 colouring. 105
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
That’s what I call micro managing your friends.
It gets way too confusing when you do that.
Please don’t.

106
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
My point is that the actual coloring feels iterative,
the nodes are just coloured in some order.
Why not just do that?

Fantastic question.
The key is that every time a node is coloured
at most 5 of its neighbors have been coloured
already
so that it can be given a 6th colour.
If every node in the original graph has degree at most 5,
then any order to colour works.

107
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
My point is that the actual coloring feels iterative,
the nodes are just coloured in some order.
Why not just do that?

Fantastic question.
The key is that every time a node is coloured
at most 5 of its neighbors have been coloured
already
so that it can be given a 6th colour.
If every node in the original graph has degree at most 5,
then any order to colour works.

108
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
My point is that the actual coloring feels iterative,
the nodes are just coloured in some order.
Why not just do that?

Fantastic question.
The key is that every time a node is coloured
at most 5 of its neighbors have been coloured
already
so that it can be given the 6th colour.
But some nodes may be of high degree.
Do them first. It can be more complicated than this.
How do you know which to colour first? 109
Planar Graphs
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)
• S(i) ≡ “Every planar graph with i nodes
can be coloured with at most 6 colours.”
The only thing we used about planar graphs
was that each has a node of degree at most 5.
Could we apply to algorithm to all graphs whose
minimum degree is at most 5?

Fantastic question.
When you give your recursive friend a subinstance,
it must be smaller and meet the precondition.
Any subgraph of a planar graph is planar.
A graph can have a node whose degree at most 5
and when you delete it,
it no longer has a node whose degree is at most 5. 110
Strong Induction
Strong Induction Axiom:
[∀i [(∀j<i S(j))→S(i)]] → ∀iS(i)
My goal is to prove "i S(i).
I prove ∀i [(∀j<i S(j))→S(i)].

Then I use modus ponens to get "i S(i).

S(0) S(1) S(2) S(3) S(4)



We will colour S(i) by S(i) when we know it is true.
S(i) when false.
111
Strong Induction
Strong Induction Axiom:
[∀i [(∀j<i S(j))→S(i)]] → ∀iS(i)

I prove ∀i [(∀j<i S(j))→S(i)].

To prove this using the game,


I first give you an arbitrary i.
I help you by assuming ∀j<i S(j).

I must prove S(i).

S(0) S(1) S(2) S(3) S(4) S(i-1) S(i)


… …

This is great. Before I just has S(i-1) to help me.


Now I have S(0), S(1), S(2), … , S(i-1). 112
Strong Induction
Weak Induction Axiom:
[S(0) & "i [S(i-1)S(i)]]  "i S(i)

For example, suppose I want to prove:


S(i) ≡ “My recursive algorithm gives the correct answer
on every input of size i.” at most
With weak induction, I can only assure you:
S(i-1) ≡ “My recursive algorithm gives the correct answer
on every input of size i-1.” at most
But the subinstances that I give my friends may have sizes
0, 1, 2, 3, … , i-1.

With strong induction, I can assure you that these all work,
because I can assure you of S(0), S(1), S(2), … , S(i-1).

How did we get this to work before? 113


Strong Induction
Strong Induction Axiom:
[∀i [(∀j<i S(j))→S(i)]] → ∀iS(i)
Hey! Why doesn’t strong induction need the base case S(0)
but weak induction does?
I prove ∀i [(∀j<i S(j))→S(i)].
This time I am going to give you i=0.
I help you by assuring you of ∀j<i S(j).
Oops! There are no such S(j).
Hence, I am assuring you of nothing.
I must prove S(0).
S(0) S(1) S(2) S(3) S(4)

I end up proving S(0) on my own, just like in weak induction. 114
End

You might also like