IAT 800 Recursion Oct 28 2009 IAT 800

  • Slides: 20
Download presentation
IAT 800 Recursion Oct 28, 2009 IAT 800 1

IAT 800 Recursion Oct 28, 2009 IAT 800 1

Today’s Excitement g Recursion Oct 28, 2009 IAT 800 2

Today’s Excitement g Recursion Oct 28, 2009 IAT 800 2

Recursion g Recursion basically means calling a method from inside itself. int factorial(int n)

Recursion g Recursion basically means calling a method from inside itself. int factorial(int n) { if( n > 1 ) { return( n* factorial( n-1 ) ); } else return( 1 ); } Oct 28, 2009 IAT 800 3

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) {

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } Oct 28, 2009 IAT 800 4

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) { { return( n* factorial( n-1 ) ); } } else return( 1 ); } } Oct 28, 2009 IAT 800 5

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) int factorial(int n) (n=1) { { { if(n > 1) { { { return( n* factorial( n-1 ) ); return( n* factorial( n-1 } } } else return( 1 ); } } } Oct 28, 2009 IAT 800 6

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) int factorial(int n) (n=1) { { { if(n > 1) { { { return( n* factorial( n-1 ) ); return( n* factorial( n-1 } } } else return( 1 ); } } } Oct 28, 2009 IAT 800 7

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) { { return( n* factorial( n-1 ) ); } } else return( 1 ); } } Oct 28, 2009 IAT 800 1 8

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) { { return( n* factorial( n-1 ) ); return( n* 1 ); } } else return( 1 ); } } Oct 28, 2009 IAT 800 9

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) int factorial(int n) (n=2) { { if(n > 1) { { return( n* factorial( n-1 ) ); return( 2* 1 ); } } else return( 1 ); } } Oct 28, 2009 IAT 800 10

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) {

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* factorial( n-1 ) ); } else return( 1 ); } Oct 28, 2009 2 IAT 800 11

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) {

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( n* 2 ); } else return( 1 ); } Oct 28, 2009 IAT 800 12

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) {

Calling Itself g Let’s step through what happens. factorial(3); int factorial(int n) (n=3) { if(n > 1) { return( 3* 2 ); } else return( 1 ); } Oct 28, 2009 IAT 800 13

Calling Itself g Let’s step through what happens. factorial(3); 6 Oct 28, 2009 IAT

Calling Itself g Let’s step through what happens. factorial(3); 6 Oct 28, 2009 IAT 800 14

Base Case g Must have Base Case – A case or condition that returns

Base Case g Must have Base Case – A case or condition that returns without further recursion – Stops the recursive chain – Eg factorial( int n ) • Returned 1 when n = 1 • In every other call, n decreases by 1 Oct 28, 2009 IAT 800 15

Web Crawling g HTML reader called parse. Page() – Reads HTML – Finds links

Web Crawling g HTML reader called parse. Page() – Reads HTML – Finds links – Per Link it should • Call parse. Page() Oct 28, 2009 IAT 800 16

Web Crawling 1 2 4 3 Oct 28, 2009 IAT 800 5 17

Web Crawling 1 2 4 3 Oct 28, 2009 IAT 800 5 17

Web Crawling 1 6 2 5 4 3 7 11 13 10 8 Oct

Web Crawling 1 6 2 5 4 3 7 11 13 10 8 Oct 28, 2009 14 9 15 12 IAT 800 18

Web Crawling g What base case? – Count the number of recursive calls so

Web Crawling g What base case? – Count the number of recursive calls so far – Place a limit on depth – Explore no further after depth 4 – Example stopped at depth 2 Oct 28, 2009 IAT 800 19

Recursion Remember— base cases prevent infinite cats. http: //infinitecat. com/ Oct 28, 2009 IAT

Recursion Remember— base cases prevent infinite cats. http: //infinitecat. com/ Oct 28, 2009 IAT 800 20