cosc 3101nj. elder announcements midterm exam: fri feb 27 cse c two blocks: –16:00-17:30...

160
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

Upload: george-morrison

Post on 27-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COSC 3101NJ. 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

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

Page 2: COSC 3101NJ. 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

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

Page 3: COSC 3101NJ. 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

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

Page 4: COSC 3101NJ. 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

COSC 3101N J. Elder

Recurrence Relations (Cntd…)

The Master Theorem and Examples

Page 5: COSC 3101NJ. 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

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

Page 6: COSC 3101NJ. 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

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

Page 7: COSC 3101NJ. 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

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

Page 8: COSC 3101NJ. 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

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

Page 9: COSC 3101NJ. 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

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

Page 10: COSC 3101NJ. 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

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).

Page 11: COSC 3101NJ. 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

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

Page 12: COSC 3101NJ. 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

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))

Page 13: COSC 3101NJ. 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

COSC 3101N J. Elder

End of Recurrences

Page 14: COSC 3101NJ. 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

COSC 3101N J. Elder

Central Algorithmic Techniques

Iterative Algorithms

Page 15: COSC 3101NJ. 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

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?

Page 16: COSC 3101NJ. 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

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:

Page 17: COSC 3101NJ. 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

COSC 3101N J. Elder

Two Key Types of Algorithms

• Iterative Algorithms

• Recursive Algorithms

Page 18: COSC 3101NJ. 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

COSC 3101N J. Elder

Iterative Algorithms

Take one step at a time

towards the final destination

loop (done)

take step

end loop

Page 19: COSC 3101NJ. 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

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.

Page 20: COSC 3101NJ. 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

COSC 3101N J. Elder

The Getting to School Problem

Page 21: COSC 3101NJ. 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

COSC 3101N J. Elder

Problem Specification• Pre condition: location of home and school

• Post condition: Traveled from home to school

Page 22: COSC 3101NJ. 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

COSC 3101N J. Elder

General Principle • Do not worry about the entire computation.

• Take one step at a time!

Page 23: COSC 3101NJ. 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

COSC 3101N J. Elder

Take a step • What is required of this step?

Page 24: COSC 3101NJ. 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

COSC 3101N J. Elder

A Measure of Progress

79 km

to school

75 km

to school

Page 25: COSC 3101NJ. 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

COSC 3101N J. Elder

Defining Algorithm• Extra requirements

78.999 km

79 km0 km

79 km 75 km

km

Page 26: COSC 3101NJ. 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

COSC 3101N J. Elder

• Location too confusing for algorithm

• Algorithm too lazy to define step for every location.

Computation Stuck

Page 27: COSC 3101NJ. 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

COSC 3101N J. Elder

• Algorithm specifies from which locations it knows how to step.

Safe Locations

Page 28: COSC 3101NJ. 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

COSC 3101N J. Elder

• “The computation is presently in a safe location.”

• Maybe true and maybe not.

Loop Invariant

Page 29: COSC 3101NJ. 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

COSC 3101N J. Elder

Defining Algorithm• From every safe location, define one step towards school.

Page 30: COSC 3101NJ. 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

COSC 3101N J. Elder

Take a step • What is required of this step?

Page 31: COSC 3101NJ. 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

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?

Page 32: COSC 3101NJ. 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

COSC 3101N J. Elder

From the Pre-Conditions on the input instance we must establish the loop invariant.

Establishing Loop Invariant

Page 33: COSC 3101NJ. 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

COSC 3101N J. Elder

Maintain Loop Invariant

• Can we be assured that the computation will always be in a safe location?

• By what principle?

Page 34: COSC 3101NJ. 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

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

Page 35: COSC 3101NJ. 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

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

Page 36: COSC 3101NJ. 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

COSC 3101N J. Elder

Let’s Recap

Page 37: COSC 3101NJ. 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

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

Page 38: COSC 3101NJ. 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

COSC 3101N J. Elder

Simple Example

Insertion Sort Algorithm

Page 39: COSC 3101NJ. 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

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;

}}

Page 40: COSC 3101NJ. 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

COSC 3101N J. Elder

Higher Level Abstract ViewRepresentation of an Algorithm

9 km

5 km

Page 41: COSC 3101NJ. 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

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

Page 42: COSC 3101NJ. 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

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

Page 43: COSC 3101NJ. 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

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

