Вы находитесь на странице: 1из 2

ICT 2122 Design and Analysis of Algorithms

Recursive Algorithm
Recursive function calls itself. A function is recursive if, it calls itself, it has termination condition recursive calls take smaller parameter value

Depth of a Recursion: Number of recursive calls used to complete the execution. Advantages: Simplicity of code Easy to understand

Disadvantages: Memory Speed Possibly redundant work

Analysis of Recursive Function Space: An amount of memory is need to store: A return address. Data used by the procedure.

This information is stored in a block of memory called an activation record. Activation records are placed on a stack.
return address n=1 Activation record return address n=1 return address n=2 return address n=3 return address n=4 Stack Department of Physical Science VCUJ

function fact(n) if n=0 then return 1 else return n*fact(n-1) endif endfact

ICT 2122 Design and Analysis of Algorithms

Time: When a procedure is called, it takes time to create an activation record and put it on the stack. When a procedure finishes, it takes time to remove the activation record from the stack and resume execution from the return address stored in that activation record. Recurrence relations are useful for expressing the running times of recursive algorithms. For example: function fact(n) if n=0 then return 1 else return n*fact(n-1) endif endfact Recurrence relation: = c-constant time T(n)=T(n-1)+c 1, 1 + , = 0 > 0
base case/termination condition recursive call/recursive case

Department of Physical Science

VCUJ

Вам также может понравиться