Recursion - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Recursion

Description:

Chapter 5 Recursion * – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 124
Provided by: CRL98
Category:
Tags: method | recursion | tree

less

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

Title: Recursion


1
Chapter 5
  • Recursion

2
Chap.5 Contents
  • 5.1 Introduction
  • 5.2 Factorial
  • 5.2.1 Execution Frames
  • 5.3 Decimal to Binary
  • 5.4 Towers of Hanoi
  • 5.4.1 Analysis of the move Method
  • 5.5 Searching An Array
  • 5.6 Backtracking
  • 5.6.1 An A-maze-ing Application
  • 5.8 The Cost of Recursion

3
  • Recursive v.s. Iterative
  • OCCUR ??
  • RECUR ????
  • Recursion Recursive
  • ITERATE ????
  • Iteration Iterative

4
5.1 Introduction
5
(No Transcript)
6
(No Transcript)
7
5.2 Factorial
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
5.2.1 Execution Frames
13
(No Transcript)
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
5.3 Decimal to Binary
32
(No Transcript)
33
(No Transcript)
34
(No Transcript)
35
(No Transcript)
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
43
5.4 Towers of Hanoi
Example 3 Towers of Hanoi ???
44
(No Transcript)
45
(No Transcript)
46
Design sketch for case n4
  • 1.First, see if the simplest case n1 works.
  • ??, ???????? n1.
  • 2.Next, by mathematical induction, assuming n3
    works,
  • that is, 3 disks can be magically moved to the
    destination,
  • ???, ????????n3
  • ???3?disks??????
  • 3.Then, draw design sketches to solve n4, 4
    disks problem.
  • ??, ?????????n4, 4?disks???

47
The simplest case n1
  • Simply move the disk from A to B. It is done.
  • ?????disk?A?B.
  • Thus, case n1 works.
  • ?? case n 1????.
  • Next, try to solve n4 case
  • by assuming n3 works.
  • ???, ??n3??????n4?case

48
  • ???? n4 ?????
  • ?????
  • ????? (???) ???
  • ???????????
  • ??????????? ??????

49
(No Transcript)
50
(No Transcript)
51
  • 2) ? disk n (disk 4) ?A??B

52
  • 3) ? n-1 (that is 3) disks ? C ??? B

53
(No Transcript)
54
(No Transcript)
55
(No Transcript)
56
/ move ??disks????pole?????pole??????.
The worstTime(n) is O(2n). _at_param n
n?disk?????. _at_param orig disk??????pole.
_at_param dest ???pole. _at_param temp
???????pole. _at_return
?????????????. _at_throws IllegalArgumentException
if n ?????0. / public static String move
(int n, char orig, char dest, char temp)
final String DIRECT_MOVE "Move disk " n
" from " orig " to " dest "\n"
if (n lt 0) throw new IllegalArgumentException(
) if (n 1) return DIRECT_MOVE
return move (n - 1, orig, temp, dest)
DIRECT_MOVE
move (n - 1, temp,
dest, orig) // end of move
57
5.4.1 Analysis of the move Method

58
(No Transcript)
59
(No Transcript)
60
The total number of calls to move
is ????????calls???
n -1 1 2 4 2n-1
S 2i i0
61
n-1 S 2i 2n -
1 i0 See example 6 of Appendix 2
for a proof by mathematical induction.
62
This shows that worstTime(n) is O(2n)
63
To trace the execution of this recursive move
function http//www.cs.lafayette.edu/collinsw/
hanoi2/hanoiins.html
64
(No Transcript)
65
5.5 Searching An Array
66
(No Transcript)
67
(No Transcript)
68
(No Transcript)
69
(No Transcript)
70
(No Transcript)
71
(No Transcript)
72
(No Transcript)
73
?? ?? ? ??method descriptions
  • ????????? method description,
  • ????????????.
  • ???????.
  • ?????
  • ???????

74
(No Transcript)
75
(No Transcript)
76
???? interface
  • / ? binary search ??? array ???object.
  • array ????????,???? undefined.
  • if array ?????object, ???????
  • The worstTime(n) is O(log n).
  • _at_param a ??? array
  • _at_param first array?????????index
  • _at_param last - array?????????index
  • _at_param key - ???object.

