1. Trang chủ
  2. » Giáo án - Bài giảng

Data Structure and Algorithms CO2003 Chapter 3 Recursion

47 398 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 47
Dung lượng 505,12 KB

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

Nội dung

Recursion and the basic components of recursive algorithms 2.. Recursion and the basic components of recursive algorithms... Factorial: Recursive Solution Algorithm recursiveFactorialn C

Trang 1

Data Structure and Algorithms [CO2003]

Chapter 3 - Recursion

Lecturer: Duc Dung Nguyen, PhD

Contact: nddung@hcmut.edu.vn

August 29, 2016

Faculty of Computer Science and Engineering

Hochiminh city University of Technology

Trang 2

1 Recursion and the basic components of recursive algorithms

2 Properties of recursion

3 Designing recursive algorithms

4 Recursion and backtracking

5 Recursion implementation in C/C++

Trang 3

• L.O.8.1 - Describe the basic components of recursive algorithms (functions)

• L.O.8.2 - Draw trees to illustrate callings and the value of parameters passed to them forrecursive algorithms

• L.O.8.3 - Give examples for recursive functions written in C/C++

• L.O.8.5 - Develop experiment (program) to compare the recursive and the iterative

approach

• L.O.8.6 - Give examples to relate recursion to backtracking technique

Trang 4

Recursion and the basic components of recursive algorithms

Trang 6

Basic components of recursive algorithms

Two main components of a Recursive Algorithm

1 Base case (i.e stopping case)

2 General case (i.e recursive case)

Trang 7

Figure 1 Factorial (3) Recursively (source: Data Structure - A pseudocode Approach with C++)

Trang 8

Factorial: Iterative Solution

Algorithm iterativeFactorial(n)

Calculates the factorial of a number using a loop

Pre:nis the number to be raised factorially

Post:n! is returned - result infactoN

Trang 9

Factorial: Recursive Solution

Algorithm recursiveFactorial(n)

Calculates the factorial of a number using a recursion

Pre:nis the number to be raised factorially

Trang 10

Recursion

Trang 11

Properties of recursion

Trang 12

Properties of all recursive algorithms

• A recursive algorithm solves the large problem by using its solution to a simpler

Trang 13

Designing recursive algorithms

Trang 14

The Design Methodology

Every recursive call must eithersolve a partof the problem orreduce the sizeof the problem

Rules for designing a recursive algorithm

1 Determine thebase case (stopping case)

2 Then determine the general case (recursive case)

3 Combinethe base case and the general cases into an algorithm

Trang 15

Limitations of Recursion

• A recursive algorithm generally runsmore slowlythan its nonrecursive implementation

• BUT, the recursive solutionshorterandmore understandable

Trang 16

Print List in Reverse

Trang 17

Print List in Reverse

Trang 18

Print List in Reverse

Algorithm printReverse(list)

Prints a linked list in reverse

Pre: list has been built

Post: list printed in reverse

if list is null then

return

end

printReverse (list -> next)

Trang 19

Greatest Common Divisor

Trang 20

Greatest Common Divisor

Algorithm gcd(a, b)

Calculates greatest common divisor using the Euclidean algorithm

Pre:aandbare integers

Post: greatest common divisor returned

Trang 22

+

Trang 23

3

Result

0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Trang 24

Fibonacci Numbers

Algorithm fib(n)

Calculates the nth Fibonacci number

Pre:nis postive integer

Post: thenthFibonnacci numberreturned

Trang 26

The Towers of Hanoi

Move disks from Source to Destination using Auxiliary:

1 Only one disk could be moved at a time

2 A larger disk must never be stacked above a smaller one

3 Only one auxiliary needle could be used for the intermediate storage of disks

21

Trang 27

The Towers of Hanoi

Trang 28

The Towers of Hanoi

Moved disc from pole1to pole2

Trang 29

The Towers of Hanoi

Source

3

Auxiliary21

Destination

Moved disc from pole3to pole2

Trang 30

The Towers of Hanoi

Source Auxiliary

21

Destination3

Moved disc from pole1to pole3

Trang 31

The Towers of Hanoi

Moved disc from pole2to pole1

Trang 32

The Towers of Hanoi

Source

1

Auxiliary Destination

32

Moved disc from pole2to pole3

Trang 33

The Towers of Hanoi

Source Auxiliary Destination

321

Moved disc from pole1to pole3

Trang 34

The Towers of Hanoi

Trang 35

The Towers of Hanoi : General

move(n, A, C, B)

move(n-1, A, B, C) move(1, A, C, B) move(n-1, B, C, A)

Complexity

T (n) = 1 + 2T (n − 1)

Trang 36

The Towers of Hanoi

Trang 37

The Towers of Hanoi

Algorithm move(val disks <integer>, val source <character>, val destination <character>,val auxiliary <character>)

Move disks from source to destination

Pre: disks is the number of disks to be moved

Post: steps for moves printed

print("Towers: ", disks, source, destination, auxiliary)

if disks = 1 then

print ("Move from", source, "to", destination)

else

move(disks - 1, source, auxiliary, destination)

move(1, source, destination, auxiliary)

move(disks - 1, auxiliary, destination, source)

end

return

Trang 38

Recursion and backtracking

Trang 39

Definition

A process to goback to previous stepstotry unexplored alternatives

Figure 3 Goal seeking

Trang 40

Eight Queens Problem

Place eight queens on the chess board in such a way that no queen can capture another

Trang 41

Eight Queens Problem

Algorithm putQueen(ref board <array>, val r <integer>)

Place remaining queens safely from a row of a chess board

Pre: board is nxn array representing a chess board

r is the row to place queens onwards

Post: all the remaining queens are safely placed on the board; or backtracking to the previousrows is required

Trang 42

Eight Queens Problem

for every column c on the same row r do

if cell r,c is safe then

place the next queen in cell r,c

Trang 43

Eight Queens Problem

Trang 44

Recursion implementation in C/C++

Trang 46

The Towers of Hanoi

Trang 47

The Towers of Hanoi

Ngày đăng: 29/03/2017, 18:21

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN