OCR H446, section 2.2.2 (c)
Problem decomposition means breaking a large, complex problem down into smaller, simpler sub-problems, and keeping going until each is simple enough to solve directly. The solutions are then combined to solve the whole problem.
"How do you eat an elephant? One bite at a time."
Why decompose?
- Makes complex problems manageable. A small problem is easier to understand and solve than a huge one.
- Different people can work on different parts at the same time.
- Each part can be tested separately, so faults are easier to locate.
- Parts can be reused in other programs.
- Progress is easier to measure and to plan.
- Parts can be changed or replaced without rewriting everything.
- It's the first step towards sub-procedures (2.1.3 (d)) and modular programming (2.2.1 (d)).
How to decompose
- State the whole problem in one sentence.
- Split it into its main tasks.
- Split each task into smaller tasks, and repeat.
- Stop when each piece is simple enough to code directly (a few steps, one clear job).
- Decide how the pieces connect: what each receives and returns.
- Draw it as a hierarchy (structure) diagram.
Tips
- Look for repeated actions: they become one reusable sub-problem.
- Split by function (what each part does), data or stages of a process.
- Each part should have one clear job and few links to others.
Example: a quiz program
Each leaf is a small piece of work that can be written and tested alone.
def run_quiz():
questions = load_questions()
score = 0
for q in questions:
if ask(q):
score = score + 1
show_result(score, len(questions))
Decomposition and other methods
- Abstraction (2.2.2 (e)) tells you what each part does without the detail of how.
- Concurrency (2.1.5): independent sub-problems can be solved at the same time.
- Divide and conquer (2.2.2 (d)) is a special form, which splits a problem into similar smaller versions of itself and solves them recursively.
- Recognition (2.2.2 (b)): each sub-problem may be a known type.
Worked example
Task: decompose the problem "run an online shop checkout".
- Whole problem: let a customer pay for the items in their basket.
- Main tasks:
- Show the basket.
- Collect the delivery details.
- Take payment.
- Confirm the order.
- Further splitting:
- Show the basket: get the items; calculate the subtotal; add delivery; apply discount codes; display.
- Collect delivery details: ask for name and address; validate the postcode; save.
- Take payment: choose the method; send details to the payment provider; handle success or failure.
- Confirm the order: update stock; save the order; email a receipt.
- Benefits here:
- A payment specialist can build "take payment" while another person writes "confirm the order".
- "Validate postcode" can be reused on the sign-up page.
- If the payment provider changes, only that part is replaced.
- "Calculate subtotal" can be tested with simple numbers.
Exam tip: show the levels of breakdown, and stop each branch at a task that is clearly small enough to code. Name the benefits that apply to the scenario (parallel work, separate testing, reuse). Don't confuse decomposition (splitting into different parts) with divide and conquer (splitting into similar smaller problems).
Check yourself
What is problem decomposition?
Breaking a large problem into smaller sub-problems, repeatedly, until each is simple enough to solve directly, and then combining the solutions.
Give three benefits of decomposing a problem
Any three of: easier to understand; parts can be worked on by different people at once; parts can be tested separately; parts can be reused; progress is easier to plan; one part can be changed without affecting others.
How do you know when to stop decomposing?
When each piece is simple enough to code directly, with one clear job.
What can be drawn to show how a problem has been decomposed?
A hierarchy (structure) diagram, with the whole problem at the top and the smaller parts below.