Recursion 4-11-2013 Clarkson Career Center Technical Careers: - - PowerPoint PPT Presentation

recursion
SMART_READER_LITE
LIVE PREVIEW

Recursion 4-11-2013 Clarkson Career Center Technical Careers: - - PowerPoint PPT Presentation

Recursion 4-11-2013 Clarkson Career Center Technical Careers: Preparation & Opportunities Alumni to Student Program: CS, DA&S, Comm & Media, ISBP, SoftEng Thursday, April 18 th , 4:00 pm to 7:00 pm (panel discussions,


slide-1
SLIDE 1

Recursion 4-11-2013

slide-2
SLIDE 2

Clarkson Career Center “Technical Careers: Preparation & Opportunities” Alumni to Student Program: CS, DA&S, Comm & Media, ISBP, SoftEng Thursday, April 18th , 4:00 pm to 7:00 pm

(panel discussions, roundtables, … details to follow)

slide-3
SLIDE 3

 Recursion Reading: Maciel

 Chapter 14

Recursion

 Chapter 15

Sorting Project#2: Evil Hangman, due Wed. 4/24 see the sample output

slide-4
SLIDE 4

 To err is human, to forgive divine.

 Alexander Pope, An Essay on Criticism, English

poet and satirist (1688 - 1744)

 To iterate is human, to recurse, divine.

 L. Peter Deutsch, computer scientist, or ....  Robert Heller, computer scientist, or ....  unknown ....

slide-5
SLIDE 5

 Recursion ursion is:

 A problem-solving ap

approa roach ch, that can ...

 generate simple solutions to ...  certain kinds of problems that ...  would be difficult to solve in other ways

 Recursion splits a problem:

 into one or more simpler versions of itself

lf

slide-6
SLIDE 6

Strateg rategy y for proc

  • cess

essing ing nested sted dolls lls: 1.if there is only one doll

  • 2. do what it needed for it

else

  • 3. do what is needed for the outer doll
  • 4. Process the inner nest in the same way
slide-7
SLIDE 7

if problem is “small enough” solve it directly else break into one or more smaller subproblems solve each subproblem recursively combine results into solution to whole problem

slide-8
SLIDE 8

 At least one “small” case that you can solve directly  A way of breaking a larger problem down into:

One or more smaller subproblems

Each of the same kind as the original

 A way of combining subproblem results into an

  • verall solution to the larger problem
slide-9
SLIDE 9
  • 1. The algorithm has at least one base case; one

where the problem is solved directly, without a recursive call.

  • 2. Every recursive call gets closer to a base case,

in such a way that a base case will eventually be reached.

  • 3. The algorithm works when you assume that

the recursive call works.

The first two properties guarantee that the algorithm will eventually terminate. The third property guarantees that the algorithm solves the problem correctly.

slide-10
SLIDE 10

// / print n n copies es of the he charac acter c void print( int n , char c ) { for (int i = 0; i < n; i++ ) { cout << c; } cout << endl; } Figure 14.1: A simple iterative algorithm (Maciel, p. 245)

slide-11
SLIDE 11

// / print n n copies es of the he charac acter c void print( int n , char c ) {  base case: no characters to print

 cout << endl;

 recursive case:

 print character c  print (n-1) copies of c

cout << c; print ( n-1, c ) n is zero n > 0

slide-12
SLIDE 12

// / print n n copies es of the he charac acter c void print( int n , char c ) { if ( n > 0 ) { // rec ecursive e case cout << c; print( n-1, c ); } else { // b base cas ase cout << endl; Figure 14.2: A recursive algorithm (Maciel, p. 245)  recursive case:

 print character c  print (n-1) copies of c

slide-13
SLIDE 13

// pri // print n nt n copie copies of t s of the ch he charact aracter c er c void print( int n , char c ) { if ( n > 0 ) { cout << c; print( n-1, c ); } else { cout << endl;

  • 1. The algorithm has at least one base case
  • 2. Every recursive call gets closer to a base case, in

such a way that a base case will eventually be reached.

  • 3. The algorithm works when you assume that the

recursive call works.

√ √ √

slide-14
SLIDE 14

 Don’t be concerned about how recursion

  • works. Use the definition of what the function

is designed to do, ignoring implementation.  Typically an “if” is used to select between base cases and recursive cases.  Make sure that any recursive call is only made

  • n part of the original parameters. This

guarantees termination.  Using recursion in two places results in a program that would be very difficult to write iteratively.

slide-15
SLIDE 15

// print / print n c n cop

  • pies o

es of the c f the chara haract cter c r c void print( int n , char c )  print(0, ‘X’) correctly prints nothing  print(1, ‘X’) correctly prints 1 X, given that

 print (0, ‘X’) correctly nothing

 print(2, ‘X’) correctly prints 2 X’s, given that

 print (1, ‘X’) correctly prints 1 X

 . . .  print(n, ‘X’) correctly prints n X’s, given that

 print (n-1, ‘X’) correctly prints (n-1) X’s

Principle of Mathematical Induction Principle of Mathematical Induction

slide-16
SLIDE 16

 Notes done in class

slide-17
SLIDE 17

 main ain adva vantage tage: recursive algorithms can be simpler than non-recursive algorithms that solve the same problem => easier to design, understand, implement and modify

Some good examples are efficient sorting algorithms

 main ain disadvantage: sadvantage: overhead of function calls

(which take more time and more space) The additional time is usually not very significant, but the amount of space is proportional to the number of recursive calls. “Tail-recursive” solutions are very efficient.

slide-18
SLIDE 18

/* Comp mpute ute n!, !, n >= >= 0 * */  solution 1: Iterative int nt iFact iFact (int int n) n) { int int res esult = 1 1; for ( for (in int k = 1 1; k <= = n; k++) +) { resu sult = re result * * k; } return n result; t; }  solution 2: Recursive  solution 3: Tail-recursive No Notes es don

  • ne

