Download Recursion in Computer Science: Concepts, Patterns, and Applications and more Lecture notes Programming Abstractions in PDF only on Docsity!
Admin
Assign 2 due today, Assign 3 out
Today’s topics
Reading
- Reader ch.^ 5-6 (today,^ next) Lecture # 9
Thinking recursively
Recursive decomposition is the hard part
- Find recursive sub-structure
- Solve problem using result from smaller subproblem(s)
- Identify base case
- Simplest possible case,^ directly solvable,^ recursion advances to it
Common patterns
- Handle first and/or last,^ recur on remaining
- Divide in half, recur on one/both halves
- Make a choice among options,^ recur on updated state
Placement of recursive call(s)
- Recur-then-process versus process-then-recur
Procedural vs functional
Functional recursion
- Function returns result
- Computers using result from recursive call(s)
Procedural recursion
- No return value (function returns void)
- Task accomplished during recursive calls
Example: drawing fractal
- Self-similar structure
- Repeats itself within
- Outer fractal makes recursive call to draw inner fractal(s)
A familiar fractal
void DrawFractal (double x, double y, double w, double h) { DrawTriangle(x, y, w, h); if (w < .2 || h < .2) return; double halfH = h/2; double halfW = w/2; DrawFractal(x, y, halfW, halfH); // left DrawFractal(x + halfW/2, y + halfH, halfW, halfH); // top DrawFractal(x + halfW, y, halfW, halfH); // right }
Recursive art
Piet Mondrian
- Dutch painter,^ 1872-
- cubism, neoplasticism I believe it is possible that, through horizontal and vertical lines constructed with awareness, but not with calculation, led by high intuition, and brought to harmony and rhythm, these basic forms of beauty, supplemented if necessary by other direct lines or curves, can become a work of art, as strong as it is true."
Random pseudo-Mondrian
Choose one of three options
- Divide canvas horizontally
- Divide canvas vertically
- Do nothing
Dividing produces two smaller canvases
- That can also be recursively painted in Mondrian style
Base case stops at too-small canvas
Mondrian code
void DrawMondrian(double x, double y, double w, double h) { if (w < 1 || h < 1 ) return; // base case FillRectangle(x, y, w, h, RandomColor()); // fill background switch (RandomInteger(0, 2)) { case 0: // do nothing break; case 1: // bisect vertically double midX = RandomReal(0,w); DrawBlackLine(x+midX, y, h); DrawMondrian(x, y, midX, h); DrawMondrian(x+midX, y, w-midX, h); break; case 2: // bisect horizontally double midY = RandomReal(0, h); DrawBlackLine(x, y+midY, w); DrawMondrian(x, y, w, midY); DrawMondrian(x, y + midY, w, h-midY); break; } }
Towers
Set of graduated disks stacked on a spindle
Goal is move tower from source to destination
Rules
- All disks on a spindle (when not actively being moved)
- Have one spare spindle
- Can move only one disk at a time
- Can only place disk on top of larger disk A B C
Permute code
void RecPermute(string soFar, string rest) { if (rest == "") { cout << soFar << endl; } else { for (int i = 0; i < rest.length(); i++) { string next = soFar + rest[i]; string remaining = rest.substr(0, i)
- rest.substr(i+1); RecPermute(next, remaining); } } } // "wrapper" function void ListPermutations(string s) { RecPermute("", s); }
Tree of recursive calls
P("ac","bd") Permute("", "abcd") P("b","acd") P("c","abd") P("d","abc") P("abc","d") P("abcd","") P("a","bcd") P("ab","cd") P("abd","c") P("abdc","") P("ad","bc") P("acb","d") P("acbd","") P("acd","b") P("acdb","")