Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 CS 1813 – Discrete Mathematics

Similar presentations


Presentation on theme: "Lecture 16 CS 1813 – Discrete Mathematics"— Presentation transcript:

1 Lecture 16 CS 1813 – Discrete Mathematics
Lecture 17 - CS 1813 Discrete Math, University of Oklahoma 11/28/2018 Lecture 16 CS 1813 – Discrete Mathematics Strong Induction Induction at a Discount or Bringing in the Cavalry to Prove P(n) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

2 deal — a way to split a sequence in half
deal :: [a] -> ( [a], [a] ) Examples deal [1, 2, 3, 4, 5, 6] = ( [1, 3, 5], [2, 4, 6] ) deal [1, 2, 3, 4, 5, 6, 7] = ( [1, 3, 5, 7], [2, 4, 6] ) Pattern of computation deal [x1, x2, … x2n] = ( [x1, x3, … x2n-1], [x2, x4, … x2n] ) Definition deal (x1: (x2: xs)) = ( x1: ys, x2: zs ) where (ys, zs) = deal xs deal [x] = ( [x], [ ] ) deal [ ] = ( [ ], [ ] ) What equation changes if we want the extra element (if there is one) to go into the list in the second component? CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

3 Termination Is deal a Finite Computation?
Equations for deal deal :: [a] -> ( [a], [a] ) deal (x1: (x2: xs)) = ( x1: ys, x2: zs ) where (ys, zs) = deal xs deal [x] = ( [x], [ ] ) deal [ ] = ( [ ], [ ] ) Question of termination Given: a computer system that can interpret the deal equations Question: What conditions imply termination? Given a sequence xs::[a], under what conditions will the computer system be able to deliver, in a finite amount of time, a value of the form (ys, zs) = deal xs Answer When xs has a finite number of elements, (deal xs) terminates Proof … need to know a few things about basic operations CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

4 CS 1813 Discrete Mathematics, Univ Oklahoma
Basic Operations deal :: [a] -> ( [a], [a] ) deal (x1: (x2: xs)) = ( x1: ys, x2: zs ) where (ys, zs) = deal xs deal [x] = ( [x], [ ] ) deal [ ] = ( [ ], [ ] ) Matching For any sequence xs::[a], computer system can determine, in a finite amount of time, which deal-equation matches xs Pairing For any sequences xs, ys::[a], computer system can build, in a finite amount of time, the pair (xs, ys) Insertion For any sequence xs::[a] and any value x::a, computer system can build, in a finite amount of time, the sequence (x: xs) Empty System can build, in a finite amount of time, the sequence [ ] CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

5 CS 1813 Discrete Mathematics, Univ Oklahoma
Induction with a Twist n.(mn.P(m))P(n) {StrInd} n.P(n) Basic Principle of Induction Prove P(0) is true Prove P(n+1) is true, nN arbitrary Proof of P(n+1) may assume P(n) is true Conclude nN. P(n) What if proof of P(n+1) does not make use of P(n)? Conclusion nN. P(n) is still valid This would be a direct proof using the {I} inference rule The principle of induction is a lever that makes the proof easier Another Principle of Induction Prove P(n) for arbitrary n  N Twist: Can assume kDn.P(k), where Dn= {kN | k  n} Getting Started: D0 = { } So, must prove P(0) from scratch (as with old principle of induction) Strong Induction Pretty good deal, eh? CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

6 deal Terminates (deal xs) delivers (ys, zs) in a finite amount of time
deal :: [a] -> ( [a], [a] ) deal (x1: (x2: xs)) = ( x1: ys, x2: zs ) where (ys, zs) = deal xs deal [x] = ( [x], [ ] ) deal [ ] = ( [ ], [ ] ) P(n)  length xs = n  (deal xs) terminates Proof of P(0) deal xs = deal [ ] (matching) xs has 0 elements = ( [ ], [ ] ) (pairing, empty ) (deal).[] Each step takes a finite amount of time, so (deal xs) terminates Proof of P(1) = deal [x] (matching) xs has 1 element = ( [x], [ ] ) (pairing, empty) (deal).[x] Again, each step takes a finite amount of time, so (deal xs) terminates CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

7 deal Terminates — inductive case
deal :: [a] -> ( [a], [a] ) deal (x1: (x2: xs)) = ( x1: ys, x2: zs ) where (ys, zs) = deal xs P(n)  length xs = n  (deal xs) terminates Proof of P(n) for arbitrary n  {2, 3, 4, …} deal xs = deal (x1: (x2: ws)) (matching) xs has 2 or more elements = (x1: ys, x2: zs) where (ys, zs) = deal ws (deal).:: Two insertions Pair construction Terminates because ws has n-2 elements P(n-2) assumed true Each step finite, so (deal xs) terminates Summary P(0) proved (direct proof) P(1) proved (direct proof) P(n) proved for arbitrary n  {2, 3, 4, …} (inductive proof) Conclude nN. P(n) (strong induction) qed CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

