University of Auckland · FACULTY OF COMPUTER SCIENCE

COMPSCI320 Chap.10 Shortest Paths with Negative Weights

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 10 of 13 · COMPSCI320

Shortest Paths with Negative Weights

Negative edge lengths break Dijkstra's greedy rule, and adding a constant to every edge does not repair it.

This chapter indexes a table by the number of edges a path may use, fills it pass by pass on a graph with a negative edge, explains the linear-space Bellman-Ford-Moore algorithm and its stopping rule, and shows how one extra pass detects a negative cycle such as a currency arbitrage loop.

The worked example fills every pass of the edge-budget table on a graph with one negative edge, and the figure tracks each distance falling pass by pass.

The practice cards explain how a further pass reveals a negative cycle and how exchange rates become edge lengths through logarithms.

In this chapter

What this chapter covers

  • 01

    Why Dijkstra fails on negative edges

  • 02

    Shortest paths with at most i edges

  • 03

    The table version and its cost

  • 04

    Bellman-Ford-Moore with early stopping

  • 05

    Detecting negative cycles

  • 06

    Arbitrage and distance-vector routing

Worked example · free

Bellman-Ford passes towards a destination

Q [4 marks]. Edges are s→a 4, s→b 2, a→c −3, b→c 3, c→t 1, a→t 5 and b→t 6. Compute the shortest distance from every vertex to t by passes of at most i edges. The marks are an AskSia practice weighting, not a university marking scheme.
A directed graph with one negative edge, a to c of length −3.A directed graph with one negative edge, a to c of length −3. Each node shows its final shortest distance to t; from s the best route is s, a, c, t with length 4 − 3 + 1 = 2, although its first edge is the longer one out of s.42−33156s:2a:−2b:4c:1t:0
A directed graph with one negative edge, a to c of length −3. Each node shows its final shortest distance to t; from s the best route is s, a, c, t with length 4 − 3 + 1 = 2, although its first edge is the longer one out of s.
  • 1Pass 1 uses single edges: a 5, b 6, c 1, s still infinite.
  • 1Pass 2: a improves to −3 + 1 = −2, b to 3 + 1 = 4, and s reaches 8.
  • 1Pass 3: s improves to 4 + (−2) = 2 through a.
  • 1Pass 4 changes nothing, so the distances s 2, a −2, b 4, c 1 are final.
This four-mark allocation is an AskSia practice weighting and not an official university scheme. The shortest route from s is s, a, c, t with length 2, found after three passes and confirmed by a fourth.
Sia tip — Copy the previous row before updating; every entry in pass i reads only pass i − 1.
Glossary

Key terms

Negative Cycle
A directed cycle whose edge lengths sum to less than zero, so repeated laps lower a path length without limit.
Bellman-Ford-Moore Algorithm
A dynamic programming method that relaxes every edge in repeated passes to find shortest paths with negative weights.
Successor Pointer
A stored next vertex on the current best path from a vertex towards the destination.
Distance-Vector Routing
A routing scheme in which each router updates its distances from its neighbours' vectors, as in Bellman-Ford.
Edge Budget
The maximum number of edges a path may use in the shortest-path table of the Bellman-Ford method.
FAQ

Shortest Paths with Negative Weights FAQ

Why does adding a constant to every edge not fix Dijkstra?

Adding the same amount to each edge penalises a path once per edge, so paths with many edges grow more than paths with few. The shortest path can therefore change after the shift. The lecture shows a small graph where the true shortest route uses three edges but becomes longer than a one-edge route once every length is increased.

Why are n − 1 passes enough when no negative cycle exists?

Without a negative cycle, removing any cycle from a path never makes it longer, so some shortest path is simple. A simple path visits each of the n vertices at most once and so has at most n − 1 edges. After pass n − 1 every such path has been considered, and the distances are final.

How does one extra pass detect a negative cycle?

If no negative cycle exists, a further pass cannot change any distance. If some distance still decreases in pass n, the improving path has n edges, so it repeats a vertex, and the repeated section must be a cycle of negative total length. Following successor pointers from the updated vertex finds that cycle.

Why can successor pointers mislead during the algorithm?

A pointer records the neighbour that last improved a vertex, but that neighbour's own distance may improve later. During the run, following pointers can therefore trace a path whose length differs from the stored distance, and with a negative cycle the pointers may even form a cycle. After the algorithm ends with no negative cycle, the pointers describe shortest paths.

Study strategy

Exam move

Fill one edge-budget table by hand on a graph with a negative edge, writing every row in full, then rerun it with the one-array method and note where the values arrive sooner. Practise the negative cycle argument in three sentences, and convert one set of exchange rates into logarithmic edge lengths.

Before the exam, reconstruct the arbitrage example with rates of your own and confirm that a product above one gives a cycle of negative total length. Rehearse the explanation of why a negative cycle makes shortest paths undefined, using a cycle of total length minus one.

Working through Shortest Paths with Negative Weights in COMPSCI320? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI320 Shortest Paths with Negative Weights question and get a clear, step-by-step explanation grounded in how COMPSCI320 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 34 of your University of Auckland subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMPSCI320 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works