UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Recursion and parallelism

Recursive algorithms with base cases, tracing, recursion versus iteration, and parallel computing with Amdahl's law.

Our notes

2 min read

Recursion

A recursive algorithm solves a problem by calling itself on a smaller version of the same problem. Every recursive algorithm needs:

  1. A base case that is solved directly (stops the recursion).
  2. A recursive case that reduces the problem toward the base case.

Example: factorial

n! = n × (n − 1)!, with 0! = 1.
    int fact(int n) {
        if (n == 0) return 1;
        return n * fact(n - 1);
    }
Trace fact(4): fact(4) = 4 × fact(3) = 4 × 3 × fact(2) = 4 × 3 × 2 × fact(1) = 4 × 3 × 2 × 1 × fact(0) = 4 × 3 × 2 × 1 × 1 = 24.
Each call waits on a call stack until the call below it returns; too many nested calls cause a stack overflow.

More examples

  • Fibonacci: F(n) = F(n − 1) + F(n − 2), F(0) = 0, F(1) = 1. The naive recursion repeats work and takes exponential time; a loop or storing results (memoisation) is far faster.
  • Towers of Hanoi: move n discs: move n − 1 to the spare peg, move the largest, move n − 1 on top. Moves needed: 2ⁿ − 1 (3 discs: 7 moves; 64 discs: about 1.8 × 10¹⁹).
  • Binary search and merge sort (divide and conquer).
  • Traversing trees and directories.

Recursion versus iteration

RecursionIteration (loops)
often shorter, closer to the mathematical definitionusually faster, less memory
uses the call stackno stack growth
natural for trees and divide-and-conquernatural for simple repetition

Any recursive algorithm can be rewritten iteratively (sometimes with an explicit stack).

Parallelism

Parallel computing splits a task into parts that run at the same time on several processors or cores.

  • Types: bit-level, instruction-level (pipelining), data parallelism (the same operation on many data items: adding two large arrays; GPUs excel at this), task parallelism (different tasks at once).
  • Hardware: multi-core CPUs, GPUs, clusters and supercomputers (used for CFD and finite element analysis with millions of elements).
  • Issues: dividing the work evenly (load balancing), communication between processors, race conditions (two processes updating the same variable), synchronisation, deadlock.

Amdahl's law

If a fraction p of a program can be parallelised and it runs on n processors, the speed-up is:
S = 1 / ((1 − p) + p/n).
Example: p = 0.9, n = 10: S = 1 / (0.1 + 0.09) = 5.26, not 10. Even with infinite processors, S cannot exceed 1/(1 − p) = 10. The serial part limits the gain.

Exams usually ask

  • Write and trace a recursive function, identifying the base case.
  • Compare recursion and iteration.
  • Explain parallel processing and apply Amdahl's law.

Common mistake

Leaving out the base case (or making the recursive call not smaller), which gives infinite recursion and a stack overflow.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.