Skip to content

9618 · Unit 21 · Paper 3: Advanced theory

Graph search and machine learning.

9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Graphs and route costs

§18.1 · Artificial intelligence

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.

25212462SABCDG

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 featureMeaning in this gallery
Node / vertexA location: S, A, B, C, D or G.
EdgeA direct connection that the robot can use.
WeightTravel cost on that connection, in minutes.
Undirected edgeNo arrow: the connection works in both directions with the same cost.

A path cost is the sum of its edges

S → A → C → G: 2 + 2 + 6 = 10 minutesAdd only the edges actually followed.S → B → D → G: 5 + 2 + 2 = 9 minutesA lower first edge does not guarantee a lower total.
  • 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.

Your turn

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?

  1. 1
    2 + 2 + 4 + 2 = 10 minutes
  2. 2
    4: to A, B, D, G
  3. 3
    no: S–A costs 2 and C–G costs 6; only the written weight gives the cost
Tick what you can do now
Practise with s26/31 Q9(b); s26/32 Q9(b)(ii)
1 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Dijkstra’s algorithm

§18.1 · Artificial intelligence

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.

Task: on the robot’s gallery graph, find the cheapest route from the start S (store) to the goal G (charging point), and its cost.
Start with S = 0 and A, B, C, D, G = ∞. A predecessor records where the best route entered a node. The frontier holds discovered nodes not yet settled.

Each round, in seven lines

Read the seven lines once; the next table runs them for one node.

Dijkstra · one round
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.

