Assertions & Verification & Example Loop Invariants Example Exam Questions

Similar documents
Assertions & Verification Example Exam Questions

Math Introduction to Advanced Mathematics

1 Elementary number theory

Solutions to the Second Midterm Exam

PROBLEM SOLVING TECHNIQUES SECTION - A. 1. Answer any ten of the following

Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD

Integers and Mathematical Induction

A survey of loop invariants

Discrete Mathematics Lecture 4. Harper Langston New York University

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Loops / Repetition Statements

Mathematical Induction

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm

COP 4516: Math for Programming Contest Notes

Loop invariants: analysis, classification, and examples arxiv: v4 [cs.se] 13 Jan 2014

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

Hardware versus software

A Loop invariants: analysis, classification, and examples

Unit 7 Number System and Bases. 7.1 Number System. 7.2 Binary Numbers. 7.3 Adding and Subtracting Binary Numbers. 7.4 Multiplying Binary Numbers

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Lecture 2: Getting Started

Searching Algorithms/Time Analysis

15-122: Principles of Imperative Computation (Section G)

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

A Partial Correctness Proof for Programs with Decided Specifications

Lecture Notes, CSE 232, Fall 2014 Semester

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

UCT Algorithm Circle: Number Theory

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 2014

Spark verification features

CSE 214 Computer Science II Searching

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Lemma (x, y, z) is a Pythagorean triple iff (y, x, z) is a Pythagorean triple.

CPSC 121: Models of Computation Assignment #4, due Thursday, March 16 th,2017at16:00

Conditionals !

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Review: Hoare Logic Rules

COMPUTER SCIENCE TRIPOS

Know the Well-ordering principle: Any set of positive integers which has at least one element contains a smallest element.

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

VS 3 : SMT Solvers for Program Verification

Algorithms 4. Odd or even Algorithm 5. Greatest among three numbers Algorithm 6. Simple Calculator Algorithm

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

Topic 10 Part 2 [474 marks]

ASSIGNMENT 3 Methods, Arrays, and the Java Standard Class Library

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

Lecture Notes on Quicksort

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Lecture Notes on Quicksort

Sorting is a problem for which we can prove a non-trivial lower bound.

Lecture 7: General Loops (Chapter 7)

12/30/2013 S. NALINI,AP/CSE

Program Verification using Templates over Predicate Abstraction. Saurabh Srivastava and Sumit Gulwani

34 Loop invariants: analysis, classification, and examples

1 Elementary number theory

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

(Refer Slide Time: 00:26)

Midterm I Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. September 30, 2010

Repetition Through Recursion

BEng (Hons) Telecommunications. Examinations for / Semester 2

Data Structures and Algorithms Chapter 2

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Hoare Logic: Proving Programs Correct

Recursive Functions. Biostatistics 615 Lecture 5

Comparison of x with an entry in the array

Reading 8 : Recursion

Scan Scheduling Specification and Analysis

Generalized Loop-Unrolling: a Method for Program Speed-Up

Algorithms. Notations/pseudo-codes vs programs Algorithms for simple problems Analysis of algorithms

MAT 243 Test 2 SOLUTIONS, FORM A

The Eiffel language. Slides partly based on :

The divide and conquer strategy has three basic parts. For a given problem of size n,

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

PACKING DIGRAPHS WITH DIRECTED CLOSED TRAILS

Summary of Course Coverage

Sorting. Bringing Order to the World

CSCE 110: Programming I

2.Simplification & Approximation

One of the most important areas where quantifier logic is used is formal specification of computer programs.

Chapter 5 7. num = 0 repeat: if (num is Odd ) print (num is Odd) num = num + 1 until (num < 50) 11. Suppose N is the given integer.

Module 2: Classical Algorithm Design Techniques

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION

Lecture Notes on Linear Search

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Recall our recursive multiply algorithm:

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Number Theory Open, Round 1 Test #101

Syntax of Eiffel: a Brief Overview

Chapter 4. Number Theory. 4.1 Factors and multiples

r=1 The Binomial Theorem. 4 MA095/98G Revision

Transcription:

2014 November 27 1. Assertions & Verification & Example Loop Invariants Example Exam Questions 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer must answer to verify the sequence is a correct refinement. Give a general template for refining an operation into a choice and state what questions a designer must answer to verify the choice is a correct refinement. Give a general template for redefining an operation into a and state what questions a designer must answer to verify the is a correct refinement. Explain what is meant by strong and weak assertions. 3. 4. There are 5 types of assertions that can be used in program design. Describe each type of assertion and how that assertion type is used in the design of programs. What is a invariant? When is it used? How is it used? Why is it used? Where is it used? 5. Given pre & post conditions and a invariant create the corresponding program text in Eiffel. 6. Give the best precondition, postcondition and invariant for the following algorithm to a binary search on an array A[1.. N]. Your work should use as much mathematical notation as possible to capture the relevant information. low 1 high N Result 0 while Result = 0 and low high mid (low + high)/2 if A[mid] = k then Result mid elseif A[mid] < k then low mid + 1 else high mid - 1 fi while 7. What would be the best precondition, postcondition and invariant for the following algorithm to find a span of dash characters in an array. charpointer := 1 exit when textline(charpointer) not equal dashchar charpointer := charpointer + 1

2014 November 27 Assertion & Verification Example Exam Questions Page 2 of 8 8. What would be the best precondition, postcondition and invariant for the following correct algorithm to find the maximum integer and its index in the array A[1..N]. 9. max := 0 ; maxindex := 0 j := 1 while j <= N if max < A[j] then max = A[j] ; maxindex := j if j := j + 1 while What would be the best invariant and variant for the following algorithm. require x = a * a invariant??? variant??? until x = 0 y := y + 1 ; x := x - 1 ensure y = a*a + old y 10. Using your invariant and variant, verify that the algorithm given above achieves the postcondition. Consider the following pseu code which implements the selection sort. It works by looking through an array of n elements, A, picking the smallest element and moving it to the 1 st position. It then, repeats the same thing on the sub-array, A[2..n], defined by positions 2 n, then, A[3..n], defined on positions 3..n and so on. What is the best invariant for the outer? First find the invariant for the inner (lines 3-7) and then use this to find the invariant of the outer (lines 1-11). Explain how the invariant can be used to prove the correctness of the main (lines 1-11)? (Hint: what is the relationship between A[1..k] and A[k+1..n] after k th iteration of the main?) 11. 1 for i 1 to n-1 2 small = i 3 for j i+1 to n 4 if A[j] < A[small] then 5 small = j 6 if 7 for 8 temp := A[small] 9 A[small] := A[i] 10 A[i] := temp 11 for What would be the best precondition, postcondition and invariant for the following correct algorithm to find the longest string in an array of strings A[1..N] s := null; longestindex := 0 j := 1 while j <= N

2014 November 27 Assertion & Verification Example Exam Questions Page 3 of 8 12. if length(s) < A[j] then s := A[j]; longestindex := j -if -while 13. Give, both in English and in mathematical notation, the best precondition, postcondition, invariant and variant for the following algorithm to search the array a[lb.. ub] for the position of the value key. search(a : ARRAY[ITEM]; key : ITEM) : INTEGER require??? local j, n : INTEGER j := a.upper ; Result := a.lower 1 invariant??? variant??? until j = Result if a[j] = key then Result := j else j := j 1 ensure??? 14. The following code segment computes the square of n A B C square := 0 increment := 1 for i:= 1 to n square := square + increment increment := increment + 2 for What are the best invariant and variant. Use the invariant and the variant part A to argue the correctness of the. Explain why an invariant such as "i >= 1" is not of much value The greatest common divisor (GCD) of two positive integers, a and b, denoted by GCD(a, b) is the largest natural number that divides both a and b. Some examples include GCD(9,6) = 3, GCD(16, 5) = 1. Euclid s algorithm computes the GCD of two numbers as follows Step A: Write a = q *b + r where 0 <= r <= b. Step B: If r > 0, then set a = b, b = r and go to Step A. Otherwise last b is the GCD The equation. a = q *b + r implies that GCD(a, b) = GCD(b, r) and hence the process works. Here are several iterations: a = q1*b + r1 where 0 <= r1 <= b b = q2*r1 + r2 where 0 <= r2 < r1 r1 = q3*r2 + r3 where 0 <= r3 < r2 Complete require, ensure, variant and invariant clauses for the gcd method. gcd (a, b: INTEGER): INTEGER is -- Greatest common divisor of a and b. require???

2014 November 27 Assertion & Verification Example Exam Questions Page 4 of 8 15. local x, y, remainder: INTEGER x := a y := b remainder := x \\ y -- remainder of x divided by y invariant??? variant??? until remainder = 0 x := y y := remainder remainder := x \\ y Result := y ensure??? Verify that the following algorithm to partition an array, a[1.. N], into those elements that are smaller than a[1] and those that are larger than a[1] is correct. Array on exit elements of a < old a[1] old a[1] elements of a > old a[1] partition(a : ARRAY[INTEGER]): INTEGER require a.lower 1 a.upper 1 i : 2.. a.upper a[1] a[i] local split, last : INTEGER split := 1 ; last := 2 invariant k : 2.. split a[k] < a[1] k : split+1.. last-1 a[k] > a[1] variant a.upper last + 1 until last > a.upper if a[last] < a[1] then split := split + 1 swap(a[last], a[split]) last := last + 1 else last := last + 1 swap(a[1], a[split]) Result := split ensure a[result] = old a[1] k : 1.. Result-1 a[k] < a[result] k : Result+1.. a.upper a[result] < a[k]

2014 November 27 Assertion & Verification Example Exam Questions Page 5 of 8 16. 17. 18. The following routine correctly computes 2 n 1 for a given integer n. Determine the invariant and variant and write it in the space provided within the program text. Note that both assertions have to be Eiffel executable. pow_2 (n : INTEGER) : INTEGER -- result is 2^n - 1 require n >= 0 local i, increment: INTEGER i := 0 increment := 1 Result := 0 invariant??? variant??? until i >= n Result := Result + increment increment := increment * 2 ensure Result = 2 ^ n - 1 Consider the following pseu code, which implements the selection sort. It works by looking through an array of n elements, A, picking the smallest element and moving it to the 1 st position. It then, repeats the same thing on the sub-array, A[2..n], defined by positions 2 n, then, A[3..n], defined on positions 3..n and so on. 1 i := 1 2 while i <= n { 3 small := i 4 j = i + 1 5 while j <= n { 6 if A[j] < A[small] then small := j fi 7 j := j + 1 8 } 9 temp := A[small] 10 A[small] := A[i] 11 A[i] := temp 12 13 } A What is the best invariant for the inner (lines 5..8)? B What is the best invariant for the outer (lines 2-13)? C Use the inner invariant to show the correctness of the inner (lines 3..8). A positive integer n is said to be prime if it is only divisible by 1 and itself. i : INTEGER i n (i = 1 i = n) where i n is read as i divides n The following Eiffel routine correctly tests if a given integer, n, is prime by verifying that none of the integers 2, 3,, floor(sqrt(n)) divides n. For this question you need to perform the following tasks. a) Complete the postcondition for the function prime.

2014 November 27 Assertion & Verification Example Exam Questions Page 6 of 8 b) Write a proper invariant, using BON assertion language, and variant for the. c) Use results of the previous two steps to prove the correctness of the. prime (n: INTEGER): BOOLEAN -- Asserts that n a prime number local i : INTEGER -- Test divisor max : INTEGER -- Maximum integer to try i := 2 max := floor (sqrt (n)) Result := True until i > max or not Result Result := not ((n \\ i) = 0) -- \\ is the mod function ensure??? Loop invariant Write the invariant, using BON assertion notation not Eiffel programming language, and variant. Loop variant??? The following questions have example assertions because, in earlier years students were expected to be able to prove the correctness of simple algorithms. 19. 20. Prove the following algorithm to sum the odd integers in the range 1 to N inclusive is correct. Clearly show the correspondence with the questions a designer must ask when verifying a is the correct refinement of an operation. Precondition: N > 0! Postcondition: sum = { j :1..N odd( j) j} Loop invariant: sum =!{ j :1..p odd( j) j} sum := 0 p := 0 while p < N p := p + 1 if odd(p) then sum := sum + p fi while The following algorithm multiplies two integers using addition. Prove the algorithm works correctly. The more mathematically precise you are, the higher the grade. Pre-condition: a 0 and b 0

2014 November 27 Assertion & Verification Example Exam Questions Page 7 of 8 21. 22. Post-condition: z = abs(a*b) abs is the absolute value, z > 0 Loop invariant: z + u*y = x*y 1 if a < 0 then x := a else x := a 2 if b < 0 then y := -b else y := b 3 z := 0 4 u := x 5 repeat 6 z := z + y 7 u := u 1 8 until u = 0 The following routine employs a to compute n*(n+1) for a given non-negative integer n. There is, however, an error in its implementation. Your task is to use the given invariant and the variant to prove the correctness of the. If you are careful with your proof, then the source of the error should reveal itself at the proper step of the proof. Follow the normal course of proof until you reach an inconsistency, then indicate what is the source of the error in the routine that gives the inconsistency, correct it and complete the proof. sum2 (n : INTEGER) : INTEGER -- Compute n * (n + 1) require n >= 0 local i : INTEGER i := 0 invariant Result = i * (i + 1) variant n - i + 1 until i > n Result := Result + 2 * i ensure Result = n * (n + 1) The following routine employs a to compute n^2 (n 2 ) for a given non-negative integer n. Verify the routine is correct. There is, however, an error in its implementation. Your task is to use the given invariant and the variant to prove the correctness of the. If you are careful with your proof, then the source of the error should reveal itself at the proper step of the proof. Follow the normal course of proof until you reach an inconsistency, then indicate what is the source of the error in the routine that gives the inconsistency, correct it and complete the proof. square (n: INTEGER): INTEGER -- result is n^2 require n >= 0 local i, increment: INTEGER i := 0 ; increment := -1 invariant Result = i^2 and increment = 2*i - 1 until i > n increment := 2 * i - 1 Result := Result + increment

2014 November 27 Assertion & Verification Example Exam Questions Page 8 of 8 23. ensure Result = n ^ 2 The following routine correctly computes the 2^n 1 for a given integer n. 2^n is 2 to the power of n. Prove the algorithm is correct. pow_2 (n : INTEGER) : INTEGER -- result is 2^n - 1 require n >= 0 local i, increment: INTEGER i := 0 increment := 1 Result := 0 invariant Result = 2^i 1 and increment = 2 ^ i variant n - i until i >= n Result := Result + increment increment := increment * 2 ensure Result = 2 ^ n - 1