8 CS 1813 Discrete Mathematics, Univ Oklahoma
merge merge :: Ord a => [a] -> [a] -> [a] Desired outcome Sequence in increasing order (if both arguments are) All (and only) elements from both sequences included in result Examples merge [1, 3, 4, 7, 10] [2, 5, 8, 9] = [1, 2, 3, 4, 5, 7, 8, 9, 10] merge [“if”, “it”, “were”] [“ain’t”, “is”, “it”] = [“ain’t”, “if”, “is”, “it”, “it”, “were”] Preconditions x1  x2  …  xn and y1  y2  …  ym Postconditions (zs = [z1, z2, … zn+m] = merge xs ys)  (z1  z2  …  zn+m )  (xs  zs)(ys  zs)(length(xs ++ ys)= length zs) where vs  ws means vs is a subsequence of ws Any cases not covered? merge (x: xs) (y: ys) = if y  x then y : (merge (x: xs) ys) else x : (merge xs (y: ys)) merge [ ] ys = ys merge (x: xs) [ ] = (x: xs) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page merge (x: xs) [ ]

9 Making a Sequence Ordered — Sorting
msort :: Ord a => [a] -> [a] Desired outcome Sequence containing same elements as input … … but arranged in order Preconditions Ord a, xs::[a], xs has finite number of elements Postconditions (ys = [y1, y2, … , yn] = msort xs)  y1  y2  …  yn  ((x  xs)  (x  ys))  length xs = length(msort xs) where “” means “element of” All cases covered? msort (x1: x2: xs) = merge (msort ys) (msort zs) where (ys, zs) = deal(x1: x2: xs) merge-sort msort [ ] = [ ] msort [x] = [x] Theorem: msort = qsort Definition qsort (x: xs) = qsort[y | y <- xs, y  x] ++ [x] ++ qsort[y | y<- xs, y = x] qsort [ ] = [ ] Proof: major project quicksort (Antony Hoare) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

10 CS 1813 Discrete Mathematics, Univ Oklahoma
msort Terminates Theorem (mT): (merge xs ys) terminates  length(merge xs ys) = (length xs) + (length ys) Proof — induction on n = (length xs) + (length ys) Need finite time for two additional operations Comparing: Ord a, x, y::a, system computes (x  y) in finite time Selecting: b::Bool, x, y::a, system computes (if b then x else y) in finite time Theorem (dS): n. (length xs = n)  (ys, zs) = deal xs  (deal xs) terminates  (length xs) = (length ys) + (length zs) Proof — induction on n = (length xs) Corollary (dR): (length xs)  2  (ys, zs) = deal xs  length ys  (length xs)  (length zs)  (length xs) Theorem(msT): (length xs) = n  (msort xs) terminates  length(msort xs) = n Proof — next slide CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

11 Termination Proof — msort
msort (x1: x2: xs) = merge (msort ys) (msort zs) where (ys, zs) = deal(x1: x2: xs) msort [ ] = [ ] msort [x] = [x] P(n)  (length xs) = n  (msort xs) terminates  length(msort xs) = n Proof of P(0) msort xs = msort [ ] (pattern matching) (length xs) = 0 = [ ] (build empty sequence) (msort).[] Each step takes a finite amount of time, so (msort xs) terminates Proof of P(1) = msort [x] (matching) (length xs) = 1 = [x] (no operation) (msort).[x] CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

12 msort Termination Proof — inductive case
msort (x1: x2: xs) = merge (msort ys) (msort zs) where (ys, zs) = deal(x1: x2: xs) P(n)  (length xs) = n  (msort xs) terminates  length(msort xs) = n Proof of P(n) for arbitrary n  {2, 3, 4, …} msort xs = msort(x1: x2: ws) (pattern matching) (length xs)  2 = merge (msort ys) (msort zs) (no operation) (msort).:: where (ys, zs) = deal(x1: x2: ws) merge terminates (mT) (msort ys) terminates – Why? (msort zs) terminates (similar argument) deal terminates (dS) length ys  length xs (dR) Ind Hyp implies P(length ys) is True Each step (matching, deal, msort twice, merge) takes a finite amount of time, so (msort xs) terminates length xs = length ys + length zs (dS) = length(msort ys) + length(msort zs) P(length ys), P(length zs) = length(merge (msort ys) (msort zs)) (mT) = length(msort xs) (msort).:: – as in above proof CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page qed (strong induction)

13 Lecture 17 - CS 1813 Discrete Math, University of Oklahoma
11/28/2018 End of Lecture CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page


Download ppt "Lecture 16 CS 1813 – Discrete Mathematics"

Similar presentations


Ads by Google