University of Florida Dept' of Computer - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

University of Florida Dept' of Computer

Description:

the number of steps in Euclid's. algorithm to find gcd(a,b) is. 5k, where k ... Consider the sequence of division-algorithm equations used in Euclid's alg. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 38
Provided by: michae1481
Category:

less

Transcript and Presenter's Notes

Title: University of Florida Dept' of Computer


1
University of FloridaDept. of Computer
Information Science EngineeringCOT
3100Applications of Discrete StructuresDr.
Michael P. Frank
  • Slides for a Course Based on the TextDiscrete
    Mathematics Its Applications (5th Edition)by
    Kenneth H. Rosen

2
Module 16Recursion
  • Rosen 5th ed., 3.4-3.5
  • 34 slides

3
3.4 Recursive Definitions
  • In induction, we prove all members of an infinite
    set satisfy some predicate P by
  • proving the truth of the predicate for larger
    members in terms of that of smaller members.
  • In recursive definitions, we similarly define a
    function, a predicate, a set, or a more complex
    structure over an infinite domain (universe of
    discourse) by
  • defining the function, predicate value, set
    membership, or structure of larger elements in
    terms of those of smaller ones.
  • In structural induction, we inductively prove
    properties of recursively-defined objects in a
    way that parallels the objects own recursive
    definitions.

4
Recursion
  • Recursion is the general term for the practice of
    defining an object in terms of itself
  • or of part of itself
  • This may seem circular, but it isnt necessarily.
  • An inductive proof establishes the truth of
    P(n1) recursively in terms of P(n).
  • There are also recursive algorithms, definitions,
    functions, sequences, sets, and other structures.

5
Recursively Defined Functions
  • Simplest case One way to define a function fN?S
    (for any set S) or series anf(n) is to
  • Define f(0).
  • For ngt0, define f(n) in terms of f(0),,f(n-1).
  • E.g. Define the series an 2n recursively
  • Let a0 1.
  • For ngt0, let an 2an-1.

6
Another Example
  • Suppose we define f(n) for all n?N recursively
    by
  • Let f(0)3
  • For all n?N, let f(n1)2f(n)3
  • What are the values of the following?
  • f(1) f(2) f(3) f(4)

21
9
45
93
7
Recursive definition of Factorial
  • Give an inductive (recursive) definition of the
    factorial function, F(n) n! ?1in i
    1?2??n.
  • Base case F(0) 1
  • Recursive part F(n) n ? F(n-1).
  • F(1)1
  • F(2)2
  • F(3)6

8
More Easy Examples
  • Write down recursive definitions for
  • in (i integer, n natural) using only s(i) i1.
  • an (a real, n natural) using only addition
  • an (a real, n natural) using only multiplication
  • ?0in ai (for an arbitrary series of numbers
    ai)
  • ?0in ai (for an arbitrary series of numbers
    ai)
  • n0in Si (for an arbitrary series of sets Si)

9
The Fibonacci Series
  • The Fibonacci series fn0 is a famous series
    defined by f0 0, f1 1, fn2 fn-1
    fn-2

0
1
1
2
3
5
8
13
Leonardo Fibonacci1170-1250
10
Inductive Proof about Fib. series
  • Theorem fn lt 2n.
  • Proof By induction.
  • Base cases f0 0 lt 20 1 f1 1 lt 21 2
  • Inductive step Use 2nd principle of induction
    (strong induction). Assume ?kltn, fk lt 2k.
    Then fn fn-1 fn-2 is lt 2n-1
    2n-2 lt 2n-1 2n-1 2n.

Implicitly for all n?N
Note use ofbase cases ofrecursive defn.
11
A lower bound on Fibonacci series
  • Theorem. For all integers n 3, fn gt an-2, where
    a (151/2)/2 1.61803.
  • Proof. (Using strong induction.)
  • Let P(n) (fn gt an-2).
  • Base cases For n3, note that a lt 2 f3. For
    n4, a2 (1251/25)/4 (351/2)/2 2.61803 lt
    3 f4.
  • Inductive step For k4, assume P(j) for 3jk,
    prove P(k1). Note a2 a1. Thus, ak-1
    (a1)ak-3 ak-2 ak-3. By inductive
    hypothesis, fk-1 gt ak-3 and fk gt ak-2. So, fk1
    fk fk-1 gt ak-2 ak-3 ak-1. Thus P(k1).

12
Lamés Theorem
  • Theorem ?a,b?N, abgt0,the number of steps in
    Euclidsalgorithm to find gcd(a,b) is 5k,
    where k ?log10 b?1 is thenumber of decimal
    digits in b.
  • Thus, Euclids algorithm is linear-time in the
    number of digits in b.
  • Proof
  • Uses the Fibonacci sequence!
  • See next 2 slides.

Gabriel Lamé1795-1870
13
Proof of Lamés Theorem
  • Consider the sequence of division-algorithm
    equations used in Euclids alg.
  • r0 r1q1 r2 with 0 r2 lt r1
  • r1 r2q2 r3 with 0 r3 lt r2
  • rn-2 rn-1qn-1 rn with 0 rn lt rn-1
  • rn-1 rnqn rn1 with rn1 0
    (terminate)
  • The number of divisions (iterations) is n.

