Top Banner
RECURSION FOR THE REST OF US CS FUNDAMENTALS SERIES http://bit.ly/29zaKm3
87

Recursion For the Rest of Us (CS Fundamentals Series)

Apr 12, 2017

Download

Software

Haseeb Qureshi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recursion For the Rest of Us (CS Fundamentals Series)

RECURSION FOR THE REST OF US

CS FUNDAMENTALS SERIES

http://bit.ly/29zaKm3

Page 2: Recursion For the Rest of Us (CS Fundamentals Series)

WHAT IS RECURSION?

Page 3: Recursion For the Rest of Us (CS Fundamentals Series)

THIS IS RECURSION.

Page 4: Recursion For the Rest of Us (CS Fundamentals Series)

SO IS THIS.

Page 5: Recursion For the Rest of Us (CS Fundamentals Series)

RECURSION IS JUST A FUNCTION THAT CALLS

ITSELF.

Page 6: Recursion For the Rest of Us (CS Fundamentals Series)

WHY WOULD A FUNCTION CALL ITSELF?

Page 7: Recursion For the Rest of Us (CS Fundamentals Series)

A RECURSIVE FUNCTION CALLS ITSELF BECAUSE

IT’S RECURSIVE.

Page 8: Recursion For the Rest of Us (CS Fundamentals Series)

SERIOUSLY. WHEN WOULD YOU WANT

TO USE RECURSION?

Page 9: Recursion For the Rest of Us (CS Fundamentals Series)

NAMELY: WHEN A PROBLEM CAN BE

EASILY DEFINED IN TERMS OF SUBPROBLEMS.

Page 10: Recursion For the Rest of Us (CS Fundamentals Series)

QUINTESSENTIAL EXAMPLE:FACTORIAL!

Page 11: Recursion For the Rest of Us (CS Fundamentals Series)

5! = 5 * 4 * 3 * 2 * 1

Page 12: Recursion For the Rest of Us (CS Fundamentals Series)

4! = 4 * 3 * 2 * 1

5! = 5 * 4 * 3 * 2 * 1

Page 13: Recursion For the Rest of Us (CS Fundamentals Series)

5! = 5 * 4!

Page 14: Recursion For the Rest of Us (CS Fundamentals Series)

You can easily express factorial in terms of subproblems.

Specifically, if you have the subproblem

(N - 1)! you can easily get

N!

Page 15: Recursion For the Rest of Us (CS Fundamentals Series)

N! = N * (N - 1)!

Page 16: Recursion For the Rest of Us (CS Fundamentals Series)

EASY, RIGHT?

Page 17: Recursion For the Rest of Us (CS Fundamentals Series)

ONLY ONE PROBLEM…

Page 18: Recursion For the Rest of Us (CS Fundamentals Series)

2! = 2 * 1!

Page 19: Recursion For the Rest of Us (CS Fundamentals Series)

1! = 1 * 0!

Page 20: Recursion For the Rest of Us (CS Fundamentals Series)

0! = 0 * -1! …

Page 21: Recursion For the Rest of Us (CS Fundamentals Series)
Page 22: Recursion For the Rest of Us (CS Fundamentals Series)

WE NEED TO TELL THE SNAKE WHEN TO BITE DOWN

AND STOP RECURSING.

Page 23: Recursion For the Rest of Us (CS Fundamentals Series)

LET’S JUST SAY: 0! = 1

Page 24: Recursion For the Rest of Us (CS Fundamentals Series)

1! = 1 * 0!

1! = 1 * 1

Page 25: Recursion For the Rest of Us (CS Fundamentals Series)

SO WITH THAT, 1! = 1

Page 26: Recursion For the Rest of Us (CS Fundamentals Series)

2! = 2 * 1! 3! = 3 * 2! 4! = 4 * 3! 5! = 5 * 4!

= 2= 6= 24= 120

Page 27: Recursion For the Rest of Us (CS Fundamentals Series)

WE NEEDED TWO THINGS TO GET THE RECURSIVE

ALGORITHM.

Page 28: Recursion For the Rest of Us (CS Fundamentals Series)

N * (N - 1)!

First, we need to know how to solve the problem of size N in terms of smaller subproblems.

