impera Open Impera

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?

  • The distances are kept secret for security reasons

  • It would be too expensive to print at the true scale

  • The distances between stations change every day

  • Passengers only need the order of stations and the connections

Show the answer

Passengers only need the order of stations and the connections

Remove unnecessary detail.

2Kinds of abstraction

KindMeaningExample
representationalremoving detail to get a simpler modela map
by generalisationgrouping things by what they sharea Vehicle class for cars and vans
proceduralnaming a sequence of stepscalling sort(list)
functionalusing a function without knowing its methodusing square root
data abstractionusing data through operations, hiding how it is storeda stack's push and pop
information hidinghiding details that other parts do not needprivate attributes
problem reductionturning a problem into one already solvedmodelling 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?

  • Concurrency

  • Data abstraction

  • Problem reduction

  • Decomposition

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?

  • Removing unnecessary detail from a problem

  • Breaking a problem into smaller sub-problems

  • Storing the sub-problems in a stack

  • Running several tasks at the same time

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?

  • A comment written at the top of the code

  • A type of loop that checks at the start

  • The output returned by a subroutine

  • Something that must be true before a subroutine runs

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 concurrencyDrawbacks
faster on multi-core processorssome tasks depend on others and must wait
better use of the processortwo processes may change the same data (needs locking)
programs stay responsive while workingharder to design and debug
Example

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?

  • The program always runs more slowly, but the result stays correct

  • The variable becomes a constant that neither process can change

  • The result can be wrong if they interfere with each other

  • Nothing can go wrong, because each process has its own copy

Show the answer

The result can be wrong if they interfere with each other

Race condition.

6Problem-solving methods

MethodIdeaExample
problem recognitionspot that a problem matches a known typea route-finding problem is a graph problem
backtrackingtry a path; if it fails, go back and try anothersolving a maze or a sudoku
heuristicsuse a rule of thumb for a good-enough answer quicklynearest-city-first in travelling salesman
data miningsearch large data sets for patternsshopping habits
performance modellinguse maths or simulation to predict behaviourtesting a website under heavy load
visualisationshow data or a problem as a picturegraphs and charts
pipeliningstart the next task before the last finishesfetch, 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?

  • Caching

  • Data mining

  • Backtracking

  • Pipelining

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