08A Week 8

# 08A Week 8 - CMPUT 272 Lecture 13 Reading Epp Chapter 5.5...

This preview shows pages 1–3. Sign up to view the full content.

CMPUT 272 Lecture 13 Reading: Epp Chapter 5.5 Correctness of Algorithms [Floyd, Dijkstra, Hoare, late 1960’s] A specification of an algorithm is given by two statements describing the input and output and the relationship between them: a pre-condition and a post-condition . Example: For an algorithm to sort a one-dimensional array of real numbers: Pre-condition: Input is an array A [1 ..n ] of real numbers. Post-condition: Output is an array B [1 ..n ] of real numbers with the same elements as A [1 ..n ] satisfying: B [ i ] B [ j ] for all 1 i j n . An algorithm is correct if, whenever it is executed on input that satisfies the pre-condition, the post-condition is true. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
To prove correctness of an algorithm: Can we run the algorithm on all possible inputs? NO! Suppose the input size is 40 bytes = 320 bits. Then the number of inputs is 2 320 . a 91-digit number number of protons in the known universe 79 digits number of microseconds since the big bang 24 digits Can we at least run it through a program that checks whether or not it halts on a given input? NO! The halting problem is undecidable: no such program can exist. Overall strategy: Divide the algorithm into parts, each with a pre-condition and post- condition. Prove the correctness of each part. Show that the post-condition of each part implies the pre-condition of the next part, etc. It’s easy to reason about parts that don’t contain a loop. To reason about a loop: Define a loop invariant , I ( n ): a predicate that describes how the values of the variables relate to each other after each iteration n (i.e., just before iteration n + 1 if the loop executes again). Then I. prove that I
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern