CSE20: Discrete Mathematics

Similar documents
C241 Homework Assignment 7

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

Recursion: Introduction and Correctness

Hoare Logic: Part II

Mathematical Induction. How does discrete math help us. How does discrete math help (CS160)? How does discrete math help (CS161)?

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and

Arithmetic Algorithms, Part 1

CSE 20 DISCRETE MATH SPRING

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Program verification. Hoare triples. Assertional semantics (cont) Example: Semantics of assignment. Assertional semantics of a program

Imperative Insertion Sort

ICS141: Discrete Mathematics for Computer Science I

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

CSC B36 Additional Notes sample induction and well-ordering proofs. c Nick Cheng

Mathematical Structures Combinations and Permutations

COT 2104 Homework Assignment 1 (Answers)

Top Down Design. Gunnar Gotshalks 03-1

MATH10040: Numbers and Functions Homework 1: Solutions

Stepwise Refinement! Top Down Design!

Discrete Mathematics & Mathematical Reasoning Predicates, Quantifiers and Proof Techniques

CmSc 250 Intro to Algorithms. Mathematical Review. 1. Basic Algebra. (a + b) 2 = a 2 + 2ab + b 2 (a - b) 2 = a 2-2ab + b 2 a 2 - b 2 = (a + b)(a - b)

C241 Homework Assignment 9

1. (16 points) Circle T if the corresponding statement is True or F if it is False.

Proofs of Correctness: Introduction to Axiomatic Verification

Normal Forms of Propositional Logic

C241 Homework Assignment 8

Review (11.1) 1. A sequence is an infinite list of numbers {a n } n=1 = a 1, a 2, a 3, The sequence is said to converge if lim

Hoare Logic: Reasoning About Imperative Programs

CSE 311: Foundations of Computing. Lecture 14: Induction

COM S 330 Homework 08 Solutions. Type your answers to the following questions and submit a PDF file to Blackboard. One page per problem.

CSC 344 Algorithms and Complexity. Proof by Mathematical Induction

Path Testing and Test Coverage. Chapter 9

The natural numbers. Definition. Let X be any inductive set. We define the set of natural numbers as N = C(X).

Lecture 7 Feb 4, 14. Sections 1.7 and 1.8 Some problems from Sec 1.8

Path Testing and Test Coverage. Chapter 9

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

Programming Languages and Compilers (CS 421)

x P(x) x P(x) CSE 311: Foundations of Computing announcements last time: quantifiers, review: logical Inference Fall 2013 Lecture 7: Proofs

MATH 55 - HOMEWORK 6 SOLUTIONS. 1. Section = 1 = (n + 1) 3 = 2. + (n + 1) 3. + (n + 1) 3 = n2 (n + 1) 2.

CSC236H Lecture 2. Ilir Dema. September 19, 2018

Name CMSC203 Fall2008 Exam 2 Solution Key Show All Work!!! Page (16 points) Circle T if the corresponding statement is True or F if it is False.

Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

Math 230 Final Exam, Spring 2009

The following techniques for methods of proofs are discussed in our text: - Vacuous proof - Trivial proof

Semantics and Verification of Software

ICS141: Discrete Mathematics for Computer Science I

Predicate Logic - Undecidability

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

Analysis of Algorithms

means is a subset of. So we say A B for sets A and B if x A we have x B holds. BY CONTRAST, a S means that a is a member of S.

Mathematics for Decision Making: An Introduction. Lecture 8

Lecture Notes on Software Model Checking

Mathematics 220 Homework 4 - Solutions. Solution: We must prove the two statements: (1) if A = B, then A B = A B, and (2) if A B = A B, then A = B.

Floyd-Hoare Style Program Verification

Show Your Work! Point values are in square brackets. There are 35 points possible. Some facts about sets are on the last page.

Foundations of Discrete Mathematics

Fermat's Little Theorem

CS558 Programming Languages

Mid-Semester Quiz Second Semester, 2012

Homework #1. Denote the sum we are interested in as To find we subtract the sum to find that

CS173 Lecture B, September 10, 2015

Axiomatic Semantics. Lecture 9 CS 565 2/12/08

Inductive Definitions with Inference Rules 1 / 27

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

n n P} is a bounded subset Proof. Let A be a nonempty subset of Z, bounded above. Define the set

Department of Computer Science University at Albany, State University of New York Solutions to Sample Discrete Mathematics Examination I (Spring 2008)

15.082J & 6.855J & ESD.78J. Algorithm Analysis

