thinking recursive ly

41
 Thinking Recursively

Upload: flandry2fl

Post on 04-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 1/41

Thinking Recursively

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 2/41

An Interesting Peruse

WSJ: “Best and Worst Jobs of !" #

htt$:%%online&'s(&co)%article%SB"!!!"* *!+ ,! -!-,, .!*+,,--/ -!"- 0!+ ,/&ht)l

1" Job: Software Engineer

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 3/41

Thinking Recursively2 Part II

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 4/41

Recursive Proble)3Solvingif ( 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.

}

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 5/41

Parking Rando)ly

0 5Car haslength one

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 6/41

Parking Rando)ly

0 5

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 7/41

Parking Rando)ly

0 5

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 8/41

Parking Rando)ly

0 5

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 9/41

Parking Rando)ly

0 5x + 1x

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 10/41

Parking Rando)ly

0 5x + 1x

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 11/41

Parking Rando)ly

0 5x + 1x

Place cars randomly in these ranges!

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 12/41

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);

}}

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 13/41

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 14/41

6enerating 5ondrian Paintings

Fig. 11: Three real Mondrian paintings, and three samples from ourtargeting 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

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 15/41

Slides by Eric Roberts

6enerating 5ondrians

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 16/41

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

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

drawLine( x 0 , y 0 , x 1 , y 1 )Draws a line connecting the points ( x 0, y 0 and ( x 1, y 1 .

drawPolarLine( x 0 , y 0 , r , theta )

Draws a line r pixels long in direction theta from ( x 0, y 0 . o ma"e chaining linesegments 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 isdescri1ed on the we1 site.

Slides by Eric Roberts

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 17/41

9ra'ing RectanglesdrawRect( 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

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 18/41

Slides by Eric Roberts

nce 5ore 'ith C o lo r

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 19/41

A fractal image is an i)age that is defined

in ter)s of s)aller versions of itself&

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 20/41

;ractal Trees● We can generate a

fractal tree asfollo's:

● 6ro' in so)edirection for a$eriod of ti)e&

● Then2 s$lit and

gro' t'o s)allertrees out'ard atso)e angle&

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 21/41

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)anfeedback to design aesthetically $leasing trees&

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

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 22/41

<=haustive Recursion

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 23/41

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 24/41

Subsets

● 6iven a set S 2 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 2 ?● > ? @ >a2 b2 c?● > ? @ > ?

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 25/41

6enerating Subsets● 5any i)$ortant $roble)s in co)$uter

science can be solved by listing all thesubsets of a set S and finding the “best# oneout 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 differenta)ount of ti)e to read4

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 26/41

6enerating Subsets

0 1 *, ,

*

1

1 *

00 *,

0 1,

0 1 *

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 27/41

6enerating Subsets

0 1 *, ,

*

1

1 *

00 *,

0 1,

0 1 *

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 28/41

6enerating Subsets

0 1 *, ,

*

1

1 *

00 *,

0 1,

0 1 *

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 29/41

6enerating Subsets

0 1 *, ,

*

1

1 *

00 *,

0 1,

0 1 *

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 30/41

6enerating Subsets

0 1 *, ,

*

1

1 *

00 *,

0 1,

0 1 *

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 31/41

6enerating Subsets

0 1 *, ,

*

1

1 *

00 *,

0 1,

0 1 *

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 32/41

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&

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 33/41

Tracing the Recursion

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 34/41

Tracing the Recursion

> A2 D2 I ?

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 35/41

Tracing the Recursion

> A2 D2 I ?

> D2 I ?

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 36/41

Tracing the Recursion

> A2 D2 I ?

> D2 I ?

> I ?

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 37/41

Tracing the Recursion

> A2 D2 I ?

> ?

> D2 I ?

> I ?

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 38/41

Tracing the Recursion

> A2 D2 I ?

> ?

> D2 I ?

> I ?

{ }

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 39/41

Tracing the Recursion

> A2 D2 I ?

> ?

> D2 I ?

> I ?

{ }

{#}, { }

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 40/41

Tracing the Recursion

> A2 D2 I ?

> ?

> D2 I ?

> I ?

{ }

{#}, { }

{$, #}, {$}, {#}, { }

8/13/2019 Thinking Recursive Ly

http://slidepdf.com/reader/full/thinking-recursive-ly 41/41

Tracing the Recursion

> A2 D2 I ?

> ?

> D2 I ?

> I ?

{%, $, #}, {%, $}, {%, #}, {%}{$, #}, {$}, {#}, { }

{ }

{#}, { }

{$, #}, {$}, {#}, { }