Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the...
Transcript of Recursion Iteration - files.meetup.com · transformation from recursion to iteration is one of the...
Recursion ≡
IterationA Conversation
06/27/2015
1
— Kedar Mhaswade
{“Level”: “Fundamental”, “Speaker”: “Journeyman”, “Style”: “Interactive”, “Time”: “~1h”}
2
“Level”:
3
“FUNDAMENTAL”
A RecurringTheme
4
3, 8, 10, 11, 1, 22
1, 3, 8, 10, 11, 22T(n) = 2T
(n/2)+n
T(0) =
1
5
6
If you can’t solve a problem, try to solve a similar,
simpler problem
7
8
1. EARTH
UNDERSTAND DEEPLY
You can always understand anything better than you
currently do!
“Speaker”:
9
“JOURNEYMAN”
“Style”:
10
“INTERACTIVE”
“Time”:
11
60.00
12
ITERATION
for (i = 0; i < n; i++) { // do something }
13
14
RECURSION
15
I Hate Quotations. Tell Me What You Know.
Ralph Waldo Emerson
16
To Iterate Is Human,To Recurse Divine
17
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.
1. Problem(s)
19
• Factorial • Fibonacci • Print 7-bit ASCII w/o
using a loop/goto20
Baby Problem(s)
21
Toddler Problem
22
yi
a
rb n
Print the Nodes of a
Binary Tree
Binary Tree Node: Duality
23
nothing
node
node
either
or
nothing
node
…
24
nodenode
node
nodenode node
nothing nothing nothing nothing nothing nothing
nothing
25
yi
a
rb n
root
RecursiveLeap of Faith
(Eric Roberts)
26
or, Suspension of Disbelief?
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 }
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 }
Domino Effect Achieved!
29
Now, How About an Iterative
Solution?
30
Time to work on a problem is after you have solved it.
This Question (Recursion<->Iteration)
Troubled Me
31
That Means I Lacked Understanding :—(
32
Luckily, a copy of the paper is only available on the Way Back Machine
Until, Finally …
– 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 :-)
You Won’t Be HereIf You Read That Paper!
34
Now That You’re Herelet’s just apply to our problem(s) what he said
35
36
37
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.
Invisible Wizard
39
a.c:#include<stdio.h>int main() {… return 0;}
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
It’s All About Functionsand Stack (That’s Why … At the Time of Data Structures)
41
42
43
ENDS
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?
The Last Action Rule
45
Observation
Simple Function Calls and Returns
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).
“Compiler” Does not Care if p == q
47
If the Last Action Observation is Provably Correct, It Can Make it Into a Rule!
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
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
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?
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?
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
Furthering The Art Part
53
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
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
Time Passes
56
Maybe Creativity Strikes While You’re Taking Shower,
We Do Not Know!
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!
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; }
Look Ma, No Recursion!
59
Home-work Assignment - I
• Eliminate Go To (Not That We Hate It) From Slide #58
60
Home-work Assignment - II
62
Try on your own. Solution on GitHub.
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