IS 709/809: Computational Methods for IS Research. Math Review: Algorithm Analysis

Proving Programs Correct

CS156: The Calculus of Computation

CISC-102 Winter 2016 Lecture 11 Greatest Common Divisor

Axiomatic Semantics: Verification Conditions. Review of Soundness of Axiomatic Semantics. Questions? Announcements

Loop Convergence. CS 536: Science of Programming, Fall 2018

Axiomatic Semantics. Semantics of Programming Languages course. Joosep Rõõmusaare

Imperative Insertion Sort

Program verification. 18 October 2017

Math 242: Principles of Analysis Fall 2016 Homework 1 Part B solutions

Why do we need math in a data structures course?

Computer Science 280 Spring 2002 Homework 2 Solutions by Omar Nayeem

Semantic Metatheory of SL: Mathematical Induction

G54FOP: Lecture 17 & 18 Denotational Semantics and Domain Theory III & IV

Tight Timing Estimation With the Newton-Gregory Formulae

CSCE 222 Discrete Structures for Computing. Proofs. Dr. Hyunyoung Lee. !!!!! Based on slides by Andreas Klappenecker

CSE 20. Final Review. CSE 20: Final Review

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Mathematical Induction. Rosen Chapter 4.1,4.2 (6 th edition) Rosen Ch. 5.1, 5.2 (7 th edition)

Deductive Verification

CSE 20: Discrete Mathematics

Chapter 2: The Basics. slides 2017, David Doty ECS 220: Theory of Computation based on The Nature of Computation by Moore and Mertens

Heaps Induction. Heaps. Heaps. Tirgul 6

Solutions Manual. Selected odd-numbers problems from. Chapter 3. Proof: Introduction to Higher Mathematics. Seventh Edition

Lecture Notes: Axiomatic Semantics and Hoare-style Verification

INDUCTION AND RECURSION. Lecture 7 - Ch. 4

Math 10850, fall 2017, University of Notre Dame

At least one of us is a knave. What are A and B?

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Transcription:

Spring 2018

Summary Today: Induction, Program Correctness Reading: Chap. 5

Division Theorem Theorem: For every integer a and positive integer d 1, there exist integers q, r such that a = qd + r and 0 r < d Which of the following statements express the division theorem? 1 a. d 1. q. r.[(a = qd + r) (0 r < d)] 2 d 1. a. r. q.[(a = qd + r) (0 r < d)] 3 a. q. d 1. r[(a = qd + r) (0 r < d)] 4 d 1. a. q.[(a qd) {0,..., d 1}] (A) 1; (B) 1,2; (C) 1,2,3; (D) 1,2,3,4; (E) 1,2,4

Proof of the Division Theorem Theorem: d 1. a. q.(a qd) {0,..., d 1} Proof: Let d 1 be any positive integer, and D = {0,..., d 1}. WTP: a. q.(a qd) D We first prove it for a 0, by (strong) induction on a. Base case (a < d): Let q = 0. So, a qd = a D. Inductive Step (a d): Inductive Hypothesis: Assume a {0,..., a 1}. q.(a q d) D WTP: q.(a qd) D. Let a = a d. Notice: a 0 and a < a. By I.H. q.(a q d) D So, q = q + 1 such that a qd = (a + d) (q + 1)d = a q d D.

Proof of the Division Theorem (cont.) What we proved so far: Lemma: a 0. q.(a qd) D WTP: a < 0. q.(a qd) D Proof: Let a = (a + 1) 0. By Division Lemma, q.(a q d) D Let q = (q + 1). Then, a qd = a 1 + (q + 1)d = (d 1) (a q d) D. Homework: give alternative proof for a < 0 by induction on a.

Division Algorithm (python) def divlemma(a,d): # a,d: integers, a>=0, d>0 if (a < d): return 0 else: return (1 + divlemma(a-d,d))

Division Algorithm (python) def divlemma(a,d): # a,d: integers, a>=0, d>0 if (a < d): return 0 else: return (1 + divlemma(a-d,d)) def divthm(a,d): # a,d: integers, d>0 if (a>=0): return (divlemma(a,d)) else: return (-(1+divLemma(-(a+1),d)))

Division Algorithm (haskell) divlemma :: (Integer,Integer) -> Integer divlemma (a,d) = if (a < d) then 0 else 1 + divlemma(a-d,d) divthm :: (Integer,Integer) -> Integer divthm (a,d) = if (a >= 0) then divlemma(a,d) else -(1 + divlemma(-(a+1),d))

Correctness of (Recursive) Programs divlemma :: (Integer,Integer) -> Integer divlemma (a,d) = if (a < d) then 0 else 1 + divlemma(a-d,d) Claim: For all d and a 0, divlemma(a,d) returns an integer q such that a qd {0,..., d 1}. Proof: By induction on a Base case (a < d): Inductive Step (a d):

Correctness of divthm divthm :: (Integer,Integer) -> Integer divthm (a,d) = if (a >= 0) then divlemma(a,d) else -(1 + divlemma(-(a+1),d)) Assume divlemma is correct. Claim: For all d and a, divthm(a,d) returns an integer q such that a qd {0,..., d 1}.

Correctness of divthm divthm :: (Integer,Integer) -> Integer divthm (a,d) = if (a >= 0) then divlemma(a,d) else -(1 + divlemma(-(a+1),d)) Assume divlemma is correct. Claim: For all d and a, divthm(a,d) returns an integer q such that a qd {0,..., d 1}. Question: What proof method would you use? (A) Proof by induction (B) Proof by contraposition (C) Proof by cases (D) Test the program on some random inputs

Another program f :: Integer -> Integer f n = if (n > 0) then 1+f(n-2) else 0 What function does the program compute? (A) f (n) = 1 + (n 2) (B) f (n) = n/2 (C) f (n) = n/2 (D) f (n) = max( n/2, 0) (E) f (n) = max( n/2, 0)

Let s prove it f :: Integer -> Integer f n = if (n > 0) then 1+f(n-2) else 0 Claim: f (n) = max( n/2, 0)

Let s prove it f :: Integer -> Integer f n = if (n > 0) then 1+f(n-2) else 0 Claim: f (n) = max( n/2, 0) Proof: By (strong) induction on n Base case: If n 0 then f(n) = 0 and max( 0/2, 0) = max(0, 0) = 0 Inductive Step (n 1): Assume f (k) = max( k/2, 0) for all k n f (n + 1) =...

Imperative programs def f(n,m): # n,m: integers, n,m >=0, k=0; while (n > 0): n := n-1 k := k + m return k What function does f(n,m) compute? (A) f (n, m) = n + m (B) f (n, m) = n 1 (C) f (n, m) = n m (D) None of the above

Program correctness Prog: some program or code fragment with variables x, y, z,... p, q: predicates on variables x, y, z,... p{prog}q: Prog is (partially) correct with respect to initial assertion p and final assertion q if p is true before executing Prog, and Prog terminates, then q is true after executing Prog

Program correctness: examples Prog: x :=1 ; z := y + x p : y = 1 q : z = 3 p{prog}q? (A) True; (B) False; (C) Don t know

Program correctness: examples Prog: x :=1 ; z := y + x p : y = 1 q : z = 3 p{prog}q? (A) True; (B) False; (C) Don t know p : T (true) q : z > y p{prog}q? (A) True; (B) False; (C) Don t know

Program correctness: conditionals Prog = if condition then Prog1 else Prog2 (p condition){prog1}q, (p condition){prog2}q p{prog}q

Program correctness: loops Prog = while condition do Prog1 (p condition){prog1}p p{prog}(p condition)

How to analyze imperative programs def f(n,m): # n,m: integers, n,m >=0, k=0; c=n; while (c > 0): c := c-1 k := k + m return k Loop Invariant: L(n, m, k, c) L(n, m, k, c) is satisfied before entering the loop If (c > 0) and L(n, m, k, c) are satisfied before entering the loop, then L(n, m, k, c ) is satisfied after executing the loop body once. Claim: L(n, m, k, c) is satisfied at each iteration of the loop Proof: By induction on the number of iterations.

Loop Invariants # { L(x,y,z) } while T(x,y,z): Step1(x,y,z) Step2(x,y,z) Step3(x,y,z) # { L(x,y,z) and not(t(x,y,z)) } Assume L(x, y, z) is a loop invariant, and it is satisfied before entering the loop. Then upon exiting the loop we have L(x, y, z) (T (x, y, z)). If the loop terminates, then upon termination we have L(x, y, z) (T (x, y, z)).

Loop Invariant Example def f(n,m): # n,m: integers n>=0 k=0; c=n; while (c > 0): c := c-1 k := k + m return k L : (k + c m = n m) (c 0) L is a loop invariant Upon termination we have: (k + c m = n m) (c 0) (c 0) c = 0 and the program outputs k = k + c m = n m.