15-121 data structuresab/15-121n11/lectures/lecture10.pdf · where printstars(n) prints n stars....
TRANSCRIPT
![Page 1: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/1.jpg)
15-121
Data Structures
7/12/2011 1
Ananda Gunawardena
![Page 2: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/2.jpg)
Problem Solving Techniques
7/12/2011 2
![Page 3: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/3.jpg)
Divide and Conquer
7/12/2011 3
![Page 4: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/4.jpg)
Exhaustive Search
7/12/2011 4
![Page 5: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/5.jpg)
Greedy Algorithms
7/12/2011 5
![Page 6: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/6.jpg)
Dynamic Programming
7/12/2011 6
applicable to problems that exhibit the properties of overlapping sub
problems and optimal substructure
![Page 7: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/7.jpg)
Recursion
7/12/2011 7
![Page 8: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/8.jpg)
The promise of recursion� Suppose you can express the solution to a bigger
problem using solution to a sub problem
� F(n) = n * F(n-1)
� The express the solution to what is called base case
� F(0) = F(1) = 1
� Question: What is the closed form of this function?
7/12/2011 8
![Page 9: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/9.jpg)
Change problem� Problem: Given n cents in change, find the least
number of coins to provide the change
� Solution:
� Iterative: Keep subtracting highest coin until the balance is zero
� Recursive: Assume you know how to express the solution using solution to a sub problem
� C(n) = 1 + C(n-25) if (n > 25)
7/12/2011 9
![Page 10: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/10.jpg)
7/12/2011 10
![Page 11: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/11.jpg)
What problems can be solved using
Recursion
� Problems that lend themselves to recursive solution have the following characteristics
� one or more simple cases of the problem (stopping cases) have a straightforward, non-recursive solution
� For the other cases, there is a process (using recursion) for substituting one or more reduced cases of the problem closer to a stopping case
� Eventually the problem can be reduced to stopping cases only
7/12/2011 11
![Page 12: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/12.jpg)
General Form of a recursive Function
� The recursive functions we work with will generally consist of an ifstatement with the form shown below
if the stopping case or base case is reached
solve the problem
else
reduce the problem using
recursion
7/12/2011 12
![Page 13: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/13.jpg)
Recursion� Examples
GCD(a,b) = a if a=b
= 0 if a=0 or b=0
= GCD(a%b, b) if a > b
= GCD(a, b%a) if b > a
7/12/2011 13
![Page 14: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/14.jpg)
Tower of Hanoi Problem
7/12/2011 14
![Page 15: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/15.jpg)
Rules of the game� Move one disk at a time
� Cannot place a larger disk on the top of a smaller disk
� Find
� Moves you need to solve Hanoi(n) problem
7/12/2011 15
![Page 16: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/16.jpg)
Thinking about the game� Consider small cases
� N = 1 trivial
� N = 2
� N = 3
7/12/2011 16
![Page 17: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/17.jpg)
Generalizing Hanoi
7/12/2011 17
Suppose you need to move n disks from a
origin to Destination using intermediate
•You break the problem into parts
• move first (n-1) disks from origin to
intermediate using destination
• move the n-th disk from origin to
destination
• move the (n-1) disks from intermediate to
destination using origin
![Page 18: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/18.jpg)
Tracing Hanoi
7/12/2011 18
![Page 19: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/19.jpg)
Tracing Recursionpublic void foo(n) {
if (n == 0) return 0;
else return n+foo(n-1);
}
7/12/2011 19
![Page 20: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/20.jpg)
Recursive Solutions� Implementing a recursive solution is generally less
efficient in terms of system overhead, due to the overhead of extra function calls;
� however recursive functions
� allow us to think of solutions to problems that may be recursive in nature
� allow us to work with data structures that are best accessed recursively� Eg: binary search trees
7/12/2011 20
![Page 21: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/21.jpg)
Famous Recursive Solutions
7/12/2011 21
![Page 22: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/22.jpg)
Types of Recursion� Head recursion
public void foo(n){
if (n>0) foo(n-1);
System.out.println(n);
}
� Tail Recursion
public void foo(n){
if (n>0) System.out.println(n);
else foo(n-1);
}
7/12/2011 22
![Page 23: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/23.jpg)
What is the output?public void printPattern(int n){
if (n > 0) {
printPattern(n-1);
printStars(n);
printPattern(n-1);
}
Where printStars(n) prints n stars.
Eg: printStarts(3) � ***
7/12/2011 23
![Page 24: 15-121 Data Structuresab/15-121N11/lectures/lecture10.pdf · Where printStars(n) prints n stars. Eg: printStarts(3) *** 7/12/2011 23. Next List and Recursion Binary Search using Recursion](https://reader036.vdocuments.net/reader036/viewer/2022081620/611be3e4bb870d5d513e4ced/html5/thumbnails/24.jpg)
Next� List and Recursion
� Binary Search using Recursion
� Maze Solver
� Thinking recursively
7/12/2011 24