Chapter 6 Recursive - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Chapter 6 Recursive

Description:

Chapter 6 Recursive K. H. Rosen, Discrete Mathematics and Its Applications, 5th Edition ... – PowerPoint PPT presentation

Number of Views:197
Avg rating:3.0/5.0
Slides: 16
Provided by: acth
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 Recursive


1
Chapter 6Recursive
  • ???????? ????????????????
  • K. H. Rosen, Discrete Mathematics and Its
    Applications, 5th Edition, McGraw-Hill.
  • A. V. Aho and J. D. Ullman, Foundations of
    Computer Science, C Edition, W.H. Freeman.
  • J. Martin, Introduction to Languages and the
    Theory of Computation, 3rd Edition, McGraw-Hill.

1/2552 204111
2
Outline
  • Concept of Recursive
  • Recursive Functions
  • Simple Recursive Procedure

3
6.1 Concept of Recursive
  • A recursive (or inductive) definition involves
  • One or more basis rules,
    in which some simple objects are
    defined, and
  • One or more inductive rules, whereby
    larger objects are defined in terms of
    smaller ones in the collection.

4
6.1 Concept of Recursive
  • Factorial function is defined by 123n to get
    n!, we can also defined the value of n! as
    follows
  • Basis step 1! 1
  • Induction step n! n(n-1)!
  • n1, 1! 1
  • n2, 21! 21 2
  • n3, 32! 32 6
  • n4, 43! 46 24
  • and so on.

5
6.1 Concept of Recursive
  • 1 if n0
  • n(n-1)! if n gt 0
  • Factorial function is defined on the set of
    natural number, first by defining the value at 0,
    and then
  • by defining the value at any larger natural
    number in terms of its value at the previous one.
  • There is an obvious analogy here to the basis
    step and the induction step in a proof by
    mathematic induction.

n!
6
6.2 Recursive Functions
  • We use two steps to defined a function with the
    set of nonnegative integer as its domain
  • Basis step Specify the value of the function at
    zero.
  • Recursive step Give a rule for finding its value
    at an integer from its values at smaller
    integers.
  • Such a definition is called a recursive or
    inductive definition.

7
6.2 Recursive Functions
  • Ex. Suppose that f is defined recursively by
  • f(0) 3
  • f(n1) 2f(n) 3
  • Find f(1), f(2), f(3), and f(4).
  • From recursive definition it follows that
  • f(1) 2f(0) 3 2(3)3 63 9,
  • f(2) 2f(1) 3 2(9)3 183 21,
  • f(3) 2f(2) 3 2(21)3 423 45,
  • f(4) 2f(3) 3 2(45)3 903 93.

8
6.2 Recursive Functions
  • Definition
  • The Fibonacci numbers, f0, f1, f2, are defined
    by the equations f0 0, f1 1, and
  • fn fn-1 fn-2 for n 2, 3, 4,
  • Find the Fibonacci numbers f2, f3, f4, f5, f6
  • f2 f1 f0 10 1
  • f3 f2 f1 11 2
  • f4 f3 f2 21 3
  • f5 f4 f3 32 5
  • f6 f5 f4 53 8

9
6.2 Exercise
  • Prove that for every ngt0, f(n) lt (5/3)n
  • Define recursive definition
  • Find f(0), f(1), f(2), f(3), f(4)

10
6.3 Simple Recursive Procedure
  • Ex.1 ????????????????????? recursive
    ??????????????????????????????? an
  • ???????
  • ?? Basis step
  • ????? n 0, a0 1
  • ?? Recursive step
  • ????? n k, ak a ak-1

11
6.3 Simple Recursive Procedure
  • procedure power ( a, n)
  • if (n 0) then
  • return 1 / basis /
  • else
  • return a power(a, n-1) / recursive /
  • end procedure

12
6.3 Simple Recursive Procedure
  • Ex.2 a recursive function of factorial is defined
    as
  • We can write pseudo-code for this problem as
  • main procedure
  • read n
  • result factorial(n)
  • print Factorial of n is result
  • end main procedure

13
6.3 Simple Recursive Procedure
  • procedure fact (n)
  • if (n 0)
  • return 1 / basis /
  • else
  • return n fact(n-1) /
    induction /
  • end procedure

14
6.3 Execise
  • ???????? pseudocode ????????????? recursive
    ???????????????????????
  • f(0) 3
  • f(n1) 2f(n) 3
  • ???????? pseudocode ????????????? Fibonacci
    number
  • ???????? 1 ??? 2 ???????? n ???????????? 5
    ???????????????? pseudocode ????????
  • ???????? pseudocode ??????????????????????????????
    ? (Greatest Common Divisor GCD)
    ?????????????????? 2 ????? a ??? b ?????? a
    ???????? b
  • ?????????????????? ??? ?????????????????????????
    ????????????????????? ???? 16, 40
    ???????????????? 8

15
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com