Recursion - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Recursion

Description:

{ int mid = (bot top)/2; if (target = tArray[mid]) return bSearch(target, tArray,mid,top) ... return bSearch(target, tArray,bot,mid); 8. Complexity of Binary Search ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 22
Provided by: julie57
Category:
Tags: bot | recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • Write a simple recursive routine
  • List key features of recursive routines
  • Give natural examples of recursion
  • Derive a recurrence relationship from a
    complexity analysis of a recursive routine.

2
Example Printing an Integer
  • Write a routine to print integer
  • Only putChar(char c) is available
  • Issues
  • Breaking out the digits
  • nextD number 10
  • number number / 10
  • Ensuring its printed in the right order
  • Solution
  • Use an array how big does it need to be?
  • Insert the characters
  • Print out in reverse order

3
Recursive Solution
  • An n-digit integer is a (n-1) digit integer
    followed by a single digit
  • void printInt(int i)
  • if (i gt 10)
  • printInt(i/10)
  • putChar(i10 '0')

4
Features of a Recursive Routine
  • Has a parameter
  • At least one base case
  • A simple problem, directly solved
  • At least one recursive case
  • Solves a complex sub-problem by calling the
    routine again
  • The sub-problem is
  • Like the problem represented by the parameter
  • Less complex than the parameter

5
Greatest Common Divisor
  • Examples
  • GCD(10,15) 5
  • GCD(10, 9) 1
  • Definition of GCD
  • If A GN and B GM,
  • where M N have no common factors
  • G is GCD(A,B)
  • Key insight
  • If ABq r, then GCD(A,B) GCD(B,r)
  • The parameters are smaller

6
GCD
  • int gcd(int f, int s)
  • if (f 0)
  • return s
  • else if (s gt f)
  • return gcd(s f,f)
  • else
  • return gcd(f s,s)

7
Searching a sorted array
  • int bSearch(int target, int tArray, int bot,
    int top)
  • if (bot 1 top)
  • return (targettArraylower? lower -1)
  • else
  • int mid (bot top)/2
  • if (target gt tArraymid)
  • return bSearch(target, tArray,mid,top)
  • else
  • return bSearch(target, tArray,bot,mid)

8
Complexity of Binary Search
  • Time to search an array of size n be T(n)
  • T(1) c
  • T(2) k T(1) k c
  • T(4) k T(2) k k c
  • T(n) k T(n / 2)
  • k k T(n/4)
  • k k k T(n/8)
  • if n 2p there will be p lots of k
  • p is log2 n
  • T(n) ? k log2 n c
  • T(n) ? k log2 n

9
Recursive Data Recursive Code
  • An array of size 2n is made from two arrays of
    size n
  • A linked list
  • Is NULL or a head item followed by the rest.
  • The rest of the list is also a list
  • A tree structure
  • Is either empty (null) or
  • A node with two tree structures

3
9
1
6
8
4
10
Routine to search a tree
  • boolean find(Key k, Tree t)
  • if (t null)
  • return false
  • else if (t.data k)
  • return true
  • else if (t.data gt k)
  • return find(k, t.left)
  • else
  • return find(k, t.right)

11
Complexity Analysis tree search
  • Worst case item not in the tree
  • Basic operation data comparison
  • T(0) 0 - empty tree
  • T(1) 2 T(0) - tree with 1 level
  • T(2) 2 T(1) - tree with 2 levels
  • T(l) 2 T(l-1)
  • T(l) 2 2 2 T(0)
  • T(l) 2l
  • A tree with l levels can hold 2l1 items
  • i.e. search N items in 2log2 N comparisons

12
Routine to print a tree
  • void printTree(Tree t)
  • if (t ! null)
  • printTree(t.left)
  • printItem(t.data)
  • printTree(t.right)
  • Look at the correspondence between the code and
    the definition of the data

13
Complexity Analysis print tree
  • Basic operation printItem
  • The number of prints is obvious
  • Formally
  • T(0) 0 - empty tree
  • T(1) 1 - tree with 1 level
  • T(2) 1 2T(1) - tree with 2 levels
  • T(l) 1 2T(l-1)
  • T(l) 1 2(1 2 (1 2T(l-2)))
  • T(l) 1 2 4 8 2l-1 2l-1
  • A tree with l levels can hold 2l1 items
  • i.e. print N items in N printItems

14
Parsing I
  • ParseStatement()
  • switch (currentWord)
  • if ParseIf()
  • break
  • switch ParseSwitch()
  • break
  • while ParseWhile()
  • break

15
Parsing II
  • ParseIf()
  • check(if)
  • check(()
  • ParseCondition()
  • check ())
  • ParseStatement()
  • if (lookFor(else))
  • ParseStatement()

16
Games Playing
  • FindBestMove (player, Board)
  • res loss
  • for eachPossibleMove(player, Board)
  • MakeMove(player, Board)
  • result FindBestMove(!player, Board)
  • if (result loss)
  • return win
  • else if (res ! win result
    draw)
  • res draw
  • return res

17
Searching a Maze
  • String FindRoute(x, y)
  • if (Marked(x,y)) return
  • if (mazex,y is exit)
  • return (x,y)
  • else
  • SetMarked(x,y)
  • for All neighbouring cells
  • route FindRoute(a,b)
  • if (route ! ) return
    (x,y) route
  • return

18
Sorting
  • quickSort(theArray, lower,upper)
  • if (lower gt upper)
  • return
  • split partition(theArray, lower, upper)
  • quickSort(theArray, lower,split-1)
  • quickSort(theArray, split1, upper)

19
Complexity of quicksort
  • A partition of an n-array is linear ? k n
  • Let time for quicksort be T(n)
  • T(1) c
  • T(2) k n 2T(1) k n 2c
  • T(4) k n 2 T(2) k n k n c
  • T(n) k n 2 T(n / 2)
  • k n 2 (k n/2 2 T(n/4))
  • if n 2p there will be p lots of k n
  • p is log2 n
  • T(n) ? k n log2 n c
  • T(n) ? k n log2 n

20
Is Recursion Essential?
  • A recursive routine can be done iteratively
  • Some routines are easy to convert
  • e.g. binary search
  • Some routines are more difficult
  • E.g. quicksort
  • Need to keep a stack of partial results
  • Recursive routines are often shorter
  • Recursive routines may be more expensive
  • Additional housekeeping
  • Additional memory

21
Summary Recursion
  • A powerful technique
  • Simple programs to solve complex problems
  • Games
  • Parsing
  • Natural for recursive data
  • Trees
  • Graphs
  • Complexity analysis recurrence relations
  • can calculate Cost(n) from the cost of handling a
    smaller number of items
Write a Comment
User Comments (0)
About PowerShow.com