15-121 data structuresab/15-121n11/lectures/lecture10.pdf · where printstars(n) prints n stars....

24
15-121 Data Structures 7/12/2011 1 Ananda Gunawardena

Upload: others

Post on 21-Mar-2021

1 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Next� List and Recursion

� Binary Search using Recursion

� Maze Solver

� Thinking recursively

7/12/2011 24