Professional Documents
Culture Documents
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
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)].
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).
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)
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
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. ….
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.
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.”
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
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
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
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]}
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
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
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
Acceleration(x) = -x(t)
T T
Acceleration(x) = -x(t)
T T
Acceleration(x) = -x(t)
T T
Acceleration(x) = -x(t)
T 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 +ε
Acceleration(x) = -x(t)
T T, T +ε
T +ε
Acceleration(x) = -x(t)
T T, T +ε
T +ε
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 +ε
Acceleration(x) = -x(t)
T T, T +ε
T +ε
Acceleration(x) = -x(t)
T T, T +ε
T +ε
Acceleration(x) = -x(t)
T T, T +ε
T +ε
Acceleration(x) = -x(t)
T T, T +ε
T +ε
∞
∞
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. ∞
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
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
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.”
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.”
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.”
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.”
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).
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.”
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.”
Technique:
Divide and Conquer
Your job is to
produce an output 14,23,25,30,31,52,62,79,88,98
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
95
Merge Sort Split Set into Two
Use this help to
solve your own
instance.
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.
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.
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.”
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.
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.”
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.
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)].
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).