Recursion - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Recursion

Description:

It is almost certain that one of the main heresies would have been a belief (or ... http://satirist.org/learn-game/methods/search/quiesce.html ... – PowerPoint PPT presentation

Number of Views:153
Avg rating:3.0/5.0
Slides: 24
Provided by: rosspa
Category:

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • Russell Centanni

2
  • If computers had existed in the Middle Ages,
    programmers would have been burned at the stake
    by other programmers for heresy. It is almost
    certain that one of the main heresies would have
    been a belief (or disbelief) in recursion.

3
Overview
  • Recursion
  • Minimax
  • Alpha-Beta Pruning
  • Quiescence Search
  • Conclusion Advantages and Disadvantages

4
Recursion Basics
  • Mathematically, a recursive function is one that
    uses itself in its own definition.
  • Must have a base case.
  • Must approach this base case.
  • There are two types of recursion
  • Recursive definition
  • Recursive use

5
Recursion Types
  • Recusrive definition Example Factorial
  • Factorial(n) (n0)?1, n Factorial(n-1)
  • Recursive Use Example
  • F(x) (x0) ?1, F( F(x2) )
  • Both Ex Ackermans Function
  • A(m,n) m0 ? n1,
  • n0 ? A(m-1, 1),
  • A(m-1, A(m,n-1))

6
Recursion Implementation
  • Must be supported by the programming language.
  • Recursive calls must pass results to the calling
    function.
  • calling function must still exist.
  • many copies of the recursive function.
  • Implemented with a push-down stack.
  • call stack

7
Recursive Trace
  • int factorial(int n)
  • if (n gt 1) return n f(n - 1)else
    return 1

factorial(1)
factorial(2)
factorial(3)
8
Recursive Trace
  • int factorial(int n)
  • if (n gt 1) return n f(n - 1)else
    return 1

result1
result2
result6
return to main
9
Recursion vs. Iteration
  • Iteration is typically faster and uses less
    memory.
  • Some problems are easier to solve using
    recursion.
  • Recursive definition can always be replaced by
    iteration.
  • Recursive use of procedures cannot.
  • Translating a recursive function to iteration is
    called unrolling.

10
Recursive vs. Iteration
  • int factorial(int n)
  • if (n gt 1) return n f(n - 1)else
    return 1
  • int factorial(int n)
  • int result 1
  • while ( n gt 0 )
  • result n--
  • return result

11
Recursive Data Structures
  • Data can also be defined recursively.
  • tree nodes
  • class TreeNode     Object element    
    TreeNode left     TreeNode right
  • Recursively defined data lends itself especially
    well to recursive algorithms.

12
Recursive Tree Traversals
  • INORDER
  • printTree(TreeNode t)
  • if( t not empty)
  • printTree(t.left)
  • print(t.element)
  • printTree(t.right)
  • PREORDER
  • printTree(TreeNode t)
  • if( t not empty)
  • print(t.element)
  • printTree(t.left)
  • printTree(t.right)
  • POSTORDER
  • printTree(TreeNode t)
  • if( t not empty)
  • printTree(t.left)
  • printTree(t.right)
  • print(t.element)

13
Uses for Recursion
  • Sorting (Mergesort)
  • Searching (Search Trees)
  • Permutations
  • Recognizing Grammars and Regular Expressions.
  • Artificial Intelligence problems.
  • Game Trees

14
Minimax
  • Moves are given a value.
  • Player1 wishes to minimize.
  • chooses move with the smallest value.
  • Player2 wishes to maximize.
  • chooses move with the largest value.
  • Assumes best move is chosen.
  • Visualized with a recursion tree.

15
Recursion Trees
  • Recursive steps can be viewed as a tree.
  • Example factorial(3)

Level 0
Level 1
Level 2
16
Minimax Recursion Tree
  • Any two player game with a method to determine
    good and bad moves.
  • Tic-Tac-Toe
  • Checkers or Chess
  • Every possible move.
  • Limit the recursion levels

17
Alpha-Beta Pruning
  • Minimax can be computationally expensive.
  • Method to reduce the number of nodes in a
    recursion tree.
  • Ignores fruitless sub-trees.
  • reduces the possibilities searched

18
Alpha-Beta Pruning
19
Quiescence Search
  • Limit levels of recursion, worse moves.
  • Quiescence Search allows further searching of a
    game tree.
  • Search sub-trees that are likely to contain
    better moves.
  • in chess, search capture moves.

20
Recursion Advantages
  • Expressive Power
  • Recursive code is typically much shorter than
    iterative.
  • More appropriate for certain problems.
  • Intuitive programming from mathematic definitions.

21
Recursion Advantages
  • factorial(n)
  • (n0)?1,
  • n Factorial(n-1)
  • int factorial(int n)
  • if (n0)
  • return 1
  • else
  • return nfactorial(n-1)

22
Recursion Disadvantages
  • Usually slower due to call stack overhead.
  • Burroughs B5000 implemented a hardware stack.
  • Faulty Programs are very difficult to debug.
  • Difficult to prove a recursive algorithm is
    correct.
  • especially with recursive use.

23
References
  • Barron, D. W. Recursive Techniques in
    Programming. American Elsevier Publishing
    Company, Inc. New York. 1968.
  • Dewdney, A. K. The New Turing Omnibus 66
    Excursions in Computer Science. Henry Holt and
    Company, LLC. New York. 2001.
  • D'Souza, Erwin Francis. Recursion Programming.
    February 14, 2005. http//personal.vsnl.com/erwin
    /recursion.htm
  • Scott, Jay. quiescence search. August 11,
    2000. February 16, 2005. http//satirist.org/lear
    n-game/methods/search/quiesce.html
  • Sebesta, Robert W. Concepts of Programming
    Languages, Sixth Edition. Pearson Education,
    Inc. Boston, Massachusets. 2004.
  • Weiss, Mark Allen. Data Structures Algorithm
    Analysis in Java. Addison Wesley Longman, Inc.
    Reading, Massachusetts. 1999.
Write a Comment
User Comments (0)
About PowerShow.com