This is also called the inductive step.

(See: induction)

Page 29: Recursion For the Rest of Us (CS Fundamentals Series)

0! = 1 Second, we need the base case.

The recursion has to end somewhere, so we just hard-code it in.

Page 30: Recursion For the Rest of Us (CS Fundamentals Series)

AND THAT’S ALL THERE IS TO IT.

Page 31: Recursion For the Rest of Us (CS Fundamentals Series)

WORDS OF WARNING

▸ Recursion is often weird and unintuitive.

▸ Don’t try to do something recursively that’s better done iteratively!

▸ But there are times when it’s the most elegant way to solve a problem.

▸ E.g., factorial, sorting, many tree and graph problems (or generally when using any recursive data structure).

Page 32: Recursion For the Rest of Us (CS Fundamentals Series)

DEBUGGING RECURSION

▸ Recursion is usually more difficult to debug.

▸ If you don’t define your base case properly, you’ll usually trigger a stack overflow.

Page 33: Recursion For the Rest of Us (CS Fundamentals Series)

DEBUGGING RECURSION

▸ Console.log is your friend.

▸ Recursion usually works not at all, or everything works at once. This makes it trickier to introspect.

▸ Consider using: if (condition) debugger;

Page 34: Recursion For the Rest of Us (CS Fundamentals Series)

SIMPLE EXAMPLE function sum(arr)

Page 35: Recursion For the Rest of Us (CS Fundamentals Series)

MORE RECURSION ADVICE

▸ Start with the inductive step.

▸ Don’t go straight into coding it, first map it out and make sure you understand it.

▸ Then enter in the base case.

▸ Usually if you’re hard-coding multiple base cases, you’re overcomplicating it.

Page 36: Recursion For the Rest of Us (CS Fundamentals Series)

TIPS ON GENERATING THE INDUCTIVE STEP

▸ First, think about what a subproblem would look like. How can I make the input smaller?

▸ Is it size N - 1? N - 3? N / 2? All of these are valid subproblem sizes; what would make the solution easy?

▸ Think of recursion like cheating…

Page 37: Recursion For the Rest of Us (CS Fundamentals Series)

PRETEND YOU SMUGGLED IN AN ALGORITHM THAT ALREADY

SOLVES THE PROBLEM

Page 38: Recursion For the Rest of Us (CS Fundamentals Series)

BUT ONLY FOR SIZE N - 1…

Page 39: Recursion For the Rest of Us (CS Fundamentals Series)

HOW WOULD YOU USE THAT ALGORITHM TO GENERATE THE ANSWER FOR SIZE N?

Page 40: Recursion For the Rest of Us (CS Fundamentals Series)

FINALLY, JUST HARD-CODE IN A BASE CASE.

Page 41: Recursion For the Rest of Us (CS Fundamentals Series)

TA-DA! SPECS PASSING.

Page 42: Recursion For the Rest of Us (CS Fundamentals Series)

BASICALLY, RECURSION IS MAGIC.

Page 43: Recursion For the Rest of Us (CS Fundamentals Series)
Page 44: Recursion For the Rest of Us (CS Fundamentals Series)

HARDER EXAMPLES function triplets(arr)

function join(strings, separator)

Page 45: Recursion For the Rest of Us (CS Fundamentals Series)

OKAY, LET’S DO SOME RECURSION.

Page 46: Recursion For the Rest of Us (CS Fundamentals Series)

SO WHAT’S THIS MAXIMUM CALL STACK

SIZE EXCEEDED

BUSINESS?

Page 47: Recursion For the Rest of Us (CS Fundamentals Series)
Page 48: Recursion For the Rest of Us (CS Fundamentals Series)

FIRST, WE NEED TO UNDERSTAND HOW

FUNCTION CALLING WORKS.

Page 49: Recursion For the Rest of Us (CS Fundamentals Series)
Page 50: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariablesTHE STACK:

Page 51: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:fn:two line 22Local

Variables

Page 52: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:fn:two line 22Local

Variables

fn:three line 27LocalVariables

>> Finished function 3.

Page 53: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:fn:two line 22Local

Variables

fn:three line 28LocalVariables

