Recursion (??) - PowerPoint PPT Presentation

Loading...

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



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Recursion (??)

Description:

Title: Recursion Author: Kelly Last modified by: MC SYSTEM Created Date: 2/11/2005 9:45:11 AM Document presentation format: Company – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 77
Provided by: Kell1207
Learn more at: http://sist.sysu.edu.cn
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Recursion (??)


1
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

2
An algorithm is recursive (???) if it calls
itself (directly or indirectly) to do part of its
work.
3
Factorials A Recursive Definition
  • In Mathematics

Base case(???)
General case that reduces to simpler case(???)
4
concise and elegant - can require keeping
track of many partial computations
5
The process of recursive call
6
  • 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.

7
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.
8
Divide and conquer (???)
We have to get to this state and then move the
bottom disk
9
1
2
3
A similar problem
10
2
1
3
Solved by recursion
11
The Solution
?? ????????,????(Brahma,??????????????64????????
?,????????,????????????????,?????,??????,?????????
,?????,?64??????????? 264-1?????,???????1????,????
184467440737095551615?????,?????,??????????
Our goal move(8, 1, 3, 2)
12
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 /
13
Recursive Function
The base case is count 0
14
Designing recursive algorithms
15
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.

16
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
17
Tree of function calls and the execution order
Recursive call
Execution order
18
Recursion tree for three disks
Number of nodes is the number of disk movements.
19
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.
20
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

21
Recursion and Stacks
  • Recursive calls are implemented by using stacks
  • Recursion can be replaced by stacks and iteration
    (??)

22
Function A calls B, B calls C
Save returning address and parameters
A
B
C
Start from the saved address
First started, last finished
23
  • Computation process of factorial (4)
  • Factorial(4) 4factorial(3)
  • 3factorial(2)
  • 2factorial(1)
  • 1factorial(0)
  • 11

24
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.
25
  • 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.

26
  • 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.

27
D calls itself
Activation record for the main program M
Stack frames(???) each column shows the stack
contents at a given time
28
  • 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.

29
int main() move(2,1,3,2) .
L0
30
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
31
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
32
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
33
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
34
Recursive call at L1
Recursive call at L3
L2, 0, 2, 1, 3
L4, 1, 2, 3, 1
L0, 2, 1, 3, 2
35
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
36
Recursive call at L3, push()
L4, 0, 1, 3, 2
L4, 1, 2, 3, 1
L0, 2, 1, 3, 2
37
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
38
Final state when move(2, 1,3,2) is done
39
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

40
Analyzing space and time complexity
What correspond to time complexity?
41
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
42
Complexity of factorial
  • ???????????????????? O(n)

????? T(0) 1 T(n) 1 T(n-1) O(n)
(recurrence relation ????)
43
Complexity of Hanoi
????? T(n) T(n-1) 1T(n-1) T(0) 0
T(n) O(2n)
  • ?????
  • ??????
  • ?????????
  • O(n)

44
Complexity and recursive trees
  • ???????????????????????????
  • ???????????????
  • ???????????????,????????????

45
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.

46
Recursive or Nonrecursive
Recursive version
  • Factorials

Nonrecursive version
47
The recursive version is more expensive in space
and time
48
(No Transcript)
49
F5 is repeated
Recursion tree for F7
F3 is repeated 5 times
50
Nonrecursive version
Using Iteration (??)
51
  • 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.

52
Hanoi with tail recursion
Last executed statement is a recursive call
53
Hanoi without tail recursion
54
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.

55
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

56
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.

57
Eight Queens Puzzle
58
Four Queens Solution
Take 1 of the 4 legitimate positions
guarded
backtrack
Dead end
backtrack
backtrack
59
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.

60
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.

61
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
62
The main program
63
The Queen Class
(count, col) denotes a possible position
64
The position is empty
65
Occupied
count is modified after insertion.
66
It is not necessary to check the lower part. (?)
67
The Backtracking Function
68
Guarded positions are not investigated
Effectively pruning a recursion tree
Dead end
Part of the recursion tree for eight queens
problem
69
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.

70
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
71
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.

72
Upward_free rowcol0--6
Downward_free board_size-1row-col0--6
73
New Queens
Number of diagonals
74
Modify the column and two diagonals that are
guarded now.
75
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

76
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.
About PowerShow.com