How is the shortest path between two vertices in a weighted network found?
Find the shortest path between two vertices in a weighted network by inspection or by systematic labelling
A focused answer to the HSC Maths Standard 2 dot point on shortest paths. Inspection for small networks, systematic labelling (Dijkstra-style) for larger ones, a stage-by-stage walkthrough, ties and verification edge cases, and worked road and Sydney transport examples.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
What this dot point is asking
NESA wants you to find the shortest path between two named vertices in a weighted network. For small networks, inspection is enough: list all the reasonable paths and pick the smallest total. For larger ones, a systematic labelling method (Dijkstra's algorithm) is expected. Once a graph has more than a handful of vertices, this is the method markers prefer to see.
"Shortest" means smallest total weight, whatever the weight represents: distance in km, drive time in minutes, or cost in dollars. The method is identical in each case; only the units change.
The answer
What the question asks
Given a weighted graph with vertices and edge weights (distances, times, costs), find the path from a starting vertex to an ending vertex with the smallest total weight.
The shortest path may not be the path with the fewest edges. A route built from several small-weight edges can beat a direct route across one large-weight edge, which is exactly why the diagram above takes three edges (---, total ) rather than the two-edge route --, which totals .
Method 1: inspection
For small networks (say vertices or fewer), list all sensible candidate paths between the start and end vertex, compute each total weight, and pick the smallest.
This works when:
- The graph is small.
- There are few branching options.
- You can spot every reasonable path visually.
The risk with inspection is missing a path, so be systematic: fix the first step out of the start vertex, exhaust every route that begins that way, then move to the next first step. A path should never revisit a vertex, because looping back only adds weight and can never shorten a route.
Method 2: systematic labelling (Dijkstra's algorithm)
For larger networks, use systematic labelling. The idea is to give every vertex a running label equal to the shortest distance found so far from the start, then lock in (finalise) vertices one at a time in increasing order of distance:
- Label the start vertex with . Label all other vertices with (or just "unknown").
- From the current vertex, look at each neighbour and compute the proposed label as current label + edge weight. If this is less than the neighbour's current label, update it (and record the predecessor).
- Mark the current vertex as finalised; its label will not change again.
- Move to the unfinalised vertex with the smallest current label.
- Repeat steps 2 to 4 until the end vertex is finalised.
The final label on the end vertex is the shortest total weight. Trace back through the predecessors to find the actual path.
Why do you always finalise (lock in) the smallest unfinalised label? Because nothing left in the graph can reach it for less. Any other route to it would first pass through a vertex whose label is the same or bigger. All the edge weights are positive, so going that way can only add more. That is why a locked-in label never needs to change again.
Tracking the path
While labelling, record the predecessor: which vertex you came from when you set (or last improved) the current label. After finishing, trace backwards from the end vertex to the start using these predecessors, then reverse to get the path in forward order. Without the predecessors you get the shortest distance but cannot name the shortest route, and most questions ask for both.
Find the shortest path, stage by stage
The diagram below is run one settled vertex at a time, so you can see exactly where every label comes from. The network has vertices to ; we want the shortest path from to . Each vertex carries its running shortest distance from (the start). A vertex with an accent ring has been settled: its label is final and will not change.
Stage 1, settle the start and label its neighbours. Set and settle it. Look at the edges leaving : - has weight , so gets the tentative label ; - has weight , so gets . Vertices and are not yet reachable, so they stay at .
Stage 2, settle the smallest label and relax its neighbours. The smallest unfinalised label is , so settle . Now check 's neighbours. The route to via is , which beats 's current , so update to (its predecessor becomes ). The route to via is , so becomes . This relaxation step, replacing a label when a shorter route appears, is the heart of the method.
Stage 3, settle C, then settle D. The smallest unfinalised label is now , so settle . From : the route to is , beating 's current , so update to ; the route to is , so becomes . The next smallest unfinalised label is , so settle it; the route - would give , which ties 's existing , so stays at (we keep the route already recorded).
Stage 4, settle the end vertex and trace the path back. The only label left is , so settle it; the algorithm is done. Now trace back using predecessors: was set from (), was set from (), and came from . Reversed, the shortest path is --- with total . Note the tie spotted in stage 3: the route ---- also totals , so there are two equally short paths.
Laid out as a running table, the four stages are:
| Settle | |||||
|---|---|---|---|---|---|
| Start | |||||
A bold entry is a settled (final) label. Reading down a column shows how a vertex's label can fall (for example drops from to , and from to ) but never rises, and never changes once bold.
Equal-weight ties
If two paths have the same total weight, there are multiple shortest paths, and the run above is a clean example: --- and ---- both total . Systematic labelling reports only one of them (whichever it recorded first), so a tie is easy to miss. When a question asks for "the" shortest path and a tie exists, state every tied route; when it asks only for the shortest distance, the single value is enough. The tell-tale sign of a tie is the moment in stage 3 where the route - re-derives 's existing label exactly rather than beating it.
Checking you have not missed a shorter route
The most common worry with inspection is "could there be a shorter path I did not list?" Three checks settle it:
- Cross-check inspection against labelling. If a small graph gives the same answer by both methods, you can be confident. The worked example below does exactly this.
- Confirm the total equals the end label. The summed weights along your chosen path must equal the final label on the end vertex. If they disagree, you have either mis-added or mis-traced the predecessors.
- Confirm every cheaper first move was explored. A shorter route would have to leave the start along a cheaper edge or reach the end through a vertex with a smaller settled label. Because labelling settles vertices in increasing order, anything cheaper was already locked in earlier, so nothing shorter can remain.
Directed versus undirected edges
In Standard 2, road and transport networks are usually undirected. That means an edge between and can be travelled either way for the same weight, so - and - cost the same. Sometimes a question uses directed edges, which are edges with arrows. A one-way street is one example, or a route where the uphill and downhill times differ. The method does not change, but there is one extra rule: when you update a vertex's neighbours, only follow edges the way their arrow points. On a directed graph, the shortest path from to may differ from the shortest path from to . So read the arrows before you start, and keep to them the whole way through.
Sanity checks
- Total weight on the path equals the final label on the end vertex.
- Each edge used is an actual edge in the graph (and, if directed, used the right way).
- The path is connected: each consecutive pair of vertices is joined by an edge.
- No vertex is visited twice; a repeat means a loop has crept in.
How exam questions ask about shortest paths
The wording varies, but every version reduces to the same task. Learn to translate it:
- "Find the shortest path / shortest route from to ." Give the path as a sequence of vertices and its total weight with units. The route, not just the number, is required.
- "Find the shortest distance / least time / cheapest cost from to ." The total weight is the headline answer; still show the route as method.
- "Show your working", or a -plus mark question on a larger graph. Use systematic labelling and show the labels (a running table is ideal); inspection without working can lose method marks once the graph is non-trivial.
- "Is there more than one shortest path?" A tie question: list every route that achieves the minimum total.
- "Explain why this route is shortest." Point to the labelling: the end vertex was settled at that value, and no unsettled vertex had a smaller label, so no shorter route exists.
- "One road is closed, or a new road opens." Re-run the affected part: delete or add the edge, then recompute. Often only one or two labels change.
One distinction is worth a mark. A question that says "connect all the towns with the least total road length" is a minimum spanning tree problem, not a shortest path. A shortest path runs point to point between two named vertices. A minimum spanning tree (MST) instead joins up every vertex. Decide which one is being asked before you start.
Exam-style practice questions
Practice questions written in the style of NESA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
2022 HSC-style4 marksIn a road network with vertices and weighted edges representing distances in km, find the shortest path from to and state its length.Show worked answer →
List all possible paths from to and their lengths.
Say the paths are: -- (length ), -- (length ), --- (length ), --- (length ).
The shortest is km, via either --- or --- (both give the same length).
Markers reward listing the candidate paths systematically, computing each total, identifying the shortest, and noting if multiple paths tie. State all tied shortest paths if asked.
2023 HSC-style4 marksA driver in Sydney wants the shortest route from her home (vertex ) to her workplace (vertex ) using a weighted graph where edges are road segments and weights are typical drive times in minutes. Find the shortest route and its total time.Show worked answer →
Apply systematic labelling: starting at , label each vertex with the shortest known total time from . Update labels as shorter paths are found.
Step through: label with . Label its neighbours with the edge weights to them. From the next vertex with the smallest label, update its neighbours, and so on, always taking the smallest unfinalised label next.
Continue until is labelled. The final label on is the shortest total time. Trace back via the predecessors to find the actual route.
Markers reward systematic application of the labelling, accurate intermediate values, the shortest time, and the route traced back. State the route as a sequence of vertices and the total time with units.
Practice questions
Original practice questions graded from foundation to exam level, each with a full worked solution. Try them before revealing the solution.
foundation1 marksA small network has three towns , and . The roads (distances in km) are - of length , - of length , and a direct road - of length . Find the shortest path from to and state its total length.Show worked solution →
- List the routes from to
- With only three towns there are two simple paths: the direct road -, and the route through , --.
- Add up each total
- Direct: - is km. Via : -- is km.
- Pick the smaller total
- , so the route through wins.
- Answer
- the shortest path is -- with total length km. (Check: the only alternative, the direct road -, is km, which is longer, so km is the minimum.)
foundation2 marksA delivery van travels on a network of four suburbs , , , . The road segments (drive time in minutes) are - of , - of , - of , and a direct freeway - of . Find the shortest route from to and state its total time.Show worked solution →
- Identify the candidate routes
- From to there are two simple paths: the freeway -, and the chain ---.
- Total each route
- Freeway: - is minutes. Chain: --- is minutes.
- Choose the smaller
- The chain at minutes beats the freeway at minutes, even though it uses three roads instead of one.
- Answer
- the shortest route is ---, taking minutes. (Check: the only other route, the direct freeway, is minutes, which is longer, so minutes is the least.)
foundation2 marksA cyclist rides on a network with vertices , , , . The paths (distances in km) are - of , - of , - of , and a direct path - of . Find the shortest path from to and explain why it uses more edges than the direct path.Show worked solution →
- Find the candidate paths
- From to there are two simple routes: the direct path -, and the longer chain ---.
- Total each one
- Direct: - is km. Chain: --- is km.
- Compare
- , so the three-edge chain is shorter than the single direct edge.
- Why the longer-looking route wins
- "Shortest path" means least total weight, not fewest edges. The three small edges (, , ) add to less than the one big edge of .
- Answer
- the shortest path is --- at km. (Check: the only alternative, -, is km, confirming km is the minimum.)
foundation2 marksA commuter chooses between routes from home to work on a toll network, where each weight is the cost in dollars. The roads are - of , - of , - of and - of . Find the cheapest route from to and state its total cost.Show worked solution →
- List the routes
- Two simple paths run from to : -- and --.
- Total each cost
- Via : -- is dollars. Via : -- is dollars.
- Pick the cheaper
- , so the route through is cheaper.
- Answer
- the cheapest route is -- at a total cost of dollars. (Check: the only alternative, --, costs dollars, which is more, so dollars is the minimum.)
core3 marksA road network has vertices , , , , with edge distances (km): - of , - of , - of , - of , - of , - of , - of . Find the shortest path from to and state its total length.Show worked solution →
- Set up systematic labelling from
- Label . Its neighbours: and . Settle .
- Settle the smallest label,
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (no change, keep predecessor ); . Settle .
- Settle
- From : becomes (predecessor ). Settle , then settle .
- Trace back
- from , from , from : the path is ---.
- Answer
- shortest path ---, total km. (Check: ---- gives km, which is longer, so km is the minimum.)
core3 marksA driver crosses a town network with vertices , , , , , where weights are drive times in minutes: - of , - of , - of , - of , - of , - of , - of . Find the shortest route from to and state its total time.Show worked solution →
- Label from
- Set . Neighbours: and . Settle .
- Settle the smallest,
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (predecessor ). Settle , then settle .
- Trace back
- from , from , from , from : the route is ----.
- Answer
- shortest route ----, total minutes. (Check: the direct-looking --- gives minutes, which is longer, so minutes is the minimum.)
core4 marksA courier uses a network from depot to drop-off with vertices , , , , and edge times (minutes): - of , - of , - of , - of , - of , - of , - of . (a) Find the shortest route from to and its total time. (b) The road - is closed for works; find the new shortest route and time.Show worked solution →
Part (a), label from . Set . Neighbours: , . Settle .
Settle . From : becomes (predecessor ); . Settle .
Settle . From : becomes (predecessor ); . Settle .
Settle . From : becomes (predecessor ). Settle , then .
- Trace back
- from , from , from , from , giving ----, total minutes.
- Part (b), delete - and re-solve
- With - gone, the only way into is -. The labels up to are unchanged, so via ---.
- Answers
- (a) ----, minutes; (b) ---, minutes. (Check (a): --- gives minutes, longer than . Check (b): -- gives minutes, longer than .)
core4 marksA network of paths has vertices , , , , , with distances (km): - of , - of , - of , - of , - of , - of , - of , - of , - of . Find the shortest path from to and state its total length.Show worked solution →
- Label from
- Set . Neighbours: , . Settle .
- Settle
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (no change); (predecessor ). Settle .
- Settle
- From : becomes (no change). Settle , then .
- Trace back
- from , from , from , from : path ----.
- Answer
- shortest path ----, total km. (Check: --- gives km, longer, so km is the minimum.)
exam4 marksA network has vertices , , , , , with edge weights (minutes): - of , - of , - of , - of , - of , - of , - of , - of . Find the shortest path from to , state its total time, and say whether more than one shortest path exists.Show worked solution →
- Label from
- Set . Neighbours: , . Settle .
- Settle
- From : becomes (predecessor ); (predecessor ). Settle .
- Settle
- From : becomes (no change, keep predecessor ). Settle .
- Settle
- From : ; (predecessor ). Settle .
- Settle
- From : becomes . This re-derives exactly rather than beating it, the tell-tale sign of a tie. Settle , then .
- Trace back the two routes
- Direct from : --- is . Via : ---- is .
- Answer
- the shortest time is minutes, and there are two shortest paths, --- and ----, since both total . (Check: the next best route ---- gives minutes, longer than .)
exam5 marksA regional road network has towns , , , , , , with distances (km): - of , - of , - of , - of , - of , - of , - of , - of , - of , - of , - of . Find the shortest path from to and state its total length.Show worked solution →
- Label from
- Set . Neighbours: , . Settle .
- Settle
- From : becomes (no change); ; . Settle .
- Settle
- From : becomes (no change). Settle .
- Settle
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (predecessor ); . Settle .
- Settle
- From : becomes (predecessor ). Settle , then .
- Trace back
- from , from , from , from , from : path -----.
- Answer
- shortest path -----, total km. (Check: ---- gives km, longer, so km is the minimum.)
exam5 marksA one-way courier network has directed edges (drive time in minutes), each travelled only in the stated direction: to of , to of , to of , to of , to of , to of , to of , to of , to of . Find the shortest directed route from to and state its total time.Show worked solution →
- Label from , following arrows only
- Set . Out-edges of : , . Settle .
- Settle
- Out-edges of : becomes (predecessor ); . Settle .
- Settle
- Out-edges of : becomes (predecessor ); . Settle .
- Settle
- Out-edges of : becomes (predecessor ); . Settle .
- Settle
- Out-edge of : becomes (predecessor ). Settle , then .
- Trace back
- from , from , from , from , from : route -----.
- Answer
- shortest directed route -----, total minutes. (Check: ---- gives minutes, longer, so minutes is the minimum; arrows were followed throughout.)
exam5 marksA logistics network has hubs , , , , , , with edge costs (dollars): - of , - of , - of , - of , - of , - of , - of , - of , - of , - of , - of , - of . Find the cheapest route from to and state its total cost.Show worked solution →
- Label from
- Set . Neighbours: , . Settle .
- Settle
- From : becomes (no change); ; . Settle .
- Settle
- From : becomes (no change). Settle .
- Settle
- From : becomes (predecessor ); ; . Settle .
- Settle
- From : becomes (predecessor ); becomes (predecessor ). Settle .
- Finish
- Settle : becomes (no change). Settle , then .
- Trace back
- from , from , from , from : route ----.
- Answer
- cheapest route ----, total dollars. (Check: ----- gives dollars, more, so dollars is the minimum.)
