recursion iteration - files.meetup.com · transformation from recursion to iteration is one of the...

63
Recursion Iteration A Conversation 06/27/2015 1 — Kedar Mhaswade

Upload: others

Post on 01-Oct-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Recursion ≡

IterationA Conversation

06/27/2015

1

— Kedar Mhaswade

Page 2: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

{“Level”: “Fundamental”, “Speaker”: “Journeyman”, “Style”: “Interactive”, “Time”: “~1h”}

2

Page 3: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

“Level”:

3

“FUNDAMENTAL”

Page 4: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

A RecurringTheme

4

3, 8, 10, 11, 1, 22

1, 3, 8, 10, 11, 22T(n) = 2T

(n/2)+n

T(0) =

1

Page 5: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

5

Page 6: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

6

Page 7: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

If you can’t solve a problem, try to solve a similar,

simpler problem

7

Page 8: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

8

1. EARTH

UNDERSTAND DEEPLY

You can always understand anything better than you

currently do!

Page 9: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

“Speaker”:

9

“JOURNEYMAN”

Page 10: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

“Style”:

10

“INTERACTIVE”

Page 11: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

“Time”:

11

60.00

Page 12: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

12

ITERATION

Page 13: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

for (i = 0; i < n; i++) { // do something }

13

Page 14: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

14

RECURSION

Page 15: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

15

Page 16: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

I Hate Quotations. Tell Me What You Know.

Ralph Waldo Emerson

16

Page 17: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

To Iterate Is Human,To Recurse Divine

17

Page 18: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Dijkstra

18

Walking with me through Eindhoven, my five-year old son suddenly said,

“Dad, not every boat has a life-boat, has it?” “How come?” I said.

“Well, the life-boat could have a smaller life-boat, but then

that would be without one.” (Quora)

knew that the concept of recursion was not difficult.

Page 19: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

1. Problem(s)

19

Page 20: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

• Factorial • Fibonacci • Print 7-bit ASCII w/o

using a loop/goto20

Baby Problem(s)

Page 21: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

21

Page 22: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Toddler Problem

22

yi

a

rb n

Print the Nodes of a

Binary Tree

Page 23: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Binary Tree Node: Duality

23

nothing

node

node

either

or

nothing

node

Page 24: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

24

nodenode

node

nodenode node

nothing nothing nothing nothing nothing nothing

nothing

Page 25: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

25

yi

a

rb n

root

Page 26: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

RecursiveLeap of Faith

(Eric Roberts)

26

or, Suspension of Disbelief?

Page 27: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

27

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {7 printTree(n.left); //recursive call #18 print(n.key); //print this key9 printTree(n.right); // recursive call #211 }12 }

Page 28: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

28

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 if (n != null) { // simplest base case7 printTree(n.left); // recursive call #18 print(n.key); // print this key9 printTree(n.right); // recursive call #210 }11 }12 }

Page 29: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Domino Effect Achieved!

29

Page 30: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Now, How About an Iterative

Solution?

30

Time to work on a problem is after you have solved it.

Page 31: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

This Question (Recursion<->Iteration)

Troubled Me

31

That Means I Lacked Understanding :—(

Page 32: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

32

Luckily, a copy of the paper is only available on the Way Back Machine

Until, Finally …

Page 33: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

– Donald Knuth

I have always felt that the transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it at about the time s/he is studying data structures.

33

Yay! I was right, after all :-)

Page 34: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

You Won’t Be HereIf You Read That Paper!

34

Page 35: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Now That You’re Herelet’s just apply to our problem(s) what he said

35

Page 36: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

36

Page 37: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

37

Page 38: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

38

In fact, in a computer, there is no difference between a program and a program’s data except how it is used by the computer. They are both stored and accessed the same way.

— Jonathan Bartlett (Programming From the Ground Up)

Sorry John [Backus], No Escape From Neumann Yet.

Page 39: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Invisible Wizard

39

a.c:#include<stdio.h>int main() {… return 0;}

Page 40: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

40

I guess the first thing I did well at was when I worked on the theory that goes on behind how compilers work. I worked on the theory that underlies algebraic languages …

— Donald Knuth

Page 41: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

It’s All About Functionsand Stack (That’s Why … At the Time of Data Structures)

41

Page 42: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

42

Page 43: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

43

ENDS

Page 44: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

44

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 if (n != null) {7 printTree(n.left); // rec call #18 print(n.key); // print this key9 printTree(n.right);//rec call #210 }11 }12 }

Call #2 ≇ Call #1, isn’t it?

Page 45: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

The Last Action Rule

45

Observation

Simple Function Calls and Returns

Page 46: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

46

Does the Title of that Article Make Sense Now?

If the last action of procedure p before it returns is to call procedure q, simply go to the beginning of procedure q instead (and return to not p, but its caller).

Page 47: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

“Compiler” Does not Care if p == q

47

If the Last Action Observation is Provably Correct, It Can Make it Into a Rule!

Page 48: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

48

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 Q: if (n != null) {7 printTree(n.left); // rec call #18 print(n.key); // print this key9 printTree(n = n.right); //last action!10 goto Q;11 }12 }13 }

Pretend Java Has go to

aka, Tail Call

Page 49: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

49

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 Q: if (n != null) {7 printTree(n.left); // recursive call #18 print(n.key); // print this key9 n = n.right;10 goto Q;11 }12 }13 }

Pretend Java Has go to

Page 50: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

50

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 Q: if (n != null) { // top-tested, always!7 printTree(n.left); // recursive call #18 print(n.key); // print this key9 n = n.right;10 goto Q;11 }12 }13 }

Don’t You Like “Go To”s?

Page 51: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

51

Then Here’s a While!

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 while (n != null) {7 printTree(n.left); //the only recursive call8 print(n.key); // print this key9 n = n.right;10 }11 }12 }

What Just Happened?

Page 52: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

52

ART SCIENCE

Our Original Solution By Taking the

Recursive Leap of Faith (This Slide)

Tail Call Elimination We Just Implemented

(This Slide)

“Science is knowledge which we understand so well that we can teach it to a computer; and if we don't fully understand something, it is an art to deal with it”

— Donald Knuth

Page 53: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Furthering The Art Part

53

Page 54: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

54

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 Q: if (n != null) {7 printTree(n.left); // recursive call #18 print(n.key); // print this key9 n = n.right;10 goto Q;11 }12 }13 }

Last Action in a Function is Different

Page 55: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

55

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {6 Q: if (n != null) {7 n = n.left;8 goto Q;9 R: print(n.key); // unreachable label!10 n = n.right;11 goto Q;12 }13 }14 }

Naivety-I

We are NEVER GOing To Line #9!

a

b

nothing nothing

nothing

Page 56: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Time Passes

56

Maybe Creativity Strikes While You’re Taking Shower,

We Do Not Know!

Page 57: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

57

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {

6 Q: if (n != null) {

7 n = n.left;8 goto Q;9 R: print(n.key); //10 n = n.right;11 goto Q;12 }

13 }14 }

How About Saving the Context Using Stack (What Else), You Think …

Stack s = new Stack(); // Our Stack, on the system heap!

s.push(n); // save current context

if (!s.isEmpty()) { n = s.pop(); //reinstate saved context goto R; }

a

b

nothing nothing

nothing

b

a

unreachable label!

Page 58: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

58

1 class Node {2 Node left, right;3 int key;4 5 static void printTree(Node n) {

7 Q: if (n != null) {

9 n = n.left;10 goto Q;11 R: print(n.key); //12 n = n.right;13 goto Q;14 }1516171819 }20 }

6 Stack s = new Stack(); // Our Stack, on the system heap!

8 s.push(n); // save current context

if (!s.isEmpty()) { n = s.pop(); // reinstate saved context goto R; }

Page 59: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Look Ma, No Recursion!

59

Page 60: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Home-work Assignment - I

• Eliminate Go To (Not That We Hate It) From Slide #58

60

Page 61: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

http://bit.ly/ recurse-iterate

61

Page 62: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Home-work Assignment - II

62

Try on your own. Solution on GitHub.

Page 63: Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the most fundamental concepts of computer science, and that a student should learn it

Credits and Thanks!• You All!• Suresh B Velagapudi

• Meetup, HackerDojo

• Programming languages

• Book Cover Images: Amazon

• Other Images: The Web, Wikimedia Commons

• http://www.freesoftwaremagazine.com/files/nodes/1194/knuth-drofina.jpg

• Quora

• Amazon, MIT Press (Book References)

• Slight Detour

• Wikipedia (Several)

• Jonathan Bartlett — download.savannah.gnu.org/.../ProgrammingGroundUp-1-0-booksize.pdf

• Stack Frame (From SO)

• Where Rubber Meets the Road

• Science-Art

63