CS200: Recursion and induction Prichard Ch. 6.1 & 6.3 CS200 - - - PowerPoint PPT Presentation

cs200 recursion and induction
SMART_READER_LITE
LIVE PREVIEW

CS200: Recursion and induction Prichard Ch. 6.1 & 6.3 CS200 - - - PowerPoint PPT Presentation

CS200: Recursion and induction Prichard Ch. 6.1 & 6.3 CS200 - Recursion 1 CS200 - Recursion 2 Backtracking n Problem solving technique that involves moves: guesses at a solution. n Depth First Search: in case of failure retrace


slide-1
SLIDE 1

CS200: Recursion and induction

Prichard Ch. 6.1 & 6.3

1 CS200 - Recursion

slide-2
SLIDE 2

2 CS200 - Recursion

slide-3
SLIDE 3

3

Backtracking

n Problem solving technique that involves

moves: guesses at a solution.

n Depth First Search: in case of failure retrace

steps and try a new move in a state with still unexplored guesses

Think of it as walking through a tree shaped state space.

CS200 - Recursion

3 guesses here 2 guesses in each state here leaf states can fail (F) or succeed (S)

slide-4
SLIDE 4

CS200 - Recursion 4

F F F S F F F F F S F F F F F S F F F F F S F F F F F S F F F F F S F F F F F S F F Found!

slide-5
SLIDE 5

Depth First Search

n Looking for a path out of

the maze

n Strategy:

q Prioritize directions: right,

straight or left.

q At a dead end “backtrack”

and try a different direction

n Recursive solution?

5 CS200 - Recursion

^

slide-6
SLIDE 6

6

The Eight Queens Problem

Place 8 Queens! No queen can attack any other queens.

CS200 - Recursion

slide-7
SLIDE 7

7

Solution with recursion and backtracking

placeQueen (in currColumn:integer) if ( currColumn > 8) { 
 The problem is solved } else { while (unconsidered squares exist in currColumn and the problem is unsolved) { Determine if the next square is safe. if (such a square exists){ place a queen in the square placeQueens(currColumn+1) // try next column if (no queen safe in currColumn+1) {

  • remove queen from currColumn
  • try the next square in that column

} } } }

CS200 - Recursion

slide-8
SLIDE 8

8

Example

Q Q Q Q Q

1 2 3 4 5 6 7 8

1 3 5 2 4

CS200 - Recursion

slide-9
SLIDE 9

9

Hit ‘Dead End’

Q Q Q Q Q

1 2 3 4 5 6 7 8

1 3 5 2 4 8

CS200 - Recursion

slide-10
SLIDE 10

10

Backtrack

Q Q Q Q Q

1 2 3 4 5 6 7 8

1 3 5 2 8 7 2

CS200 - Recursion

If you go

  • n, this

will fail and you need to back track to col 4

slide-11
SLIDE 11

Backtrack: an 8 queens solution

CS200 - Recursion 11

Q Q Q Q Q Q Q Q

The only symmetric one There are 11 more “fundamental” solutions see: wikipedia.org/wiki/ Eight_queens_puzzle

slide-12
SLIDE 12

Questions

n What is the maximum depth of the runt time

stack for 8 Queens?

n How big could the call tree get?

CS200 - Recursion 12

slide-13
SLIDE 13

13

Recursion

n Specifies a solution to one or more base

cases

n Then demonstrates how to derive the

solution to a problem of an arbitrary size

q From solutions to smaller sized problems.

CS200 - Recursion

slide-14
SLIDE 14

14

Correctness of the Recursive Factorial Method

Specification of the problem (e.g., Mathematical definition, SW requirements) Algorithm (e.g., pseudo code) Does your algorithm satisfy the specification of the problem?

CS200 - Recursion

slide-15
SLIDE 15

15

Correctness of the Recursive Factorial Method

Definition of Factorial factorial(n) = n (n – 1) (n – 2) … 1 for any integer n > 0 factorial(0) = 1 Definition of method fact(N) 1: fact (in n: integer): integer

2: if (n is 0) { 3: return 1 4: } else { 5: return n* fact(n-1) 6: }

CS200 - Recursion

slide-16
SLIDE 16

16

Inductive proof fact computes the factorial of its argument.

Basis step: fact(0) = 1 Inductive Step: Show that for an arbitrary positive integer k, if fact(k) returns k!, then fact(k+1) returns (k+1)! do it do it

CS200 - Recursion

slide-17
SLIDE 17

17

The Towers of Hanoi Example

n Move pile of disks from source to destination n Only one disk may be moved at a time. n No disk may be placed on top of a smaller disk.

CS200 - Recursion

slide-18
SLIDE 18

18

States in the Towers of Hanoi

Source Destination Spare

CS200 - Recursion

slide-19
SLIDE 19

19

Recursive Solution

CS200 - Recursion

// pegs are numbers, via is computed // number of moves are counted // empty base case public void hanoi(int n, int from, int to){ if (n>0) { int via = 6 - from - to; hanoi(n-1,from, via); System.out.println("move disk " + n + " from " + from + " to " + to); hanoi(n-1,via,to); } }

let’s run it

slide-20
SLIDE 20

20

Cost of Towers of Hanoi

n How many moves does hanoi(n) make? n from the recursive code:

moves(1) = 1 moves(N) = moves(N-1)+1+moves(N-1) (if N>1)

n By inspection, we can infer that a closed form

formula for the number of moves:

moves(N) = 2N - 1 (for all N>=1)

n Can we prove it?

CS200 - Recursion

slide-21
SLIDE 21

21

Proof

n Basis Step

q Show that the property is true for N = 1.

21 - 1 = 1, which is consistent with the recurrence relation’s specification that moves(1) = 1

n Inductive Step

q Property is true for an arbitrary k è property is true for

k+1

q Assume that the property is true for N = k

moves(k) = 2k-1

q Show that the property is true for N = k + 1 q Do it, do it

CS200 - Recursion

slide-22
SLIDE 22

22

Proof – cont.

n moves(k+1) = 2 * moves(k) + 1

= 2 * (2k -1) +1 = 2*2k - 2 +1 = 2k+1-1 Therefore the inductive proof is complete.

CS200 - Recursion

slide-23
SLIDE 23

0+1+2…+n = n(n+1)/2 n=0,1,2….

base: 0 = 0*1/2=0 Check step: assume: 0+1+2…+k = k(k+1)/2

show that 0+1+2…+k+ (k+1) = (k+1)(k+2)/2

0+1+2…+k+ (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2 = k(k+1)/2 + 2(k+1)/2 = (k+2)(k+1)/2 = (k+1)(k+2)/2 Check

CS200 - Recursion 23