05Recursion - CS 240 Chapter 5 - Recursion CS 240 Chapter 5...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 240 Chapter 5 - Recursion CS 240 Chapter 5 - Recursion 1Page 1 Chapter 5 Recursion Recursion can be a very effective technique for solving what would otherwise be extremely elaborate problems. Sample recursive applications include: Backtracking algorithms. Programming language definition. Matrix operations. In addition, a solid understanding of recursion is helpful in analyzing the time complexity of algorithms. Mathematical induction. Recurrence relations. Recursion-related analysis techniques include: CS 240 Chapter 5 - Recursion CS 240 Chapter 5 - Recursion 2Page 2 Recursive Backtracking: Flood Fill Algorithm Starting with a seed pixel thats inside a polygonal region, recursively visit the four adjacent pixels, coloring any that havent been colored, and that arent on the polygons boundary. Reaching the boundary is the recursions termination condition. Seed pixel Boundary pixel LRUD-visited pixel void floodfill( int x, int y) { if (!filled(x,y)) { color(x,y); floodfill(x-1,y); // Left floodfill(x+1,y); // Right floodfill(x,y-1); // Up floodfill(x,y+1); // Down } } CS 240 Chapter 5 - Recursion CS 240 Chapter 5 - Recursion 3Page 3 Recursive Grammars Languages, in particular programming languages, are defined by their grammars , sets of recursive rules which provide syntax. Example: A Simple Calculator Grammar program: END expr_list END expr_list: expression ; expression ; expr_list expression: term + expression term expression term term: primary / term primary * term primary primary: NUMBER NAME NAME = expression- primary ( expression ) Using this grammar, the following is a syntactically correct calculator program: pi = 3.1416; rad = 2.5; ht = 10; area = pi * rad * rad; surfacearea = 2 * (area + pi * rad * ht); END Recursive grammars are also prominent in more sophisticated languages, making the following language features possible: nested loops, conditionals, function calls cascaded operators (<<, >>, =, etc.) multiple cases in a switch statement CS 240 Chapter 5 - Recursion CS 240 Chapter 5 - Recursion 4Page 4 Recursive Matrix Operations Many matrix operations can be defined recursively, using combinations of submatrix operations to implement the large matrix operation. Example: Determinants...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 240 taught by Professor Klein,s during the Spring '08 term at Southern Illinois University Edwardsville.

Page1 / 16

05Recursion - CS 240 Chapter 5 - Recursion CS 240 Chapter 5...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online