5-Recursion -1 - PowerPoint PPT Presentation

About This Presentation
Title:

5-Recursion -1

Description:

... Every postage amount of at least 18 cents can be formed using just 4-cent and 7-cent stamps: - First 4 cases: 18=2*7+4, 19=7+3*4, 20=5*4, 21=3*7 ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 19
Provided by: icsUciEdu2
Learn more at: https://ics.uci.edu
Category:
Tags: recursion | stamps

less

Transcript and Presenter's Notes

Title: 5-Recursion -1


1
Strong Induction
  • Normal Induction If we prove that 1)
    P(n0) 2) For any kn0, if P(k) then P(k1)
  • Then P(n) is true for all nn0.
  • Strong Induction If we prove that 1)
    Q(n0) 2) For any kn0, if Q(j) for all j from
    n0 to k then Q(k1) Then Q(k) is true for all
    kn0.
  • These two are equivalent!
  • Why? Set P(n) to be the predicate If n0 j
    n then Q(j)
  • P(n0) Q(n0)
  • P(k) Q(j) holds for all j between n0 and k

2
Proof by Strong Induction Jigsaw Puzzle
  • Each step in assembling a jigsaw puzzle
    consists of putting together two already
    assembled blocks of pieces where each single
    piece is considered a block itself.
  • P(n) It takes exactly n-1 steps to assemble a
    saw puzzle of n pieces.
  • Basis Step P(1) is (trivially) true.
  • Inductive Step We assume P(j) true for all jk
    and well argue P(k1)
  • The last step in assembling a puzzle of k1
    pieces is to put together two blocks one of size
    jgt0 and one of size k1-j, for some j.
  • Since 0ltj,k1-jk, P(j) and P(k1-j) are
    both assumed true.
  • And so, the total number of steps to assemble a
    puzzle with n1 pieces is 1 (j-1)
    ((k1-j)-1) k (k1)-1.
  • (This implies P(k1), and hence ends the
    inductive part, and thus also the proof.)

3
More Examples of Theorems with easy Proofs using
Strong Induction
  • Thm1 The second player always wins the following
    game
  • Starting with 2 piles each containing the same
    number of matches, players alternately remove any
    non-zero number of matches from one of the piles.
  • The winner is the person who removes the last
    match.
  • Thm2 Every ngt1 can be written as the product of
    primes.
  • Thm3 Every postage amount of at least 18 cents
    can be formed using just 4-cent and 7-cent
    stamps
  • - First 4 cases 18274, 19734, 2054,
    2137
  • - Afterwards, using strong induction Take any
    k21. By (strong) inductive assumption, and by
    the 4 cases above, k-3i7j4 for some i,j.
    Therefore k1i7(j1)4, which ends the
    inductive step.
  • (Btw, starting from 18 may seem weird, but
    you just cant do it for 17...)

4
Basis for Induction Integers are well ordered
  • Induction is based on the fact that the integers
    are well ordered, i.e.
  • Any non-empty set of integerswhich is bounded
    below i.e. there exists b (not necessarily in
    S) s.t. b x for all x in Scontains a smallest
    element i.e. e in S s.t. for all x in S we
    have e x.
  • Why does well-ordering imply induction?
  • Assume () P(0) and () P(k)?P(k1) for all
    k0.
  • Well show that P(n)T for all n0 (thus showing
    that induction works)
  • Define () S ngt0 P(n)F. Assume S
    non-empty.
  • By well-ordering of N, there is a least element e
    of S.
  • Consider element e1 Since P(e) F then by
    () also P(e1) F.
  • Note that e1 cannot be equal to 0 because P(0)
    T by ().
  • But then by (), e1 is in S, so e is not the
    least element of S
  • Contradiction!
  • Therefore S must be empty. Therefore P(n)T for
    all n 0.