Page 44: COSC 3101NJ. 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

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.

Page 45: COSC 3101NJ. 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

COSC 3101N J. Elder

Defining Measure of Progress

14

9825 6

279

3023,31,52,88

6 elements

to school

Page 46: COSC 3101NJ. 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

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

Page 47: COSC 3101NJ. 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

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

Page 48: COSC 3101NJ. 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

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

Page 49: COSC 3101NJ. 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

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

Page 50: COSC 3101NJ. 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

COSC 3101N J. Elder

OkI know you knew Insertion Sort

But hopefully you are beginning to appreciate

Loop Invariants

for describing algorithms

Page 51: COSC 3101NJ. 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

COSC 3101N J. Elder

Assertions

in Algorithms

Page 52: COSC 3101NJ. 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

COSC 3101N J. Elder

Purpose of Assertions

Useful for– thinking about algorithms

– developing

– describing

– proving correctness

Page 53: COSC 3101NJ. 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

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.

Page 54: COSC 3101NJ. 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

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.

Page 55: COSC 3101NJ. 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

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.

Page 56: COSC 3101NJ. 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

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..

Page 57: COSC 3101NJ. 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

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.

Page 58: COSC 3101NJ. 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

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>

Page 59: COSC 3101NJ. 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

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?

Page 60: COSC 3101NJ. 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

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

Page 61: COSC 3101NJ. 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

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

Page 62: COSC 3101NJ. 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

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>

Page 63: COSC 3101NJ. 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

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>

Page 64: COSC 3101NJ. 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

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>

Page 65: COSC 3101NJ. 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

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

Page 66: COSC 3101NJ. 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

COSC 3101N J. Elder

<preCond> codeA loop <loop-invariant> exit when <exit Cond> codeBendloop codeC<postCond>

Definition of Correctness?

Iterative Algorithms Loop Invariants

Page 67: COSC 3101NJ. 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

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

Page 68: COSC 3101NJ. 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

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

Page 69: COSC 3101NJ. 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

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

Page 70: COSC 3101NJ. 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

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

Page 71: COSC 3101NJ. 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

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

Page 72: COSC 3101NJ. 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

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

Page 73: COSC 3101NJ. 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

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!

Page 74: COSC 3101NJ. 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

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

Page 75: COSC 3101NJ. 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

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

Page 76: COSC 3101NJ. 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

COSC 3101N J. Elder

Algorithm Termination

km

0 km

79 km 75 km

Measure of progress

Exit

Page 77: COSC 3101NJ. 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

COSC 3101N J. Elder

Algorithm Correctness

Partial CorrectnessTermination Correctness

Page 78: COSC 3101NJ. 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

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

Page 79: COSC 3101NJ. 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

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.

Page 80: COSC 3101NJ. 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

COSC 3101N J. Elder

Don’t start coding

You must design a working algorithm first.

Page 81: COSC 3101NJ. 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

COSC 3101N J. Elder

Exemplification:Try solving the problem on small input examples.

Rudich www.discretemath.com

Page 82: COSC 3101NJ. 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

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.

Page 83: COSC 3101NJ. 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

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?

Page 84: COSC 3101NJ. 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

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

Page 85: COSC 3101NJ. 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

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.

Page 86: COSC 3101NJ. 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

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?

Page 87: COSC 3101NJ. 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

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.

Page 88: COSC 3101NJ. 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

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?

Page 89: COSC 3101NJ. 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

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.

Page 90: COSC 3101NJ. 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

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.

Page 91: COSC 3101NJ. 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

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

Page 92: COSC 3101NJ. 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

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

Page 93: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariants for

Iterative Algorithms

Three

Search Examples

Page 94: COSC 3101NJ. 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

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

Page 95: COSC 3101NJ. 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

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

Page 96: COSC 3101NJ. 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

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

Page 97: COSC 3101NJ. 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

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

Page 98: COSC 3101NJ. 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

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

Page 99: COSC 3101NJ. 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

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

Page 100: COSC 3101NJ. 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

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.

Page 101: COSC 3101NJ. 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

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

Page 102: COSC 3101NJ. 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

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

Page 103: COSC 3101NJ. 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

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

Page 104: COSC 3101NJ. 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

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.

Page 105: COSC 3101NJ. 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

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.

Page 106: COSC 3101NJ. 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

