{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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 Document Right Arrow Icon

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

View Full Document Right 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 that’s inside a polygonal region, recursively visit the four adjacent pixels, coloring any that haven’t been colored, and that aren’t on the polygon’s boundary. Reaching the boundary is the recursion’s 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

{[ snackBarMessage ]}

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 bookmark
Ask a homework question - tutors are online