e in class ss

0! = 1 n! = n*(n-1)!, n>0

slide-19
SLIDE 19
  • 1. if array is empty
  • 2. return -1 as result
  • 3. else if middle element matches
  • 4. return index of middle element as result
  • 5. else if target < middle element
  • 6. return result of searching lower portion of array
  • 7. else
  • 8. return result of searching upper portion of array
slide-20
SLIDE 20
slide-21
SLIDE 21

template <typename T> int bin_search(const std::vector<T>& items, int first, int last, const T& target) { if (first > last) return -1; // Base case, item not found else { // Next probe index. int mid = (first + last)/2; if (target < items[mid]) return bin_search(items, first, mid-1, target); else if (items[mid] < target) return bin_search(items, mid+1, last, target); else return middle; // Base case for // successful search. } }

slide-22
SLIDE 22

template <typename Item_Type> int binary_search(const std::vector<Item_Type>items, const Item_Type& target) { return binary_search(items, 0, items.size()-1, target); }

 C++ Standard library function binary_search defined in <algorithms> does this.

slide-23
SLIDE 23

 Towers of Hanoi  Counting grid squares in a blob  Backtracking, as in maze search

slide-24
SLIDE 24

 Desire: Process an image presented as a two- dimensional array of color values  Information in the image may come from

 X-Ray  MRI  Satellite imagery  Etc.

 Goal: Determine size of any area considered abnormal because of its color values

slide-25
SLIDE 25

 A blob is a collection of contiguous cells that are abnormal  By contiguous we mean cells that are adjacent, horizontally, vertically, or diagonally

slide-26
SLIDE 26

 user enters the position of a cell in a blob

  • e.g. <1,4>, where rows & columns start at 0

 algorithm returns the number of cells in that blob

  • what is the size of the blob which contains cell <1,4>?

 white => cell is OK  blue => cell is abnormal  blob == contiguous abnormal cells (horizontal, vertical and diagonal)

slide-27
SLIDE 27

Algorithm count_cells(x, y): if (x, y) outside grid return 0 else if color at (x, y) normal return 0 else Set color at (x, y) to “Temporary” (normal) return 1 + sum of count_cells on neighbors

slide-28
SLIDE 28

int countCells(color grid[ROWS][COLS], int r, int c) {

if (r < 0 || r >= ROWS || c < 0 || c >= COLS) { return 0; } else if (grid[r][c] != ABNORMAL) { return 0; } else { grid[r][c] = TEMPORARY; return 1 + countCells(grid,r-1,c-1) + countCells(grid,r-1,c)

+ countCells(grid,r-1,c+1) + countCells(grid,r,c+1) + countCells(grid,r+1,c+1) + countCells(grid,r+1,c) + countCells(grid,r+1,c-1) + countCells(grid,r,c-1);

} }

slide-29
SLIDE 29

 Backtracking: systematic trial and error search for solution to a problem

 Example: Finding a path through a maze

 In walking through a maze, probably walk a path as far as you can go

 Eventually, reach destination or dead end  If dead end, must retrace your steps  Loops: stop when reach place you’ve been before

 Backtracking systematically tries alternative paths and eliminates them if they don’t work

slide-30
SLIDE 30

 If you never try exact same path more than once, and You try all possibilities, You will eventually find a solution path if one exists  Problems solved by backtracking: a set of choices  Recursion implements backtracking straightforwardly

 Activation frame remembers choice made at that

decision point

 A chess playing program likely involves backtracking

slide-31
SLIDE 31
  • 1. if (x,y) outside grid, return false
  • 2. if (x,y) barrier or visited, return false
  • 3. if (x,y) is maze exit, color PATH and return true
  • 4. else:
  • 5. set (x,y) color to PATH (“optimistically”)
  • 6. for each neighbor of (x,y)
  • 7. if findPath(neighbor), return true
  • 8. set (x,y) color to TEMPORARY (“visited”)
  • 9. return false
slide-32
SLIDE 32

bool findMazePath(color grid[ROWS][COLS],int r,int c) { if (r < 0 || c < 0 || r >= ROWS || c >= COLS) return false; // Cell is out of bounds. else if (grid[r][c] != BACKGROUND) return false; // Cell is on barrier or dead end. else if (r == ROWS - 1 && c == COLS - 1) { grid[r][c] = PATH; // Cell is on path return true; // and is maze exit. } else . . . }

slide-33
SLIDE 33

{ // Recursive case. // Attempt to find a path from each neighbor. // Tentatively mark cell as on path. grid[r][c] = PATH; if (findMazePath(grid, r - 1, c) || findMazePath(grid, r + 1, c) || findMazePath(grid, r, c - 1) || findMazePath(grid, r, c + 1 ) ) { return true; } else { grid[r][c] = TEMPORARY; // Dead end. return false; } }

slide-34
SLIDE 34

 Recursion

 Maciel: Chapter 14

 Sorting

 Maciel: Chapter 15