Page 54: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:fn:two line 22Local

Variables

fn:three line 28LocalVariables

Page 55: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

Page 56: Recursion For the Rest of Us (CS Fundamentals Series)

NOW WE POP FROM THE STACK.

Page 57: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28Local

Variables

Page 58: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:fn:two line 22Local

Variables

Page 59: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:fn:two line 23Local

Variables

>> Done with function 2.

Page 60: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:

fn:twoline 23

LocalVariables

Page 61: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:

Page 62: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

THE STACK:

>> And done with function 1!

Page 63: Recursion For the Rest of Us (CS Fundamentals Series)

SO HOW CAN WE BREAK THIS?

Page 64: Recursion For the Rest of Us (CS Fundamentals Series)
Page 65: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

THE STACK:

fn:one line 17LocalVariables

Page 66: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

THE STACK:

fn:one line 17LocalVariables

fn:two line 22LocalVariables

Page 67: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

THE STACK:

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

Page 68: Recursion For the Rest of Us (CS Fundamentals Series)

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

THE STACK:

fn:one line 17LocalVariables

fn:two line 22LocalVariables

fn:three line 28LocalVariables

fn:one line 17LocalVariables

Page 69: Recursion For the Rest of Us (CS Fundamentals Series)

THE STACK:

Page 70: Recursion For the Rest of Us (CS Fundamentals Series)

EVENTUALLY, WE RUN OUT OF MEMORY

(OR SPACE ON THE STACK).

Page 71: Recursion For the Rest of Us (CS Fundamentals Series)

AND WE GET A…

Page 72: Recursion For the Rest of Us (CS Fundamentals Series)
Page 73: Recursion For the Rest of Us (CS Fundamentals Series)

THIS IS WHY RECURSIVE ALGORITHMS CAN’T

INFINITE LOOP*

* Without tail recursion or explicit loops

Page 74: Recursion For the Rest of Us (CS Fundamentals Series)

AND THIS IS ALSO WHY RECURSION IS SOMETIMES LESS

SPACE-EFFICIENT THAN ITERATION

Page 75: Recursion For the Rest of Us (CS Fundamentals Series)

EACH STACK FRAME HAS TO BE HELD IN MEMORY

Page 76: Recursion For the Rest of Us (CS Fundamentals Series)

AND GETS EVALUATED ONE BY ONE.

Page 77: Recursion For the Rest of Us (CS Fundamentals Series)

IT’S ALSO TRUE THAT EVERY RECURSIVE ALGORITHM

IMPLICITLY USES A STACK

Page 78: Recursion For the Rest of Us (CS Fundamentals Series)

SO IF YOU WANT TO IMPLEMENT A RECURSIVE ALGORITHM

ITERATIVELY, YOU OFTEN NEED TO MANAGE YOUR OWN STACK.

Page 79: Recursion For the Rest of Us (CS Fundamentals Series)

COOL.

Page 80: Recursion For the Rest of Us (CS Fundamentals Series)

SO HOW DO YOU CALCULATE THE BIG O OF A RECURSIVE ALGORITHM?

Page 81: Recursion For the Rest of Us (CS Fundamentals Series)

Number of stack frames

*

average runtime per stack frame

Page 82: Recursion For the Rest of Us (CS Fundamentals Series)

IT GETS MORE COMPLICATED THAN THAT, BUT THAT’S A GOOD STARTING POINT.(SEE MASTER METHOD, RECURRENCES,

RECURSION TREES, ETC.)

Page 83: Recursion For the Rest of Us (CS Fundamentals Series)

DEMO RECURSIVE RUNTIME ANALYSIS

Page 84: Recursion For the Rest of Us (CS Fundamentals Series)
Page 85: Recursion For the Rest of Us (CS Fundamentals Series)

QUESTIONS?

Page 86: Recursion For the Rest of Us (CS Fundamentals Series)

I AM HASEEB QURESHI

You can find me on Twitter: @hosseeb

You can read my blog at: haseebq.com

Page 87: Recursion For the Rest of Us (CS Fundamentals Series)

PLEASE DONATE IF YOU GOT SOMETHING OUT OF THIS

<3

Ranked by GiveWell as the most

efficient charity in the world!