Professional Documents
Culture Documents
01 Iterative
01 Iterative
2
Verify vs Create
I will teach you to factor.
Input: An integer x
Output: Its prime factors p1,p2,…,pr
Verify: p1p2…pr = x
Input: x = 12
Output: Factors 2,2,3
Verify: 223 = 12
Input: x = 7152988453
Output: Factors 182983,39091
Verify: 18298339091 = 7152988453
Easy!
Aaaah!
You never taught me how to factor.
That is much much harder!.
4
Verify vs Create
If you give me a bunch of algorithms
and teach me how to verify that they work.
This does not necessarily teach me
how to create new algorithms.
Let’s try.
Aaaah!
You never taught me how to factor.
That is much much harder!.
5
Actions vs Assertions
Paradigm Shift
6
Actions vs Assertions
An Algorithm viewed
as a sequence of
Actions: Landmarks: ie an Assertion
of the static
picture
of what is true
.. Straight at that moment
.. Right in the journey.
.. Straight
.. Left …..
Proved somehow
4. ….
5. γ Assumption
6. Case 2:
7. β Proved somehow
Cases Conclusion
8. ….
20
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Cases Goal: γ.
nothing 1. Cases α or β Proved somehow
end if 2. Case 1:
<assertion1: x is even> 3. α Assumption
WeWeOur
checkgoal
provetheisγcode
to
= Proved somehow
prove
and<assertion
see γ 1=>are
there
Consider 4. ….
<assertion
We
only
is prove
two
themet β.γ1>=
paths
when
path α. 5. γ Assumption
<assertion
denoted
following 1> β.
α path
and α. 6. Case 2:
is met
We whenγ =
proved 7. β Proved somehow
following
<assertionpath β. Cases Conclusion
1>
in all the cases.
8. ….
21
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path α:
<assertion1: x is even> x is an integer
0
x0 is odd x1 is even
x1=x0+1
Jeff says that we fly
in from Mars and are
assured only that
<assertion0>
is true.
22
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path α:
<assertion1: x is even> x is an integer
0
x0 is odd x1 is even
x1=x0+1
In case path α,
the if condition is true
and the first code is executed
giving this relationship
between x0 and x1.
23
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path α:
<assertion1: x is even> x is an integer
0
x0 is odd x1 is even
x1=x0+1
Our goal is to prove that
when the computation gets to it,
<assertion1> is true.
24
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path α:
<assertion1: x is even> x is an integer
0
x0 is odd x1 is even
x1=x0+1
We then argue that if we assume
the LHS, we can prove the RHS.
25
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path β:
<assertion1: x is even> x is an integer
0
¬<x0 is odd> x1 is even
x1=x0
Jeff says that we fly
in from Mars and are
assured only that
<assertion0>
is true.
26
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path β:
<assertion1: x is even> x is an integer
0
¬<x0 is odd> x1 is even
x1=x0
In case path β,
the if condition is not true
and the second code is executed
giving this relationship
between x0 and x1.
27
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path β:
<assertion1: x is even> x is an integer
0
¬<x0 is odd> x1 is even
x1=x0
Our goal is to prove that
when the computation gets to it,
<assertion1> is true.
28
α
Actions
β
vs Assertions
<assertion0: x is an integer> But there are two paths
if( <x is odd> ) then through the code!
x=x+1
else Let x0 denote the value of x at assertion0.
nothing Let x1 denote the value of x at assertion1.
end if Case path β:
<assertion1: x is even> x is an integer
0
¬<x0 is odd> x1 is even
x1=x0
We then argue that if we assume
the LHS, we can prove the RHS.
29
α Actions vs Assertions
<assertion0>
if( <condition1> ) then Definition of Correctness
code<1,true>
else <assertion0>
code<1,false> any <conditions> <assertionr>
end if code
<assertion1>
…
¬<condition1>
<assertionr-1> code<1,false>
if( <conditionr> ) then By cases:
code<r,true> <assertion0>
else <assertion1>
code
code<r,false>
Each sub-path goes from
end if
one assertion to the 31
Actions vs Assertions
<assertion0>
if( <condition1> ) then Case path α: Step r
code<1,true> <assertionr-1>
else <conditionr> <assertionr>
code<1,false> code<1,true>
end if Case path β:
<assertion1> <assertionr-1>
<assertionr>
…
¬<conditionr>
α β
<assertionr-1> code<1,false>
if( <conditionr> ) then By cases:
code<r,true> <assertionr-1>
else
<assertionr>
code
code<r,false>
Each sub-path goes from
end if
one assertion to the 32
Actions vs Assertions
<assertion0>
if( <condition1> ) then
Sequentiual
<assertion0>
code<1,true> <assertion1>
else code
code<1,false> <assertion1>
<assertion2>
end if code
<assertion1> Note <assertion1> is both the end of one
…
…
…
<assertionr-1>
if( <conditionr> ) then <assertionr-1>
<assertionr>
code<r,true> code
else By transitivity:
code<r,false> <assertion0>
end if
<assertionr>
code
34
Actions vs Assertions
<assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false> Must check 2r not 2r different
sub-paths through the code.
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
Each sub-path goes from
end if
one assertion to the 35
Max( a,b,c ) Actions vs Assertions
“preCond:
Input has 3 numbers.”
m=a
“assert: m is max in {a}”
if( b>m )
m=b
endif
<preCond>
“assert: m is max in {a,b}” <assertion0>
if( c>m ) code
m=c
endif
“assert: m is max in {a,b,c}”
return(m)
“postCond:
return max in {a,b,c}”
36
Max( a,b,c ) Actions vs Assertions
“preCond:
Input has 3 numbers.”
m=a
“assert: m is max in {a}”
if( b>m ) <assertion0>
m=b <assertion1>
endif <condition1>
“assert: m is max in {a,b}” code<1,true>
if( c>m )
m=c <assertion0>
endif ¬<condition1> <assertion1>
“assert: m is max in {a,b,c}” code<1,false>
return(m)
“postCond:
return max in {a,b,c}”
37
Max( a,b,c ) Actions vs Assertions
“preCond:
Input has 3 numbers.”
m=a
“assert: m is max in {a}”
if( b>m ) <assertion1>
m=b <assertion2>
endif <condition2>
“assert: m is max in {a,b}” code<2,true>
if( c>m )
m=c <assertion1>
endif ¬<condition2> <assertion2>
“assert: m is max in {a,b,c}” code<2,false>
return(m)
“postCond:
return max in {a,b,c}”
38
Max( a,b,c ) Actions vs Assertions
“preCond:
Input has 3 numbers.”
m=a
“assert: m is max in {a}”
if( b>m )
m=b
endif
<assertion2>
“assert: m is max in {a,b}” <postCond>
if( c>m ) code
m=c
endif
“assert: m is max in {a,b,c}”
return(m)
“postCond:
return max in {a,b,c}”
39
Max( A )
“preCond:
Actions vs Assertions
Input is array A[1..n]
of n values.” Definition of Correctness
i=1 <preCond>
m = A[1] any <conditions> <postCond>
loop code
“loop-invariant: Each path through this code
m is max in {A[1]..A[i]}” must be check.
exit when (i=n) How many such paths are there?
There are ∞ paths!
m = max(m,A[i+1])
i=i+1 Is there a faster way?
endloop Break up the code Each sub-path goes from
return(m) with more assertions. one assertion to the
“postCond: next.
Only 3 sub-paths!
return max in {A[1]..A[n]}” 40
Actions vs Assertions
Assertions:
– Useful for thinking
developing
describing
proving correctness
about algorithms
41
Actions vs Assertions
Assertions:
– An assertion is a statement/picture
about the current state of the data structure
that is either true or false.
eg. the amount in your bank account is not negative.
– An assertion is not a task for the algorithm to perform.
– It is only a comment that is added
for the benefit of the reader
and for the writer!
– To make sure no one gets lost.
42
Actions vs Assertions
Assertions:
– As long as the input meets the precondition,
the assertion should be true independent of
• the path followed through the code
• and the input.
– If it is false,
then something has gone wrong in the logic of the algorithm.
43
Actions vs Assertions
Assertions:
– is a function A from current state to true/false
Eg A = “x is odd”
A(<x=5, y=3, line84> ) = True Computation is
on the path
A(<x=8, y=3, line84> ) = False Something has
Current state of the computation: gone wrong!
– Everything that is true at a particular instant in time.
– Imagine flying in from Mars,
what do you need to know about the present
to be able to continue.
– It does not say anything about the past,
i.e. how the computation got here.
– Eg <x=8, y=3, current line of code = line 84> 44
Actions vs Assertions
Assertions:
Don't Be Frightened
An assertion does not need to
consist of formal mathematical mumble jumble.
Use an informal description and a picture.
45
Algorithm Termination
• Computation steadily approaches goal
79 km 75 km
1
0 T+
i-1 i
Trust who passes you
the baton
next i
and go around once
i
49
<preCond>
codeA Iterative Algorithms
loop
<loop-invariant>
exit when <exit Cond>
codeB
codeC
<postCond>
Trying to understand
the entire algorithm at once
is the most common reason
for panicking and drowning.
50
<preCond>
codeA Iterative Algorithms
loop
<loop-invariant>
exit when <exit Cond>
Loop Invariants
codeB
codeC
<postCond>
A loop invariant is a
5 km statement/picture
about the state of your computation
9 km to make sure it does not get lost.
Your algorithm must only maintain it
while making progress 51
Iterative Algorithms
Loop Invariants
Paradigm Shift
52
Iterative Algorithms
An Algorithm viewed
as a sequence of
Loop Invariants
Actions: Landmarks: ie an Assertion
of the static
picture
of what is true
.. Straight at that moment
.. Right in the journey.
.. Straight
.. Left …..
m=4
m = max(m,5)
m = max(m,3)
m = max(m,7)
m = max(m,1)
Input:
4 5 3 7 1
Max of 4 5 3 is 5
Output: Max is 7.
56
Max( A ) Postconditions:
“preCond: The statement of what must be
Input is array A[1..n] true when the
of n values.” algorithm/program returns.
Input:
4 5 3 7 1
Max of 4 5 3 is 5
“postCond:
Output: Max is 7.
return max in{A[1]..A[n]}” 57
Max( A ) Loop Invariant:
“preCond: any assumptions (picture) that
Input is array A[1..n] must be true at the top of the loop.
of n values.”
Input:
“loop-invariant: 4 5 3 7 1
m is max in {A[1]..A[i]}”
Max of 4 5 3 is 5
“postCond:
Output: Max is 7.
return max in {A[1]..A[n]}” 58
Max( A ) Establish
“preCond: the loop invariant.
Input is array A[1..n]
of n values.”
i=1
m = A[1]
Input:
“loop-invariant: 4 5 3 7 1
Max of 4 5 3 is 5
“postCond:
Output: Max is 7.
return max in {A[1]..A[n]}” 59
Max( A ) Establish
“preCond: the loop invariant.
Input is array A[1..n]
of n values.”
i=0
m = 0 -
Input:
“loop-invariant: -44 5 3 7 1
“postCond:
Output: Max is 7.
return max in {A[1]..A[n]}” 60
Max( A ) Maintain
“preCond: the loop invariant.
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop Input:
“loop-invariant: 4 5 3 7 1
m = max(m,A[i+1])
i=i+1 Max of 4 5 3 is 5
endloop Max of 4 5 3 7 is 7
“postCond:
Output: Max is 7.
return max in {A[1]..A[n]}” 61
Max( A ) Obtaining
“preCond: Postcondition
Input is array A[1..n]
of n values.”
i=1
m = A[1]
loop Input:
“loop-invariant: 4 5 3 7 1
From these we
need to prove
that the code
maintains the
loop invariant.
75
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]] "i S(i)
• S(i) ≡ “My iterative algorithm maintains its loop invariants
for the first i iterations.”
preCond
From these we codeA
loop
need to prove loop-invariant
that the code exit when exit Cond
codeB
maintains the endloop
loop invariant. codeC
postCond
Exit
Maintaining the loop invariant:
loop-invariantt-1
¬exit Cond loop-invariantt
codeB
Induction
Induction Axiom:
[S(0) & "i [S(i-1)S(i)]] "i S(i)
• S(i) ≡ “My iterative algorithm maintains its loop invariants
for the first i iterations.”
My goal is to prove "i
S(i).
Then I prove "i [S(i-1)S(i)].
85
Logic Game
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
86
Logic Game
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]
87
Logic Game
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
88
Logic Game
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
89
Logic Game
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.
90
Logic Game
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
91
Logic Game
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. 92
Logic Game
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
Let's demonstrate
with a strange
example.
94
Your are at the top of the loop.
You do not know what
Code vs
happened before. Math Assertions
Let xt and yt be the values of x and y
at the beginning of the iteration.
xt yt
loop
x y
4: “loop-invariant: ??
5: exit when ??? Does this mean that Could be denoted
6: x = x + 1 this is the t th
iteration?
x´ and y´
7: y = y + x
No! In order be able to
8: x = x y do it only once, Don’t consider
end loop you can’t know what
happened before. t-1 or t+1
95
Your are at the top of the loop.
You do not know what
Code vs
happened before. Math Assertions
Let xt and yt be the values of x and y
at the beginning of the iteration.
Let xt+1 and yt+1 be their values after going around again.
( xt +1)(yt+xt+1) yt +xt+1
loop
x y
4: “loop-invariant: ??
xt+1 = (xt+1)(yt+xt+1)
5: exit when ???
yt+1 = yt+xt+1
6: x = x + 1
7: y = y + x Excellent.
8: x = x y These are math assertions giving
end loop the relationship between
the old and new values.
96
Your are at the top of the loop.
You do not know what
Code vs
happened before. Math Assertions
Let xt and yt be the values of x and y
at the beginning of the iteration.
Let xt+1 and yt+1 be their values after going around again.
( xt +1)(yt+xt+1) yt +xt+1
loop
x y
4: “loop-invariant: ??
xt+1 = (xt+1)(yt+xt+1)
5: exit when ???
yt+1 = yt+xt+1
6: x = x + 1
7: y = y + x Let's build a DFA from 2001
(Deterministic Finite Automata)
8: x = x y
end loop δ( qline=4, x=xt, y=yt , iteration)
= q line=4, x=(x +1)(y +x +1), y=y +x +1
t t t t t
97
loop
“loop-invariant: ??
Code vs
exit when ???
x=x+1
Math Assertions
y=y+x
x=xy <loop-invariantt>
end loop
¬<exit Cond> <loop-invt+1>
codeB
<loop-invariantt>
Induction
true about xt and yt
¬<exit Cond> <loop-invariantt+1>
true about xt and yt
true about xt+1 and yt+1
codeB Assume LI is true after t iterations
xt+1 = (xt+1)(yt+xt+1)
and prove true after t+1
yt+1 = yt+xt+1
98
A Relay Race
0
T+
1
• Your job is only to
i-1 i • Trust who passes you the baton
• Go around once
i • Pass on the baton
i
99
Person 0: Carries baton region to safe region
Establishing Loop Invariant
<preCond>
<loop-invariant>
codeA
<preCond>
0
codeA
loop
<loop-invariant>
exit when <exit Cond>
codeB
endloop
codeC
<postCond>
100
Person i: Running around the track
101
Last Person: Carries baton from safe region to finish.
Clean up loose ends
<loop-invariant>
Exit
<exit Cond> <postCond>
codeC <preCond>
T+
1
codeA
loop
<loop-invariant>
exit when <exit Cond>
codeB
endloop
codeC
<postCond>
102
Partial Correctness
Establishing Loop Invariant
<preCond>
<loop-invariant>
codeA
Maintaining Loop Invariant
Exit
<loop-invariantt>
¬<exit Cond> <loop-invariantt+1>
codeB
Clean up loose ends
<loop-invariant>
Exit
<exit Cond> <postCond>
codeC
Proves that IF the program terminates then it works
<PreCond> & <code> Þ <PostCond> 103
Designing an Algorithm
Define Problem Define Loop
Invariants
Exit
Exit
104
Physics( )
“preCond: Conservation of Energy
Input is a roller coaster
with car starting high.”
loop
loop
“loop-invariant:
Et = E0”
A loop invariant is
exit when (car at end)
→ → any assumptions
x t+t,v t+t
→ → (picture)
= Newton(x t,v t)
that must be true
endloop
at the top of the loop.
return(vfinal) Et+t = Et
“postCond: 106
Physics( )
“preCond: Conservation of Energy
Input is a roller coaster
with car starting high.”
loop
“loop-invariant:
Et = E0”
A loop invariant is
exit when (car at end)
→ → like property
x t+t,v t+t
→ → in physics that is
= Newton(x t,v t)
conserved/maintained.
endloop
return(vfinal)
“postCond: 107
Physics( )
“preCond: Conservation of Energy
Input is a roller coaster
with car starting high.”
loop
“loop-invariant:
Et = E0”
exit when (car at end)
→ →
x t+t,v t+t <loop-invariantt>
→ →
¬<exit Cond> <loop-invt+1>
= Newton(x t,v t)
codeB
endloop
Et+t = Et = E0
return(vfinal)
“postCond: 108
Physics( )
“preCond: Conservation of Energy
Input is a roller coaster
with car starting high.”
E0 = ½mv02 + h0m
loop
“loop-invariant:
Et = E0”
exit when (car at end)
<preCond> <loop-inv>
x t+t,v t+t
codeA
= Newton(x t,v t)
endloop
return(vfinal)
“postCond: 109
Physics( )
“preCond: Conservation of Energy
Input is a roller coaster
with car starting high.”
E0 = ½mv02 + h0m
loop
“loop-invariant:
Et = E0”
exit when (car at end)
<loop-invariant>
x t+t,v t+t <postCond>
<exit Cond>
= Newton(x t,v t) codeC
endloop
vfinal = (2E0/m-2hfinal)½
return(vfinal)
“postCond: 110
Physics( )
“preCond: Conservation of Energy
Input is a roller coaster
with car starting high.”
E0 = ½mv02 + h0m
loop
“loop-invariant:
Et = E0”
exit when (car at end)
x t+t,v t+t
= Newton(x t,v t)
endloop
vfinal = (2E0/m-2hfinal)½
return(vfinal)
“postCond: 111
Designing an Algorithm
Define Problem Define Loop
Invariants
Exit
Exit
112
Differential Equations
Consider a block bouncing on a spring.
We will solve the differential equation expressing its movement.
T T
113
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.
114
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.
115
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.
116
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.
117
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.
118
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
123
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 +ε
126
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 +ε
Exit
Exit
132
Life( me)
“preCond: Game of Life
I am born.”
Hopefully my parents help
loop
“loop-invariant:
I am well
and reasonably on track” One day at a time.
exit when (I die)
Maintain the loop invariant
Make a little progress
Make the world a little better.
endloop
next
“postCond:
It was a well spent and good life”
133
Designing an Algorithm
Define Problem Define Loop
Invariants
Exit
Exit
134
Designing an Algorithm
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
135
I like understanding things
using a story.
136
The Getting to School Problem
137
Problem Specification
• Pre condition: location of home and school
• Post condition: traveled from home to school
138
Algorithm
• What is an Algorithm?
139
Algorithm
• The algorithm defines the computation route.
140
Algorithm
• Is this reasonable?
141
Complexity
• There are an infinite number of input instances
• Algorithm must work for each.
142
Complexity
• Difficult to predict where computation might be
in the middle of the computation.
143
Location of Computation
• The current “State” of computation
is determined by values of all
variables.
144
Location of Computation
• Suppose the computation ends up here?
145
Location of Computation
• Suppose the computation ends up here?
146
Don’t Panic
• Where ever the computation might be,
take best step towards school.
good enough
147
General Principle
• Do not worry about the entire computation.
• Take one step at a time!
next
148
Defining Algorithm
• Wherever the computation might be,
take step towards school.
149
Take a step
• What is required of this step?
150
A Measure of Progress
75 km
79 km
151
Defining Algorithm
• Is this sufficient to define a working algorithm?
79 km 75 km
152
Working Algorithm
• Computation steadily approaches goal
79 km 75 km
153
Defining Algorithm
• Extra requirements
79 km 75 km
79 km
km 0 km
78.999 km
154
Define a Step
• Wherever the computation might be,
take best step towards school.
155
Computation Stuck
• Some location too confusing for algorithm
• Algorithm too lazy to define step for every
location.
156
Safe Locations
• Algorithm specifies from which locations
it knows how to step.
157
Loop Invariant
• “The computation is presently in a safe
location.”
• Maybe true and maybe not.
If not something has gone wrong.
158
Defining Algorithm
• From every safe location,
define one step towards school.
159
Take a step
• What is required of this step?
160
Maintain Loop Invariant
• If the computation is in a safe location,
it does not step into an unsafe one.
161
Maintain Loop Invariant
• If the computation is in a safe location,
it does not step into an unsafe one.
162
Maintain Loop Invariant
• If the computation is in a safe location,
it does not step into an unsafe one.
164
Maintain Loop Invariant
165
Maintain Loop Invariant
• By Induction the computation will
always be in a safe location.
S (0)
iS ( i )
iS ( i ) S ( i 1 )
166
Ending The Algorithm
Define Exit Condition Exit
Termination:
With sufficient progress,
0 km Exit
168
Designing an Algorithm Is this sufficient?
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
79 km 75 km
km 0 km Exit Exit
169
Consider an Algorithm
Exit
Exit
170
Loop Invariant Maintained
Exit
171
Computation can always proceed
Exit
172
Computation always makes progress
79 km 75 km
Exit
79 km 75 km
173
Computation Terminates
km
0 km
0 km Exit
79 km 75 km
0 km Exit
174
Computation Terminates
Exit
Exit
175
Consider an Algorithm
Exit
Exit
This is sufficient!
176
Code from LI
It is hard to design an algorithm
(or even worse write its code),
before you understand
the proof of why the algorithm works.
We will
• First design the loop invariant
• From it, we will follow mechanical steps,
ie minimal thinking
and the code will fall out.
We will do an easy example.
but the same ideas can help with many problems.
177
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2
178
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 What is the loop invariant?
Each iteration add in a new object.
Action not picture
If you fight me,
I will fail you
179
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 What is the loop invariant?
What do you want to be true in the middle of the computation?
The post condition is that a set of In the middle of the computation
things have been added together. some prefix of them
have been added together!
i could denote the index of
next object to add in.
Hence, the number added
so far would be i-1.
180
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + 2i2
+ (i-1)
• LoopWhat
Invariant:
do you want tos be
= true
12 +in2the
2
+middle
32 + of…the computation?
The post condition is that a set of In the middle of the computation
things have been added together. some prefix of them
have been added together!
i could denote the index of or i could denote
next object to add in. the number added.
Hence, the number added Hence, the next to add in
so far would be i-1. would be the i+1st .
It is a matter of esthetics. Though it changes everything.
All the matters is that
you are consistent! 181
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s =I =1input
2
+ 22 + 32 + …
S = output
i,s = our current data structure.
The loop invariant tells us a
relationship between i and s.
182
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant:You might
s =be1tempted
2
+ 22 + 32 + …
to think
about the range of i.
Could it be 0 or start at 1?
Does it end at I-1, I, or I+1?
middle
183
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant:You might
s =be1tempted
2
+ 22 + 32 + …
to think
about the range of i.
Could it be 0 or start at 1?
Does it end at I-1, I, or I+1?
Trying to understand
the entire algorithm at once
is the most common reason
for panicking and drowning.
184
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant:You might
s =be1tempted
2
+ 22 + 32 + …
to think
about the range of i.
Could it be 0 or start at 1?
Does it end at I-1, I, or I+1?
Exit
79 km 75 km
• Step to make progress
• i = i+1
• Exit Condition
Exit • Don’t worry about it yet. 186
Code from LI
In order to make progress,
we may head in one direction.
Exit
79 km 75 km
• Step to make progress
• i = i+1
187
Code from LI
In order to make progress,
we may head in one direction.
Exit
79 km 75 km
• Step to make progress
• i = i+1
188
Code from LI
Exit
189
Code from LI
190
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s = 1 2 + 22 + 32 + …
• Partial Step: i = i+1
Maintaining Loop Invariant
Exit
<loop-invariantt>
¬<exit Cond> <loop-invariantt+1>
codeB
next
<loop-invariantt+1>192
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s =(codeB
12 +in2loop)
2
+ 32 + …
• Partial Step: i = i+1
<loop-invariantt> s = 12 + 2 2 + 32 + … + i 2
t t
¬<exit Cond> it = 5
codeB: it+1 = it + 1 (math)
st+1
Write down LHS/RHS
of what we must prove
=12 + 22 + 32 + … + it+12 <loop-invariantt+1>193
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s = 1 2 + 22 + 32 + …
• Partial Step: i = i+1
<loop-invariantt> s = 12 + 2 2 + 32 + … + i 2
t t
¬<exit Cond> it = 5
codeB: it+1 = it + 1
st+1 = st + it+12
?? This completes the proof.
st+1 = st + it+12
=[12 + 22 + 32 + … + it2 ]+ it+12
=12 + 22 + 32 + … + it+12 <loop-invariantt+1>194
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s = 1 2 + 22 + 32 + …
• Partial Step: i = i+1
s = s + i2 it+1 = it + 1
i=i+1 st+1 = st + it+12
We now translate
i=i+1 this math code
s = s + i2
195
Code from LI
Sum( I )
“preCond: The input is an integer I”
?
loop
“loop-invariant: s = 12 + 22 + 32 + …+ i2
exit when (i=I)
i=i+1
s = s + i2
endloop
?
197
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s ,=s 1=2 s++22i2+ 32 + …
• Partial Step: i = i+1
<loop-invariant> s = 12 + 2 2 + 32 + … + i 2
<exit Cond> ?? What math condition do I need here ??
codeC: ?? What math relationship do I need here ??
I need:
• s=S S is our output
• i=I i is our measure of progress
What if I = 7 and I = 4 6 8
It is all scary! 200
loop while( i≠I ) i<I
“loop-invariant” Code from LI
exit when i=I
code
endloop
“i=I”
S=s What is the code to give post condition?
“postCond”
201
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + i2
• Loop Invariant: s ,=s 1=2 s++22i2+ 32 + …
• Partial Step: i = i+1
Establishing Loop Invariant
<preCond>
<loop-invariant>
codeA
i s
i=0
s=0 0 0
1 1
2 1+4=5
3 1+4+9=14 202
Code from LI
Sum( I )
“preCond: The input is an integer I”
i=1
m = A[1]
loop
“loop-invariant: s = 12 + 22 + 32 + …+ i2
exit when (i=I)
i=i+1
s = s + i2
endloop
S=s
return(S)
“postCond: Output = S = 12 + 22 + 32 + …+ I2”
203
Code from LI
• Precond: The input is an integer I.
• Postcond: Output = S = 12 + 22 + 32 + … +
I2 + 2i2
+ (i-1)
• Loop Invariant: s ,=s 1=2 s++22i2+ 32 + …
• Partial Step: i = i+1
That was fun. Let’s do it again.
We define i to be
the number added so far. Let’s redefine this to be i-1.
204
Designing an Algorithm
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
205
Pointers
206
Pointers
207
Pointers
208
Pointers
209
Pointers
210
Pointers
More on Pointers
211
Pointers
212
Pointers
213
Pointers
Oh dear,
• Getting from A to F is a lot of work.
• This F has one record already changed.
so maybe one iteration has happened already.
Let's roll
backwards one
more iteration.
214
Pointers
215
Pointers
Where should done point?
216
Pointers
Where should done point?
None of the nodes are “done”!
Ok
217
Pointers
218
I get nervous. Pointers
Let’s run our generic
loop code once to
make sure it works.
220
Pointers
221
Pointers
222
Pointers
223
Pointers
224
Pointers
225
Pointers
226
Pointers
227
Designing an Algorithm
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
228
Different Representations
of Algorithms
• Code
• Running example
• DFA and Turing Machines
• Higher level abstract view
229
Code
Representation of an Algorithm
class InsertionSortAlgorithm extends SortAlgorithm {
void sort(int a[]) throws Exception {
for (int i = 1; i < a.length; i++) {
int j = i;
int B = a[i];
while ((j > 0) && (a[j-1] > B)) {
a[j] = a[j-1];
j--; }
a[j] = B;
}}
Pros and Cons?
230
Code
Representation of an Algorithm
Pros: Cons:
• Runs on computers • I am not a
• Precise and computer
succinct • I need a higher
• Perception that level of intuition.
being able to code • Prone to bugs
is the only thing • Language
needed to get a job. dependent
(false) 231
Running Example
Representation of an Algorithm
Try out a problem or
solution on small examples.
232
Running Example
Representation of an Algorithm
88 52
98
14
31
98 88
25 30
62 23
14
79
233
Running Example
Representation of an Algorithm
14,23,25,30,31,52,62,79,88,98
237
Higher Level Abstract View
Representation of an Algorithm
1
0 T+
i-1
i
238
Levels of Abstraction
It is hard to think of love in terms of the firing of
neurons.
vs
vs
239
Higher Level Abstract View
Representation of an Algorithm
Pros: Cons:
• Intuitive for • Mathematical
humans mumbo jumbo
• Useful for • Too abstract
– thinking about • Students resist it
– designing
– describing
algorithms
• View from which 240
Value Simplicity
Abstract away the inessential features of a problem
=
Goal: Understand and think about complex
algorithm in simple ways.
Don’t tune out. There are deep ideas within
the simplicity.
241
Simple Example
242
Reconsidering Simple Examples
244
Higher Level Abstract View
Representation of an Algorithm
5 km
9 km
245
Designing an Algorithm
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
246
Problem Specification
• Precondition: The input is a list of n values
with the same value possibly repeated.
• Post condition: The output is a list consisting
of the same n values in non-decreasing order.
88 52
14
31 14,23,25,30,31,52,62,79,88,98
25 98 30
62 23
79
247
Location of Computation
• “State” of computation determined
by values of all variables.
248
Location of Computation
• “State” of computation determined
by values of all variables.
88 52 14
31 25 62 30 14,23,25,30,31,52,62,79,88,98
98 23
79
88 14<52
31 25<62 30
23
79<98
249
Computation Stuck
• Location too confusing for algorithm
• Algorithm too lazy to define step for
every location.
88 52 14
31 25 62 30 14,23,25,30,31,52,62,79,88,98
98 23
79
88 14<52
31 25<62 30
23
79<98
250
Safe Locations
• Algorithm specifies from which locations
it knows how to step.
88 52 14
31 25 62 30 14,23,25,30,31,52,62,79,88,98
98 23
79
251
Define Loop Invariant
• Loop Invariant:
• Input numbers split to left and right.
• Numbers on left are sorted.
88 52 14
31 25 62 30 14,23,25,30,31,52,62,79,88,98
98 23
79
30 14
23,31,52,88 25 62
98
79
252
Location Not Determined
30 23
14,52,62,79 25 88
98
31
30 14
23,31,52,88 25 62
98
79
253
Defining Measure of Progress
30 14
23,31,52,88 25 62
98
79
6 elements
254
Define Step
30 14
23,31,52,88 25 62
98
79
255
Define Step
• Select arbitrary element from side.
• Insert it where it belongs.
30 14
23,31,52,88 25 62
98
79
30 14
23,31,52,62,88 25
98
79
256
Making progress while Exit
30 14
23,31,52,88 25 62
98
79
30 14
6 elements 23,31,52,62,88 25
98
79
257
Beginning & km 0 km Exit Exit
Ending
88 52 14
31 25 62 30
98 23
79 88 52 14
31 25 62 30 n elements
98 23
79
14,23,25,30,31,52,62,79,88,98 0 elements
14,23,25,30,31,52,62,79,88,98
258
Running Time
Inserting an element into a list of size i
takes (i) time.
Total = 1+2+3+…+n =?
30 14
23,31,52,88 25 62
98
79
30 14
23,31,52,62,88 25
98
79
259
Adding Made Easy
1 + 2 + 3 + ... + n-1 + n = S = number of white dots
1 2........n
260
Adding Made Easy
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
261
Adding Made Easy
1 + 2 + 3 + ... + n-1 + n = S = number of white dots
n + n-1 + n-2 + ... + 2 + 1 = S = number of yellow dots
n
2S dots
n
= n(n+1) dots in the
n
grid
n
n (n+1) n
S= 2 n+1 n+1 n+1 n+1 n+1
262
Adding Made Easy
263
Adding Made Easy
264
Running Time
Inserting an element into a list of size i
takes (i) time.
Total = 1+2+3+…+n = (n2)
30 14
23,31,52,88 25 62
98
79
30 14
23,31,52,62,88 25
98
79
265
Algorithm Definition Completed
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
266
Insertion Sort
as a Relay Race
88 52 14
31 25 62 30
98 23
79
267
Establish Loop Invariant
from preconditions
88 52 14
31 25 62 30
98 23
79
268
Maintaining Loop Invariant
88 52 14
31 25 62 30
98 23
79
1
269
Maintaining Loop Invariant
i-1
i
88 52 14
31 25 62 30
98 23
79
270
Maintaining Loop Invariant
88 52 14
31 25 62 30
98 23
79
i
271
Maintaining Loop Invariant
i-1
i
52 14
31,88 25 62 30
98 23
79
272
Maintaining Loop Invariant
52 14
31,88 25 62 30
98 23
79
i
273
Maintaining Loop Invariant
i-1
i
52 14
25,31,88 62 30
98 23
79
274
Maintaining Loop Invariant
52 14
25,31,88 62 30
98 23
79
i
275
Maintaining Loop Invariant
i-1
i
14
25,31,52,88 62 30
98 23
79
276
Maintaining Loop Invariant
14,23,25,30,31,52,62,79,88,98
277
Clean up loose ends
1
T+
14,23,25,30,31,52,62,79,88,98
278
Code from LI - Sorting
You did not formally prove sorting works.
You did this handwavy picture thing.
From that how are we to know what to do?
Oh? Sorry.
I feel I did prove it formally.
I feel I followed the same steps.
But let’s try it again.
279
Code from LI - Sorting
• Precond: A[1,..,n] are numbers.
• Postcond: A[1,..,n] is sorted.
• Loop Invariant: A[1,..,i] is sorted & A[i+1,..,n] are rest.
• Partial Step: i = i+1
Maintaining Loop Invariant
<loop-invariantt>
Exit
¬<exit Cond> <loop-invariantt+1>
codeB
280
Code from LI - Sorting
• Precond: A[1,..,n] are numbers.
• Postcond: A[1,..,n] is sorted.
• Loop Invariant: A[1,..,i] is sorted & A[i+1,..,n] are rest.
• Partial Step: i = i+1
<loop-invariantt> At[1,..,it] is sorted & At[it+1,..,n] are rest
At[1,..,it] is sorted & At[it+1] & At[it+2,..,n] are rest
¬<exit Cond>
codeB: it+1 = it + 1
ng
rt
th i
Inse
What math do we
No
need from codeB?
282
Code from LI - Sorting
• Precond: A[1,..,n] are numbers.
• Postcond: A[1,..,n] is sorted.
• Loop Invariant: A[1,..,i] is sorted & A[i+1,..,n] are rest.
• Partial Step: i = i+1
283
Code from LI - Sorting
• Precond: A[1,..,n] are numbers.
• Postcond: A[1,..,n] is sorted.
• Loop Invariant: A[1,..,i] is sorted & A[i+1,..,n] are rest.
• Partial Step: i = i+1
284
Code from LI - Sorting
• Precond: A[1,..,n] are numbers.
• Postcond: A[1,..,n] is sorted.
• Loop Invariant: A[1,..,i] is sorted & A[i+1,..,n] are rest.
• Partial Step: i = i+1
285
Code from LI - Sorting
• Precond: A[1,..,n] are numbers.
• Postcond: A[1,..,n] is sorted.
• Loop Invariant: A[1,..,i] is sorted & A[i+1,..,n] are rest.
• Partial Step: i = i+1
A[1,..,n] is sorted.
<postcond>
286
Code from LI - Sorting
We said you don’t have
Sum( A ) to do the inner loop.
“preCond: A[1,..,n] are numbers”
i=0
I implored you to not worry
loop about the entire computation.
“loop-invariant:
A[1,..,i] is sorted & A[i+1,..,n] are rest.
exit when (i=n) one concept
at a time.
Insert A[i+1] into A[1,..,i]
endloop
nothing
Trying to understand
“postCond: A[1,..,n]
the entireisalgorithm
sorted” at once
is the most common reason
for panicking and drowning.
287
Code from LI - Sorting
We said you don’t have
Sum( A ) to do the inner loop.
“preCond: A[1,..,n] are numbers”
i=0
loop
“loop-invariant:
A[1,..,i] is sorted & A[i+1,..,n] are rest.
exit when (i=n)
Insert A[i+1] into A[1,..,i] But let’s do it for fun.
endloop
nothing
“postCond: A[1,..,n] is sorted”
288
Code from LI - Inserting
• Precond: a1 , ...,ai is sorted & c
• Postcond: a1,..,aj-1,c,aj,..,ai is sorted
• Loop Invariant: a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
• Partial Step: j = j-1
rt
Inse
What math do we
need from codeB?
At+1[1,..,it,it+1] is sorted
289
Code from LI - Inserting
• Precond: a1 , ...,ai is sorted & c
• Postcond: a1,..,aj-1,c,aj,..,ai is sorted
• Loop Invariant: a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
• Partial Step: j = j-1
• Exit condition: aj-1<c
<loop-invariantt> a1,..,aj-2,aj-1,c,aj,..,ai with a’s sorted & c≤aj
What math do we
need from codeB?
??
a1,..,aj-2,c,aj-1,aj,..,ai with a’s sorted & c≤aj-1
<loop-invariantt+1>
290
Code from LI - Sorting
Insert(a1,...,ai & c)
“preCond: a1, ...,ai is sorted & c”
?
loop
“loop-invariant:
a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
exit when (aj-1<c)
j = j-1
endloop
?
“postCond: a1,..,aj-1,c,aj,..,ai is sorted”
291
Code from LI - Inserting
• Precond: a1 , ...,ai is sorted & c
• Postcond: a1,..,aj-1,c,aj,..,ai is sorted
• Loop Invariant: a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
• Partial Step: j = j-1
• Exit condition: aj-1<c
Establishing Loop Invariant
<preCond>
<loop-invariant>
codeA
292
Code from LI - Inserting
• Precond: a1 , ...,ai is sorted & c
• Postcond: a1,..,aj-1,c,aj,..,ai is sorted
• Loop Invariant: a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
• Partial Step: j = j-1
• Exit condition: aj-1<c
<Precond> a1 , ...,ai is sorted & c
codeA: Put c on end (if not already there)
& j=i+1
294
Code from LI - Inserting
• Precond: a1 , ...,ai is sorted & c
• Postcond: a1,..,aj-1,c,aj,..,ai is sorted
• Loop Invariant: a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
• Partial Step: j = j-1
• Exit condition: aj-1<c
<loop-invariant> a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
Ensure consecutive
pairs are sorted.
a1,..,aj-1,c,aj,..,ai is sorted
<postcond>
295
Code from LI - Sorting
Insert(a1,...,ai & c)
“preCond: a1, ...,ai is sorted & c”
j = i+1
loop
“loop-invariant:
a1,..,aj-1,c,aj,..,ai with a’s sorted & c≤aj
exit when (aj-1<c)
j = j-1
endloop
nothing
“postCond: a1,..,aj-1,c,aj,..,ai is sorted”
296
Ok
I know you knew Insertion Sort
297
Explaining Insertion Sort
We maintain a subset of elements sorted within a
list. The remaining elements are off to the side
somewhere. Initially, think of the first element in
the array as a sorted list of length one. One at a
time, we take one of the elements that is off to the
side and we insert it into the sorted list where it
belongs. This gives a sorted list that is one element
longer than it was before. When the last element
has been inserted, the array is completely sorted.
30 14
23,31,52,88 25 62
98
79
299
Insertion Sort
Selection
What changes?
This was on an exam and most got it
WRONG
• Loop Invariant:
• Input numbers split to left and right.
• Numbers on left are sorted.
• All left ≤ All right
31 88
14,23,25,30 ≤ 52 62
98
79
300
Define Step
31 88
14,23,25,30 ≤ 52 62
98
79
301
Define Step
31 88 88
14,23,25,30 ≤ 52 62 14,23,25,30,31 ≤ 52 62
98 98
79 79
302
Maintaining Loop Invariant
Exit
31 88 88
14,23,25,30 ≤ 52 62 14,23,25,30,31 ≤ 52 62
98 98
79 79
<loop-invariantt>
¬<exit Cond>
codeB
• Numbers on
left were sorted. • Numbers now on
• New number added to end
left are sorted.
came from right.
• All left ≤ All right <loop-invariantt+1>303
Maintaining Loop Invariant
Exit
31 88 88
14,23,25,30 ≤ 52 62 14,23,25,30,31 ≤ 52 62
98 98
79 79
<loop-invariantt>
¬<exit Cond>
codeB
• Was All left ≤ All right
• New number is smallest • Now
from right
All left ≤ All right
<loop-invariantt+1>304
Don't Redo Work 35
X
30 A
• Prefix Averages: 25
• Input: an array X[i] of n values 20
• Output: The average of each 15
prefix 10
A[i] = (X[1] + X[2] + … + X[i])/i 5
0
1 2 3 4 5 6 7
for i = 1 to n do
Which line works the hardest?
% compute A[i] # times it is executed
=1+2+3+….+n n
n
s=0 = O(n2) n
for j = 1 to i do The other lines effect this time n
s=s+
by a multiplicative constant n
X[j]
A[i] = s / i or less n
n n n n n
306
Don't Redo Work
• Prefix Averages:
• Input: an array X[i] of n values
• Output: The average of each
prefix
A[i] = (X[1] + X[2] + … + X[i])/i
i=0; s = 0;
loop
% LI: A[1], … , A[i] computed Which line works the hardest?
% LI: s = X[1] + X[2] + … + X[i] # times it is executed
exit when i = n =n
i = i+1
s = s + X[i]
The other lines effect this time
A[i] = s / i by a multiplicative constant
return A or less
Time(n) = O(n)
(Linear is better than quadratic.)
307
Designing an Algorithm
308
A Starry Night
How did Van Gogh come up with his famous
painting, A Starry Night?
There's no easy answer.
Designing algorithms is an art form.
309
A Sequencevs of Actions
A Sequence of Assertions
Max( a,b,c )
“preCond: Input has 3 numbers.”
m=a
“assert: m is max in {a}”
if( b>m )
m=b
endif
“assert: m is max in {a,b}”
if( c>m )
m=c
endif It is helpful to have
“assert: m is max in {a,b,c}” different ways of
return(m)
looking at it.
“postCond:
return max in {a,b,c}” 310
Designing Loop Invariants
Coming up with the loop invariant is the
hardest part of designing an algorithm.
It requires practice, perseverance, and insight.
Yet from it
the rest of the algorithm
follows easily
311
Use This Process
Don't come up with the loop invariant after
the fact to make me happy.
Use it to design your algorithm.
312
Don’t start coding
You must design a working algorithm first.
313
Exemplification:
Try solving the problem
on small input examples.
315
Picture from the Middle
Leap into the middle of the algorithm.
316
The Work Completed
The loop invariant should state what work
has been completed towards solving the
problem and what works still needs to be
done.
79 km 75 km
317
Flow Smoothly
The loop invariant should flow smoothly from
the beginning to the end of the algorithm.
– At the beginning, it should follow easily from
the preconditions.
– It should progress in small natural steps.
– Once the exit condition has been met, the
postconditions should easily follow.
318
Ask for 100%
A good philosophy in life is to ask for 100%
of what you want,
but not to assume that you will get it.
– What would you like to be true in the middle of
your computation?
319
Ask for 100%
Pretend that a genie has granted your wish.
– You are now in the middle of your computation
and your dream loop invariant is true.
320
Ask for 100%
Maintain the Loop Invariant:
– From here, are you able to take some
computational steps that will make progress
while maintaining the loop invariant?
321
Ask for 100%
• If you can maintain the loop invariant, great.
• If not,
– Too Weak: If your loop invariant is too weak, then the
genie has not provided you with everything you need to
move on.
– Too Strong: If your loop invariant is too strong, then you
will not be able to establish it initially or maintain it.
322
No Assumptions:
Suppose a Martian jumped
into the top of the loop.
All you would know is that <loop-invariant> was true.
It alone must provide enough information
so that, after going around the loop, you can establish
that the loop invariant is again true.
323
Know What a LI Is Not
``the LI is ...''
– code
– A precondition
– A postcondition
– A statement that is always true
Eg: “1+1=2”
– The steps taken by the algorithm
324
Differentiating between Iterations
x=x+2
– Meaningful as code
– False as a mathematical statement
x’ = xi = value at the beginning of the iteration
x” = xi+1 = new value after going around the
loop one more time.
x” = x’+2
– Meaningful as a mathematical statement
325
Max( A )
“preCond: Iterative Algorithms
Input is array A[1..n] Loop Invariants
of n values.”
i=1
m = A[1]
loop
“loop-invariant:
m is max in {A[1]..A[i]}” Step i
exit when (i=n) <loop-invariantt>
m = max(m,A[i+1]) ¬<exit Cond> <loop-invt+1>
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])
m is max in {A[1]..A[n]}” 326
<preCond> <exit Cond> <postCond>
codeA
loop
<loop-invariant>
exit when <exit Cond> Same Miracle
codeB
codeC
<postCond> Miracle
329
Find Errors
If I = -5, s = 1-5 j = 0
330
Find Errors
Variables need to
be documented.
Fine: Purpose outlined
in LI
331
Find Errors
332
Find Errors
333
Find Errors
Let s’ and i’ be
values of s and i
when at the top of the loop.
Let s” and i” be
values after going
around again.
334
Find Errors
LI s’ = j=1i’ j.
Code s” = s’+i’
i” = i’+1.
Together:
s” = (j=1i’ j) + i’.
i’ is added in twice.
i” should be added.
335
Find Errors
i = 1 j=1i j = 1 = s.
336
Find Errors
Exit
Better to say:
loop
exit when i>I
loop-invariant: _____
exit when i > I
337
Find Errors
Exit
LI s = j=1i j.
exit when i>I
Exit i > I
Together:
s = j=1I+1 j.
Post Cond.
338
Find Errors
Test:
• on typical value
i = 3 s=1+2+3.
• on special values
exit when i>I
i = 1 s=1.
i = 0 s=1.
339
Typical Types of Loop Invariants
• Fairy God Mother
• More of the input
• More of the output
• Narrowing the search space
• Shrinking Input Instance
• Data Structure (System) Invariant
340
Typical Types of Loop Invariants
• Fairy God Mother
– I have arrived from Mars, some amount of
progress has been made already and my Fairy
God Mother has set the state of the computation
to be just what I want so that I feel as
comfortable as I could be given the amount of
progress that has been made.
• More of the input
• More of the output
• Narrowing the search space
• Shrinking Input Instance
• Data Structure (System) Invariant 341
Fairy God Mother
343
79 km • Measure of Progress: Fairy God Mother
My distance from center
• Loop Invariant:
He is opposite of me.
It is my job to
• establish
• maintain
• and get the post condition
from this loop invariant
344
Establishing Loop Invariant Fairy God Mother
<preCond> I’m somewhere
codeA Swim to middle He is opposite of me
<loop-invariant>
Similar Triangles
1 r rd His distance =d
d My distance = rd
His time = d/4
My Time = rd
Need rd < d/4
r < 1/4
But he moves 4 times faster!!!
So the circumference of my circle
must be ¼ of his. 347
Fairy God Mother
351
<loop-invariant> Can’t Do Three In Row
<loop-invariantt>
¬<exit Cond> Never stop
codeB: Aaah!
<loop-invariantt+1>352
<loop-invariant> Can’t Do Three In Row
… ? b? w
x ? …
i
353
<loop-invariant> Can’t Do Three In Row
a … x w … ? Impossible
i
b … b
b/x w
x b/x … ?
i
There weren’t blocks of 3 before
c … b ww b
x b/x … ? and we did not create a new one.
i
d … x w x b/x … ? Impossible
i
e … w w x b/x … ? Impossible
i
354
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
Change slides to
2^3 = 8
or 2^4 = 16
b wwwww
wwwwww
Garbage that is ignored
wwwwww
wwwwww This “garbage” might help white
wwwwww because it does not have a black
on one side.
wwwwww
f(i) It might hurt black because he
wwwwww might waste his time putting a
wwwwww black on the other side.
wwwwww But it is too confusing for us to
keep track of.
wwwwww We want a clean picture/loop
wwwwww invariant.
So we ignore it. 356
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
wwwwww
wwwwww
wwwwww
wwwwww
wwwwww
f(i)
wwwwww
wwwwww
wwwwww
wwwwww
wwwwww
357
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
<loop-invariant>
f(n) w w w w w w w ww
=1 <PostConditions>
n 358
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
359
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
<loop-invariantt>
<loop-invariantt+1>360
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
wwwwwww b
wwwwww I think black will go here.
wwwwww
wwwwww But it can be really hard to know
wwwwww where black will go.
f(i) He could do a move that looks
wwwwww
stupid and then win because of it.
wwwwww How do you KNOW he does not
wwwwww have a better move?
wwwwww We have to assume he is SMART.
wwwwww
361
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
wwwwwww
wwwwww b wwwww b
wwwwww Garbage that is ignored
wwwwww
wwwwww Remember the garbage?
f(i) He might go there.
wwwwww
But this would be good for white
wwwwww because white has decided to
wwwwww ignore this block.
wwwwww Because white told black that it
will be ignored, we don’t have to
wwwwww
worry about black going there.
362
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
wwwwwww
wwwwww
Black could go here,
wwwwww b but it is a shorter block.
wwwwww
wwwwww
f(i)
wwwwww
wwwwww
wwwwww
wwwwww
wwwwww
363
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
b wwwwwww
wwwwww
wwwwww Black could go here,
but this is basically the same.
wwwwww
wwwwww The key is that black can kill
f(i) one of our “special” blocks.
wwwwww
wwwwww
wwwwww
wwwwww
wwwwww
364
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
wwwwwww b
wwwwwww b
This really does feel
wwwwwww b like black’s best move.
wwwwwww b
wwwwwww b Garbage
f(i)
wwwwwww b
wwwwwww b
wwwwwww b
wwwwwww b
wwwwwww f(i)/10 Black skip every 10th move.
365
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
i+1
wwwwwww f(i)/10 = f(i+1)
366
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
367
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
f(0)
Done
368
n in a Row
Precondition: Infinite line. Int n. Opponent skips every 10th move.
PostConditions: Continuous block of n whites.
w b
w b
This really does feel
w b like black’s best move.
w b
w b Garbage
f(0)
w b
w b
w b
w b Black skip every 10th move.
w f(0)/10 = f(1)
369
Typical Types of Loop Invariants
• Fairy God Mother
• More of the input
• More of the output
• Narrowing the search space
• Shrinking Input Instance
• Data Structure (System) Invariant
370
More of the Input
Loop Invariant
The input consists of an array of objects
Solution
Extra
372
More of the Input
Loop Invariant
The input consists of an array of objects
Solution
Solution Extra
Extra
Solution
Solution Extra
Extra
Exit
79 km 75 km
i to i+1
374
More of the Input
Loop Invariant
The input consists of an array of objects
Solution
Exit
In the end, we have read in the entire input.
The LI gives us that we have a solution
for this entire input.
375
Insertion Sort
The input consists of an array of integers
52,23,88,31,25,30,98,62,14,79
Solution
23,31,52,88
376
Insertion Sort
The input consists of an array of integers
52,23,88,31,25,30,98,62,14,79
23,31,52,88
23,25,31,52,88
379
More of the Input
Loop Invariant
The input consists of an array of objects
380
More of the Input
Loop Invariant
The input consists of an array of objects
Solution
A student midterm answer:
We have read in the first i objects.
We have a solution for each.
Extra
Solution
A student midterm answer:
We have a solution for the
prefix consisting of elements [1..i].
(plus some additional information)
382
Longest Block of Ones
00101111001100011111000011001
383
Longest Block of Ones
00101111001100011111
384
Longest Block of Ones
00101111001100011111
386
More of the Output
Loop Invariant
The output consists of an array of objects
Exit
i to i+1 Exit
Done when
output n objects.
79 km 75 km
387
Selection Sort
Input:
52,23,88,31,25,30,98,62,14,79
The output consists of an array of integers
14,23,25,30,31,52,62,79,88,98
Done when
Exit
output n objects.
388
More of the Input/Output
Loop Invariant
The input consists of a set of objects
390
Leaning Tower
Input: Lots of 2in wide blocks
Output: Stack them leaning as far over as possible.
391
Leaning Tower
Input: Lots of 2in wide blocks
Output: Stack them leaning as far over as possible.
it will falling
Where should we
put the next block?
it will falling
Where should we
put the next block?
Mass = m1 Mass = m2
mass2
mass1
Center of mass
R
m1r1 m2 r2 Center of mass is 1/i from
m1 m2
11 (i 1) 0 1 right edge of ith block.
1 (i 1) i 398
Leaning Tower
Input: Lots of 2in wide blocks
Output: Stack them leaning as far over as possible.
1
1/i
Add the next block. d i 1.. n 1 ln( n)
i
n ed
1/i
d i 1.. n 1 ln( n)
i
n 2d
https://www.youtube.com/watch?
v=pBYPXsGka74&feature=youtu.be 400
Leaning Tower
Input: Lots of 2in wide blocks
Output: Stack them leaning as far over as possible.
https://www.youtube.com/watch?
v=pBYPXsGka74&feature=youtu.be 401
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.
∞
∞
402
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. ∞
403
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.
404
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.
405
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
406
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
409
Typical Types of Loop Invariants
• Fairy God Mother
• More of the input
• More of the output Three
• Narrowing the search space Binary Search like
• Shrinking Input Instance examples
• Data Structure (System) Invariant
410
Define Problem: Binary Search
• PreConditions
– Key 25
– Sorted List
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
• PostConditions
– Find key in list (if there).
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
411
Define Loop Invariant
• Maintain a sub-list
• Such that
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
412
Define Loop Invariant
• Maintain a sub-list.
• If the key is contained in the original list,
then the key is contained in the sub-list.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
413
Define Step
• Make Progress
• Maintain Loop Invariant
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
414
Define Step
• Cut sub-list in half.
• Determine which half the key would be in.
• Keep that half.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
415
Define Step
• Cut sub-list in half.
• Determine which half the key would be in.
• Keep that half.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
Make Progress 79 km 75 km
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
79 km
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
75 km
418
km
Initial Conditions
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
n km
419
Ending Algorithm Exit
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
0 km
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
422
Algorithm Definition Completed
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
423
Study:
• Many experienced programmers were asked
to code up binary search.
424
Make Precise Definitions
• Maintain a sub-list with end points i & j.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
i j
425
Make Precise Definitions
• Maintain a sub-list with end points i & j.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
i j
Does not matter which,
but you need to be consistent.
426
Make Precise Definitions
• If the sub-list has even length,
which element is taken to be mid?
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
i j i j
mid or mid ?
2 2
427
Make Precise Definitions
• If the sub-list has even length,
which element is taken to be mid?
mid
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
Common Bugs 79 km 75 km
• Second fix,
by making the left half slightly bigger.
• New bug?
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
No progress is made.
Loop for ever! 433
Why is Binary Search so Easy to Get Wrong?
• How many
i j i j
1 mid OR ? possible
2 2 algorithms?
• How many
2 if key L(mid) OR > ?
correct
i = mid i = mid + 1 algorithms?
3 • Probability of
else OR else
success by
guessing?
j = mid -1 j = mid
434
Moral
• Use the loop invariant method to think
about algorithms.
• Be careful with your definitions.
• Be sure that the loop invariant is always
maintained.
• Be sure progress is always made.
435
Loop Invariants
for
Iterative Algorithms
A second
Binary Search like
example
436
Binary Search Tree
• <preCond>
– Key 17
– A binary search tree.
38
25 51
key 17?
17 31 42 63
4 21 28 35 40 49 55 71
• <postCond>
– Find key in BST (if there).
437
Binary Search Tree
Data Structure Invariant
38
25 51
key 17? ≤ ≤
17 31 42 63
4 21 28 35 40 49 55 71
438
Binary Search Tree
Search Loop Invariant
• We have maintained a sub-tree.
• If the key is contained in the original tree,
then the key is contained in this sub-tree.
38
25 51
key 17? ≤ ≤
17 31 42 63
4 21 28 35 40 49 55 71
439
Binary Search Tree
• Cut sub-tree in half.
• Determine which half the key would be in.
• Keep that half.
38
25 51
key 17? ≤ ≤
17 31 42 63
4 21 28 35 40 49 55 71
25 51
key 17?
17 31 42 63
4 21 28 35 40 49 55 71
441
Algorithm Definition Completed
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
442
Loop Invariants
for
Iterative Algorithms
A third
Binary Search like
example
443
Card Trick
• A volunteer, please.
444
Pick a Card
Done
445
Loop Invariant:
The selected card is one of
these.
446
Which column?
left
447
Loop Invariant:
The selected card is one of
these.
448
Selected column is placed
in the middle.
449
I will rearrange the cards.
450
Relax Loop Invariant:
I will remember the same about
each column.
451
Which column?
right
452
Loop Invariant:
The selected card is one of
these.
453
Selected column is placed
in the middle.
454
I will rearrange the cards.
455
Which column?
left
456
Loop Invariant:
The selected card is one of
these.
457
Selected column is placed
in the middle.
458
Here is your card.
Wow!
459
The “Partitioning” Problem
(used in quicksort)
Input: Output:
p=52
88 52 14 88
14 98
31
25 98 30 31 30 23 ≤ 52 ≤ 62
25 79
62 23
79
460
Define Loop Invariant
p=52
31 23 25 98 30 14 62 79 88
≤p p≤
or
p=52
31 23 71 25 98 30 62 79 88
≤p p≤
461
Defining Measure of Progress
p=52
31 23 25 98 30 14 62 79 88
4 elements
not
looked at
462
Define Step
• Make Progress
• Maintain Loop Invariant
p=52
31 23 25 98 30 14 62 79 88
≤p p≤
463
Define Step
• Make Progress
• Maintain Loop Invariant
p=52
31 23 25 98 30 14 62 79 88
≤p p≤
p=52
31 23 14 25 98 30 62 79 88
≤p p≤
464
Define Step
Four cases
p=52 p=52
31 23 25 98 30 14 62 79 88 31 23 14 25 98 30 62 79 88
31 23 14 25 98 30 62 79 88 31 23 14 25 98 30 62 79 88
p=52 p=52
31 23 25 98 30 71 62 79 88 31 23 71 25 98 30 62 79 88
31 23 25 98 30 71 62 79 88 31 23 25 98 30 71 62 79 88
465
Beginning & km 0 km Exit Exit
Ending
p=52
88 52 14
31 25 62 30
98 23 p=52
79
88 25 31 98 62 14 30 79 23
n-1 elements
not
looked at
p=52
23 30 25 31 14 62 98 79 88
0 elements
25 62
not 79
looked at 23 31 14 ≤ 52 ≤ 98
30 88
466
Running Time
Each iteration takes (1) time.
Total = (n)
p=52
31 23 25 98 30 14 62 79 88
≤p p≤
p=52
31 23 14 25 98 30 62 79 88
≤p p≤
467
Algorithm Definition Completed
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
468
31 88 25 52 98 62 14 30 79 23
p=52 88 25 31 98 62 14 30 79 23
23 88 25 31 98 62 14 30 79
23 25 31 98 62 14 30 79 88
23 25 31 98 62 14 30 79 88
Tracing an
23 30 25 31 98 62 14 79 88
Example
23 30 25 31 98 62 14 79 88
23 30 25 31 98 62 14 79 88
23 30 25 31 62 14 98 79 88
23 30 25 31 14 62 98 79 88
23 30 25 31 14 62 98 79 88
469
Typical Types of Loop Invariants
• Fairy God Mother
• More of the input
• More of the output
• Narrowing the search space
• Shrinking Input Instance
An instance has been produced that is smaller
than the actual instance and these two instances
require the same output. Hence, it is sufficient for
me to forget about the actual instance and simply
give the output for the new smaller one.
• Data Structure (System) Invariant
470
GCD(a,b) Greatest Common Divisors
Input: <a,b> = <64,44>
Output: GCD(a,b) = 4
<a,b> <a-b,b>
GCD(a,b) = GCD(a-b,b)
GCD(64,44) = GCD(20,44) = 4
Maintain values <x,y> such that
GCD(a,b) = GCD(x,y)
Let xt and yt be the values of x and y at the
beginning of the iteration and let xt+1 and
yt+1 be their value after going around again.
471
GCD(a,b) Greatest Common Divisors
Input: <a,b> = <64,44>
Output: GCD(a,b) = 4
<a,b> <a-b,b>
GCD(a,b) = GCD(a-b,b)
GCD(64,44) = GCD(20,44) = 4
Maintain values <x,y> such that
GCD(a,b) = GCD(x,y)
<loop-invariant
¬<exit Cond> t> GCD(a,b) = GCD(xt,yt)
codeB: xt+1 = xt-yt , yt+1 = yt
GCD(a,b) = GCD(xt,yt) = GCD(xt-yt,yt) = GCD(xt+1,yt+1)
<loop-invariant >472
GCD(a,b) Greatest Common Divisors
Input: <a,b> = <64,44>
Output: GCD(a,b) = 4
<a,b> <a-b,b>
GCD(a,b) = GCD(a-b,b)
GCD(64,44) = GCD(20,44) = 4
Maintain values <x,y> such that
GCD(a,b) = GCD(x,y)
Exit
79 km 75 km
codeB: xt+1 = xt-yt
x is smaller
473
GCD(a,b)
Input: <a,b> = <64,44>
<x,y> = <64,44>
= <20,44> GCD(a,b) = GCD(x,y)
= <44,20>
= <24,20>
= < 4,20>
= <20, 4>
= <16, 4>
= <12, 4>
Running time?
= < 8, 4>
= < 4, 4>
= < 0, 4>
GCD(a,b) = GCD(x,y) = 4 474
GCD(a,b)
Input: <a,b> = <9999999999999,2>
<x,y> = <9999999999999,2>
= <9999999999997,2>
= <9999999999995,2>
= <9999999999993,2>
= <9999999999991,2>
Time = O(a)
Poly Time?
475
Running Time
Grade School vs Kindergarten
********
********
a × b = a + a + a + ... + a
********
********
********
n2 ********
********
b
********
********
********
****************
83920
478
Size of Input Instance
5
83920 1’’
2’’
• Size of - n = 2 in2
paper - n = 17 bits
Which are reasonable?
• # of bits - n = 5 digits
• # of digits - n = 83920
• Value
479
Size of Input Instance
5
83920 1’’
2’’
• Size of - n = 2 in2 • Intuitive
paper - n = 17 bits
• # of bits - n = 5 digits
• # of digits - n = 83920
• Value
480
Size of Input Instance
5
83920 1’’
2’’
• Size of - n = 2 in2 • Intuitive
paper - n = 17 bits • Formal
• # of bits - n = 5 digits
• # of digits - n = 83920
• Value
481
Size of Input Instance
5
83920 1’’
2’’
• Size of - n = 2 in2 • Intuitive
paper - n = 17 bits • Formal
• # of bits • Reasonable
- n = 5 digits
• # of digits - n = 83920 # of bits =
3.32 * # of digits
• Value
482
Size of Input Instance
5
83920 1’’
2’’
• Size of - n = 2 in2 • Intuitive
paper - n = 17 bits • Formal
• # of bits • Reasonable
- n = 5 digits
• Unreasonable
• # of digits - n = 83920
• Value # of bits = log2(Value)
Value = 2# of bits
483
The Time Complexity of an Algorithm
• Specifies how the running time
• depends on the size of the input.
A function mapping
Work for me to
give you the instance.
Work for you to
“size” of input read the instance.
t
i
m
e
input size
For big inputs, n << n2 << 2n
489
Cryptography
n = # of bits vs N = value
490
Cryptography
n = # of bits vs N = value
Work for me to
give you the instance. n= 28
n= 28
Work for you to N = 3495284884937375456635355579
solve it. Time = 1028
>> Age of universe 493
Is this Silly?
n = # of bits vs N = value
I know other professors
would give this answer.
494
Is this Silly?
n = # of bits vs N = value
If N is one 32-bit integer,
then N≤231
Silly(N)
loop i = 1..N
Print( “HI”)
Time = O(N) = 2O(n) = O(1)
Size = n = O(log(N))
N = 2O(n)
495
Is this Silly?
n = # of bits vs N = value
If N is stored in n
32-bit integers
A function mapping
Work for me to
give you the instance.
Work for you to
“size” of input read the instance.
498
The Time Complexity of an Algorithm
N 1 value op
# values standard n standard O(1)
499
GCD(a,b)
Input: <a,b> = <9999999999999,2>
<x,y> = <9999999999999,2>
= <9999999999997,2>
= <9999999999995,2>
= <9999999999993,2>
= <9999999999991,2>
Work for me to
give you the instance. n= 13
501
GCD(a,b)
Speedup
<x,y> <x-y,y>
<x-2y,y>
<x-3y,y>
<x-4y,y>
<x-iy,y>
<remainder with x/y,y>
= <x mod y,y>
<y,x mod y> smaller than y
502
GCD(a,b)
Input: <a,b> = <44,64>
<x,y> = <44,64>
= <64,44> GCD(a,b) = GCD(x,y)
= <44,20> <x,y> <y,x mod y>
= <20, 4>
= < 4, 0>
GCD(a,b) = GCD(x,y) = 4
503
GCD(a,b)
Input: <a,b> = <10000000000001,9999999999999>
<x,y> = <10000000000001,9999999999999>
= <9999999999999,2>
Little progress
= <2,1> Lots of progress
= <1,0>
GCD(a,b) = GCD(x,y) = 1
505
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
506
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
507
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
509
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
79 km • Measure of Progress
• x.
Exit
79 km 75 km
• Step to make progress
• x = x-1
• Exit Condition
Exit • x=0 511
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
512
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
Exit
79 km 75 km
• Step to make progress
• x = x-1
514
Multiplying
In order to make progress,
we may head in one direction.
Exit
79 km 75 km
• Step to make progress
• x = x-1
515
Multiplying
In order to make progress,
we may head in one direction.
Exit
79 km 75 km
• Step to make progress
• x = x-1
516
Multiplying
Exit
517
Multiplying
518
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
next
<loop-invariantt+1>520
Multiplying
Exit
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
<loop-invariantt> XY = xtyt + st
¬<exit Cond> xt > 0
codeB: xt+1 = xt - 1 yt+1 = yt
st+1 = (st ?)
X Y
Write down LHS/RHS
of what we must prove
= xt+1yt+1 + st+1 <loop-invariantt+1>521
Multiplying
Exit
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
<loop-invariantt> XY = xtyt + st
¬<exit Cond> xt > 0
codeB: xt+1 = xt - 1 yt+1 = yt
st+1 = (st ?)
XY = xtyt + st Use the LI
XY = xtyt + st
= xtyt – yt + (st + yt)
= (xt -1)yt + (st ?)
= x y +s <loop-invariantt+1>525
Designing an Algorithm
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
526
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
83920 1’’
2’’
• Size of - n = 2 in2 • Intuitive
paper - n = 17 bits • Formal
• # of bits • Reasonable
- n = 5 digits
• Unreasonable
• # of digits - n = 83920
• Value # of bits = log2(Value)
Value = 2# of bits
528
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
Exit
79 km 75 km
• Step to make progress
• x = x/2
531
Multiplying
Exit
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
<loop-invariantt> XY = xtyt + st
¬<exit Cond> xt > 0
codeB: xt+1 = xt /2 yt+1 = (yt ?)
st+1 = (st ?)
XY = xtyt + st Use the LI
XY = xtyt + st
538
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
539
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Ethiopian Algorithm (Practice Test 1):
s=0
x=X, y=Y
loop
exit when x=0
if(x is even)
x = x/2, y=2y x=x–u
else (Where u is as large
x = x-1, s = s+Y as possible.)
return(s)
540
Multiplying
Exit
• Precondition: Integers X and Y.
• Postcondition: XY
• Loop Invariant: XY = xy + s
<loop-invariantt> XY = xtyt + st
¬<exit Cond> xt > 0
codeB: xt+1 = xt - ut yt+1 = yt
st+1 = (st ?)
XY = xtyt + st Use the LI
XY = xtyt + st
= xtyt – utyt +
= (xt -ut)yt + (st ?)
= x y +s <loop-invariantt+1>544
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Faster Algorithm:
s=0
x=X, y=Y
loop
exit when x=0
x = x-u[i]
s = s+v[i] with v[i] = u[i]Y
return(s)
• Faster Algorithm:
u[i] = 2i, v[i] = 2iY, i = 0..?
s=0
x=X, y=Y
i = the last value in producing u[i]
loop
exit when x=0
while( x < u[i] ) i=i-1
x = x-u[i] But we don’t want x
to become negative.
s = s+v[i]
return(s)
548
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Faster Algorithm:
u[i] = 2i, v[i] = 2iY, i = 0..?
s=0
x=X, y=Y
i = the last value in producing u[i]
loop
exit when x=0
while( x < u[i] ) i=i-1
x = x-u[i] Actually we will see that
s = s+v[i] u[i] gets used once if the ith bit of x is 1
return(s) and zero times if its is 0.
549
Multiplying
x3 = 1102
x = 10101102
u[0] = 12
u[1] = 102
u[2] = 1002
u[7] = 100000002
Exit
79 km 75 km
• Step to make progress
• i = i-1
551
Multiplying
In order to make progress,
we may head in one direction.
Exit
79 km 75 km
• Step to make progress
• i = i-1
552
Multiplying
In order to make progress,
we may head in one direction.
Exit
79 km 75 km
• Step to make progress
• i = i-1
553
Multiplying
Exit
554
Multiplying
555
Multiplying
Loop Invariants given in Practice Test:
LI0: u[i] = 2i, v[i] = 2iY, i = 0..?
LI1: X × Y = x × Y + s Exit
LI2: x ≥ 0
LI3: x < 2i = u[i]
LI2: x ≥ 0
LI3: x < 2i = u[i]
There are two case:
557
Multiplying
Loop Invariants given in Practice Test:
LI0: u[i] = 2i, v[i] = 2iY, i = 0..?
LI1: X × Y = x × Y + s
LI2: x ≥ 0
LI3: x < 2i = u[i]
There are two case:
xt+1 = xt - u[it-1]
st+1 = st + v[it-1]
This reestablishes LI1.
559
Multiplying
• Precondition: Integers X and Y.
• Postcondition: XY
• Faster Algorithm:
u[i] = 2i, v[i] = 2iY, i = 0..?
s=0
x=X, y=Y
i=
loop the last value in producing u[i]
exit when x=0
i = i-1
if( x > u[i] )
x = x-u[i]
s = s+v[i]
return(s) 560
Typical Types of Loop Invariants
• Fairy God Mother
• More of the input
• More of the output
• Narrowing the search space
• Shrinking Input Instance
• Data Structure (System) Invariant
561
Data Structure Invariants
How about invariants for
a data structure or a system?
Implementer
564
Data Structure Invariants
Establishing Loop Invariant
Constructor
preCondConstructor InvariantsData Struc
565
Data Structure Invariants
Clean up loose ends
InvariantsData Struc
Exit Destructor postCondData Struc
preCondDestructor
A destructor for the data structure
must clean up loose ends.
566
Data Structure Invariants
Maintaining Loop Invariant
Exit
InvariantsData Struc t InvariantsData Struc t+1
Push Operation
preCondPush postCondPush
Assume we fly in from Mars and InvariantsData Struc t is true:
567
Data Structure Invariants
InvariantsData Struc t
preCondPush
next
postCondPush
InvariantsData Struc t+1
568
Data Structure Invariants
InvariantsData Struc t
preCondPush
Don’t panic.
Just draw the pictures
and move the pointers.
postCondPush
InvariantsData Struc t+1
569
Data Structure Invariants
InvariantsData Struc t
preCondPush
postCondPush
InvariantsData Struc t+1
570
Data Structure Invariants
InvariantsData Struc t Special Case: Empty
preCondPush
postCondPush
InvariantsData Struc t+1
571
Data Structure Invariants
InvariantsData Struc t
preCondFind
Suppose you want to
find where the new
element 6 would go?
postCondFind
We want the location sandwiched.
572
Data Structure Invariants
InvariantsData Struc t
preCondFind
postCondFind
We want the location sandwiched.
573
Data Structure Invariants
How do you
initialize this walk?
574
Data Structure Invariants
575
Data Structure Invariants
Exit
576
Data Structure Invariants
preCondInsert The location for the new
element has been found.
PostCondInsert The new element has been
inserted where it belongs.
postCondFind
We want the location sandwiched.
577
Data Structure Invariants
preCondInsert The location for the new
element has been found.
PostCondInsert The new element has been
inserted where it belongs.
postCondInsert
Invariants 578
Bucket (Quick) Sort for Humans
Input: A pile of things to sort.
(Ex: Exams)
Output: Sort them
Requirements:
• Easy to execute by humans
• Only can have 7 piles on desk
• Can move one exam (or pile)
at a time.
• Fast – O(nlog(n)) time.
Likely the only algorithm
in this course which you will
execute by hand yourself. 579
Bucket (Quick) Sort for Humans
Input: [A-Z]
580
Bucket (Quick) Sort for Humans
[A-Z]
Psychology study:
Humans can only think about
5 things at once.
[A-E]
Is the first letter [F-K]
of the name in [L-O]
on the first exam [P-T]
[U-Z]
581
Bucket (Quick) Sort for Humans
[A-Z]
Bucket Sort
• Put exams one at a time
in the correct bucket. 582
Bucket (Quick) Sort for Humans
[A-E]
[] [F-K]
[L-O]
sorted [P-T]
[U-Z]
Bucket Sort
• Put exams one at a time
in the correct bucket. 584
Bucket (Quick) Sort for Humans
[A]
[] [B]
[C]
[D]
sorted [E]
[F-K]
[L-O]
[P-T]
[U-Z]
A sorted pile (upside down)
• before all the rest
A stack of piles
• each pile unsorted
• all in one pile before all in next 585
Bucket (Quick) Sort for Humans
[A]
[] [B]
[C]
[D]
sorted [E]
[F-K]
[L-O]
[P-T]
[U-Z]
586
Bucket (Quick) Sort for Humans
[AA-AE]
…
[]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
…
[]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
[AA-AE]
588
Bucket (Quick) Sort for Humans
[AF-AK]
…
[]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
[AA-AE]
When sufficiently small
sort by hand sorted
589
Bucket (Quick) Sort for Humans
[AF-AK]
…
[AA-AE]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
…
[AA-AE]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
[AF-AK]
sorted
591
Bucket (Quick) Sort for Humans
[AL-AO]
…
[AA-AK]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
…
[AA-AK]
[AU-AZ]
[B]
…
sorted
[E]
[F-K]
…
[U-Z]
[AL-AO]
[AP-AT]
[AU-AZ]
sorted
sorted
sorted
593
Bucket (Quick) Sort for Humans
[B]
…
[AA-AZ]
[E]
[F-K]
sorted
…
[U-Z]
594
Bucket (Quick) Sort for Humans
[B]
…
[A]
[E]
[F-K]
sorted
…
[U-Z]
…
[E]
sorted [F-K]
…
[U-Z]
596
Bucket (Quick) Sort for Humans
[BA-BE]
…
[A]
[BU-BZ]
[C]
…
sorted
[E]
[F-K]
…
[U-Z]
sorted
[ZU-ZZ]
sorted
598
Bucket (Quick) Sort for Humans
[A-Z]
sorted
Exit
599
Bucket (Quick) Sort for Humans
Fast – O(nlog(n)) time.
• Each time you touch an exam,
the size of its pile goes
down by a factor of 5
• Hence I touch it only log5n times.
(Assuming names distributed
randomly through alphabet)
[A-Z]
600
RadixSort 344
125
224
Input: A of stack of N punch cards. 125
225
Each card contains d digits. 333
325
Each digit between 0&(k-1) 134
Output: Sort the cards. 224 333
334 134
Bucket Sort Machine: 143 334
Select one digit 225 344
Separates cards into k piles. 325 143
wrt selected digit. 243 243
Stable sort: If two cards are the same for that digit,
their order does not change. 601
RadixSort
344 125 125
125 134 224
333 Sort wrt which 143 Sort wrt which 225
134 digit first? 224 digit Second? 325
224 225 134
334 The most 243 The next most 333
143 significant. 344 significant. 334
225 333 143
325 334 243
243 325 344
All meaning in first sort lost.
602
RadixSort
344 333 224
125 143 125
333 Sort wrt which 243 Sort wrt which 225
134 digit first? 344 digit Second? 325
224 134 333
334 The least 224 The next least 134
143 significant. 334 significant. 334
225 125 143
325 225 243
243 325 344
603
RadixSort
344 333 2 24
125 143 1 25
333 Sort wrt which 243 Sort wrt which 2 25
134 digit first? 344 digit Second? 3 25
224 134 3 33
334 The least 224 The next least 1 34
143 significant. 334 significant. 3 34
225 125 1 43
325 225 2 43
243 325 3 44
Is sorted wrt first i digits.
604
RadixSort
2 24 1 25
1 25 1 34
2 25 Is sorted wrt 1 43 Is sorted wrt
3 25 first i digits. 2 24 first i+1 digits.
3 33 2 25
1 34 2 43
3 34 These are in the
1 43 3 25 correct order
2 43 Sort wrt i+1st 3 33 because sorted
3 44 digit. 3 34 wrt high order digit.
i+1
3 44
605
RadixSort
2 24 1 25
1 25 1 34
2 25 Is sorted wrt 1 43 Is sorted wrt
3 25 first i digits. 2 24 first i+1 digits.
3 33 2 25
1 34 2 43
3 34 These are in the
1 43 3 25 correct order
2 43 Sort wrt i+1st 3 33 because was sorted &
3 44 digit. 3 34 stable sort left sorted.
i+1
3 44
606
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 3 3 3
607
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Stable sort: If two records are the same for that digit,
their order does not change.
Therefore, the 4th record in input with digit 1 must be
the 4th record in output with digit 1.
It belongs in output index 8, because
8 records go before it
ie 5 records with a smaller digit & Count
3 records with the same digit These608
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output:
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
# of records with digit v: 5 9 3 2
Value v: 0 1 2 3
# of records with digit v: 5 9 3 2
# of records with digit < v: 0 5 14 17
N records, k different values. Time to count? (k2)
Too much
610
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output:
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
# of records with digit v: 5 9 3 2
# of records with digit < v: 0 5 14 17
Computed
N records, k different values. Time to count? (k) 611
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
# of records with digit < v: 0 5 14 17
Location of first record
with digit v.
612
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 ? 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of first record 0 5 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
613
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 0 5 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
614
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 0 6 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
615
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 1 6 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
616
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 6 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
617
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 7 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
618
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 7 14 18
with digit v.
Algorithm: Go through the records in order
putting them where they go.
619
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 8 14 18
with digit v.
Algorithm: Go through the records in order
putting them where they go.
620
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 1 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 9 14 18
with digit v.
Algorithm: Go through the records in order
putting them where they go.
621
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 1 1 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 9 14 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
622
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 10 14 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
623
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 3 10 14 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
624
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 1 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 3 10 15 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
625
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 1 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 3 10 15 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
626
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 5 14 17 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
Time = (N) (Counting Ops)
Total = (N+k) ×(log N + log k) (bit ops) 627
Radix/Counting Sort
Input: N numbers, each L bits long.
Output: Sort the numbers.
111101110100101000101
N 101001010100010110111
110100011010011110101
L
111 101 110 100 101 000 101
N 101 001 010 100 010 110 111
110 100 011 010 011 110 101
= log k (k will be set to N)
d = L / log k 628
Radix/Counting Sort
Input: N numbers, each L bits long.
Each card contains d digits.
Each digit between 0&(k-1)
Output: Sort the numbers.
Use Radix Sort: Sort wrt each digit using Counting Sort.
d = L / log k
629
Radix/Counting Sort
630
Radix/Counting Sort
“Linear” Time
But sorting should take (N log N) time!!! 632
Radix/Counting Sort
Time = (L × N) bit ops (N log N) bit ops
Size = N numbers, each L bits long.
= (L × N)
634
A Upper Bound on Sorting
MergeSort Sorting 10 n log(n)
$ A, " I, [ A(I) = P(I) and Time(A,I) ≤ Tupper(|I|)]
I have an algorithm A that I
claim works and is fast.
Oh yeah, I have an input I for
which it does not .
I win if A on input I gives
• the right output and
• runs fast enough.
I win!
635
A Lower Bound on Sorting
??? Sorting 0.1 n log(n)
" A, $ I, [ A(I) = P(I) or Time(A,I) ³ Tlower(|I|)]
I have an algorithm A that I
claim works and is fast.
Oh yeah, I have an input I for
which it does not .
I win if A on input I gives
• the wrong output or
• runs slow.
I win!
636
A Lower Bound on Sorting
Narrowing the Search Space Loop Invariant
• If the key is contained in the original list, then
it is contained in the narrowed subspace.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
638
A Lower Bound on Sorting
Narrowing the Search Space Loop Invariant
• If the key is contained in the original list, then
it is contained in the narrowed subspace.
instance on which
the algorithm fails
639
A Lower Bound on Sorting
My t+1st time step
79 km 75 km • a7 < a2
• 5th bit of a7 + a2
• Any yes/no question
I Imaintain
keep thealarger
set ofhalf.
instances
Oh dear!
The first t time steps of my
1 2 3 4 5 6 7 8
algorithm A are the same
3 2 5 8 6 1 4 7
given any of these
4 7 5 2 8 1 3 6 instances!!!
5 4 2 3 6 8 1 7
Now, its t+1st step is the same.
640
A Lower Bound on Sorting
Initially, I have n! Permutations.
After t time steps I have N!/2t
/2 factors each at least N/2. N
N! = 1 × 2 × 3 × … × N/2 × … × N
N factors each at most N.
N! NN
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
641
A Lower Bound on Sorting
Initially, I have n! Permutations.
After t time steps I have N!/2t
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
642
Exit
A Lower Bound on Sorting
I give you algorithm A
I must output an instance I claim it sorts.
4 7 5 2 8 1 3 6
643
Exit
A Lower Bound on Sorting
I must output an instance
4 7 5 2 8 1 3 6
644
Exit
A Lower Bound on Sorting
Oops!
I must output an instance
4 7 5 2 8 1 3 6 I must give
5 4 2 3 6 8 1 7 the wrong
answer on
on which alg A either
• Takes too much time one of these!
• Or gives the wrong answer
646
End Iterative Algorithms
Math Review
Recursive Algorithms
647
A Lower Bound on Sorting
648
A Lower Bound on Sorting
• Sorting
Values:
– Input: Indexes:
23 67 34 21 87 3 13 11
1 2 3 4 5 6 7 8
Values: 3 11 13 21 23 34 67 87
– Output: Indexes: 6 8 7 4 1 3 2 5
• Lower Bound:
– Input: An Algorithm for Sorting A
Values: 23 67 34 21 87 3 13 11
– Output: An instance I Indexes: 1 2 3 4 5 6 7 8
on which alg A either
• Takes too much time
• Or gives the wrong answer
Values: 11 3 13 21 23 34 67 87
Indexes: 8 6 7 4 1 3 2 5
649
Algorithm Definition Completed
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
650
A Lower Bound on Sorting
I give you algorithm A
I claim it sorts.
I must output an instance
Values: 23 67 34 21 87 3 13 11
Indexes: 1 2 3 4 5 6 7 8
It might as well be a
permutation of 1..N
(indexes not shown)
4 7 5 2 8 1 3 6
651
A Lower Bound on Sorting
I give you algorithm A
I claim it sorts.
Need to know
what the algorithm does
before we can know
what input to give it.
653
A Lower Bound on Sorting
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
Oh dear!
The first t time steps of my
algorithm A are the same given
any of these instances!!!
654
A Lower Bound on Sorting
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
Oh dear!
The first 0 time steps of my
algorithm A are the same given
any of these instances!!!
655
A Lower Bound on Sorting
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
• a7 < a2
• 5th bit of a7 + a2
• Any yes/no
question
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
• a7 < a2
• 5th bit of a7 + a2
I partition my instances based on
• Any yes/no
what they do on this t+1st step.
question
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
4 7 5 2 8 1 3 6
5 4 2 3 6 8 1 7
659
A Lower Bound on Sorting
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
Oh dear!
The first t+1 time steps of my
algorithm A are the same given
any of these instances!!!
660
A Lower Bound on Sorting
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
661
A Lower Bound on Sorting
1 2 3 4 5 6 7 8
3 2 5 8 6 1 4 7
N! = 1 × 2 × 3 × … × N/2 × … × N
N factors each at most N.
N! NN
664
Exit
A Lower Bound on Sorting
I must output an instance
Values: 23 67 34 21 87 3 13 11
Indexes: 1 2 3 4 5 6 7 8
665
Exit
A Lower Bound on Sorting
Oops!
I must output an instance
I must give
Values: 23 67 34 21 87 3 13 11
Indexes: 1 2 3 4 5 6 7 8
the wrong
answer on
on which alg A either
• Takes too much time one of these!
• Or gives the wrong answer
667
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 i is increasing
• Step: i = i+2
• Exit Cond: i=131
668
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 ??
• Step: i = i+2
• Exit Cond: i=131
Establishing Loop Invariant
<preCond>
<loop-invariant>
codeA
Exit
79 km 79 km 75 km km
Trivially true
Let measure be the value of i
(or 131-i)
672
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 ??
• Step: i = i+2
• Exit Cond: i=131
? 0 km Exit
Sufficient progress is made
but alg fails to exit!
673
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 ??
• Step: i = i+2
• Exit Cond: i=131
≥
674
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 ?? i is odd
• Step: i = i+2
• Exit Cond: i≥131
675
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 i is odd
• Step: i = i+2
• Exit Cond: i≥131
Establishing Loop Invariant
<preCond> i=0
codeA ? i = i+1
i is odd
<loop-invariant>
676
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 i is odd & i ≤ 132
• Step: i = i+2
• Exit Cond: i≥131
Clean up loose ends
<loop-invariant> i is odd & i ≤ 132
Exit
<exit Cond> i≥131
codeC
i=131
<postCond> 677
Find Errors
• PreCond: i=0 • Loop Invariant:
• PostCond: i=131 i is odd & i ≤ 132
• Step: i = i+2
• Exit Cond: i≥131
Maintaining Loop Invariant
<loop-invariant> i’ is odd and ≤132
Exit
¬<exit Cond> i’ < 131
codeB i’’ = i’+2
<loop-invariant> 678
Algorithm Definition Completed
Define Problem Define Loop Define Measure of
Invariants Progress
79 km
Exit
79 km 75 km
km 0 km Exit Exit
679
More Pointers
680
More Pointers
2039 0
element next
head 2039
8715 9321
element next
temp 8715
2039 8715
element next
element next
head 2039
8715 9321
element next
temp 8715
element next
9321
Name Location Value
of box of box in box
head head.next = temp.next
Location Value
head.next of box x in box y
temp.next
red = stepped on put green = go
682
More Pointers
A classic line
2 to move along 2
2 a linked list. 2
2 684
More Pointers
3 3
3 3
3
685
More Pointers
done = todo;
todo = todo.next;
todo.next = done;
Oops!
We don’t want to
drop the old values
getting the new.
23
13
12 686
More Pointers
No.
done = todo;
The value of
todo changes. todo = todo.next;
todo.next = done;
done-old = done;
todo-old = todo;
todo-next-old = todo.next;
done = todo-old;
23
13 todo = todo-next-old;
12 todo.next = done.old; 687
More Pointers
No.
done = todo;
The value of
todo changes. todo = todo.next;
todo.next = done;
done-old = done;
todo-old = todo;
todo-next-old = todo.next;
done = todo-old;
23
13 todo = todo-next-old;
12 todo-old.next = done-old; 688
More Pointers
Good
done = todo;
But fewer
todo = todo.next;
lines?
todo.next = done;
done-old = done;
todo-old = todo;
todo-next-old = todo.next;
todo.next = done-old;
23
13 done = todo-old;
12 todo = todo-next-old; 689
More Pointers
Good
done = todo;
But fewer
todo = todo.next;
lines?
todo.next = done;
Node temp = todo.next;
todo.next = done;
Can now be
stepped on.
10
0 1
690
More Pointers
Good
done = todo;
But fewer
todo = todo.next;
lines?
todo.next = done;
Node temp = todo.next;
todo.next = done;
done = todo;
Can now be
stepped on.
12
2 691
More Pointers
Good
done = todo;
But fewer
todo = todo.next;
lines?
todo.next = done;
Node temp = todo.next;
todo.next = done;
done = todo;
todo = temp;
Can now be
stepped on.
3
23 692
Cake Cutting
Precondition: A cake and the preferences of n people.
The area under a piece of cake states how much the person values it.
Area under the entire cake is 1.
Postcondition: Partition the cake among the n people
so that each gets an amount that SHE personally values ≥ 1/n.
693
Cake Cutting
Precondition: A cake and the preferences of n people.
Area ≥ i/n
Area ≥ i/n
loop Some n-i people have pieces that they are happy with.
inv: The remaining i people value the remaining cake ≥ i/ . 694
Cake Cutting
Precondition: A cake and the preferences of n people.
i-1
Area ≥ /ni
Area = /n
1
6
8 9
696
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Read the middle value.
6 If too big?
4 Now what?
6
6
4
697
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
6 If the key is contained in the original list,
4 then the key is contained in the sub-list.
6
Now what?
6
4 What is the running time
till all possible 5’s are eliminated?
9
What would be a prettier
narrowed search space?
698
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
6 Look at the bottom right corner.
Does not feel right.
699
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
4 then the key is contained in the sub-list.
Look at the top right corner.
700
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
6 Look at the top right corner.
This maintains the loop invariant.
701
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
6 Look at the top right corner.
This maintains the loop invariant.
Running time?
9 - Each iteration eliminates a row or col.
702
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
6 Look at the top right corner.
This maintains the loop invariant.
Running time?
9 - Each iteration eliminates a row or col.
703
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
4 Look at the top right corner.
This maintains the loop invariant.
Running time?
9 - Each iteration eliminates a row or col.
704
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
4 This maintains the loop invariant.
Running time?
9 - Each iteration eliminates a row or col.
705
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
This maintains the loop invariant.
6
Running time?
9 - Each iteration eliminates a row or col.
706
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
This maintains the loop invariant.
6
Running time?
9 - Each iteration eliminates a row or col.
707
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
This maintains the loop invariant.
4
Running time?
9 - Each iteration eliminates a row or col.
708
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
This maintains the loop invariant.
4 Running time?
9 - Each iteration eliminates a row or col.
709
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
This maintains the loop invariant.
Running time?
4 9 - Each iteration eliminates a row or col.
710
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 Maintain a sub-list.
If the key is contained in the original list,
then the key is contained in the sub-list.
Look at the top right corner.
This maintains the loop invariant.
Running time?
9 - Each iteration eliminates a row or col.
- The # rows + # col = n+n
- Time = 2n-1
711
Matrix Search
Precondition: nn matrix, each row and each column sorted. A key=5.
PostConditions: Find key in list (if there).
0 4 6 Note that this is property sorted.
5
4 6 Even if a 4 or 6 is changed to a 5.
4 6
4 6 What happens if when searching
4 6
5
you do not check each 4&6?
4 6
4 6 How much time is needed?
6 9 2n-1.
712