Docsity

Recursion Cheat Sheet AS/A level Computer Science, Cheat Sheet of Computer science

Toggle sidebar
bg1
Recursion
Recursion is a programming construct in which a subroutine calls itself during its execution. This continues until a certain condition - called the
stopping condition - is met, at which point the recursion stops.
-Without the stopping condition (also know as the base case or edge case ) the result would be an endless loop of execution. This could
lead to stack overflow errors.
- In the stopping condition, a value is returned, but for all other input values, the routine must call itself – i.e. the parameter must cause
the function to call itself until the parameter meets the stopping condition.
e.g
function addNums(numbers)
if length(numbers) > 1 then
numbers[0] = numbers[0] + addNums(numbers[1:])
print(numbers[0])
return numbers[0]
else:
print(numbers[0])
return numbers[0]
endif
endfunction
marks = [3,6,2,8]
total = addNums(marks)
print("Total = ", total)
numbers length(numbers
)
numbers[0] Output
[3,6,2,8] 4 3 + addNums([6,2,8])
[6,2,8] 3 6+ addNums([2,8])
[2,8] 2 2+ addNums([8])
[8] 1 8 8
[2,8] 2 2+8 10
([6,2,8] 3 6+(2+8) 16
[3,6,2,8] 4 3+(6+(2+8)) 19
(( ( Total =
Base
casev
Subroutine call within
the subroutine
Here is a trace table of the function call. Once the base case is
reached (when numbers[0] is 8 and length(numbers) is not >1 ) the
subroutine unwinds back through the calls.
Iteration Recursion
Lines of code are executed
repeatedly based on the value of a
Boolean condition
When a function that is defined in
terms of itself is called.
Uses a single stack frame Uses multiple stack frames
Generally faster because of less
overhead
Slower due to function call overhead
*
Risk of infinite loops if conditions are
wrong
Risk of stack overflow if recursion
depth is too large
Examples of use: looping through an
array, input validation
Examples of use: calculating
Fibonacci numbers, tree traversal,
DNS lookup
*Overhead: any combination of excess or indirect computation time,
memory etc. In recursion, there is an overhead because as more calls
Pros: Cons:
For the
programmer:
Easier for some problems:
some programs e.g divide
and conquer lend themselves
to recursion.
Concise: programs can often
be expressed in a more
concise way than using
iteration, especially for
structures like trees.
More readable: because
recursive functions are often
shorter, it makes them more
readable than the equivalent
iterative loop.
Functional programming:
some functional programming
languages like Haskell and
Lisp actually favour recursion
to iteration.
Difficult: if bases cases are
logically incorrect or
recursion depth is too deep, it
can lead to stack overflow or
infinite recursion.
Debugging: tracing and
debugging recursive
algorithms can be tricky,
especially in deep recursion.
For the
computer
system:
More elegant for some
problems: e.g backtracking,
tree/depth-first traversal.
Parallel processing: easier
to implement parallel
processing on recursive
algorithms than iterative
algorithms. (Loops are
inherently sequential,
whereas many recursive
algorithms like divide and
Stack overflow: if recursion
depth is too large, the system
could run out of stack space.
Function call overhead: every
recursive call requires
pushing and popping from the
call stack, making it slower
than iteration in some cases.
Higher memory usage: each
call adds a new stack frame,
which increases memory
addNums[3,6,2,
8]
addNums[6,2,8]
addNums[3,6,2,8
]
addNums[2,8
addNums[6,2,8]
addNums[3,6,2,8]
addNums[8]
addNums[2,8]
addNums[6,2,8]
addNums[3,6,2,8]
addNums[2,8
addNums[6,2,8]
addNums[3,6,2,8]
addNums[6,2,8]
addNums[3,6,2,8]
addNums[3,6,2,
8]
Call stack:
pf2

Partial preview of the text

Download Recursion Cheat Sheet AS/A level Computer Science and more Cheat Sheet Computer science in PDF only on Docsity!

Recursion

Recursion is a programming construct in which a subroutine calls itself during its execution. This continues until a certain condition - called the

stopping condition - is met, at which point the recursion stops.

- Without the stopping condition (also know as the base case or edge case ) the result would be an endless loop of execution. This could

lead to stack overflow errors.

- In the stopping condition, a value is returned, but for all other input values, the routine must call itself – i.e. the parameter must cause

the function to call itself until the parameter meets the stopping condition.

e.g

function addNums(numbers)

if length(numbers) > 1 then

numbers[0] = numbers[0] + addNums(numbers[1:])

print(numbers[0])

return numbers[0]

else:

print(numbers[0])

return numbers[0]

endif

endfunction

marks = [3,6,2,8]

total = addNums(marks)

print("Total = ", total)

numbers length(numbers

numbers[0] Output

[3,6,2,8] 4 3 + addNums([6,2,8])

[6,2,8] 3 6+ addNums([2,8])

[2,8] 2 2+ addNums([8])

[8] 1 8 8

[2,8] 2 2+8 10

[6,2,8] 3 6+(2+8) 16

[3,6,2,8] 4 3+(6+(2+8)) 19

Total =

Base

casev

Subroutine call within

the subroutine

Here is a trace table of the function call. Once the base case is

reached (when numbers[0] is 8 and length(numbers) is not >1 ) the

subroutine unwinds back through the calls.

Iteration Recursion

Lines of code are executed

repeatedly based on the value of a

Boolean condition

When a function that is defined in

terms of itself is called.

Uses a single stack frame Uses multiple stack frames

Generally faster because of less

overhead

Slower due to function call overhead

Risk of infinite loops if conditions are

wrong

Risk of stack overflow if recursion

depth is too large

Examples of use: looping through an

array, input validation

Examples of use: calculating

Fibonacci numbers, tree traversal,

DNS lookup

*Overhead: any combination of excess or indirect computation time,

memory etc. In recursion, there is an overhead because as more calls

Pros: Cons:

For the

programmer:

Easier for some problems: some programs e.g divide and conquer lend themselves to recursion. Concise: programs can often be expressed in a more concise way than using iteration, especially for structures like trees. More readable: because recursive functions are often shorter, it makes them more readable than the equivalent iterative loop. Functional programming: some functional programming languages like Haskell and Lisp actually favour recursion to iteration. Difficult: if bases cases are logically incorrect or recursion depth is too deep, it can lead to stack overflow or infinite recursion. Debugging: tracing and debugging recursive algorithms can be tricky, especially in deep recursion.

For the

computer

system:

More elegant for some problems: e.g backtracking, tree/depth-first traversal. Parallel processing: easier to implement parallel processing on recursive algorithms than iterative algorithms. (Loops are inherently sequential, whereas many recursive algorithms like divide and Stack overflow: if recursion depth is too large, the system could run out of stack space. Function call overhead: every recursive call requires pushing and popping from the call stack, making it slower than iteration in some cases. Higher memory usage: each call adds a new stack frame, which increases memory addNums[3,6,2, 8] addNums[6,2,8] addNums[3,6,2,8 ] addNums[2, addNums[6,2,8] addNums[3,6,2,8] addNums[8] addNums[2,8] addNums[6,2,8] addNums[3,6,2,8] addNums[2, addNums[6,2,8] addNums[3,6,2,8] addNums[6,2,8] addNums[3,6,2,8] (^) addNums[3,6,2, 8]

Call stack:

19