Loading...

PPT – Recursion (??) PowerPoint presentation | free to download - id: 74a1cc-YjE0N

The Adobe Flash plugin is needed to view this content

Recursion (??)

- Introduction to Recursion
- -- Principles of recursion
- -- Recursion trees
- 2. How recursion is implemented on the machine
- -- Stacks and recursion
- 3. Analyzing Recursion
- -- when to use recursion
- 4. Backtracking (???) a method to try all

possibilities using recursion

An algorithm is recursive (???) if it calls

itself (directly or indirectly) to do part of its

work.

Factorials A Recursive Definition

- In Mathematics

Base case(???)

General case that reduces to simpler case(???)

concise and elegant - can require keeping

track of many partial computations

The process of recursive call

- Every recursive process consists of two parts
- A smallest, base case that gives a solution
- A general method that reduces a particular case

to one or more of the smaller cases, reducing the

problem all the way to the base case eventually.

The tower of Hanoi

Task move the tower from 1 to 3 using 2 as

temporary storage. Rules 1. move only one disk

at one time 2. No larger disk can be

placed on a smaller disk.

Divide and conquer (???)

We have to get to this state and then move the

bottom disk

1

2

3

A similar problem

2

1

3

Solved by recursion

The Solution

?? ????????,????(Brahma,??????????????64????????

?,????????,????????????????,?????,??????,?????????

,?????,?64??????????? 264-1?????,???????1????,????

184467440737095551615?????,?????,??????????

Our goal move(8, 1, 3, 2)

Our goal move(8, 1, 3, 2)

Steps to solve the problem move(7, 1, 2, 3)

/move 7 disks from tower 1 to 2 / cout ltlt

move disk 8 from tower 1 to 3.ltltendl move(7,

2, 3, 1) / move 7 disks from tower 2 to 3 /

Recursive Function

The base case is count 0

Designing recursive algorithms

Tree of function calls(?????)

- Tree of function calls is a tool to analyzing the

process of function calls. - A node(??) denotes a function call with the

function name - A function may invokes other functions, which are

denoted as children - Different recursive calls appear as different

vertices with the same name - Recursion tree the recursive part of the tree of

function calls.

invoke the function

Meaning of the tree of function calls the

traversal of the tree is the trace of the

function calls

Return from the function

Tree of function calls and the execution order

Recursive call

Execution order

Recursion tree for three disks

Number of nodes is the number of disk movements.

The number of moves to solve Hanoi for 64 disks?

The number of inner nodes 1222 263 264

1 1.6 1019 It will take more than 500

billion (????)years to finish the task suppose

one can move one disk every second.

Note also that our program also produces the best

possible solution.

Recursion (??)

- Introduction to Recursion
- -- Solving problems using recursion
- -- Recursion trees
- 2. How recursion is implemented on the machine
- -- Stacks and recursion
- 3. Analyzing Recursion
- -- when to use recursion
- 4. Backtracking (???) a method to try all

possibilities using recursion

Recursion and Stacks

- Recursive calls are implemented by using stacks
- Recursion can be replaced by stacks and iteration

(??)

Function A calls B, B calls C

Save returning address and parameters

A

B

C

Start from the saved address

First started, last finished

- Computation process of factorial (4)
- Factorial(4) 4factorial(3)
- 3factorial(2)
- 2factorial(1)
- 1factorial(0)
- 11

Stack Frames for Subprograms

- When a program calls a subprogram, the system
- Store returning location and parameters
- Allocate storage for all the local variables.
- Then the control is passed to the subprogram.

The information about the returning address and

parameters is stored in the activation

record(????) for the function call.

- When the subprogram is finished,
- Return the result of the subprogram
- Release the storage allocated for the subprogram

- the control is passed to the caller and execution

starts at the returning address.

- Temporary storage areas for use by functions

would be allocated in a list with the property

last in, first out. - So, stack is the data structure to manage

function call mechanism. - When a function call is made
- the activation record is pushed into the stack,

and - when the function is finished, the activation

record for the function is popped off.

D calls itself

Activation record for the main program M

Stack frames(???) each column shows the stack

contents at a given time

- A function may call itself. There is no

difference between this recursive call and other

function calls. - For each recursive call, an activation record for

the instance of the call is pushed into the

stack.

int main() move(2,1,3,2) .

L0

Stack frames for move(2, 1, 3, 2)

Activation record (address, n, start, finish,

tempt)

L2, 0, 1, 3, 2

L2, 1, 1, 2, 3

L0, 2, 1, 3, 2

Return, Pop(), goto L2

L2, 0, 1, 2, 3

L2, 1, 1, 2, 3

L0, 2, 1, 3, 2

Starting from L2, move disk 1 from 1 to 2

Recursive call at L3, so push()

L4, 0, 3, 2, 1

L2, 1, 1, 2, 3

L0, 2, 1, 3, 2

Return, pop(), then goto L4

Starting at L4, so pop() again, and goto L2

L2, 1, 1, 2, 3

L0, 2, 1, 3, 2

Do L2 move disk 2 from 1 to 3

Recursive call at L1

Recursive call at L3

L2, 0, 2, 1, 3

L4, 1, 2, 3, 1

L0, 2, 1, 3, 2

Pop(), goto L2

L2, 0, 2, 1, 3

L4, 1, 2, 3, 1

L0, 2, 1, 3, 2

Moving disk 1 from 2 to 3

L4, 1, 2, 3, 1

L0, 2, 1, 3, 2

Recursive call at L3, push()

L4, 0, 1, 3, 2

L4, 1, 2, 3, 1

L0, 2, 1, 3, 2

L4, 0, 1, 3, 2

L4, 1, 2, 3, 1

L0, 2, 1, 3, 2

Pop(), goto L4, so pop() again, goto L4, pop()

again

Final state when move(2, 1,3,2) is done

Recursion (??)

- Introduction to Recursion
- -- Solving problems using recursion
- -- Recursion trees
- 2. How recursion is implemented on the machine
- -- Stacks and recursion
- 3. Analyzing Recursion
- -- when to use recursion
- 4. Backtracking (???) a method to try all

possibilities using recursion

Analyzing space and time complexity

What correspond to time complexity?

3 time units As and Ds time units

3

The time complexity is proportional to the number

of nodes.

3

2

2

1

time complexity count the total time units in

the function call tree.

1

Complexity of factorial

- ???????????????????? O(n)

????? T(0) 1 T(n) 1 T(n-1) O(n)

(recurrence relation ????)

Complexity of Hanoi

????? T(n) T(n-1) 1T(n-1) T(0) 0

T(n) O(2n)

- ?????
- ??????
- ?????????
- O(n)

Complexity and recursive trees

- ???????????????????????????
- ???????????????
- ???????????????,????????????

What we learn about recursion

- A recursive algorithm must have a base case,

which gives a direct solution, and a method

which reduces general cases to simpler cases. - Recursion is done by using stacks
- When a call is made, the activation record is

pushed into the stack and control is passed to

the called function - When the call is finished, the activation record

is popped off the stack and execution starts from

the address in the popped record. - The recursive tree of a recursive algorithm can

be used to analyze the algorithm - The space complexity is proportional to the

height of the recursive tree - The time complexity is proportional to the

number of nodes.

Recursive or Nonrecursive

Recursive version

- Factorials

Nonrecursive version

The recursive version is more expensive in space

and time

(No Transcript)

F5 is repeated

Recursion tree for F7

F3 is repeated 5 times

Nonrecursive version

Using Iteration (??)

- Space complexity for recursive version is linear

in n. - Time complexity for the recursive version is

exponential with n (gt 2n/2). - Time complexity for the nonrecursive version is

linear in n. - The space complexity for the nonrecursive

version it uses only 4 variables.

Hanoi with tail recursion

Last executed statement is a recursive call

Hanoi without tail recursion

When to use recursion

- If time and space is not an issue, then recursive

version is much easier to understand and

preferred. - If duplicate tasks are involved, then data

structure other than stacks will be appropriate. - Recursion can always be replaced by iteration and

stacks. But no point to do it if not necessary.

Recursion (??)

- Introduction to Recursion
- -- Solving problems using recursion
- -- Recursion trees
- 2. How recursion is implemented on the machine
- -- Stacks and recursion
- 3. Analyzing Recursion
- -- when to use recursion
- 4. Backtracking (???) a method to try all

possibilities using recursion

Backtracking(??)

- A method to try all possibilities using

recursion. - When there are several possibilities,
- take one and go on
- go back to the most recent choice, and try

another possibility when a dead end is reached.

Eight Queens Puzzle

Four Queens Solution

Take 1 of the 4 legitimate positions

guarded

backtrack

Dead end

backtrack

backtrack

Backtracking

- Backtracking is a recursive method that goes back

to the most recent choice and tries another

possibility. - In (b), we go back to row 2 and put the queen in

column 4 instead. - In (c) , there is no other possibilities in row

two. We go back to row 1 and put the queen in

column 2.

Outline of the solution

- Imagining using a class called Queens to

represent a partial configuration(??, ??) of

queens - Add a queen in the configuration and continue the

search from the new configuration - If this does not lead to a solution, then remove

the newly added queens and try another position

and continue the search until a solution is

reached.

Exit a solution is found

When it is returned here, the queen added at p

is removed and try next possible position.

Exit dead end

The main program

The Queen Class

(count, col) denotes a possible position

The position is empty

Occupied

count is modified after insertion.

It is not necessary to check the lower part. (?)

The Backtracking Function

Guarded positions are not investigated

Effectively pruning a recursion tree

Dead end

Part of the recursion tree for eight queens

problem

Analysis of Backtracking

- Effectiveness of backtracking positions that are

discovered impossible prevent the later

investigation of fruitless paths. - Number of configurations that there is only one

queen in each row 88 - Number of configurations that there is only one

queen in each column 8! - Backtracking cut this number further it does not

investigate impossible paths. See the recursion

tree.

Lower Bounds

- However, the amount of work done by backtracking

still grows rapidly with n. - To place a queen on the first n/4 rows, the

minimum positions to investigate - n(n-3)(n-6)(n-3n/4) gt (n/4)n/4 gt 2n

The queen guards at most three positions in the

next row

Task Improvement

- The loops in function unguarded takes

considerable time when checking if a position is

guarded in three directions. - An improvement is to keep track which columns are

guarded using an one dimension bool array, which

diagonals are guarded using two bool arrays.

This makes the function unguarded much simpler.

Upward_free rowcol0--6

Downward_free board_size-1row-col0--6

New Queens

Number of diagonals

Modify the column and two diagonals that are

guarded now.

bool Queensunguarded(int col)const /Post

Return true if the position (count, col) is

unguarded, false else. / return

col_free(col) upward_freecountcol

downward_freecount-colboard_size-1

Summary

- Recursion concept and recursion trees
- Principles of designing recursive algorithms
- Divide and conquer, a method to solve problems

using recursion - How recursion is implemented in machine
- Complexity analysis using recursion trees.