More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.

Slides:



Advertisements
Similar presentations
THE WELL ORDERING PROPERTY Definition: Let B be a set of integers. An integer m is called a least element of B if m is an element of B, and for every x.
Advertisements

Functional Verification III Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 23.
Reasoning About Code; Hoare Logic, continued
B. Sharma, S.D. Dhodapkar, S. Ramesh 1 Assertion Checking Environment (ACE) for Formal Verification of C Programs Babita Sharma, S.D.Dhodapkar RCnD, BARC,
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
Introduction to Computing Science and Programming I
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
3.3 Divisibility Definition If n and d are integers, then n is divisible by d if, and only if, n = dk for some integer k. d | n  There exists an integer.
One of the most important problems is Computational Geometry is to find an efficient way to decide, given a subdivision of E n and a point P, in which.
Basic properties of the integers
ISBN Chapter 3 Describing Syntax and Semantics.
CSE115/ENGR160 Discrete Mathematics 04/12/11 Ming-Hsuan Yang UC Merced 1.
Discrete Mathematics Lecture 4 Harper Langston New York University.
Lecture 14 Go over midterm results Algorithms Efficiency More on prime numbers.
The max flow problem
© Love Ekenberg The Algorithm Concept, Big O Notation, and Program Verification Love Ekenberg.
Discrete Structures Chapter 2 Part B Mathematical Induction
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Algorithm Efficiency and Sorting
Describing Syntax and Semantics
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
The Polynomial Time Algorithm for Testing Primality George T. Gilbert.
Reading and Writing Mathematical Proofs
Testing Theory cont. Introduction Categories of Metrics Review of several OO metrics Format of Presentation CEN 5076 Class 6 – 10/10.
Great Theoretical Ideas in Computer Science.
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
The Complexity of Primality Testing. What is Primality Testing? Testing whether an integer is prime or not. – An integer p is prime if the only integers.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
Additional Problems.
MS 101: Algorithms Instructor Neelima Gupta
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Recursive Algorithms &
Reasoning about programs March CSE 403, Winter 2011, Brun.
Chinese Remainder Theorem Dec 29 Picture from ………………………
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Software Testing. System/Software Testing Error detection and removal determine level of reliability well-planned procedure - Test Cases done by independent.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Divide & Conquer Themes –Reasoning about code (correctness and cost) –iterative code, loop invariants, and sums –recursion, induction, and recurrence relations.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
Handbook of Applied Cryptography - CH4, from 4.1~4.3
6.5 Stochastic Prog. and Benders’ decomposition
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
CS 3343: Analysis of Algorithms
Copyright © Cengage Learning. All rights reserved.
ITEC452 Distributed Computing Lecture 5 Program Correctness
Application: Algorithms
6.5 Stochastic Prog. and Benders’ decomposition
Algorithms and Data Structures Lecture II
Presentation transcript:

More on Correctness

Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less than or equal to the given number Starting from the smallest prime (2), check whether all prime numbers less than the given number is a factor For every factor, compute its multiplicity also

Program prime_factor implicit none integer:: i, m, n read *, n i = 2; m = n ! n is the number to be factorized outer : do if ( i*i > m) exit inner : do if ( modulo(m,i) /= 0) exit m = m/i print *, i end do inner i = i+1 end do outer if ( m > 1) print *, m end program prime_factor

Correctness of the solution Study the program It may print the same number more than once. Why? No check for primality is made! Is the program correct? How do you check this? Test the program. How? Run the program for some inputs

Program Testing This is the standard approach used to ensure the correctness various issues to be addressed –What inputs should be given? Give inputs that test `corner cases', `boundary conditions', `representative inputs', eg. – 1, 2 (corner cases), –7, 53, 81, 224 (representative cases) Choose inputs that exercise all the `paths' in the program –What outputs are expected? Independent computation of outputs necessary –How many inputs should be given? As much to gain confidence

Program Verification Program Testing is good for detecting bugs in the final implementations It is a bit ad hoc and not recommended for ensuring correctness of algorithms or high level programs It can not ensure correctness - `it can reveal presence of bugs never their absence’ A more rigorous approach is Formal Verification (FV) FV proves the correctness mathematically In general difficult and followed very little in practice

Correctness of the solution FV requires a very precise definition of correctness What is the definition of correctness? When input constraints are satisfied the program terminates producing output with the desired properties In this example: –Input constraint: The number (n>0) –Output property: Any number printed is a prime factor (safety) All prime factors are printed (liveness) Correctness demands that these two properties should hold for any number

Loop Invariance For proving these properties, identify loop invariants: –a loop invariant is a property relating the values of variables such that if it is true before an iteration starts then it is true at end of iteration property preserved under iterations loop invariant, if it holds before the start of the loop then it will hold at the end How? Proof by induction on the number of iterations

Prime Factorization a loop invariant for outer loop –none of j, 1 < j < i, is a factor of m –product of all numbers printed and m is n This is true at start of iteration To prove that it is indeed an invariant, –let m = l and i = k at the start of an iteration. –Then l not divisible by any j, 1 < j < k

Proof At the end of this iteration, –value of i = k+1. k is not a factor of the new value of m, since in the iteration m is divided by k as many times as possible –None of j, 1 < j < k continue to be a factor of new value of m (why?) –every time k is printed, m is divided by k and hence product of m and all numbers printed is n –Hence the loop invariant holds at end of iteration

Proof of Safety Property To prove that any number printed is a prime factor Proof: Case1: k is printed in the inner loop Then k divides m but no number < k divides m. So k is prime. Loop invariant implies that m is a factor and hence k is a factor Case2: m is printed At end of loop, value of m is either 1 or a prime Loop invariant ensures that at end of loop, product of printed numbers and m is n since m is printed, it is prime.

Proof of Liveness Property To prove that all prime factors are printed –Any prime factor p of n has the property: either p*p <= n or p has multiplicity 1 –The loops checks all numbers i s.t i*i <= n, whether it is factor –Both the loops terminate (why?) –The prime factor p s.t. p*p > n, if it exists is m when the outer loop terminates

Formal Verification More rigorous, proves the correctness Does not run the program for particular inputs More time consuming but should be done for increased confidence Many applications required more confidence Safety-critical applications –Aircraft control, nuclear plant control, Missile avionics, patient monitoring systems, etc CFDVS at IIT Bombay Lesson for you: Write loop invariants Prove that the loop terminates