Where a r0,b r1, and gcd(a,b)rn.
Continued on next slide
14
Lamé Proof, continued
  • Since r0 r1 gt r2 gt gt rn, each quotient qi
    ?ri-1/ri? 1.
  • Since rn-1 rnqn and rn-1 gt rn, qn 2.
  • So we have the following relations between r and
    f
  • rn 1 f2
  • rn-1 2rn 2f2 f3
  • rn-2 rn-1 rn f2 f3 f4
  • r2 r3 r4 fn-1 fn-2 fn
  • b r1 r2 r3 fn fn-1 fn1.
  • Thus, if ngt2 divisions are used, then b fn1 gt
    an-1.
  • Thus, log10 b gt log10(an-1) (n-1)log10 a
    (n-1)0.208 gt (n-1)/5.
  • If b has k decimal digits, then log10 b lt k, so
    n-1 lt 5k, so n 5k.

15
Recursively Defined Sets
  • An infinite set S may be defined recursively, by
    giving
  • A small finite set of base elements of S.
  • A rule for constructing new elements of S from
    previously-established elements.
  • Implicitly, S has no other elements but these.
  • Example Let 3?S, and let xy?S if x,y?S. What
    is S?

16
The Set of All Strings
  • Given an alphabet S, the set S of all strings
    over S can be recursively defined by e ? S (e
    , the empty string)
  • w ? S ? x ? S ? wx ? S
  • Exercise Prove that this definition is
    equivalent to our old one

Bookuses ?
17
Other Easy String Examples
  • Give recursive definitions for
  • The concatenation of strings w1w2.
  • The length ?(w) of a string w.
  • Well-formed formulae of propositional logic
    involving T, F, propositional variables, and
    operators in , ?, ?, ?, ?.
  • Well-formed arithmetic formulae involving
    variables, numerals, and ops in , -, , ?.

18
Rooted Trees
  • Trees will be covered in more depth in chapter 9.
  • Briefly, a tree is a graph in which there is
    exactly one undirected path between each pair of
    nodes.
  • An undirected graph can be represented as a set
    of unordered pairs (called arcs) of objects
    called nodes.
  • Definition of the set of rooted trees
  • Any single node r is a rooted tree.
  • If T1, , Tn are disjoint rooted trees with
    respective roots r1, , rn, and r is a node not
    in any of the Tis, then another rooted tree is
    r,r1,,r,rn?T1??Tn.

19
Illustrating Rooted Tree Defn.
  • How rooted trees can be combined to form a new
    rooted tree

r

T1
r1
T2
r2
Tn
rn
Draw some examples
20
Extended Binary Trees
  • A special case of rooted trees.
  • Recursive definition of EBTs
  • The empty set ? is an extended binary tree.
  • If T1,T2 are disjoint EBTs, then e1?e2? T1?T2 is
    an EBT, where e1 ? if T1 ?, and e1 (r,r1)
    if T1?? and has root r1, and similarly for e2.

Draw some examples
21
Full Binary Trees
  • A special case of extended binary trees.
  • Recursive definition of FBTs
  • A single node r is a full binary tree.
  • Note this is different from the EBT base case.
  • If T1,T2 are disjoint FBTs, then e1?e2?T1?T2,
    where e1 ? if T1 ?, and e1 (r,r1) if T1??
    and has root r1, and similarly for e2.
  • Note this is the same as the EBT recursive case!
  • Can simplify it to If T1,T2 are disjoint FBTs
    with roots r1 and r2, then (r, r1),(r,r2)?
    T1?T2 is an FBT.

Draw some examples
22
Structural Induction
  • Proving something about a recursively defined
    object using an inductive proof whose structure
    mirrors the objects definition.
  • Example problem Let 3?S, and let xy?S if x,y?S.
    Show that S n?Z (3n) (the set of positive
    multiples of 3).

23
Example continued
  • Let 3?S, and let xy?S if x,y?S. Let A n?Z
    (3n).
  • Theorem AS. Proof We show that A?S and S?A.
  • To show A?S, show n?Z ? (3n)? n?S.
  • Inductive proof. Let P(n) n?S. Induction
    over positive multiples of 3. Base case n3,
    thus 3?S by defn. of S. Inductive step Given
    P(n), prove P(n3). By inductive hyp., n?S, and
    3?S, so by defn of S, n3?S.
  • To show S?A let n?S, show n?A.
  • Structural inductive proof. Let P(n)n?A. Two
    cases n3 (base case), which is in A, or nxy
    (recursive step). We know x and y are positive,
    since neither rule generates negative numbers.
    So, xltn and yltn, and so we know x and y are in A,
    by strong inductive hypothesis. Since 3x and
    3y, we have 3(xy), thus xy ? A.

24
Recursive Algorithms (3.5)
  • Recursive definitions can be used to describe
    algorithms as well as functions and sets.
  • Example A procedure to compute an.
  • procedure power(a?0 real, n?N)
  • if n 0 then return 1 else return a
    power(a, n-1)

