csc236 fall 2014 - department of computer science...

14
http://www.cdf.toronto.edu/ ~ csc236h/fall/

Upload: others

Post on 23-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

CSC236 fall 2014

correct after & before

Danny Heap

[email protected]

BA4270 (behind elevators)

http://www.cdf.toronto.edu/~csc236h/fall/

416-978-5899

Using Introduction to the Theory of Computation,

Chapter 2

Page 2: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

Outline

power

notes

annotations

Page 3: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

Master Theorem(for divide-and-conquer recurrences)

If f from the previous slide has f 2 �(nd), then

T (n) =

8>><>>:

�(nd) if a < bd

�(nd logn) if a = bd

�(n logb a) if a > bd

Page 4: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

optimize...

def pow(b, n, mod):

if n == 0:

return 1

r = b if n % b == 1 else 1

half_pow = pow(b, n//b, mod)

return (half_pow * half_pow * r) % mod

def pow2(b, n, mod):

if n == 0:

return 1

r = b if n % b == 1 else 1

return (pow2(b, n//b, mod) * pow2(b, n//b, mod) * r) % mod

def pow3(b, n, mod):

return b**n % mod

(see power.py)

Page 5: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

iterative power...

def power(x, y) :

z = 1

m = 0

while m < y :

z = z * x

m = m + 1

return z

I precondition?

I postcondition?

I notation for mutation

Page 6: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

partial correctnessprecondition+execution+termination imply postcondition

a loop invariant helps get us closer

Page 7: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

partial correctnessprecondition+execution+termination imply postcondition

a loop invariant helps get us closer

Page 8: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

prove partial correctness

Page 9: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

prove terminationassociate a decreasing sequence in N with loop iterations

it helps to add claims to the loop invariant

Page 10: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

put it together | correctness

Page 11: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

correctness by designdraw pictures of before, during, after

pre: A sorted, comparable with x

post: 0 � p � n and A[0..p-1] < x �A[p .. n-1]

Page 12: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

\derive" conditions from pictures

Page 13: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

do we have termination?

Page 14: CSC236 fall 2014 - Department of Computer Science ...heap/236/F14/Lectures/W9/thursday-annotate… · Master Theorem (for divide-and-conquer recurrences) If f from the previous slide

notes