1 / 30

CSCE 310: Data Structures Algorithms

2. Giving credit where credit is due:Most of the lecture notes are based on the slides from the Textbook's companion websitehttp://www.aw-bc.com/info/levitinSeveral slides are from Hsu Wen Jing of the National University of SingaporeI have modified them and added new slides. CSCE 310: Data

lyneth
Télécharger la présentation

CSCE 310: Data Structures Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. 1 CSCE 310: Data Structures & Algorithms

    2. 2 Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook’s companion website http://www.aw-bc.com/info/levitin Several slides are from Hsu Wen Jing of the National University of Singapore I have modified them and added new slides CSCE 310: Data Structures & Algorithms

    3. 3 Example: a recursive algorithm Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)

    4. 4 Example: another recursive algorithm

    5. 5 Example: recursive evaluation of n ! Definition: n ! = 1*2*…*(n-1)*n Recursive definition of n!: Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) M(n): number of multiplications to compute n! Could we establish a recurrence relation for deriving M(n)? Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)

    6. 6 Example: recursive evaluation of n ! M(n) = M(n-1) + 1 for n > 0 A recurrence relation is an equation or inequality that describes a function in terms of its value on smaller inputs Explicit formula for the sequence (e.g. M(n)) in terms of n only

    7. 7 Example: recursive evaluation of n ! Definition: n ! = 1*2*…*(n-1)*n Recursive definition of n!: Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) M(n) = M(n-1) + 1 Initial Condition: M(0) = ? Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)

    8. 8 Example: recursive evaluation of n ! Recursive definition of n!: Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) M(n) = M(n-1) + 1 Initial condition: M(0) = 0 Explicit formula for M(n)? Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 ------------ M(n) =M(n-1) + 1 M(0) = 0 (# recursive calls is the same)

    9. 9 Time efficiency of recursive algorithms Steps in analysis of recursive algorithms: Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best case for inputs of size n Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution (see Appendix B)

    10. 10 EXAMPLE: tower of hanoi Problem: Given three pegs (A, B, C) and n disks of different sizes Initially, all the disks are on peg A in order of size, the largest on the bottom and the smallest on top The goal is to move all the disks to peg C using peg B as an auxiliary Only 1 disk can be moved at a time, and a larger disk cannot be placed on top of a smaller one

    11. 11 EXAMPLE: tower of hanoi Step 1: Solve simple case when n<=1? Just trivial

    12. 12 EXAMPLE: tower of hanoi Step 2: Assume that a smaller instance can be solved, i.e. can move n-1 disks. Then?

    13. 13 EXAMPLE: tower of hanoi

    14. 14 EXAMPLE: tower of hanoi

    15. 15 EXAMPLE: tower of hanoi

    16. 16 EXAMPLE: tower of hanoi

    17. 17 EXAMPLE: tower of hanoi

    18. 18 EXAMPLE: tower of hanoi

    19. 19 EXAMPLE: tower of hanoi

    20. 20 In-Class Exercise P. 76 Problem 2.4.1 (c): solve this recurrence relation: x(n) = x(n-1) + n P. 76 Problem 2.4.4: consider the following recursive algorithm: Algorithm Q(n) // Input: A positive integer n If n = 1 return 1 else return Q(n-1) + 2 * n – 1 A. Set up a recurrence relation for this function’s values and solve it to determine what this algorithm computes B. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. C. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it.

    21. 21 Example: BinRec(n)

    22. 22 Smoothness rule If T(n) ? ?(f(n)) for values of n that are powers of b, where b ? 2, then T(n) ? ?(f(n))

    23. 23 Example: BinRec(n)

    24. 24 Fibonacci numbers The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … Fibonacci recurrence: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1

    25. 25 Solving linear homogeneous recurrence relations with constant coefficients Easy first: 1st order LHRRCCs: C(n) = a C(n -1) C(0) = t … Solution: C(n) = t an Extrapolate to 2nd order L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = ? Characteristic equation (quadratic) Solve to obtain roots r1 and r2 (quadratic formula) General solution to RR: linear combination of r1n and r2n Particular solution: use initial conditions

    26. 26 Solving linear homogeneous recurrence relations with constant coefficients Easy first: 1st order LHRRCCs: C(n) = a C(n -1) C(0) = t … Solution: C(n) = t an Extrapolate to 2nd order L(n) = a L(n-1) + b L(n-2) … A solution?: L(n) = ? Characteristic equation (quadratic) Solve to obtain roots r1 and r2 (quadratic formula) General solution to RR: linear combination of r1n and r2n Particular solution: use initial conditions Explicit Formula for Fibonacci Number: F(n) = F(n-1) +F(n-2)

    27. 27 1. Definition based recursive algorithm Computing Fibonacci numbers

    28. 28 2. Nonrecursive brute-force algorithm Computing Fibonacci numbers

    29. 29 Computing Fibonacci numbers

    30. 30 In-Class Exercises Another example: A(n) = 3A(n-1) – 2A(n-2) A(0) = 1 A(1) = 3 P.83 2.5.4. Climbing stairs: Find the number of different ways to climb an n-stair stair-case if each step is either one or two stairs. (For example, a 3-stair staircase can be climbed three ways: 1-1-1, 1-2, and 2-1.)

    31. Announcement Reading List of this Week Chapter 3

More Related