5
Well Ordering can be used directly to prove
things (i.e. not necessarily via induction)
  • Theorem If a and b are integers, not both 0,
    then ?s,t?Z (sa tb gcd( a,b )).
  • Proof For any a,b define S ngt0 ?s,t?Z n
    sa tb.
  • By the well-ordering of Z, S has a smallest
    element, call it d.
  • Choose s and t so that d sa tb.
  • Claim d is a common divisor of a and b.
  • Proof that d a
  • Writing d qa r where 0rltd. If r0 then d
    a.
  • If rgt0 then since r d qa (sa tb)
    qa (s - q)a tb
  • we would have r?S.
  • But since rltd it would mean that d is not the
    smallest element of S
  • gt contradiction gt and therefore r0 (and hence
    d a).
  • Similarly, d b.
  • d is the greatest common divisor since any common
    divisor of a and b must also divide sa tb d.

6
Recursive (Inductive) Definitions
  • A function f N?R is defined recursively by
    specifying(1) f(0), its value at 0, and(2)
    f(n), for ngt0, in terms of f(1),.,f(n-1), i.e
    . in terms of f(k)s for kltn.
  • Examples
  • f(n)n! can be specified as f(0)1 and, for ngt0,
    f(n)nf(n-1).
  • For any a, fa(n)an, can be specified as fa(0)1
    and,for ngt0, fa(n)afa(n-1)
  • Note In many cases, we specify f(k) explicitly
    not only for f(0) but also for f(1), f(2), ,
    f(m) for some mgt0, and then use a recursive
    formula to define f(n) for all ngtm.

7
Fibonacci Numbers are Recursively Defined
  • The Fibonacci numbers f0,f1,f2,,fn,, are
    defined by
  • (1) f00, f11
  • (2) fnfn-1fn-2, for n2
  • (Btw, we can also use F(n) instead of fn to
    designate n-th Fibonacci number.)
  • The first 18 Fibonacci numbers are
  • f00, f11, f21, f32, f43, f55, f68,
    f713, f821, f934, f1055, f1089, f11144,
    f12233, f13377, f14610, f15987, f111597,
    f122584, f134181, f146765, f1510946,
    f1617711, f1728657, f1846368, ...
  • Ref http//en.wikipedia.org/wiki/Fibonacci_number
  • Question Are they growing exponentially, i.e.
    like fn an for some a?

8
Fibonacci numbers are related to a Golden
Sectioni.e. the splitting of an interval so that
(larger part) / whole (smaller p. / larger p.)
Ref http//en.wikipedia.org/wiki/Golden_ratio
9
Fibonacci Growth
  • Theorem If n3 then fn gt f n-2
  • where f is the solution to golden ratio, i.e. f
    (1?5) / 2
  • Proof by Strong Induction
  • Basis Step (for n3 and 4)
  • f3 2 gt f 1.618
  • f4 3 gt f2 (12?55)/4 (3?5)/2 f1
    2.168
  • Fact you can check f2 f1
  • Inductive Step Assume fkgtfk-2 for all k s.t.
    3kn.
  • fn1 fnfn-1 gt fn-2fn-3 fn-3(f1) fn-3f2
    f(n1)-2

10
Recursively Defined Sets
  • Always (1) Basis Step and (2) Recursive Step
  • Set S of multiples of 3(1) 3?S (2) If x?S
    and y?S, then xy ?S
  • Strings S over an alphabet S. Let ? be the
    empty string.(1) ?? S (2) If w?S and x?S,
    then wx?S
  • Examples S0,1 S0,1,2,3,4,5,6,7,8,9
    Sa,b,c,d,e,,x,y,z
  • Now recursively define length of a string, L
    S?N where (1) L(?)0 (2) L(wx)L(w)1
  • String Catenation ()(1) If u?S, then
    u?u(2) If u,w?S and x?S, then u(wx)(uw)x

11
Recursively Defined Sets
  • Always (1) Basis Step and (2) Recursive Step
  • Well-Formed Boolean Formula
  • T, F, and s, where s is a propositional variable
    are all well-formed formula (WFF).
  • If E and F are WFFs, then (E), (E?F), (E?F),
    (E?F), and (E?F) are all WFFs.
  • Well-Formed Arithmetic Expression
  • x is a well-formed arithmetic expression (WFA) if
    x is either a numeral or a variable.
  • If F and G are WFAs, then (FG), (F-G), (FG),
    (F/G), and (E?F) are all WFAs.