COSC 3101N J. Elder

Code

Page 107: COSC 3101NJ. 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

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

Page 108: COSC 3101NJ. 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

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.

Page 109: COSC 3101NJ. 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

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

Page 110: COSC 3101NJ. 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

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.

Page 111: COSC 3101NJ. 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

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

Page 112: COSC 3101NJ. 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

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

Page 113: COSC 3101NJ. 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

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

Page 114: COSC 3101NJ. 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

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

Page 115: COSC 3101NJ. 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

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

Page 116: COSC 3101NJ. 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

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

Page 117: COSC 3101NJ. 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

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

Page 118: COSC 3101NJ. 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

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

Page 119: COSC 3101NJ. 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

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

Page 120: COSC 3101NJ. 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

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

Page 121: COSC 3101NJ. 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

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?

Page 122: COSC 3101NJ. 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

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.

Page 123: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariants for

Iterative Algorithms

A Second

Search Example:

The Binary Search Tree

Page 124: COSC 3101NJ. 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

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).

Page 125: COSC 3101NJ. 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

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

≤ ≤

Page 126: COSC 3101NJ. 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

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

Page 127: COSC 3101NJ. 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

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

Page 128: COSC 3101NJ. 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

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

Page 129: COSC 3101NJ. 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

COSC 3101N J. Elder

• A volunteer, please.

Card Trick

Page 130: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariants for

Iterative Algorithms

A Third

Search Example:

A Card Trick

Page 131: COSC 3101NJ. 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

COSC 3101N J. Elder

Pick a Card

Done

Page 132: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariant:The selected card is

one of these.

Page 133: COSC 3101NJ. 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

COSC 3101N J. Elder

Which column?

left

Page 134: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariant:The selected card is

one of these.

Page 135: COSC 3101NJ. 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

COSC 3101N J. Elder

Selected column is placed

in the middle

Page 136: COSC 3101NJ. 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

COSC 3101N J. Elder

I will rearrange the cards

Page 137: COSC 3101NJ. 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

COSC 3101N J. Elder

Relax Loop Invariant:I will remember the same about each

column.

Page 138: COSC 3101NJ. 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

COSC 3101N J. Elder

Which column?

right

Page 139: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariant:The selected card is

one of these.

Page 140: COSC 3101NJ. 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

COSC 3101N J. Elder

Selected column is placed

in the middle

Page 141: COSC 3101NJ. 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

COSC 3101N J. Elder

I will rearrange the cards

Page 142: COSC 3101NJ. 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

COSC 3101N J. Elder

Which column?

left

Page 143: COSC 3101NJ. 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

COSC 3101N J. Elder

Loop Invariant:The selected card is

one of these.

Page 144: COSC 3101NJ. 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

COSC 3101N J. Elder

Selected column is placed

in the middle

Page 145: COSC 3101NJ. 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

COSC 3101N J. Elder

Here is your card.

Wow!

Page 146: COSC 3101NJ. 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

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

Page 147: COSC 3101NJ. 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

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

Page 148: COSC 3101NJ. 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

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.

Page 149: COSC 3101NJ. 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

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:

Page 150: COSC 3101NJ. 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

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 ≤

Page 151: COSC 3101NJ. 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

COSC 3101N J. Elder

Defining Measure of Progress

4 elements

not

looked at

31 23 25 98 30 14 62 79 88

p=52

Page 152: COSC 3101NJ. 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

COSC 3101N J. Elder

Define Step

• Make Progress

• Maintain Loop Invariant

31 23 25 98 30 14 62 79 88

p=52

≤ p p ≤

Page 153: COSC 3101NJ. 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

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 ≤

Page 154: COSC 3101NJ. 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

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

Page 155: COSC 3101NJ. 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

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

Page 156: COSC 3101NJ. 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

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 ≤

Page 157: COSC 3101NJ. 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

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

Page 158: COSC 3101NJ. 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

COSC 3101N J. Elder

23793014629852258831

p=52 237930146298312588

793014629831258823

887930146298312523

887930146298312523

887914629831253023

887914629831253023

887914629831253023

887998146231253023

887998621431253023

887998621431253023

Tracing an Example

Page 159: COSC 3101NJ. 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

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?

Page 160: COSC 3101NJ. 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

COSC 3101N J. Elder

End of Iterative Algorithms