Download presentation
Presentation is loading. Please wait.
1
Computers as an Expressive Medium
Lab 7: Recursion, Web Crawling, and You Mayhew Seavey
2
Today’s Nonsense Recursion – Why is my head spinning?
Web Crawling – Recursing in HTML
3
Recursion Recursion basically means calling a method from inside itself. What the?!? int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1;
4
Inside Itself?! Let’s step through what happens. factorial(3);
int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3)
5
Inside Itself?! Let’s step through what happens.
factorial(3); int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3) int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 2)
6
Inside Itself?! Let’s step through what happens.
factorial(3); int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3) int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 2) int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 1)
7
Inside Itself?! Let’s step through what happens. 1 factorial(3);
int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3) int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 2) 1
8
Inside Itself?! Let’s step through what happens.
factorial(3); int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3) int factorial(int n) { if(n > 1) { return n * 1; } else return 1; (n = 2)
9
Inside Itself?! Let’s step through what happens.
factorial(3); int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3) int factorial(int n) { if(n > 1) { return 2 * 1; } else return 1; (n = 2)
10
Inside Itself?! Let’s step through what happens. 2 factorial(3);
int factorial(int n) { if(n > 1) { return n * factorial(n-1); } else return 1; (n = 3) 2
11
Inside Itself?! Let’s step through what happens. factorial(3);
int factorial(int n) { if(n > 1) { return n * 2; } else return 1; (n = 3)
12
Inside Itself?! Let’s step through what happens. factorial(3);
int factorial(int n) { if(n > 1) { return 3 * 2; } else return 1; (n = 3)
13
Inside Itself?! Let’s step through what happens. factorial(3); 6
14
Base Case The most important thing to include in a recursive call is the “base case”, something that will assure that the function stops calling itself at some point. In our example, we made sure it only called the recursive function if n was greater than 1, and each time we call it, n gets smaller, so we know it will eventually get to a number less than or equal to 1.
15
Web Crawling Let’s use recursion for something more interesting.
Say we have some method “parsePage”, that looks at a web page. Suppose we then want that method to follow the links on that page and parse the pages it is linked to. We’d then want to call the “parsePage” method on those links from inside the parsePage method we have.
16
Web Crawling
17
Web Crawling
18
Web Crawling You can see here the need for a base case. One way of controlling our search is by placing a limit on the depth of the links we follow. For instance, in the visual example, we followed the links from our start page (depth 1), and then the links from those pages (depth 2).
19
End of Slides Recursion is a slippery slope.
Web crawling is a glorious ride. Let’s take a closer look now at that HTML Parser code from lecture, which uses recursion in the way we just talked about.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.