Recursion Suppose You Are Waiting in L ine HI YOU! A Very Long - - PowerPoint PPT Presentation

recursion suppose you are waiting in l ine
SMART_READER_LITE
LIVE PREVIEW

Recursion Suppose You Are Waiting in L ine HI YOU! A Very Long - - PowerPoint PPT Presentation

Recursion Suppose You Are Waiting in L ine HI YOU! A Very Long Line You want to determine how many people are in front of you, but you cannot see and youre not allowed to move . You are only allowed to speak to the person in front of


slide-1
SLIDE 1

Recursion

slide-2
SLIDE 2

Suppose You Are Waiting in Line…

HI YOU!

slide-3
SLIDE 3

… A Very Long Line

slide-4
SLIDE 4

You want to determine how many people are in front of you, but you cannot see and you’re not allowed to move. You are only allowed to speak to the person in front of you and behind you. How do you do it?

slide-5
SLIDE 5

Recursion

 A programming technique that breaks

down a complex problem into smaller, manageable pieces

 Recursive solutions solve a problem by

applying the same algorithm to each piece and then combining the results.

slide-6
SLIDE 6

The General Formula

#people in front of person in front of you + person in front of you = # people in front of you

slide-7
SLIDE 7

The Solution

1.

Tap the shoulder of the person in front of you and ask how many people are in front of him/her

2.

Wait for his/her response and add 1

1.

If someone asked, tell them how many people are in front of you

slide-8
SLIDE 8

A Diagram

 Ask and wait

 Ask and wait

 Ask and wait….

 …. Reached first in line. Tell person behind it is 0.

 Tell person behind it is 0+1

 Tell person behind it is 1+1…

 Tell Person behind it is x +1

slide-9
SLIDE 9

Recursive Algorithms

 There are two main components to

recursive algorithms

 1) Base Case  2) The Recursive Case

slide-10
SLIDE 10

Recursive Algorithms

 There are two main components to

recursive algorithms

 1) Base Case: The point where you stop

applying the recursive case

 2) Recursive Case: The set of instructions that

will be used over and over

slide-11
SLIDE 11

In the Queue Problem…

 Recursive case is

 Tap person in front of you. Ask #people in

front of him. Wait for his answer and add 1.

 Base case is

 person 0. You do not do execute the above.

slide-12
SLIDE 12

Recursion and Programming

 A recursive function is a function that calls

itself numberOfPeopleInFront(person){

If (there is no one to tap) return 0 Else tap person in front of you (F) #ppl in front of F = numberOfPeopleInFront(F) return #ppl in front of F + 1

 }

slide-13
SLIDE 13

Pseudo-code diagram

 Ask and wait

 Ask and wait

 Ask and wait….

 …. Reached first in line. Return 0.

 Return 0+1

 Return 1+1…

 Return x +1

slide-14
SLIDE 14

Towers of Hanoi

 A prominent recursive problem  Starting Configuration:  Goal: Move tower from A to B

slide-15
SLIDE 15

Rules

 Move one disk at a time  A larger disk cannot be placed on top of

a smaller disk

 We can use some needles as temporary

storage

slide-16
SLIDE 16

Subgoals

 Get top x-1 disks from A to C  Get bottom disk from A to B  Move top x-1 disks from C to B

slide-17
SLIDE 17

Recursion Behind Towers

 Base Case: Moving the Largest Disk to Needle

B

 Recursive Case: Do same for the x – 1 disk

above it

 http://www.mazeworks.com/hanoi/  Fun Fact: It takes at least 2n-1 moves to solve

the puzzle

slide-18
SLIDE 18

Fractals

 A rough or fragmented geometric shape

that can be split into parts, each of which is (at least an approx. of) a reduced copy

  • f the whole

 Base case: Starting shape  Recursive case: Repeating shape in

different sizes

Koch Snowflake