Docsity

Recursion- Data structure, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Recursion- Data structure and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Lecture 4: Recursion

Kamilla Klonowska

Fractal: Sierpinski triangle

  • The following sequence of triangles is called the Sierpinski triangle.
  • It is a fractal.
  • Write an algorithm that constructs the Sierpinski triangle sequence.

Fractal: Pythagoras tree

  • The following sequence of squares is called the Pythagoras tree.
  • It is a fractal.
  • Write an algorithm that constructs the Pythagoras tree sequence Order 0 Order 1 Order 2 Order 3

Nesting of triangles

  • Figure below represents a nesting of triangles.
  • Describe the figure and write an algorithm that can construct it.

Classic Tower of Hanoi problem

  • Problem
    • There are 3 pegs and n disks of different sizes
    • Goal is to move n disks from original peg to one of the empty peg
  • Rules:
    • Only the top disk on a peg can be moved to another peg
    • A larger disk can not be placed on top of a smaller disk

Recursive humor

  • A common joke is the following "definition" of recursion. [Wikipedia] Recursion See "Recursion“
  • A variation on this joke is: Recursion If you still don't get it, see: "Recursion" (which actually does terminate, as soon as the reader "gets it")

Steps to Design a Recursive Algorithm

  • Base case(s)
    • There must be at least one base case, for a small value of n, that can be solved directly
  • Make progress (toward the base case(s))
    • A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)
  • Combine the solutions of the smaller problems in such a way as

to solve the larger problem

Proof by mathematical induction

• For any integer N ≥ 1, prove that the sum

1 + 2 + 3 + … + N = N(N+1)/

• Using induction method

  • Base cases
    • Check it is true for small parameters: N = 1 in this case
  • Induction
    • Suppose it is true for some arbitrary case, i.e. k
    • and show it is also true for the next case, i.e. k + 1

Basic recursion

  • Factorial 0! = 1 n! = n (n – 1)! for n > 0
  • Java method implementation public static int factorial ( int n ) { if (n <= 0 ) return 1; else return n * factorial( n – 1); }
  • Recursive implementation is direct translation of recursive

definition

Ex: Printing numbers in any base

  • Bases
    • Binary, octal, decimal, hexa-decimal
  • Print a nonnegative number N in decimal form (suppose we do not have a number output function available) - Print one digit at a time, eg. 1369: first 1, then 3, then 6 and then 9 - It is difficult to print the first digit  better to print the last digit

How it works?

  • Understand how a function call works
    • Stack is used to keep an activation record
    • It contains relative information: values of parameters, local variables, return address
    • When returned: activation record is removed from stack, and program control is transferred back to calling position
  • Recursive call
    • Each recursive call saves an activation record , and program control is transferred.
  • The stack size is limited. Too many recursion may overflow the stack
  • Any recursive method can be removed by using a stack and iteration

Fibonacci numbers

  • Fibonacci numbers are defined by

F

0

= 1, F

1

= 1, F

k

= F

k- 1

+ F

k- 2

  • Prove that

F

k

k

, for k ≥ 1

  • Proof Base cases k = 1, F 1 = 1 < (5/3) k = 5/ k = 2, F 2 = F 1 + F 0 = 2 < (5/3) k = (5/3) 2 = 25/ Induction Suppose it is true for k: Fk < (5/3) k show, Fk+1 < (5/3) k+

Running time analysis on recursive method

  • Analyze running time for a recursive method
    • Get a recursive function on T(n)
  • Let T(n) be the running time for fib1( ): T(n) = 1, n =0, 1 T(n) = T(n-1) + T(n-2) + 1, n > 1 T(n) = Fn + 1 Fk > (3/2) k , for k > 4 Fk < (5/3) k , for k ≥ 1 (3/2) n +1 < T(n) < (5/3) n +1 for n > 4
  • So T(n) grows exponentially. T(n) = O((5/3) n )
  • The recursive method for Fibonacci number is bad