This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

046 Recitation 1: Proving Correctness

Acknowledgement Much of these notes is adapted from David Liben Nowell’s notes for the same subject (thanks David!) My contact information: Bill Thies (last name rhymes with “peace”) thies@mit.edu Stata 32-G890 617-253-6284 Office hours TBA, probably in this room Announcements: -notation to be covered in lecture next week PS 1 out, due Mon 9/20 Today: Admin Correctness - Recursive - Iterative

Outline

Bill Thies, Fall 2004

for illustrative purposes only INPUT: int a. then f(n) is correct [strong induction] ^^^^^^^^^^^^^^^^^^^ inductive hypothesis (Don’t need strong induction in this case.5 + AVE(a.) Proving Horner Correct .b) if a=b return a else return 0. Inductive step: if f(0)…f(n-1) is correct. b: 0 · a · b OUTPUT: (a+b)/2 AVE(a. Base case: f(n) is correct for n=0 2. b-1) INDUCTION on n 1.Correctness of Recursive Algorithms Simple algorithm for averaging two numbers.

. could be a relationship between variables.g. b-1) b=a+n = 0. Base: a=b AVE(a. Other examples will come up.5 + (a+b-1) / 2 = (a+b)/2 To take away: Whenever doing induction.b) = AVE(a. Inductive step (on n) If AVE(a. Hypothesis = 0.5 + AVE(a. the number of elements of an array that are sorted. In this case. It is not always a single variable you are doing induction on.a+n-1) is correct. induction on b-a (= n). a+n-1) correct by ind.Correctness of AVE 1.. a+n) is correct AVE(a.a+0) = a =? (a+a)/2 = a [ok] b=a 2.5 + AVE(a. e. then AVE(a. important to say what variable or expression the induction is on.b) = 0.

b) avg a for i = a to b-1 avg avg + 0. There are many such relationships.b) avg a ia while (i · b-1) avg avg + 0.5 i i+1 return avg . Important part is finding the ones that are useful for the algorithm. It makes it clear where the loop boundaries are. Doing this on above yields: AVE(a. I recommend replacing for loops by while loops. For thinking about loop invariants.5 return avg How to prove right value is returned? Loop invariant: relationship between variables in algorithm that holds at start of every loop iteration.Correctness of Iterative Algorithms AVE(a.

i=a avg = (a+b)/2 avg = (a+i)/2 avg = (a+a)/2 = a [ok] iter i: avg = (a+i)/2 iter i+1: avg + 0.Procedure for Proving Correctness 1. 4. and increases every iteration until it hits b 5. 3. 6. In case of AVE: 1. 2. then L holds on i+1 Prove that (L and “loop terminates”) POST Prove that loop terminates 5. so avg = (a+b)/2 i starts at a · b. Give PRE condition that holds before loop Give POST condition that holds after loop Guess loop invariant L Prove L using induction Base case: L holds on first iter Inductive step: if L holds on i.5 =? (a+i+1)/2 avg = (a+i)/2 [ok] Loop exit: i=b. PRE: POST: L: Base: Ind: avg=a. . 6. 4. 2. 3.

evaluate f(x) = j=0n aj xj Naïve: T(n) = j=0n (j) = (n2) Horner: f(x) = a0 + x(a1 + x(a2 + x(… x(an)))) T(n) = (n) Horner’s Algorithm (Horner’s Rule) INPUT: A[0…n]. x OUTPUT: j=0n A[j] xj = A[0] + A[1]x + A[2]x2 + … + A[n]xn HORNER(A[0…n]. x) val 0 in while (i 0) val A[i] + x · val ii–1 return val Example: i n n-1 n-2 n-3 val 0 A[n] A[n-1] + A[n]x A[n-2] + A[n-1]x + A[n]x2 .Polynomial Evaluation f(x) = a0 + a1 x + a2 x2 + … + an xn Q: Given x.

L only has to be a guess. and we will fill in the ??: L: val(i) = j=??n A[j] xj-?? look at example iterations for i=n-3. A[n-1] and A[n]. Thus. 2. val(i) = val(n) = 0 at POST. The value of j at A[n] is n. If you can’t see from inspection that it’s correct. at PRE. i=n POST: val= j=0n A[j] xj L: .this is the tricky part . x2 as i takes on the values (n-1). Remember. i. So if we are referencing A[j]. in general. Look at the exponent for A[n] for successive iterations of i. . this is i+1 to n: L: val(i) = j=i+1n A[j] xj-?? Now we need the exponent of x.Proving Horner Correct 1.hints: find something “in between” PRE and POST that incorporates the loop counter. 3. It is x0. val(i) = val(-1) = j=0n A[j] xj look at example iterations to see what the pattern is . and (n-3). in general. The coefficients involved are A[n-2]. then j in this summation ranges from n-2 to n. PRE: val=0. x1. that’s all the more reason that we are proving correctness in the first place. we could guess that the exponent is xj-(i+1): L: val(i) = j=i+1n A[j] xj-(i+1) You might also see this by just inspecting the examples: the sum is being built from the “end” of the polynomial towards the bottom.start with this template. (n-2).

Proving Horner Correct (cont’d) We didn’t cover this in recitation. The loop terminates because i is initialized to n. so it’s [ok] Show that if L holds for val(i) then it holds for val(i-1) Ind. i=-1. Evaluate val(-1) = j=-1+1n A[j] xj-(-1+1) = j=0n A[j] xj This is POST. 4. = 0 val is 0 in PRE. move x inside summation: val(i) = A[i+1] x0 + j=i+2n A[j] xj-(i+1) Combine sum: val(i) = j=i+1n A[j] xj-(i+1) 5. val re-assigned: val = A[i] + x · val(i) Then i re-assigned to i-1. so this value is carried to next iter: val(i-1) = A[i] + x · val(i) By inductive hypothesis: val(i-1) = A[i] + x · j=i+1n A[j] xj-(i+1) Renaming “i” to “i+1”: val(i) = A[i+1] + x · j=i+2n A[j] xj-(i+2) Add x0 after A[i+1]. 6. Prove L holds by induction: Base: at start of loop i=n L: val = j=n+1n … = sum is empty. . then decremented on every loop iteration. so it proves correctness. step: in loop. [ok] At end of loop.

- Intellectual Property Rights
- Chord implementation using RMI
- Chord implementation using RMI
- BTP Thesis
- Networking notes B.Tech
- Flipkart Interview Guide
- 3rd Semester 2013
- Sample Resume
- Algorithm Notes NSIT
- Lab Notes Microwave Engineering
- First step to wealth book by Dani Johnson
- OS-Lab Manual Nisha
- NSIT,DELHI REQUEST FOR PROPOSAL
- Advanced programming lab IT-320 NSIT
- Beej's Guide to Network Programming
- Job Description for Internship at CFBI
- ACM ICPC Amritapuri 2014
- Networking Lab
- Microprocessor File
- OS-Lab Manual Nisha
- Errata in Algorithm & Data Structure in C by narsimha Karaunmuchi
- Handa Ka Funda - Math Formulas 5.0
- SQL Tutorial
- Java Quick Guide
- MAQ Software_Job Description_Software Intern (Summer Interns

recitiation 1 MIT

recitiation 1 MIT

- c3
- Problem Set 17 Solutions
- Sigma Matrices
- Fractions Complete Gude
- Graph Notes
- Sets
- DiffEq
- Floating Point Multiplier
- M_AV_Calculus-I
- Worksheet of Linear Equation With One Variable
- 6097
- FP2 Solomon E QP
- Relation Examples
- Complex Number Use in Analysing Alternating Signals - Short
- CHSAM1
- Function Assignment 1
- Sinh Maclaurin
- Partitions of Integers, by Florentin Smarandache
- important question class10 real numbers
- A Notes on Design & Analysis of Algorithm
- MTKI 221211 Vektor
- Differential Equations - Solved Assignments - Semester Spring 2010
- congruencias dificiles
- Chapter 1 Function
- ode intro
- ON SOME DIOPHANTINE EQUATIONS
- Objective Ch 3 FSC Part1 Imran
- 2012 Prelim 3 AMaths P2_with Answer Key
- 11 Maths NcertSolutions Chapter 1 2
- mm-37

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd