A levels Computer Science · Theory of computation
Abstraction and decomposition
1What is abstraction?
Abstraction means removing unnecessary detail to focus on what matters for the problem.
- A London Underground map shows stations and connections, but not true distances or streets, because a passenger only needs the order of stops and where to change.
- A computer model of traffic keeps cars, roads and lights, but ignores the colour of each car.
- Variables, subroutines and classes are abstractions: they hide how something works behind a name.
Check yourself
Why does the London Underground map leave out the true distances between stations?
Show the answer
Passengers only need the order of stations and the connections
Remove unnecessary detail.
2Kinds of abstraction
| Kind | Meaning | Example |
|---|---|---|
| representational | removing detail to get a simpler model | a map |
| by generalisation | grouping things by what they share | a Vehicle class for cars and vans |
| procedural | naming a sequence of steps | calling sort(list) |
| functional | using a function without knowing its method | using square root |
| data abstraction | using data through operations, hiding how it is stored | a stack's push and pop |
| information hiding | hiding details that other parts do not need | private attributes |
| problem reduction | turning a problem into one already solved | modelling a map as a graph |
Check yourself
Using a stack through push and pop without knowing whether it is stored in an array is an example of what?
Show the answer
Data abstraction
Hide the storage.
3Decomposition and top-down design
Decomposition breaks a large problem into smaller sub-problems, each easier to solve. Keep going until each part is simple enough to code as one subroutine. This is top-down design, shown with a structure chart.
A structure chart. The whole problem is broken into sub-problems, which are broken down again until each part is simple to code.
Benefits:
- each part can be written and tested separately;
- different people can work on different parts;
- parts can be reused in other programs.
Composition is the opposite: combining the solved parts to build the whole solution.
Check yourself
What is decomposition?
Show the answer
Breaking a problem into smaller sub-problems
Divide it up.
4Thinking ahead
Before writing code, plan:
- inputs and outputs: what data comes in and what must come out;
- preconditions: what must be true before a subroutine runs (for example, a binary search needs a sorted list). Stating them means the subroutine need not check, and makes it reusable;
- caching: storing results that may be needed again so they need not be recalculated (for example, a browser caching web pages). This saves time but uses memory, and cached data may go out of date;
- reusable components: using existing, tested libraries saves time and reduces errors.
Check yourself
What is a precondition?
Show the answer
Something that must be true before a subroutine runs
Assumed on entry.
5Thinking logically and concurrently
Thinking logically means identifying the decision points in a solution and the conditions that decide which way it goes, so the order of steps is correct.
Thinking concurrently means spotting parts of a problem that can happen at the same time.
| Benefits of concurrency | Drawbacks |
|---|---|
| faster on multi-core processors | some tasks depend on others and must wait |
| better use of the processor | two processes may change the same data (needs locking) |
| programs stay responsive while working | harder to design and debug |
a game can load the next level while the player is still on the current one.
Check yourself
What is a risk when two concurrent processes update the same variable?
Show the answer
The result can be wrong if they interfere with each other
Race condition.
6Problem-solving methods
| Method | Idea | Example |
|---|---|---|
| problem recognition | spot that a problem matches a known type | a route-finding problem is a graph problem |
| backtracking | try a path; if it fails, go back and try another | solving a maze or a sudoku |
| heuristics | use a rule of thumb for a good-enough answer quickly | nearest-city-first in travelling salesman |
| data mining | search large data sets for patterns | shopping habits |
| performance modelling | use maths or simulation to predict behaviour | testing a website under heavy load |
| visualisation | show data or a problem as a picture | graphs and charts |
| pipelining | start the next task before the last finishes | fetch, decode and execute overlapping |
Automation puts the model into action: turning the abstraction into an algorithm and code that a computer runs.
Check yourself
Which method tries a route and goes back to the last decision point if it fails?
Show the answer
Backtracking
Undo and retry.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera