48 Slides--Recursion

# 48 Slides--Recursion - CS103A HO#48 Recursion 2/29/08 1 To...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS103A HO#48 Recursion 2/29/08 1 To move n disks: First move n – 1 disks to the middle peg Then move 1 disk to the right peg The move n – 1 disks to the right peg To move n disks: First move n – 1 disks to the middle peg Then move 1 disk to the right peg The move n – 1 disks to the right peg If H n is the number of moves for n disks, then H n = H n-1 + 1 + H n-1 H n = 2H n-1 + 1 H 1 = 1 Move(n, start, end, other): if n = 1 then reposition disk to end peg else { Move(n – 1, start, other, end) Move(1, start, end, other) Move(n – 1, other, end, start) } If H n is the number of moves for n disks, then H n = H n-1 + 1 + H n-1 H n = 2H n-1 + 1 H 1 = 1 Recursive Definition of a Set To define a set S recursively, we specify-- one or more initial elements of S-- a rule for constructing new elements of S given those already in the set Example: 3 є S If x, y є S, then x + y є S Recursive Definition of a Set To define a set S recursively, we specify-- one or more initial elements of S-- a rule for constructing new elements of S given those already in the set Example: 3 є S If x, y є S, then x + y є S Nothing else is in S. ( Extremal or limiting clause) CS103A HO#48 Recursion 2/29/08 2 Recursive Definition of a Set To define a set S recursively, we specify-- one or more initial elements of S-- a rule for constructing new elements of S given those already in the set Example: S is the smallest set such that 3 є S If x, y є S, then x + y є S Recursive Definition of a Set To define a set S recursively, we specify-- one or more initial elements of S-- a rule for constructing new elements of S given those already in the set Example: 3 є S If x, y є S, then x + y є S What is S? Recursive Definition of a Set To define a set S recursively, we specify-- one or more initial elements of S-- a rule for constructing new elements of S given those already in the set Example: p, q are WFF's If x is a WFF, then ¬x is a WFF. If x and y are WFF's, then (x ∨ y), (x ∧ y), (x → y), and (x ↔ y) are WFF's Recursive Definition of a Set Example: The set of binary numbers with more 0's than 1's 00 000 001 010 100 0000 0001 0010 0100 1000 Recursive Definition of a Set Example: The set of binary numbers with more 0's than 1's 00 000 001 010 100 0000 0001 0010 0100 1000 0 є S If r, s є S, then the following are in S: rs rs1 r1s 1rs Proving Properties of Recursively Defined Sets 3 є S If x, y є S, then x+y є S Prove: If w є S, then 3 | w. Every element of S is either the base case or is produced by some number of applications of the recursive rule. We can do a proof by strong induction on the number of applications of the rule to show that for n ≥ 1, if w є S is produced by n applications, then 3 | w. CS103A HO#48 Recursion 2/29/08 3 Proving Properties of Recursively Defined Sets 3 є S If x, y є S, then x+y є S Prove: If w є S, then 3 | w....
View Full Document

## This note was uploaded on 10/01/2011 for the course CS 103A taught by Professor Plummer,r during the Winter '07 term at Stanford.

### Page1 / 10

48 Slides--Recursion - CS103A HO#48 Recursion 2/29/08 1 To...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online