cosc 3101nj. elder announcements midterm exam: fri feb 27 cse c two blocks: –16:00-17:30...
TRANSCRIPT
COSC 3101N J. Elder
Announcements
• Midterm Exam: Fri Feb 27 CSE C• Two Blocks:
– 16:00-17:30
– 17:30-19:00
• The exam will be 1.5 hours in length. • You can attend either block.• The exam will be closed book.• Please remember to bring ID
COSC 3101N J. Elder
Announcements (Cntd…)
• Tutorial: Dr. Braverman (Instructor for Section M) will lead a tutorial session tomorrow:– Date: Thursday, Jan 22 – Time: 13:00-15:00 – 1005 TEL Building
• This is an opportunity to ask questions
COSC 3101N J. Elder
Announcements (Cntd…)
• I have been making announcements by email. If you have not received an email from me, please send me your current email address.
• Assignment 1 is now due at 12 noon Mon, Jan 26
• My office hour is now Mon 12:30-1:30
• The bookstore still has copies of Jeff Edmonds’ notes (shelved with course kits)
• HTA 6.4 and CLRS 28.2, 30, 31.3-31.9, 33.4 are NOT required reading
COSC 3101N J. Elder
Recurrence Relations (Cntd…)
The Master Theorem and Examples
COSC 3101N J. Elder
Master Theorem: I ntuition
Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b
Work at top level ( ) f n
log log / logWork at bottom level number of base cases = b a a bn n
logRunning time = max(work at top, work at bottom) = max( ( ), )b af n n
I f they are equal, then all levels are important:
logRunning time = sum of work over all levels = logb an n
COSC 3101N J. Elder
Theorem 4.1 (Master Theorem)
Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b
log0 such that ( ) (1. I F )b af n O n
logTH ( ) (E )N b aT n n
log2. I F ( ) ( ) b af n n
log( ) (TH l g )EN o b aT n n n
log0 such that 3. I F ( ) ( )b af n n
AND
0 01, >0 such that ( / ) ( ) c n af n b cf n n n
( )TH ( ( )EN )T n f n
Dominated by base cases
Work at each level is comparable:
Sum work over all levels
Dominated by top level work
Additional regularity condition
COSC 3101N J. Elder
Example 2: ( ) 4 ( / 2) 2nT n T n
4
2
a
blog 2b an n
( ) 2nf n
logThus ( ) ( ) (Case 3: dominated by top level)b af n n
0 0
1, >0
Additional r
such that
egularity condition:
( / ) ( ) c n af n b cf n n n
n/ 2Thus we require that 4 2 2nc
/ 24 2 nc
0
1Let 6
2n c
0regularity condition holds f or 6, 0.5n c
Thus ( ) ( ( )) (2 ) nT n f n
COSC 3101N J. Elder
5Example 3: ( ) 4 ( / 2) logT n T n n n
4
2
a
b log 2b an n
5( ) logf n n n
logThus ( ) ( ) (Case 1: dominated by base cases)b af n O n
log 2Thus T(n)= ( ) = (n )b an
COSC 3101N J. Elder
2Example 4: ( ) 4 ( / 2)T n T n n
4
2
a
b log 2b an n
2( )f n n
logThus ( ) ( ) (Case 2: all levels signifi cant)b af n n
log 2Thus ( ) ( log ) = (n log )b aT n n n n
COSC 3101N J. Elder
Sufficiently close to: T(n) = 4T(n/2)+ n3 = θ(n3). T2(n) = ?
Evaluating: T2(n) = 4 T2(n/2+n1/2)+ n3
θ(n3).
COSC 3101N J. Elder
Evaluating: T(n) = aT(n-b)+f(n)
h = ? n-hb
n-ib
n-2b
n-b
T(0)
f(n-ib)
f(n-2b)
f(n-b)
f(n)n
Work in Level
# stack frames
Work
in stack
frame
Instance
size
a
ai
a2
a
1
n/b a · T(0)
ai · f(n-ib)
a2 · f(n-2b)
a · f(n-b)
1 · f(n)
n/b
|base case| = 0 = n-hb
h = n/b
h = n/b
i
2
1
0
Level
Likely dominated by base cases Exponential
COSC 3101N J. Elder
Evaluating: T(n) = 1T(n-b)+f(n)
h = ?
Work in Level
# stack frames
Work
in stack
frame
Instance
size
1
1
1
1
1
f(0)
f(n-ib)
f(n-2b)
f(n-b)
f(n)
n-b
n
n-hb
n-ib
n-2b
T(0)
f(n-ib)
f(n-2b)
f(n-b)
f(n)
h = n/b
i
2
1
0
Level
Total Work T(n) = ∑i=0..h f(b·i) = θ(f(n)) or θ(n·f(n))
COSC 3101N J. Elder
End of Recurrences
COSC 3101N J. Elder
Central Algorithmic Techniques
Iterative Algorithms
COSC 3101N J. Elder
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?
COSC 3101N J. Elder
Code Representation of an Algorithm
• Runs on computers• Precise and succinct• Perception that being
able to code is the only thing needed to get a job. (false)
• I am not a computer• I need a higher level of
intuition.• Prone to bugs• Language dependent
Pros: Cons:
COSC 3101N J. Elder
Two Key Types of Algorithms
• Iterative Algorithms
• Recursive Algorithms
COSC 3101N J. Elder
Iterative Algorithms
Take one step at a time
towards the final destination
loop (done)
take step
end loop
COSC 3101N J. Elder
Loop Invariants
A good way to structure many programs:– Store the key information you currently
know in some data representation.
– In the main loop, • take a step forward towards destination• by making a simple change to this data.
COSC 3101N J. Elder
The Getting to School Problem
COSC 3101N J. Elder
Problem Specification• Pre condition: location of home and school
• Post condition: Traveled from home to school
COSC 3101N J. Elder
General Principle • Do not worry about the entire computation.
• Take one step at a time!
COSC 3101N J. Elder
Take a step • What is required of this step?
COSC 3101N J. Elder
A Measure of Progress
79 km
to school
75 km
to school
COSC 3101N J. Elder
Defining Algorithm• Extra requirements
78.999 km
79 km0 km
79 km 75 km
km
COSC 3101N J. Elder
• Location too confusing for algorithm
• Algorithm too lazy to define step for every location.
Computation Stuck
COSC 3101N J. Elder
• Algorithm specifies from which locations it knows how to step.
Safe Locations
COSC 3101N J. Elder
• “The computation is presently in a safe location.”
• Maybe true and maybe not.
Loop Invariant
COSC 3101N J. Elder
Defining Algorithm• From every safe location, define one step towards school.
COSC 3101N J. Elder
Take a step • What is required of this step?
COSC 3101N J. Elder
• Can we be assured that the computation will always be in a safe location?
• If the computation is in a safe location, it does not step into an unsafe one.
Maintain Loop Invariant
No. What if it is not initially true?
COSC 3101N J. Elder
From the Pre-Conditions on the input instance we must establish the loop invariant.
Establishing Loop Invariant
COSC 3101N J. Elder
Maintain Loop Invariant
• Can we be assured that the computation will always be in a safe location?
• By what principle?
COSC 3101N J. Elder
Maintain Loop Invariant• By Induction the computation will always be in a safe location.
S
iS i S i
iS i
( )
( ) ( )
( )
0
1
COSC 3101N J. Elder
Ending The Algorithm• Define Exit Condition
• Termination: With sufficient progress,
the exit condition will be met.
• When we exit, we know– exit condition is true
– loop invariant is true
from these we must establish
the post conditions.
Exit
Exit
0 km Exit
Exit
COSC 3101N J. Elder
Let’s Recap
COSC 3101N J. Elder
Designing an Algorithm Define Problem Define Loop
InvariantsDefine Measure of Progress
Define Step Define Exit Condition Maintain Loop Inv
Make Progress Initial Conditions Ending
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
COSC 3101N J. Elder
Simple Example
Insertion Sort Algorithm
COSC 3101N J. Elder
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;
}}
COSC 3101N J. Elder
Higher Level Abstract ViewRepresentation of an Algorithm
9 km
5 km
COSC 3101N J. Elder
Designing an Algorithm Define Problem Define Loop
InvariantsDefine Measure of Progress
Define Step Define Exit Condition Maintain Loop Inv
Make Progress Initial Conditions Ending
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
COSC 3101N J. Elder
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 149825
62
52
79
3023
31 14,23,25,30,31,52,62,79,88,98
COSC 3101N J. Elder
88 14
9825 6
2
52
79
3023
31 14,23,25,30,31,52,62,79,88,98
14
9825 6
279
3023,31,52,88
• Some subset of the elements are sorted
•The remaining elements are off to the side.
Define Loop Invariant
COSC 3101N J. Elder
14
9825 6
279
3023,31,52,88
23
9825 8
831
3014,52,62,79
Location Not Determined
Which subset of the elements are sorted, is not specified.
COSC 3101N J. Elder
Defining Measure of Progress
14
9825 6
279
3023,31,52,88
6 elements
to school
COSC 3101N J. Elder
Define Step• Select arbitrary element from side.
• Insert it where it belongs.
9825 6
279
23,31,52,88
14
9825
79
3023,31,52,62,88
1430
COSC 3101N J. Elder
Making progress while Maintaining the loop invariant
9825 6
279
23,31,52,88
14
9825
79
3023,31,52,62,88
1430
79 km 75 km
5 elements
to school
6 elements
to school
Exit
Sorted sublist
COSC 3101N J. Elder
88 14
9825 6
2
52
79
3023
31
88 14
9825 6
2
52
79
3023
31n elements
to school
14,23,25,30,31,52,62,79,88,98
14,23,25,30,31,52,62,79,88,980 elements
to school
Beginning & Ending
km Exit0 km Exit
COSC 3101N J. Elder
Running Time Inserting an element into a list of size i
takes (i) time.
Total = 1+2+3+…+n = (n2)
9825 6
279
23,31,52,88
14
9825
79
3023,31,52,62,88
1430
COSC 3101N J. Elder
OkI know you knew Insertion Sort
But hopefully you are beginning to appreciate
Loop Invariants
for describing algorithms
COSC 3101N J. Elder
Assertions
in Algorithms
COSC 3101N J. Elder
Purpose of Assertions
Useful for– thinking about algorithms
– developing
– describing
– proving correctness
COSC 3101N J. Elder
Definition of Assertions
An assertion is a statement about the current state of the data structure that is either true or false.
eg. the amount in your bank account is not negative.
COSC 3101N J. Elder
Definition of Assertions
It is made at some particular point during the execution of an algorithm.
If it is false, then something has gone wrong in the logic of the algorithm.
COSC 3101N J. Elder
Definition of Assertions
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.
COSC 3101N J. Elder
Specifying a Computational Problem
Example of Assertions
• Preconditions: Any assumptions that must be true about the input instance.
• Postconditions: The statement of what must be true when the algorithm/program returns..
COSC 3101N J. Elder
Definition of Correctness
<PreCond> & <code> <PostCond>
If the input meets the preconditions, then the output must meet the
postconditions.
If the input does not meet the preconditions, then nothing is required.
COSC 3101N J. Elder
An Example:A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
…
COSC 3101N J. Elder
A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Definition of Correctness?
…
COSC 3101N J. Elder
A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
<assertion0>any <conditions>code
<assertionr>
How is this proved?
Definition of Correctness
…
COSC 3101N J. Elder
A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
<assertion0>any <conditions>code
<assertionr>
Must check 2r different •settings of <conditions>•paths through the code.
Is there a faster way?
Definition of Correctness
…
COSC 3101N J. Elder
A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Step 1
<assertion0><condition1>code<1,true>
<assertion1>
<assertion0>¬<condition1>code<1,false>
<assertion1>
…
COSC 3101N J. Elder
A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
…
Step 2
<assertion1><condition2>code<2,true>
<assertion2>
<assertion1>¬<condition2>code<2,false>
<assertion2>
COSC 3101N J. Elder
A Sequence of Assertions <assertion0>
if( <condition1> ) then
code<1,true>
else
code<1,false>
end if
<assertion1>
<assertionr-1>
if( <conditionr> ) then
code<r,true>
else
code<r,false>
end if
<assertionr>
Step r
<assertionr-1><conditionr>code<r,true>
<assertionr>
<assertionr-1>¬<conditionr>code<r,false>
<assertionr>
…
COSC 3101N J. Elder
Another Example:A Loop
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Type of Algorithm: • Iterative
Type of Assertion:• Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Definition of Correctness?
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Definition of Correctness
<preCond>any <conditions>code
<postCond>
How is this proved?
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
The computation may go around the loop an arbitrary number of times.
Definition of Correctness
<preCond>any <conditions>code
<postCond>
Is there a faster way?
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Step 0
<preCond>codeA
<loop-invariant>
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Step 1
<loop-invariant>¬<exit Cond>codeB
<loop-invariant>
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Step 2
<loop-invariant>¬<exit Cond>codeB
<loop-invariant>
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Step 3
<loop-invariant>¬<exit Cond>codeB
<loop-invariant>
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>
Step i
<loop-invariant>¬<exit Cond>codeB
<loop-invariant>
Iterative Algorithms Loop Invariants
All these steps are the sameand therefore only need be done once!
COSC 3101N J. Elder
<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloopcodeC<postCond>
Last Step
<loop-invariant><exit Cond>codeC
<postCond>
Iterative Algorithms Loop Invariants
COSC 3101N J. Elder
Partial Correctness
Proves that IF the program terminates then it works
<PreCond> & <code> <PostCond>
<loop-invariant><exit Cond>codeC
<postCond>
Clean up loose ends
<loop-invariant>¬<exit Cond>codeB
<loop-invariant>
Maintaining Loop Invariant
<preCond>codeA
<loop-invariant>
Establishing Loop Invariant
Exit
Exit
COSC 3101N J. Elder
Algorithm Termination
km
0 km
79 km 75 km
Measure of progress
Exit
COSC 3101N J. Elder
Algorithm Correctness
Partial CorrectnessTermination Correctness
COSC 3101N J. Elder
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 itthe rest of the algorithm
follows easily
COSC 3101N J. Elder
Use This Process
Don't come up with the loop invariant after the fact to make me happy.
Use it to design your algorithm.
COSC 3101N J. Elder
Don’t start coding
You must design a working algorithm first.
COSC 3101N J. Elder
Exemplification:Try solving the problem on small input examples.
Rudich www.discretemath.com
COSC 3101N J. Elder
Start with Small Steps
What basic steps might you follow to make some kind of progress towards the answer?
Describe or draw a picture of what the data structure might look like after a number of
these steps.
COSC 3101N J. Elder
Picture from the Middle
Leap into the middle of the algorithm.
What would you like your data structure to look like when you are half done?
COSC 3101N J. Elder
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
to school
75 km
to school
COSC 3101N J. Elder
Flow SmoothlyThe 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.
COSC 3101N J. Elder
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?
COSC 3101N J. Elder
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.
COSC 3101N J. Elder
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?
COSC 3101N J. Elder
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.
COSC 3101N J. Elder
No Assumptions:
Suppose a Martian jumped
into the top of the loop.
All he would know is that <loop-invariant> was true.
It alone must provide enough information
so that, after going around the loop, he can establish that the loop invariant is again true.
COSC 3101N J. Elder
Know What a LI Is Not
``the LI is not...''– code– A precondition– A postcondition– A statement that is always true
Eg “1+1=2”– The steps taken by the algorithm
COSC 3101N J. Elder
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
COSC 3101N J. Elder
Loop Invariants for
Iterative Algorithms
Three
Search Examples
COSC 3101N J. Elder
Define Problem: Binary Search
• PreConditions– Key 25– Sorted List
• 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
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
COSC 3101N J. Elder
Define Loop Invariant
• Maintain a sublist
• Such that
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
COSC 3101N J. Elder
Define Loop Invariant
• Maintain a sublist.
• If the key is contained in the original list, then the key is contained in the sublist.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
COSC 3101N J. Elder
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
COSC 3101N J. Elder
Define Step
• Cut sublist in half.
• Determine which half 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
COSC 3101N J. Elder
Define Step
• Cut sublist 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
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
mid
COSC 3101N J. Elder
Define Step
• It is faster not to check if the middle element is the key.
• Simply continue.
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
COSC 3101N J. Elder
Make Progress
• The size of the list becomes smaller.
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
79 km
75 km
79 km 75 km
Exit
COSC 3101N J. Elder
Initial Conditionskm
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
• If the key is contained in the original list,
then the key is contained in the sublist.
• The sublist is the entire original list.
n km
COSC 3101N J. Elder
Ending Algorithm
• If the key is contained in the original list,
then the key is contained in the sublist.
• Sublist contains one element.
Exit
Exit
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
0 km
• If the key is contained in the original list,
then the key is at this location.
key 25
COSC 3101N J. Elder
If key not in original list• If the key is contained
in the original list,
then the key is contained in the sublist.
• Loop invariant true, even if the key is not in the list.
• If the key is contained in the original list,
then the key is at this location.
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
key 24
• Conclusion still solves the problem.
Simply check this one location for the key.
COSC 3101N J. Elder
Running Time The sublist is of size n, n/2, n/4, n/8,…,1
Each step (1) time.
Total = (log n)
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
COSC 3101N J. Elder
Code
COSC 3101N J. Elder
Algorithm Definition CompletedDefine Problem Define Loop
InvariantsDefine Measure of Progress
Define Step Define Exit Condition Maintain Loop Inv
Make Progress Initial Conditions Ending
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
COSC 3101N J. Elder
Study:
• Many experienced programmers were asked to code up binary search.
80% got it wrong
Good thing is was not for a nuclear power plant.
COSC 3101N J. Elder
Make Precise Definitions
• Maintain a sublist with end points i & j.
key 25
i j
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
COSC 3101N J. Elder
Make Precise Definitions
• Maintain a sublist with end points i & j.
key 25
i j
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
Does not matter which,but you need to be consistent.
COSC 3101N J. Elder
• If the sublist 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
Make Precise Definitions
mid or mid ?2 2
i j i j
COSC 3101N J. Elder
• If the sublist 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
Should not matterChoose right.
Make Precise Definitions
mid2
i j
mid
COSC 3101N J. Elder
Common Bugs
• Cut sublist 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
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
mid
COSC 3101N J. Elder
Common Bugs
• If the middle element is the key,
it can be skipped over.
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
Exit
mid
COSC 3101N J. Elder
Common Bugs• Fix the bug.
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
mid
COSC 3101N J. Elder
Common Bugs• Second fix,
by making the left half slightly bigger.
key 43
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
•New bug?
mid
COSC 3101N J. Elder
Common Bugs• Second fix,
by making the left half slightly bigger.
key 25
3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
•New bug? •Suppose key = 25
mid
COSC 3101N J. Elder
74
Common Bugs• Second fix,
by making the left half slightly bigger.
key 25
95918883726053514943362521211813653
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
•New bug? •Suppose key = 25
mid
COSC 3101N J. Elder
18 74
Common Bugs• Second fix,
by making the left half slightly bigger.
key 25
959188837260535149433625212113653
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
•New bug? •Suppose key = 25
mid
COSC 3101N J. Elder
13 74
Common Bugs• Second fix,
by making the left half slightly bigger.
key 25
959188837260535149433625212118653
If key ≤ mid,then key is inleft half.
If key > mid,then key is inright half.
•New bug? •Suppose key = 25
No progress toward goal:
Loops Forever!
mid
79 km 75 km
Exit
COSC 3101N J. Elder
Why is Binary Search so Easy to Get Wrong?
?R 2
Oi j
OR > ?
i = mid+1
else
j = mid
OR
mid2
i j 1
if key L(mid)2
i = mid
else
j = mid - 1
3
• How many possible algorithms?
• How many correct algorithms?
• Probability of success by guessing?
COSC 3101N J. Elder
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.
COSC 3101N J. Elder
Loop Invariants for
Iterative Algorithms
A Second
Search Example:
The Binary Search Tree
COSC 3101N J. Elder
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
Define Problem: Binary Search Tree• PreConditions
– Key 25– A binary search tree.
– PostConditions– Find key in BST (if there).
COSC 3101N J. Elder
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
Binary Search Tree
Its left children ≤ Any node ≤ Its right children
≤ ≤
COSC 3101N J. Elder
Define Loop Invariant• Maintain a sub-tree.
• If the key is contained in the original tree, then the key is contained in the sub-tree.
key 17
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
COSC 3101N J. Elder
Define Step• Cut sub-tree in half.• Determine which half the key would be in.• Keep that half.
key 17
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
If key < root,then key is in left half.
If key > root,then key is in right half.
If key = root,then key is found
COSC 3101N J. Elder
Algorithm Definition CompletedDefine Problem Define Loop
InvariantsDefine Measure of Progress
Define Step Define Exit Condition Maintain Loop Inv
Make Progress Initial Conditions Ending
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
COSC 3101N J. Elder
• A volunteer, please.
Card Trick
COSC 3101N J. Elder
Loop Invariants for
Iterative Algorithms
A Third
Search Example:
A Card Trick
COSC 3101N J. Elder
Pick a Card
Done
COSC 3101N J. Elder
Loop Invariant:The selected card is
one of these.
COSC 3101N J. Elder
Which column?
left
COSC 3101N J. Elder
Loop Invariant:The selected card is
one of these.
COSC 3101N J. Elder
Selected column is placed
in the middle
COSC 3101N J. Elder
I will rearrange the cards
COSC 3101N J. Elder
Relax Loop Invariant:I will remember the same about each
column.
COSC 3101N J. Elder
Which column?
right
COSC 3101N J. Elder
Loop Invariant:The selected card is
one of these.
COSC 3101N J. Elder
Selected column is placed
in the middle
COSC 3101N J. Elder
I will rearrange the cards
COSC 3101N J. Elder
Which column?
left
COSC 3101N J. Elder
Loop Invariant:The selected card is
one of these.
COSC 3101N J. Elder
Selected column is placed
in the middle
COSC 3101N J. Elder
Here is your card.
Wow!
COSC 3101N J. Elder
Algorithm Definition CompletedDefine Problem Define Loop
InvariantsDefine Measure of Progress
Define Step Define Exit Condition Maintain Loop Inv
Make Progress Initial Conditions Ending
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
COSC 3101N J. Elder
Ternary Search
• How many iterations are required to guarantee success?
• What is the probability of success with just 2 iterations?
• Loop Invariant: selected card in central subset of cards/ 3in
COSC 3101N J. Elder
Loop Invariants
A good way to structure any program:– Store everything you know in a data base.
– In the main loop, update this data base
while making progress.
COSC 3101N J. Elder
The “Partitioning” Problem(used in quicksort)
88 149825
62
52
79
3023
31
p=52
14
2530
23
31
88 9862
79≤ 52 ≤
Input: Output:
COSC 3101N J. Elder
Define Loop Invariant
or
31 23 25 98 30 14 62 79 88
p=52
≤ p p ≤
31 23 71 25 98 30 62 79 88
p=52
≤ p p ≤
COSC 3101N J. Elder
Defining Measure of Progress
4 elements
not
looked at
31 23 25 98 30 14 62 79 88
p=52
COSC 3101N J. Elder
Define Step
• Make Progress
• Maintain Loop Invariant
31 23 25 98 30 14 62 79 88
p=52
≤ p p ≤
COSC 3101N J. Elder
Define Step
• Make Progress
• Maintain Loop Invariant
31 23 25 98 30 14 62 79 88
p=52
≤ p p ≤
31 23 14 25 98 30 62 79 88
p=52
≤ p p ≤
COSC 3101N J. Elder
Define Step
31 23 25 98 30 14 62 79 88
p=52
31 23 14 25 98 30 62 79 88
Four cases
31 23 14 25 98 30 62 79 88
p=52
31 23 14 25 98 30 62 79 88
31 23 25 98 30 71 62 79 88
p=52
31 23 25 98 30 71 62 79 88
31 23 71 25 98 30 62 79 88
p=52
31 23 25 98 30 71 62 79 88
COSC 3101N J. Elder
88 14
9825 6
2
52
79
3023
31
Beginning & Ending
km Exit0 km Exit
p=52
88 25 31 98 62 14 30 79 23
23 30 25 31 14 62 98 79 88
p=52
25
3031
14
23
62 7998
88≤ 52 ≤
n-1 elements
not
looked at
0 elements
not
looked at
COSC 3101N J. Elder
Running Time Each iteration takes (1) time.
Total = (n)
31 23 25 98 30 14 62 79 88
p=52
≤ p p ≤
31 23 14 25 98 30 62 79 88
p=52
≤ p p ≤
COSC 3101N J. Elder
Algorithm Definition CompletedDefine Problem Define Loop
InvariantsDefine Measure of Progress
Define Step Define Exit Condition Maintain Loop Inv
Make Progress Initial Conditions Ending
km
79 km
to school
Exit
Exit
79 km 75 km
Exit
Exit
0 km Exit
COSC 3101N J. Elder
23793014629852258831
p=52 237930146298312588
793014629831258823
887930146298312523
887930146298312523
887914629831253023
887914629831253023
887914629831253023
887998146231253023
887998621431253023
887998621431253023
Tracing an Example
COSC 3101N J. Elder
GCD(a,b)
gcd( , ) gcd( , mod )
(CLRS Theorem 31.9)
x y y x y
Satisified on entry?
Maintained?
non-negative
gcd( ,0)
(CLRS Theorem Eqn. 31.9)
x xSuccess?
COSC 3101N J. Elder
End of Iterative Algorithms