1. Trang chủ
  2. » Cao đẳng - Đại học

Slide Cấu trúc dữ liệu và giả thuật - Lecture05 - Recursion - Phạm Bảo Sơn - UET - Tài liệu VNU

27 18 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 27
Dung lượng 537,52 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

This recursive step may involve a test that decides which of several possible recursive calls to make, but it should ultimately choose to make just one of these calls each time we perfor[r]

Trang 1

Data Structures and

Algorithms


"

Recursion!

Trang 2

The Recursion Pattern"

f n

n n

f

) 1 (

0 if

1 )

(

Trang 3

The Recursion Pattern"

// recursive factorial function

public static int recursiveFactorial(int n) {

Trang 4

Content of a Recursive

Method"

–   Values of the input variables for which we perform

no recursive calls are called base cases (there should be at least one base case) !

–   Every possible chain of recursive calls must

eventually reach a base case.!

–   Calls to the current method !

–   Each recursive call should be defined so that it

makes progress towards a base case.!

Trang 5

showing return value!

Example recursion trace:

return 4 * 6 = 24 final answer

call

Phạm Bảo Sơn - DSA

Trang 6

Example – English Rulers"

numbers like an English ruler:!

Trang 7

A Recursive Method for

Drawing Ticks on an English

Ruler"

// draw a tick with no label

public static void drawOneTick(int tickLength) { drawOneTick(tickLength, - 1); }

// draw one tick

public static void drawOneTick(int tickLength, int tickLabel) {

}

public static void drawTicks(int tickLength) { // draw ticks of given length

if (tickLength > 0) { // stop when length drops to 0

Trang 8

Visualizing the DrawTicks

–   a single tick of length L,!

–   an interval with a central

drawOneTick ( 2 )

drawTicks ( 2 )

drawTicks ( 1 ) drawTicks ( 0 )

drawTicks ( 0 )

drawTicks ( 0 )

drawOneTick ( 1 ) drawOneTick ( 3 )

Trang 9

Recall the Recursion Pattern"

•   Recursion: when a method calls itself!

•   Classic example the factorial function:!

–   n! = 1· 2· 3· ··· · (n-1)· n!

•   Recursive definition:!

•   As a Java method:!

// recursive factorial function

public static int recursiveFactorial(int n) {

f n

n n

f

) 1 (

0 if

1 )

(

Trang 10

Linear Recursion"

•   Test for base cases !

–   Begin by testing for a set of base cases (there should be at least one) !

–   Every possible chain of recursive calls must eventually reach

a base case, and the handling of each base case should not use recursion.!

•   Recur once !

–   Perform a single recursive call (This recursive step may

involve a test that decides which of several possible recursive calls to make, but it should ultimately choose to make just one of these calls each time we perform this step.)!

–   Define each possible recursive call so that it makes progress towards a base case.!

Trang 11

Phạm Bảo Sơn - DSA

A Simple Example of Linear

Recursion"

Algorithm LinearSum(A, n):!

Input: !

An integer array A and an

integer n >= 1, such that A has

Trang 12

Reversing an Array"

if i < j then"

return!

Trang 13

Phạm Bảo Sơn - DSA

Defining Arguments for

Recursion"

to define the methods in ways that facilitate recursion.!

paramaters that are passed to the method.!

Trang 14

Computing Powers"

recursively:!

O(n) time (for we make n recursive calls).!

1 ,

(

0 if

1 )

,

(

n x p x

n n

x p

Trang 15

Phạm Bảo Sơn - DSA

Recursive Squaring"

recursive algorithm by using repeated squaring:!

0 if

odd is

0 if

0 if

) 2 / , (

) 2 / ) 1 (

, (

1 )

x

n x p

n x p x n

x p

Trang 16

A Recursive Squaring

Method"

Algorithm Power(x, n):!

Input: A number x and integer n = 0!

Output: The value x n!

Trang 17

Phạm Bảo Sơn - DSA

Analyzing the Recursive

Each time we make a recursive call we halve the value of n; hence, we make log n recursive calls That

is, this method runs in O(log n) time

Trang 18

Tail Recursion"

•   Tail recursion occurs when a linearly recursive method makes its recursive call as its last step.!

•   The array reversal method is an example.!

•   Such methods can be easily converted to non-recursive

methods (which saves on some resources).!

•   Example:!

Algorithm IterativeReverseArray(A, i, j ):!

Trang 19

Phạm Bảo Sơn - DSA

Binary Recursion"

two recursive calls for each non-base case.!

ticks on an English ruler.!

Trang 20

A Binary Recursive Method for

Drawing Ticks"

// draw a tick with no label

public static void drawOneTick(int tickLength) { drawOneTick(tickLength, - 1); }

// draw one tick

public static void drawOneTick(int tickLength, int tickLabel) {

}

public static void drawTicks(int tickLength) { // draw ticks of given length

if (tickLength > 0) { // stop when length drops to 0

Trang 21

Phạm Bảo Sơn - DSA

Another Binary Recusive

Method"

•   Problem: add all the numbers in an integer array A:!

Input: An array A and integers i and n!

Output: The sum of the n integers in A starting at index i!

Trang 22

Output: The kth Fibonacci number F

!

Trang 23

Phạm Bảo Sơn - DSA

Analyzing the Binary Recursion Fibonacci Algorithm"

•   Let nk denote number of recursive calls made by

Trang 24

A Better Fibonacci

Algorithm "

•   Use linear recursion instead:!

Algorithm LinearFibonacci(k):!

Input: A nonnegative integer k!

Output: Pair of Fibonacci numbers (F k , F k-1)!

Trang 25

Phạm Bảo Sơn - DSA

Multiple Recursion"

recursive calls (not just one or two).!

!

Trang 26

Algorithm for Multiple

Recursion"

if k = 0 then"

else !

for all e in U do"

! ! Remove e from U ! {e is now being used}!

! ! Add e to the end of S!

! ! Add e back to U ! {e is now unused}!

! ! Remove e from the end of S!

!

Trang 27

Phạm Bảo Sơn - DSA

Visualizing PuzzleSolve"

PuzzleSolve ( 3 , () ,{ a , b , c } ) Initial call

PuzzleSolve ( 2 , c ,{ a , b } ) PuzzleSolve ( 2 , b ,{ a , c } )

PuzzleSolve ( 2 , a ,{ b , c } )

PuzzleSolve ( 1 , ab ,{ c } )

PuzzleSolve ( 1 , ac ,{ b } ) PuzzleSolve ( 1 , cb ,{ a } )

PuzzleSolve ( 1 , ca ,{ b } ) PuzzleSolve ( 1 , bc ,{ a } )

PuzzleSolve ( 1 , ba ,{ c } ) abc

Ngày đăng: 26/01/2021, 22:18

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w