Settle C (cost 4)
LineStatement that runscurrentnnewcost[n]pred[n]WritesWhat happens
1current ← CCcurrent Clowest cost on the frontier (C 4 · B 5)
lowest cost on the frontier (C 4 · B 5)
2mark C settledC ✓current C ✓C’s cost 4 is now final
C’s cost 4 is now final
3n ← Atest A is already settled: skip
test A is already settled: skip
3n ← BBn B
4new ← 4 + 15new 5
5IF 5 < 5test false: B keeps 5
test false: B keeps 5
3n ← DDn D
4new ← 4 + 48new 8
5IF 8 < ∞test true: cheaper, so update
test true: cheaper, so update
6cost[D] ← 88cost[n] 8
7pred[D] ← CCpred[n] C
3n ← GGn G
4new ← 4 + 610new 10
5IF 10 < ∞test true: cheaper, so update
test true: cheaper, so update
6cost[G] ← 1010cost[n] 10
7pred[G] ← CCpred[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.

25212462S1A2B4C3D5G6

Blue: the route found, S → B → D → G.

  1. 1
    Settle S at cost 0
  2. 2
    Settle A at cost 2
  3. 3
    Settle C at cost 4
  4. 4
    Settle B at cost 5
  5. 5
    Settle D at cost 7
  6. 6
    Settle G at cost 9
SABCDGWhy this node
start0∞∞∞∞∞S costs 0; every other node starts at ∞
S costs 0; every other node starts at ∞
settle S✓ 02 S5 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 S5 S4 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 S5 S✓ 4 A8 C10 CC 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 A7 B10 CB 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 B9 DD 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 DG 9 is the lowest; no neighbour gets cheaper
G 9 is the lowest; no neighbour gets cheaper
SettleCost / predecessor updatesFrontier after update
S (0)A: ∞ → 2 via S; B: ∞ → 5 via SA 2, B 5
A (2)C: ∞ → 4 via AC 4, B 5
C (4)D: ∞ → 8 via C; G: ∞ → 10 via CB 5, D 8, G 10
B (5)D: 8 → 7 via BD 7, G 10
D (7)G: 10 → 9 via DG 9
G (9)No improvement—

Relax an edge: compare the new total with the old one

Via C (C settled at 4, edge C–D = 4): D = 4 + 4 = 8; predecessor CThe first discovered route to D is tentative.Via C (edge B–C = 1): B = 4 + 1 = 5, not < 5; no changeOnly a strictly smaller total replaces a cost and predecessor, so B keeps predecessor S.Via B (B settled at 5, edge B–D = 2): D = 5 + 2 = 7 < 8; predecessor BReplace both the cost and the predecessor.Via D (D settled at 7, edge D–G = 2): G = 7 + 2 = 9 < 10; predecessor DG was 4 + 6 = 10 via C; the later discovery improves it.

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
Choose the smallest unvisited cost; mark the node settled.
2
For each unsettled neighbour, add the connecting edge weight to the settled cost.
3
If the result is smaller, update the neighbour’s cost and predecessor. Repeat.
The settled order S, A, C, B, D, G is the search’s progress; it is not one route travelled by the robot.
Your turn

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?

  1. 1
    B: its cost 5 is lower than D 8 and G 10
  2. 2
    D: 5 + 2 = 7 < 8, so D becomes 7 via B
  3. 3
    S → A → C → G, cost 5
Tick what you can do now
Practise with s26/31 Q9(b); s26/32 Q9(b)(ii)
2 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Comparing Dijkstra and A*

§18.1 · Artificial intelligence

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.

Same gallery graph and edge weights, from S to G.
A* estimates: h(S) = 9, h(A) = 7, h(B) = 4, h(C) = 5, h(D) = 2, h(G) = 0. At equal f it picks the larger g, then alphabetical order.
25212462S1A2B4C3D5G6

Dijkstra settles all 6 nodes

25212462Sh=91Ah=7Bh=42Ch=5Dh=23Gh=04

A* expands 4: A and C are never expanded

Line the two methods up, one feature at a time.

ComparisonDijkstraA*
Next nodeLowest tentative g.Lowest g + h.
PurposeShortest 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 searchSettles S, A, C, B, D, G.Selects S, B, D, G with the stated tie rule.
Result hereCost 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

  1. 1
    Change only h(B) to 20. After S, A has f = 9 but B has f = 25.
  2. 2
    A, then C, are expanded; G is reached with g = 10.
  3. 3
    G is selected before B, so A* returns S → A → C → G, cost 10, missing the cost-9 route through B.
A heuristic does not need to be exact. A lower bound can preserve the shortest-path guarantee; an overestimate can remove it. A* does not promise fewer expansions for every graph.
Your turn

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?

  1. 1
    5: C → B → D → G = 1 + 2 + 2; 5 ≤ 5, so yes
  2. 2
    A and C
  3. 3
    Dijkstra: f = g, so the lowest cost is chosen
Tick what you can do now
Practise with s26/32 Q9(b)(i); s26/33 Q9(b)
4 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Supervised and unsupervised learning

§18.1 · Artificial intelligence

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.

Supervised: labelledUnsupervised: no labels● inspect● cleartwo groups found

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 learningUnsupervised learning
Training dataInputs paired with known target outputs / labels.Inputs with no supplied target labels.
What is learnedA mapping from input features to a target, used on new observations.Patterns, relationships or groups within the input data.
Gallery useSurface features with a technician’s “inspect” or “clear” label.Group similar surface observations before a technician interprets the groups.
What the output meansA 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 xReflection index yTechnician label
11clear
13clear
22inspect
31inspect
04clear
  • 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.
Your turn

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?

  1. 1
    supervised: each input has a known target label
  2. 2
    no: the group (1,1), (2,2), (3,1) holds both “clear” and “inspect”, so a group is not a label
  3. 3
    to test the model on data it has not seen
Tick what you can do now
Practise with s26/31 Q9(a)
5 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

An artificial neuron and its boundary

§18.1 · Artificial intelligence

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”.

Worked question

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.

  1. Weight and sum the inputs
    s=2(2) + 1(2) = 6
  2. Apply the activation rule
    6 ≥ 6 → output 1
  3. Answer
    Output 1: inspect.
✓Check At x = 2, the boundary requires y = 6 − 2(2) = 2; the observation lies on it.

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.

0011223344output 1output 0x: surface-mark indexy: reflection index
  1. 1
    The decision boundary is 2x + y = 6. The shaded side, including the line, gives output 1.
  2. 2
    At (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.
Your turn

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?

  1. 1
    2(1) + 3 = 5 < 6: output 0
  2. 2
    2(3) + 0 = 6 ≥ 6: output 1
  3. 3
    2(3) + 2 = 8 ≥ 8: output 1
Tick what you can do now
Practise with s26/32 Q9(a) (neural-network context); w25/31 Q7(b) (older paper: neural networks)
6 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Neural networks and deep learning

§18.1 · Artificial intelligence

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.

InputHidden layersOutputweighted signals flow forwards

Input, hidden and output layers

1
The input layer receives numeric feature values, such as measurements extracted from a display image.
2
Hidden layers combine and transform incoming signals. Their outputs feed later layers.
3
The output layer supplies the result, such as an inspection category or a predicted numeric value.
  • 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?

QuestionRelevant 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.

Your turn

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?

  1. 1
    the input layer
  2. 2
    2
  3. 3
    neither by itself: “deep” describes the architecture (many hidden layers); the training data can be labelled or not
Tick what you can do now
Practise with s26/32 Q9(a); w25/31 Q7(b) (older paper: deep learning)
7 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Back propagation of errors

§18.1 · Artificial intelligence

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.

InputHidden layersOutputerror information flows backwards

The four steps of training

1
Supply a labelled training observation. Run a forward pass using the current weights to obtain the output.
2
Compare output with the target and calculate an error or loss.
3
Use calculus to determine the error gradients, starting at the output and propagating their influence backwards through hidden layers.
4
Adjust the weights and other trainable parameters in a direction that reduces the error. Repeat over training observations.

What changes during training

An image labelled “inspect” has target 1. The network outputs 0.25.

target 1−output 0.25=discrepancy 0.75
  • 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.
Your turn

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?

  1. 1
    forward pass, compare with target, calculate gradients backwards, adjust weights
  2. 2
    1 − 0.6 = 0.4
  3. 3
    the weights; the target stays fixed
Tick what you can do now
Practise with w25/32 Q9(b) (older paper: back propagation)
8 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Reinforcement learning

§18.1 · Artificial intelligence

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.

agent: the robotenvironment: the galleryaction: move, wait, inspectnew state + reward+ inspection, − collision

State, action, reward and policy

One turn of the loop for the robot, read row by row.

StageGallery inspection robot
StateCurrent position, nearby obstacles and whether the display has been inspected.
ActionMove, wait or perform an inspection.
New stateThe robot has moved or completed part of the job.
RewardPositive feedback for a completed inspection; a penalty for collision or wasted time.
Policy improvementUse 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.
Dijkstra’s algorithm on the fixed gallery graph does not learn from rewards. A reinforcement learner may learn a movement policy through experience when the environment or objective requires it.
Your turn

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.

  1. 1
    agent: the robot; environment: the gallery
  2. 2
    exploitation
  3. 3
    a reward scores what happened after an action and may come later; a label gives the right answer for one input
Tick what you can do now
Practise with s24/32 Q11 (older paper: reinforcement learning)
9 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Regression and predictions

§18.1 · Artificial intelligence

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.

123450481216observed 13predicted 12inspection time / minutesnumber of display cases
  • 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

For 4 cases: predicted time = 2(4) + 4 = 12 minutesThe fitted model predicts 12, although the observed record was 13.Residual = 13 − 12 = +1 minuteThe observation lies one minute above the line.

Compare it with the neuron’s boundary: what differs is the kind of output.

Classification boundaryRegression 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.
Your turn

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?

  1. 1
    2(3) + 4 = 10 minutes; residual 11 − 10 = +1
  2. 2
    2(6) + 4 = 16 minutes; 6 is outside the observed range 1 to 5
  3. 3
    regression: the output is a number
Tick what you can do now
Practise with s26/33 Q9(a)
10 / 11
9618 · Unit 21 · Graph search and machine learning
Revisionbyexamplicity

Choosing an AI method

§18.1 · Artificial intelligence

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.

Graph with edge costs?yesA* if there is an estimateto one goal; else DijkstranoKnown target outputs (labels)?yesRegression: predict a numberClassification: a categorynoRewards for actions over time?yesReinforcement learningnoUnsupervised learning:find groups in the data

Each row is one gallery task. Check its method against the three questions.

Task and available informationSuitable methodReason
Known graph, non-negative costs; shortest travel to every areaDijkstraExplicit cost updates give shortest routes without a learned model.
Known graph and a safe estimate to charging point GA*g + h directs the search towards that goal.
Images paired with technician decisionsSupervised classificationLabels teach a mapping to inspection categories.
Unlabelled image observationsUnsupervised learningFind groups and similarities before assigning meaning.
Complex image patterns and many training examplesDeep neural networkMany hidden layers learn successive transformations; training adjusts parameters.
Repeated actions with rewards from the galleryReinforcement learningExperience improves a policy for acting in the environment.
Case counts paired with recorded inspection timesRegressionA 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.

Your turn

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?

  1. 1
    regression: a number is predicted from labelled past records
  2. 2
    A*: a graph with costs plus an estimate to one goal
  3. 3
    unsupervised learning: no labels, find groups
Tick what you can do now
Practise with s26/31 Q9(a–b); s26/32 Q9(b)(i); s26/33 Q9(a)
11 / 11