Sie sind auf Seite 1von 41

Thinking Recursively

An Interesting Peruse
WSJ: Best and Worst Jobs of !" #
htt$:%%online&'s(&co)%article%SB"!!!"* *!+ ,! -!-,, .!*+,,--/ -!"- 0!+ ,/&ht)l

1" Job: Software Engineer

Thinking Recursively2 Part II

Recursive Proble)3Solving
if (problem is sufficiently simple) { Directly solve the problem. Return the solution. } else { Split the problem up into one or more smaller problems with the same structure as the original. Solve each of those smaller problems. Combine the results to get the overall solution. Return the overall solution. }

Parking Rando)ly

Car has length one

Parking Rando)ly

Parking Rando)ly

Parking Rando)ly

Parking Rando)ly

x+1

Parking Rando)ly

x+1

Parking Rando)ly

x+1

Place cars randomly in these ranges!

Parking Rando)ly
int parkRandomly(double low, double high) { if (high - low < 1.0) { return 0; } else { double x randomReal(low, high - 1.0); return 1 ! parkRandomly(low, x) ! re"urn 1 ! parkRandomly(x ! 1, high); } }

So What4

The beauty of our algorith) is the follo'ing recursive insight: Split an area into smaller, independent pieces and solve each piece separately.

5any $roble)s can be solved this 'ay&

6enerating 5ondrian Paintings

Fig. 11: Three real Mondrian paintings, and three samples from our targeting function. Can you tell which is which?
Source: Jerry O. Talton, Yu ou, Ste!e esser, Jared "u#e, $adom%r M&ch, and 'ladlen (oltun, )Metropolis *rocedural Modeling,+ ACM Transactions on Graphics, ,pril -.//.

Slides by Eric Roberts

6enerating 5ondrians

Slides by Eric Roberts

The 7S"!/B 6ra$hics 8ibrary


initGraphics(width, height)
Creates a graphics window with the specified dimensions.

drawLine(x0, y0, x1, y1)


Draws a line connecting the points (x0, y0 and (x1, y1 .

drawPolarLine(x0, y0, r, theta)


Draws a line r pixels long in direction theta from (x0, y0 . !o ma"e chaining line segments easier, this f#nction ret#rns the ending coordinates as a GPoint.

getWindowWidth()
$et#rns the width of the graphics window.

getWindowHeight()
$et#rns the height of the graphics window.

Many more functions e0ist in the graphics.h interface, which is descri1ed on the we1 site.
Slides by Eric Roberts

9ra'ing Rectangles
drawRect(x, y, width, height)
Draws the o#tline of a rectangle with the specified %o#nds.

fillRect(x, y, width, height)


&ills the o#tline of the specified rectangle #sing the c#rrent color.

setColor(color)
'ets the pen color to the specified color string (s#ch as " L!C"" or "R#$")

setColor("%rrggbb")
'ets the red(green(%l#e components to the specified hexadecimal )al#es.

Slides by Eric Roberts

:nce 5ore 'ith Color

Slides by Eric Roberts

A fractal image is an i)age that is defined in ter)s of s)aller versions of itself&

;ractal Trees

We can generate a fractal tree as follo's:

6ro' in so)e direction for a $eriod of ti)e& Then2 s$lit and gro' t'o s)aller trees out'ard at so)e angle&

5ore Trees

What if 'e change the a)ount of branching4 What if 'e )ake the lines thicker4 What if 'e allo' the tree to kee$ gro'ing after it branches4 Stanford Dryad $rogra) uses a co)bination of recursion2 )achine learning2 and hu)an feedback to design aesthetically $leasing trees&

7heck it out at http://dryad.stanford.edu/

<=haustive Recursion

6enerating All Possibilities

7o))only2 you 'ill need to generate all ob(ects )atching so)e criteria&

Word 8adders: 6enerate all 'ords that differ by e=actly one letter&

:ften2 structures can be generated iteratively& In )any cases2 ho'ever2 it is best to think about generating all o$tions recursively&

Subsets

6iven a set S2 a subset of S is a set T co)$osed of ele)ents of S& <=a)$les:


>!2 "2 ? @ >!2 "2 2 -2 *2 +? >dikdik2 ibe=? @ >dikdik2 ibe=? > A2 62 72 T ? @ > A2 B2 72 92 <2 A2 B ? > ? @ >a2 b2 c? >?@>?

6enerating Subsets

5any i)$ortant $roble)s in co)$uter science can be solved by listing all the subsets of a set S and finding the best# one out of every o$tion& <=a)$le:

Cou have a collection of sensors on an autono)ous vehicle2 each of 'hich has data co)ing in& Which subset of the sensors do you choose to listen to2 given that each takes a different a)ount of ti)e to read4

6enerating Subsets

0,1,* 0 * 1 1,* 0, 0,1 0,1,* *

6enerating Subsets

0,1,* 0 * 1 1,* 0, 0,1 0,1,* *

6enerating Subsets

0,1,* 0 * 1 1,* 0, 0,1 0,1,* *

6enerating Subsets

0,1,* 0 * 1 1,* 0, 0,1 0,1,* *

6enerating Subsets

0,1,* 0 * 1 1,* 0, 0,1 0,1,* *

6enerating Subsets

0,1,* 0 * 1 1,* 0, 0,1 0,1,* *

6enerating Subsets

The only subset of an e)$ty set is the e)$ty set itself& :ther'ise:

;i= so)e ele)ent x of the set& 6enerate all subsets of the set for)ed by re)oving x fro) the )ain set& These subsets are subsets of the original set& All of the sets for)ed by adding x into those subsets are subsets of the original set&

Tracing the Recursion

Tracing the Recursion


> A2 D2 I ?

Tracing the Recursion


> A2 D2 I ? > D2 I ?

Tracing the Recursion


> A2 D2 I ? > D2 I ? >I?

Tracing the Recursion


> A2 D2 I ? > D2 I ? >I? >?

Tracing the Recursion


> A2 D2 I ? > D2 I ? >I? >?
{ }

Tracing the Recursion


> A2 D2 I ? > D2 I ? >I? >?
{#}, { }

{ }

Tracing the Recursion


> A2 D2 I ? > D2 I ? >I? >?
{$, #}, {$}, {#}, { }

{#}, { }

{ }

Tracing the Recursion


> A2 D2 I ? > D2 I ? >I? >?
{%, $, #}, {%, $}, {%, #}, {%} {$, #}, {$}, {#}, { } {$, #}, {$}, {#}, { }

{#}, { }

{ }

Das könnte Ihnen auch gefallen