Graphs and route costs
A gallery inspection robot must get from its store S to charging point G, and there is more than one way through. A graph records which areas connect and what each connection costs, so routes are compared by adding numbers, not by eye.
Blue: the cheaper route, S → B → D → G.
Each part of the drawing has a name. Find each one on the graph as you read.
| Graph feature | Meaning in this gallery |
|---|---|
| Node / vertex | A location: S, A, B, C, D or G. |
| Edge | A direct connection that the robot can use. |
| Weight | Travel cost on that connection, in minutes. |
| Undirected edge | No arrow: the connection works in both directions with the same cost. |
A path cost is the sum of its edges
- In a directed graph, an arrow allows travel only in its direction.
- Costs may be distance, time or another quantity: use the meaning and units given.
- Here every weight is non-negative, as Dijkstra’s method requires.
Read the costs, not the drawing
A route algorithm needs the connections and their costs. The drawing’s page positions do not measure travel time; the written edge weights do.
1 What is the cost of the route S → A → C → D → G?
2 How many edges meet at node C?
3 Edge S–A is drawn longer than edge C–G. Does that make it cost more? Why?
- 12 + 2 + 4 + 2 = 10 minutes
- 24: to A, B, D, G
- 3no: S–A costs 2 and C–G costs 6; only the written weight gives the cost
Dijkstra’s algorithm
Trying every route works on a small map, but the number of routes explodes as a map grows. Dijkstra’s algorithm never lists them. It repeatedly settles the unvisited node with the lowest tentative cost. With non-negative edges, that cost cannot later be improved.
Each round, in seven lines
Read the seven lines once; the next table runs them for one node.
1current ← the unsettled node with the lowest cost 2mark current as settled 3FOR each unsettled neighbour n of current 4new ← cost[current] + weight of edge current–n 5IF new < cost[n] THEN 6cost[n] ← new 7pred[n] ← current
One round, line by line: settle C
C has the lowest cost on the frontier. Follow each line; the value it writes sits in its own column.
| Line | Statement that runs | current | n | new | cost[n] | pred[n] | Writes | What happens |
|---|---|---|---|---|---|---|---|---|
| 1 | current ← C | C | current C | lowest cost on the frontier (C 4 · B 5) | ||||
| lowest cost on the frontier (C 4 · B 5) | ||||||||
| 2 | mark C settled | C ✓ | current C ✓ | C’s cost 4 is now final | ||||
| C’s cost 4 is now final | ||||||||
| 3 | n ← A | test A is already settled: skip | ||||||
| test A is already settled: skip | ||||||||
| 3 | n ← B | B | n B | |||||
| 4 | new ← 4 + 1 | 5 | new 5 | |||||
| 5 | IF 5 < 5 | test false: B keeps 5 | ||||||
| test false: B keeps 5 | ||||||||
| 3 | n ← D | D | n D | |||||
| 4 | new ← 4 + 4 | 8 | new 8 | |||||
| 5 | IF 8 < ∞ | test true: cheaper, so update | ||||||
| test true: cheaper, so update | ||||||||
| 6 | cost[D] ← 8 | 8 | cost[n] 8 | |||||
| 7 | pred[D] ← C | C | pred[n] C | |||||
| 3 | n ← G | G | n G | |||||
| 4 | new ← 4 + 6 | 10 | new 10 | |||||
| 5 | IF 10 < ∞ | test true: cheaper, so update | ||||||
| test true: cheaper, so update | ||||||||
| 6 | cost[G] ← 10 | 10 | cost[n] 10 | |||||
| 7 | pred[G] ← C | C | pred[n] C | |||||
The whole run, one row per settled node
Each cell: the node’s cost, then its predecessor. Amber: cheaper this round. ✓ blue: settled. The marks on the graph give the same order.
Blue: the route found, S → B → D → G.
- 1Settle S at cost 0
- 2Settle A at cost 2
- 3Settle C at cost 4
- 4Settle B at cost 5
- 5Settle D at cost 7
- 6Settle G at cost 9
| S | A | B | C | D | G | Why this node | |
|---|---|---|---|---|---|---|---|
| start | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | S costs 0; every other node starts at ∞ |
| S costs 0; every other node starts at ∞ | |||||||
| settle S | ✓ 0 | 2 S | 5 S | ∞ | ∞ | ∞ | S 0 is the lowest; cheaper: A ∞ → 2, B ∞ → 5 |
| S 0 is the lowest; cheaper: A ∞ → 2, B ∞ → 5 | |||||||
| settle A | ✓ 0 | ✓ 2 S | 5 S | 4 A | ∞ | ∞ | A 2 is the lowest (others: B 5); cheaper: C ∞ → 4 |
| A 2 is the lowest (others: B 5); cheaper: C ∞ → 4 | |||||||
| settle C | ✓ 0 | ✓ 2 S | 5 S | ✓ 4 A | 8 C | 10 C | C 4 is the lowest (others: B 5); cheaper: D ∞ → 8, G ∞ → 10 |
| C 4 is the lowest (others: B 5); cheaper: D ∞ → 8, G ∞ → 10 | |||||||
| settle B | ✓ 0 | ✓ 2 S | ✓ 5 S | ✓ 4 A | 7 B | 10 C | B 5 is the lowest (others: D 8, G 10); cheaper: D 8 → 7 |
| B 5 is the lowest (others: D 8, G 10); cheaper: D 8 → 7 | |||||||
| settle D | ✓ 0 | ✓ 2 S | ✓ 5 S | ✓ 4 A | ✓ 7 B | 9 D | D 7 is the lowest (others: G 10); cheaper: G 10 → 9 |
| D 7 is the lowest (others: G 10); cheaper: G 10 → 9 | |||||||
| settle G | ✓ 0 | ✓ 2 S | ✓ 5 S | ✓ 4 A | ✓ 7 B | ✓ 9 D | G 9 is the lowest; no neighbour gets cheaper |
| G 9 is the lowest; no neighbour gets cheaper | |||||||
| Settle | Cost / predecessor updates | Frontier after update |
|---|---|---|
| S (0) | A: ∞ → 2 via S; B: ∞ → 5 via S | A 2, B 5 |
| A (2) | C: ∞ → 4 via A | C 4, B 5 |
| C (4) | D: ∞ → 8 via C; G: ∞ → 10 via C | B 5, D 8, G 10 |
| B (5) | D: 8 → 7 via B | D 7, G 10 |
| D (7) | G: 10 → 9 via D | G 9 |
| G (9) | No improvement | — |
Relax an edge: compare the new total with the old one
Recover the route from the predecessor chain
The costs say how far; the predecessors say which way. G ← D ← B ← S gives S → B → D → G, cost 9 minutes. The final costs are A 2, B 5, C 4, D 7, G 9.
1 Which node is settled straight after C? Why that one?
2 When B is settled, which cost changes? Show the comparison.
3 Suppose edge C–G cost 1 instead of 6. What is the cheapest route to G, and its cost?
- 1B: its cost 5 is lower than D 8 and G 10
- 2D: 5 + 2 = 7 < 8, so D becomes 7 via B
- 3S → A → C → G, cost 5
A* search
Dijkstra spreads out in every direction, even away from G. A* adds a guess of the distance still to go, so it heads for the goal. It chooses a frontier node using f = g + h: travelled cost g, estimated remaining cost h, and estimated total f.
Each node carries its estimate h. Read the list under the graph as g + h = f for each node, and follow the blue route back from G.
These h values are lower bounds on remaining travel time to G; h(G) = 0. At equal f, this trace chooses the node with larger g, then alphabetical order.
Each step, in seven lines
The same loop as Dijkstra, with two changes: choose by f, and stop as soon as the goal is chosen.
1current ← the frontier node with the lowest f = g + h 2IF current = goal THEN stop 3FOR each unexpanded neighbour n of current 4new ← g[current] + weight of edge current–n 5IF new < g[n] THEN 6g[n] ← new; pred[n] ← current 7f[n] ← g[n] + h[n]
One step, line by line: expand B
A and B both have f = 9. Follow each line for B; the value it writes sits in its own column.
| Line | Statement that runs | current | n | new | g[n] | f[n] | Writes | What happens |
|---|---|---|---|---|---|---|---|---|
| 1 | current ← B | B | current B | B and A tie at f = 9; B has the larger g (5) | ||||
| B and A tie at f = 9; B has the larger g (5) | ||||||||
| 2 | IF B = G | test false: B is not the goal | ||||||
| test false: B is not the goal | ||||||||
| 3 | n ← S | test S is already expanded: skip | ||||||
| test S is already expanded: skip | ||||||||
| 3 | n ← C | C | n C | |||||
| 4 | new ← 5 + 1 | 6 | new 6 | |||||
| 5 | IF 6 < ∞ | test true | ||||||
| test true | ||||||||
| 6 | g[C] ← 6; pred[C] ← B | 6 | g[n] 6 | |||||
| 7 | f[C] ← 6 + 5 | 11 | f[n] 11 | |||||
| 3 | n ← D | D | n D | |||||
| 4 | new ← 5 + 2 | 7 | new 7 | |||||
| 5 | IF 7 < ∞ | test true | ||||||
| test true | ||||||||
| 6 | g[D] ← 7; pred[D] ← B | 7 | g[n] 7 | |||||
| 7 | f[D] ← 7 + 2 | 9 | f[n] 9 | |||||
The whole run, one row per expanded node
Each row: the g values that got cheaper, then the frontier ordered by f. The blue route in the figure is the result.
| g updated | Frontier: node f | Why this node | |
|---|---|---|---|
| start | — | S 9 | only S is known: g = 0, f = 0 + 9 = 9 |
| only S is known: g = 0, f = 0 + 9 = 9 | |||
| expand S | A 2B 5 | B 9A 9 | S is the only node on the frontier |
| S is the only node on the frontier | |||
| expand B | C 6D 7 | D 9A 9C 11 | B and A tie at f = 9; B has the larger g (5) |
| B and A tie at f = 9; B has the larger g (5) | |||
| expand D | G 9 | G 9A 9C 11 | D and A tie at f = 9; D has the larger g (7) |
| D and A tie at f = 9; D has the larger g (7) | |||
| expand G | — | A 9C 11 | G and A tie at f = 9; G has the larger g (9); G is the goal: stop |
| G and A tie at f = 9; G has the larger g (9); G is the goal: stop | |||
| Expand | Updates to g and predecessor | Frontier: g + h = f |
|---|---|---|
| S | A: ∞ → 2 via S; B: ∞ → 5 via S | B: 5+4=9; A: 2+7=9 |
| B | C: ∞ → 6 via B; D: ∞ → 7 via B | D: 7+2=9; A: 2+7=9; C: 6+5=11 |
| D | G: ∞ → 9 via D | G: 9+0=9; A: 2+7=9; C: 6+5=11 |
| G | No improvement | A: 2+7=9; C: 6+5=11 |
How the run reaches G
- 1After S, both A and B have f = 9. B wins the stated tie: g(B) = 5, h(B) = 4.
- 2From B, D gets g = 5 + 2 = 7, then f = 7 + 2 = 9. From D, G gets g = 9 and f = 9.
- 3Stop when G is selected as the lowest-f node, not when it first appears on the frontier.
- 4Following predecessors gives S → B → D → G, actual cost 9 minutes. The h estimates are never added to the route cost.
1 After S is expanded, what are f(A) and f(B)?
2 When B is expanded, what f does C get?
3 Why does A* stop at G without expanding A or C?
- 1f(A) = 2 + 7 = 9; f(B) = 5 + 4 = 9
- 2g = 5 + 1 = 6, so f = 6 + 5 = 11
- 3G is selected with f = 9; A has f = 9 but loses the tie on g, and C has f = 11, so neither can give a cheaper route
Comparing Dijkstra and A*
Dijkstra and A* found the same route, S → B → D → G. Both keep the best known travelled costs and predecessors. What A* adds is information about the destination, and that is only as good as its heuristic.
Dijkstra settles all 6 nodes
A* expands 4: A and C are never expanded
Line the two methods up, one feature at a time.
| Comparison | Dijkstra | A* |
|---|---|---|
| Next node | Lowest tentative g. | Lowest g + h. |
| Purpose | Shortest paths from a source to all reachable nodes; it can stop once a chosen goal is settled. | Direct a source-to-goal search using an estimate of remaining cost. |
| Gallery search | Settles S, A, C, B, D, G. | Selects S, B, D, G with the stated tie rule. |
| Result here | Cost 9 to G. | Cost 9 to G, without expanding A or C. |
What makes an estimate safe
A* can only be trusted to find the cheapest route if its guesses are never too high.
- An admissible heuristic never exceeds the true least remaining cost.
- A consistent heuristic also satisfies h(u) ≤ cost(u,v) + h(v) on every edge. This matters when a search closes nodes permanently.
- The gallery estimates meet both conditions. For example, h(C) = 5 ≤ 1 + h(B) = 5.
- Taking h = 0 everywhere removes the guidance: A* then orders nodes like Dijkstra.
What goes wrong with an overestimate
- 1Change only h(B) to 20. After S, A has f = 9 but B has f = 25.
- 2A, then C, are expanded; G is reached with g = 10.
- 3G is selected before B, so A* returns S → A → C → G, cost 10, missing the cost-9 route through B.
1 The true cheapest cost from C to G is needed to check h(C) = 5. What is it, and is h(C) admissible?
2 Which nodes does A* never expand on the gallery graph?
3 If h = 0 for every node, which method does A* behave like?
- 15: C → B → D → G = 1 + 2 + 2; 5 ≤ 5, so yes
- 2A and C
- 3Dijkstra: f = g, so the lowest cost is chosen
Supervised and unsupervised learning
Nobody can write a rule for every mark a display case might show, so the robot learns from examples instead. Machine learning improves performance using experience or data. What decides the method is whether each training example comes with the right answer.
The same five surface observations, first with each technician’s label, then with no labels: the points are only grouped.
Labelled or unlabelled: the difference
| Supervised learning | Unsupervised learning | |
|---|---|---|
| Training data | Inputs paired with known target outputs / labels. | Inputs with no supplied target labels. |
| What is learned | A mapping from input features to a target, used on new observations. | Patterns, relationships or groups within the input data. |
| Gallery use | Surface features with a technician’s “inspect” or “clear” label. | Group similar surface observations before a technician interprets the groups. |
| What the output means | A predicted category can be compared with a known category. | A cluster number does not by itself mean “damaged”. |
Features are inputs; labels are outcomes
These are the five dots in the labelled picture: two features in, one label out.
| Surface-mark index x | Reflection index y | Technician label |
|---|---|---|
| 1 | 1 | clear |
| 1 | 3 | clear |
| 2 | 2 | inspect |
| 3 | 1 | inspect |
| 0 | 4 | clear |
- The two indices are numeric features taken from each image.
- Remove the label column and the observations are unlabelled: fit for finding groups, but they no longer give the right decision.
Why test on separate data
- A model that only remembers its training records may fail on new displays.
- Evaluate predictions on separate observations with known outcomes.
- Poor or unrepresentative training data gives unreliable decisions, even when training performance is high.
1 A table of display photos has a column “technician decision”. Supervised or unsupervised?
2 Look at the unlabelled picture. Does every point in the larger group have the same label?
3 Why are some observations kept out of training?
- 1supervised: each input has a known target label
- 2no: the group (1,1), (2,2), (3,1) holds both “clear” and “inspect”, so a group is not a label
- 3to test the model on data it has not seen
An artificial neuron and its boundary
The robot must turn two measurements of a surface into one decision: inspect it, or leave it. An artificial neuron multiplies each numeric input by a weight, sums the results and applies an activation function. This threshold neuron outputs 1 for “inspect” and 0 for “clear”.
A gallery classifier receives surface-mark index x = 2 and reflection index y = 2. Its weights are 2 and 1. It outputs 1 when 2x + y ≥ 6, and 0 otherwise. Find its output.
- Weight and sum the inputss=2(2) + 1(2) = 6
- Apply the activation rule6 ≥ 6 → output 1
- AnswerOutput 1: inspect.
Each dot is one observation, coloured by its output. The line is 2x + y = 6, and the worked point (2,2) sits exactly on it.
- 1The decision boundary is 2x + y = 6. The shaded side, including the line, gives output 1.
- 2At (2,1) the sum is 5, so the output is 0. At (3,1) it is 7, so the output is 1.
Changing a parameter moves the boundary
Learning means changing these numbers, so it helps to see what each one does:
- Raise the threshold to 8: (3,1) now outputs 0.
- Change a weight: the inputs’ relative influence changes and the boundary turns.
- Training adjusts these parameters. One straight boundary cannot separate every possible pattern.
1 Find the output for x = 1, y = 3.
2 Find the output for x = 3, y = 0.
3 With the threshold raised to 8, what is the output for x = 3, y = 2?
- 12(1) + 3 = 5 < 6: output 0
- 22(3) + 0 = 6 ≥ 6: output 1
- 32(3) + 2 = 8 ≥ 8: output 1
Neural networks and deep learning
One neuron can only split its inputs with a straight boundary, and a real image needs far more than that. An artificial neural network (ANN) connects neurons so their outputs become inputs to later neurons. Weighted connections and activation functions transform the input into a prediction.
Follow the arrows from the two inputs, through two hidden layers, to one output. Every circle is a neuron that weights, sums and activates.
Input, hidden and output layers
- The drawn network has 2 hidden layers.
- Many hidden layers make a deep-learning network: each layer learns a more complex feature from the one before.
- A deep network can learn useful image features itself, instead of relying only on indices chosen by hand.
Deep, supervised or unsupervised?
| Question | Relevant distinction |
|---|---|
| Are target outcomes supplied? | Supervised versus unsupervised training information. |
| Are many hidden neural layers used? | Deep learning describes the network architecture. |
| Does every learning method use neurons? | No: machine learning also includes methods such as regression without a neural network. |
Large datasets and plenty of processing can make deep learning effective, but more layers do not guarantee better predictions. The network still needs suitable training data and evaluation.
1 Which layer receives the feature values?
2 How many hidden layers does the drawn network have?
3 Is “deep learning” a kind of supervised or unsupervised learning?
- 1the input layer
- 22
- 3neither by itself: “deep” describes the architecture (many hidden layers); the training data can be labelled or not
Back propagation of errors
A new network’s weights are only guesses, so its first predictions are poor. Back propagation uses the discrepancy between a network’s prediction and a known target to work out how each adjustable parameter should change. The error information works backwards through the layers.
The same network, worked backwards: the error found at the output is passed back layer by layer, so every weight learns its share of the blame.
The four steps of training
What changes during training
An image labelled “inspect” has target 1. The network outputs 0.25.
- Keep the supplied target fixed.
- Change each parameter according to its contribution to the loss, then run another forward pass.
- The discrepancy is not an instruction to add 0.75 to every weight.
- Repeated updates aim to reduce prediction error.
- Check separate test data as well: a lower training loss alone does not show that unseen displays will be classified correctly.
- The threshold neuron illustrates a decision rule; gradient-based training uses suitable differentiable operations.
1 Put in order: adjust weights, forward pass, compare with target, calculate gradients backwards.
2 A network outputs 0.6 for an image whose target is 1. What is the discrepancy?
3 During training, which changes: the target or the weights?
- 1forward pass, compare with target, calculate gradients backwards, adjust weights
- 21 − 0.6 = 0.4
- 3the weights; the target stays fixed
Reinforcement learning
No one can show the robot the right move in every corner of the gallery, but it can tell when things go well or badly. In reinforcement learning, an agent takes actions in an environment and learns from rewards. Its policy guides which action to take in a state.
Follow the loop: the robot acts on the gallery, and the gallery answers with a new state and a reward. Then it goes round again.
State, action, reward and policy
One turn of the loop for the robot, read row by row.
| Stage | Gallery inspection robot |
|---|---|
| State | Current position, nearby obstacles and whether the display has been inspected. |
| Action | Move, wait or perform an inspection. |
| New state | The robot has moved or completed part of the job. |
| Reward | Positive feedback for a completed inspection; a penalty for collision or wasted time. |
| Policy improvement | Use the accumulated outcomes to favour actions leading to greater reward. |
Exploration and exploitation
An agent that only repeats what has worked may never find something better.
- Exploration tries an unfamiliar action to discover its effect.
- Exploitation chooses an action already expected to work well.
- Only exploiting can miss a better option; only exploring wastes useful experience.
How a reward differs from a label
- A supervised record gives the right category for its input.
- A reward scores the consequence of an action; it need not say which action should have been chosen.
- A reward may arrive after several actions, so the agent must learn which choices led to it.
1 Name the agent and the environment for the gallery robot.
2 The robot always takes the route it already knows works. Is that exploration or exploitation?
3 Give one way a reward differs from a supervised label.
- 1agent: the robot; environment: the gallery
- 2exploitation
- 3a reward scores what happened after an action and may come later; a label gives the right answer for one input
Regression and predictions
Some answers are numbers, not categories: how many minutes will this inspection take? Regression is supervised learning that fits a mathematical function to paired input/output observations. It predicts a numeric value, rather than choosing a category.
The gallery records display-case counts and inspection times: (1,6), (2,7), (3,11), (4,13), (5,13), in minutes. A fitted line is predicted time = 2 × cases + 4.
- Dots are observations; the line gives predictions.
- The dashed gaps are residuals: observed minus predicted time.
- Fitting makes the prediction error as small as possible; the line need not pass through every dot.
Use the fitted relationship on a new input
Compare it with the neuron’s boundary: what differs is the kind of output.
| Classification boundary | Regression fit |
|---|---|
| Outputs inspect / clear from image features. | Outputs inspection time from case count. |
| Separates categories. | Approximates a numeric relationship in labelled observations. |
- A relationship need not be linear and may use several inputs. Check predictions on separate data.
- Predictions far beyond the observed range are less dependable.
- A fitted association alone does not show cause and effect.
1 Use the line to predict the time for 3 cases. What is the residual for the recorded 3-case inspection?
2 Predict the time for 6 cases. Why is this less dependable?
3 Predicting minutes from case count: classification or regression?
- 12(3) + 4 = 10 minutes; residual 11 − 10 = +1
- 22(6) + 4 = 16 minutes; 6 is outside the observed range 1 to 5
- 3regression: the output is a number
Choosing an AI method
The same robot can use several methods because routing, recognising and predicting are different tasks. Identify what information is available and what result is needed.
Ask three questions in order
The first “yes” names the method. A deep neural network is a way of building a supervised or unsupervised model.
Each row is one gallery task. Check its method against the three questions.
| Task and available information | Suitable method | Reason |
|---|---|---|
| Known graph, non-negative costs; shortest travel to every area | Dijkstra | Explicit cost updates give shortest routes without a learned model. |
| Known graph and a safe estimate to charging point G | A* | g + h directs the search towards that goal. |
| Images paired with technician decisions | Supervised classification | Labels teach a mapping to inspection categories. |
| Unlabelled image observations | Unsupervised learning | Find groups and similarities before assigning meaning. |
| Complex image patterns and many training examples | Deep neural network | Many hidden layers learn successive transformations; training adjusts parameters. |
| Repeated actions with rewards from the gallery | Reinforcement learning | Experience improves a policy for acting in the environment. |
| Case counts paired with recorded inspection times | Regression | A fitted function predicts a numeric outcome. |
How to justify your choice
- Route search: cite the given edge weights and the destination.
- Supervised learning: name the labels. Unsupervised learning: say they are absent.
- Reinforcement learning: name an action and a reward.
- Regression: name the quantity being predicted.
Which results are certain
A correct search trace proves the cheapest route for the supplied graph under its conditions. A learned model’s predictions need evaluation against new observations. A network’s output is not made certain merely by drawing it as a node in a graph.
1 A café wants to predict tomorrow’s number of customers from records of past days. Which method?
2 A delivery robot has a map with travel times and an estimate of the distance left to its depot. Which method?
3 A shop has purchase records with no labels and wants to find similar shoppers. Which method?
- 1regression: a number is predicted from labelled past records
- 2A*: a graph with costs plus an estimate to one goal
- 3unsupervised learning: no labels, find groups