MEG 202 · Basic Software Engineering
Our notes
2 min readRecursion
A recursive algorithm solves a problem by calling itself on a smaller version of the same problem. Every recursive algorithm needs:
- A base case that is solved directly (stops the recursion).
- 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
| Recursion | Iteration (loops) |
|---|---|
| often shorter, closer to the mathematical definition | usually faster, less memory |
| uses the call stack | no stack growth |
| natural for trees and divide-and-conquer | natural 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
-
Best pick Introduction to Computer Science, 4.3 Parallel Programming Models
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 4.3 Parallel Programming Models, CC BY-NC-SA 4.0 · licence
-
Introduction to Computer Science, 3.4 Algorithmic Paradigms
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 3.4 Algorithmic Paradigms, CC BY-NC-SA 4.0 · licence
-
Recursion (computer science)
(opens en.wikipedia.org in a new tab)
Wikipedia, Recursion (computer science), CC BY-SA 4.0 · licence
-
Parallel computing
(opens en.wikipedia.org in a new tab)
Wikipedia, Parallel computing, CC BY-SA 4.0 · licence
-
NOUN CIT 216: Fundamentals of Data Structures
NOUN course book (PDF, about 3.3 MB). Arrays, records, lists, stacks, queues, sorting and searching.
(opens nou.edu.ng in a new tab)
-
NOUN CIT 237: Programming and Algorithms
NOUN course book (PDF, about 1.6 MB). Designing and writing algorithms.
(opens nou.edu.ng in a new tab)
-
OpenStax, Introduction to Computer Science (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Computer Science (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
Concurrency Vs Parallelism!
4 min · ≈ 18 MB · about ₦10
(opens youtube.com in a new tab)
-
Recursion in C
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
Sequential vs Parallel vs Distributed Computing Explained Simply
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
Amdahl's Law Explained: Why Parallel Computing Has Limits 🚀
4 min · ≈ 18 MB · about ₦10
(opens youtube.com in a new tab)
-
[T3-2020 Functional Programming and Parallel Programming] Lecture 4: Pattern Matching and Recursion
1 h 4 min · ≈ 288 MB · about ₦162
(opens youtube.com in a new tab)
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.