OCR H446, section 2.2.2 (b)
Problem recognition has two parts.
- Recognising the problem. Realising that there is a problem to be solved, and working out exactly what it is from the description: what the system must do, what its inputs will be and what it must output. Then deciding whether it can be solved by computational methods at all (2.2.2 (a)).
- Recognising its type. Matching it to a known kind of problem, so that you can use a method that is already known to work. Most "new" problems turn out to be old problems in disguise.
Why recognise problems?
- Saves time: you reuse a tested algorithm instead of inventing one.
- Better solutions: standard algorithms are often well optimised.
- Fewer errors.
- Helps you decide whether a problem is solvable (2.2.2 (a)).
- Lets you use the right data structure.
Common problem types and their usual solutions
| If the problem is about... | It's probably a... | Typical solution |
|---|---|---|
| Finding an item in a collection | Searching problem | Linear search; binary search if sorted |
| Putting items in order | Sorting problem | Merge sort, quick sort, insertion sort |
| The shortest or cheapest route | Shortest path problem | Dijkstra's algorithm, A* |
| Visiting every node of a network or tree | Traversal problem | Depth-first or breadth-first |
| Choosing the best from many combinations | Optimisation problem | Heuristics, dynamic programming |
| Trying choices and undoing bad ones | Constraint / search problem | Backtracking |
| Looking up values by a key | Lookup problem | Dictionary / hash table |
| Doing something in a first-come order | Queueing problem | Queue |
| Reversing or undoing | Last-in-first-out problem | Stack |
| Matching patterns in text | Pattern matching | Regular expressions |
| Grouping similar items | Classification / clustering | Data mining, machine learning |
| Splitting into similar smaller problems | Divide and conquer | Recursion (2.2.2 (d)) |
Recognising by features
Ask questions such as:
- What is the input and what is wanted as output? (a list to order; a map and two points)
- What structure is the data? (a list, a tree, a network of connections)
- Is it a question of finding, ordering, counting, choosing or routing?
- Have I seen a similar problem before? Could this be a version of one I know?
- Does it repeat in a smaller form? (a hint of recursion)
Same problem, different disguises
| Real problem | Underlying problem |
|---|---|
| A satnav finding the quickest route | Shortest path on a weighted graph |
| Routing data packets across the Internet | Shortest path on a weighted graph |
| Finding the fewest flights between two airports | Shortest path (unweighted: breadth-first) |
| Putting exam scripts in order of candidate number | Sorting |
| Finding a name in a phone book | Searching (binary, since it's sorted) |
| Printing jobs waiting for a printer | Queue |
| Undo in a text editor | Stack |
| Checking a Sudoku | Constraint problem |
| Solving a Sudoku or a maze | Backtracking |
What if no known type fits?
- Break it into parts (2.2.2 (c)) and recognise each part.
- Simplify it (abstraction, 2.2.2 (e)).
- Look for a similar problem and adapt its solution.
- If it can't be solved exactly, use a heuristic.
Worked example
Task: a delivery company wants to find the order in which a van should visit 8 addresses, to keep the total distance short. Recognise the problem and suggest a method.
- Recognition: it's a travelling salesman problem (visiting every point once and returning), a well-known optimisation problem.
- Is it tractable? Trying every order means routes, which is fine for a computer, so an exact solution by trying all routes is possible. For 50 addresses there are about orders, which is intractable, so a heuristic such as nearest neighbour (always go to the closest unvisited address) would be used instead. It's fast, but doesn't guarantee the shortest route.
- Data structure: a weighted graph with addresses as nodes and distances as the weights.
def nearest_neighbour(dist, start):
n = len(dist)
route = [start]
left = set(range(n)) - {start}
while left:
here = route[-1]
nxt = min(left, key=lambda c: dist[here][c])
route.append(nxt)
left.remove(nxt)
return route
By recognising the problem as a known one, we got a method, an idea of how hard it is, and the right data structure without starting from scratch.
Exam tip: start from the scenario: say what the system must do, its inputs and its outputs, and whether it can be solved computationally. Then name the known problem that it matches, and say why (for example "finding the shortest route on a map is a shortest path problem on a weighted graph"). Then say which algorithm or structure it leads to.
Check yourself
What is problem recognition?
Realising there is a problem to solve and working out exactly what it is (what the system must do, its inputs and outputs, and whether it can be solved computationally), then matching it to a known type so that a known solution can be used.
Give two benefits of recognising a problem as a known type
Any two of: saves time by reusing a tested algorithm; solutions are often well optimised; fewer errors; helps choose the right data structure; shows whether the problem is tractable.
A route planner finds the quickest way between two towns. What type of problem is this?
A shortest path problem on a weighted graph.
What could you do if a problem doesn't match any known type?
Break it into smaller parts and recognise each one, simplify it using abstraction, adapt a similar known solution, or use a heuristic.