12
Recursively Defined StructuresRooted Trees
  • Rooted Trees(1) A single vertex, r, is a rooted
    tree with root r.(2) Suppose that T1,T2,,Tn are
    rooted trees with roots r1,r2,,rn
    respectively. Then the graph formed by starting
    with a root, r, which is not in any of these
    trees and adding an edge from r to each of
    these roots is also a rooted tree, whose root is
    r.
  • Basis Step 1
    Step 2

13
Recursively Defined StructuresExtended Binary
Trees
  • Extended Binary Trees(1) The empty set is an
    Extended Binary Tree(2) If T1 and T2 are
    extended binary trees, then the following tree,
    denoted T1T2 , is also an extended binary
    tree pick a new root
  • r, and attach T1 as the left subtree and
    T2 as the right subtree.
  • Step 1 Step 2 Step 3

14
Full Binary Trees
  • Full Binary Trees(1) A single vertex is a Full
    Binary Tree(2) If T1 and T2 are full binary
    trees, then the following tree, denoted T1T2 ,
    is also a full binary tree pick a new root, r,
    and attach T1 as the left subtree and attach T2
    as the right subtree.
  • (Definition change only in the base case, but
    unlike Extended Binary
  • Trees, Full Binary Tree has exactly 0 or 2
    child-nodes)
  • Base Step 1 Step 2

15
(Recursive) Definitions of Functions on Full
Binary Trees
  • Definition of Height, h(T), of a Full Binary Tree
    T(1) If T has a single (root) node/vertex then
    h(T)0.(2) O/w h(T1T2 )1max(h(T1),h(T2 ))
  • The Number of vertices, n(T), of a Full Binary
    Tree, T, is given by(1) If T has a single (root)
    node/vertex, n(T)1.(2) n(T1T2 )1n(T1)n(T2 )

16
Structural Induction
  • If a set is recursively defined, to show a
    predicate true for all elements in the set one
    needs to
  • (1) Show the predicate true on all base cases
  • (2) Show that if the predicate is true for each
    of the elements used to construct a new element,
    then the same predicate is also true for that new
    element.
  • E.g. to prove P(T) holds for all T?FBT, these
    steps are
  • (1) showing P(vertex) holds, because base case of
    FBT definition is just a vertex
  • (2) Showing that if P(T1) and P(T2) then P(T1T2
    ), because the recursive clause of FBT definition
    defines a new FBT as T1T2 .
  • Example Well show that if T is a full binary
    tree, then n(T) 2h(T)1-1
  • (proof on next slide)

17
Structural Induction Example
  • Thrm If T is a full binary tree, then
    n(T)2h(T)1-1
  • Proof
  • Basis Step If T is just the root vertex,
  • n(T)1, h(T)0 and therefore n(T)1 201-1 1
  • Inductive Step When T T1T2 , we compute
  • n(T)1n(T1)n(T2) Definition of n(T)
    1(2h(T1)1-1)(2h(T2)1-1) Inductive
    hypothesis 2max(2h(T1)1,2h(T2)1)-1 Arithmeti
    c
  • 221max(h(T1),h(T2)) -1 Arithmetic
  • 22h(T)-1 Definition of h(T)

18
Recursive Algorithms
  • An algorithm is recursive if it solves a
    problem by reducing it to an instance of the same
    problem with smaller input.
  • Examples
  • procedure factorial (n nonnegative integer)if
    n0 then factorial(n)1else factorial(n)n
    factorial(n-1)
  • procedure power(a nonzero real, n nonnegative
    integer)if n0 then power(a,n)1else
    power(a,n)apower(a,n-1)
  • procedure gcd(a,b nonnegative integers with
    altb)if a0 then gcd(a,b)belse gcd(a,b)gcd(b
    mod a, a)
  • procedure fibonacci (n nonnegative integer)if
    n1 then fibonacci(n)1else fibonacci
    (n)fibonacci (n-1)fibonacci (n-2)
Write a Comment
User Comments (0)
About PowerShow.com