77
  • _at_return ??? object ? index,
  • if ? object ?? array ? ????? index
  • else ?? - insertion point -1
  • insertion point ???
    object ????? array ?? index
  • _at_throws ClassCastException
  • if array ?????? compare ? elements (?
    strings ? integers)
  • or
  • ??? object ??? array ?? elements ??
    compare /
  • public static int binarySearch (Object a,

  • int first,

  • int last,

  • Object key)

78
(No Transcript)
79
(No Transcript)
80
(No Transcript)
81
(No Transcript)
82
(No Transcript)
83
(No Transcript)
84
(No Transcript)
85
(No Transcript)
86
(No Transcript)
87
(No Transcript)
88
(No Transcript)
89
(No Transcript)
90
(No Transcript)
91
(No Transcript)
92
(No Transcript)
93
(No Transcript)
94
5.6 Backtracking
Backtracking is the strategy of trying to reach
a goal by a sequence of chosen positions, with
re-tracing in reverse order of
positions that cannot lead to the
goal. Backtracking ???? ?????? positions
????????,???????????positions???????.
95
Strategy try to go west if unable to go west,
try to go south if unable to go south, backtrack
(until you can go south). ????, ???????, ?????
??????, ????? (???????) Do not go to any
position that has been shown not to lead to a
goal. The goal is either G1 or G2. Start at P1.
???????????????????. ????G1??G2.
???P1. P3 P2 P1
P4 P5 G2 P7
P6 G1
           
           
96
When a position is visited, it is marked as
(potentially) being on a path to the
goal. ?????????, ???????(???)???????. If we
discover otherwise, the marking must be undone,
so that position will never again be
visited. ????????, ???????, ??????????. For
example, P4 is not visited from P5. ??, P4
????P5??.  
97
We will solve this ?????????? maze-search
problem within the general framework of
backtracking, ?????backtracking ? framework
, which can easily be utilized in other
applications. ???????????applications??.
98
  The Application interface and the Backtrack
class are generic ??Application
interface?Backtrack class??? They are the same
for any backtracking project. ????backtracki
ng project, ??????.
99
import java.util.   public interface
Application   // ??pos????? ???true.
boolean isOK (Position pos)    //
??pos????????????. void markAsPossible
(Position pos)   // ??pos??? ??true.
boolean isGoal (Position pos)
100
//??pos???????????. void
markAsDeadEnd (Position pos)    
//????????. String toString()     //??
iterator ? pos ?????? positions.
IteratorltPositiongt iterator (Position pos)  
// interface Application
101
(No Transcript)
102
(No Transcript)
103
(No Transcript)
104
(No Transcript)
105
(No Transcript)
106
(No Transcript)
107
(No Transcript)
108
(No Transcript)
109
5.6.1 An A-maze-ing Application
110
Maze searching 1 Corridor
0 Wall start 1 1 1 0 1 1 0
0 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0
1 0 1 0 1 0 1 1 0 0 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1
0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 finish Iterator
choices north, east, south, west Marked as
possible 9 dead end 2
111
Solution 9 Path 2 dead end 9 9 9 0 2 2 0 0
0 2 2 2 2 1 0 9 9 9 0 2 2 2 2 2 0 2 1 0 0 0 9 0 2
0 2 0 2 0 2 1 0 0 0 9 2 2 0 2 0 2 2 2 1 1 1 1 9 0
0 0 0 1 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 9
9 9 9 9 9 9 9 9
112
All that need to be developed are ????????
the Position class, the class that
implements the Application interface and a
tester. ??class??Application interface?
tester. For this application, a position is
simply a row and column, so ?????application, Pos
ition??row?column, ??
113
(No Transcript)
114
(No Transcript)
115
(No Transcript)
116
(No Transcript)
117
(No Transcript)
118
(No Transcript)
119
(No Transcript)
120
(No Transcript)
121
(No Transcript)
122
Exercise Recall the solution when the order was
north, east, south, west 9 9 9 0 2 2 0 0 0
2 2 2 2 1 0 9 9 9 0 2 2 2 2 2 0 2 1 0 0 0 9 0 2 0
2 0 2 0 2 1 0 0 0 9 2 2 0 2 0 2 2 2 1 1 1 1 9 0 0
0 0 1 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 9 9
9 9 9 9 9 9 9
123
Re-solve with the order north, east, west,
south start 1 1 1 0 1 1 0 0 0 1 1 1 1 1 0 1 1
1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 1 0 1 0 1 0 1 1 0 0
0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
finish Hint Only one 1 remains.
About PowerShow.com