25
Efficiency of Recursive Algorithms
  • The time complexity of a recursive algorithm may
    depend critically on the number of recursive
    calls it makes.
  • Example Modular exponentiation to a power n can
    take log(n) time if done right, but linear time
    if done slightly differently.
  • Task Compute bn mod m, where m2, n0, and
    1bltm.

26
Modular Exponentiation Alg. 1
  • Uses the fact that bn bbn-1 and that xy mod
    m x(y mod m) mod m.(Prove the latter theorem
    at home.)
  • procedure mpower(b1,n0,mgtb ?N)
  • Returns bn mod m.if n0 then return 1
    elsereturn (bmpower(b,n-1,m)) mod m
  • Note this algorithm takes T(n) steps!

27
Modular Exponentiation Alg. 2
  • Uses the fact that b2k bk2 (bk)2.
  • procedure mpower(b,n,m) same signature
  • if n0 then return 1else if 2n then return
    mpower(b,n/2,m)2 mod melse return
    (mpower(b,n-1,m)b) mod m
  • What is its time complexity?

T(log n) steps
28
A Slight Variation
  • Nearly identical but takes T(n) time instead!
  • procedure mpower(b,n,m) same signature
  • if n0 then return 1else if 2n then return
    (mpower(b,n/2,m)
    mpower(b,n/2,m)) mod melse return
    (mpower(b,n-1,m)b) mod m

The number of recursive calls made is critical!
29
Recursive Euclids Algorithm
  • procedure gcd(a,b?N)if a 0 then return belse
    return gcd(b mod a, a)
  • Note recursive algorithms are often simpler to
    code than iterative ones
  • However, they can consume more stack space, if
    your compiler is not smart enough.

30
Recursive Linear Search
  • Note there is no real advantage to using
    recursion here, rather than just looping for loc
    i to j
  • procedure search(a series i, j integer x
    item to be found)
  • Find x in series a at a location i and ltjif
    ai x return i At the right item? Return
    it!if i j return 0 No locations in range?
    Failure!return search(i1, j, x) Try rest of
    range

31
Recursive Binary Search
  • procedure binarySearch(a, x, i, j) same
    sigFind location of x in a, i and ltj m
    ?(i j)/2? Go to halfway point.if x
    am return m Did we luck out?if xltam
    ? iltm return If its to the left,
    binarySearch(a,x,i,m-1)Check that ½else if
    amltx ? mltj return If its to right,
    binarySearch(a,x,m1,j)Check that ½else
    return 0 No more items, failure.

32
Recursive Fibonacci Algorithm
  • procedure fibonacci(n ? N)if n0 return 0if n1
    return 1return fibonacci(n-1)fibonacci(n-2)
  • Is this an efficient algorithm?
  • Is it polynomial-time in n?
  • How many additions are performed?

33
Analysis of Fibonacci Procedure
  • Theorem The preceding procedure for fibonacci(n)
    performs fn1-1 addition operations.
  • Proof By strong structural induction over n,
    based on the procedures own recursive
    definition.
  • Base cases fibonacci(0) performs 0 additions,
    and f01-1 f1 - 1 1 - 1 0. Likewise,
    fibonacci(1) performs 0 additions, and f11-1
    f2-1 1-1 0.
  • Inductive step For ngt1, by strong inductive
    hypothesis, fibonacci(n-1) and fibonacci(n-2) do
    fn-1 and fn-1-1 additions respectively, and
    fibonacci(n) adds 1 more, for a total of fn-1
    fn-1-11 fnfn-11 fn11.

34
A more efficient algorithm
  • procedure findFib(a,b,m,n)Given afm-1, bfm,
    and mn, return fnif mn return breturn
    findFib(b, ab, m1, n)
  • procedure fastFib(n) Find fn in T(n) steps.if
    n0 return 0return findFib(0,1,1,n)

35
Recursive Merge Sort
  • procedure sort(L ?1,, ?n)if ngt1 then m
    ?n/2? this is rough ½-way point L
    merge(sort(?1,, ?m), sort(?m1,,
    ?n))return L
  • The merge (next slide) takes T(n) steps, and
    merge-sort takes T(n log n).

36
Recursive Merge Method
  • procedure merge(A,B sorted lists)Given two
    sorted lists A(a1,,aA), B(b1,,bB), return
    a sorted list of all.
  • if A () return B If A is empty, its B.if
    B () return A If B is empty, its A.if
    a1ltb1 return (a1,merge((a2,aA),B))return
    (b1,merge(A,(b2,,bB)))

37
Merge Routine
  • procedure merge(A, B sorted lists)L empty
    listi0, j0, k0while iltA ? jltB A
    is length of A if iA then Lk Bj j j
    1 else if jB then Lk Ai i i
    1 else if Ai lt Bj then Lk Ai i i
    1 else Lk Bj j j 1 k k1return L

Takes T(AB) time
Write a Comment
User Comments (0)
About PowerShow.com