Documentation

CslibFrontier.Algorithms.WordRAM.BellmanFord

The Bellman–Ford algorithm on the word RAM #

Single-source shortest paths with possibly negative integer edge weights, and detection of reachable negative cycles, implemented on the standard-basis word RAM (Cslib.WordRAM.Basis.standard) in O(|V|(|V| + |E|)) instructions Bellman Cormen et al., Section 24.1.

Representation #

An instance (Cslib.Algorithms.WordRAM.BellmanFord.Instance) is a directed multigraph on Fin n with adjacency lists adj : Fin n → List (Fin n × ℤ) and source source : Fin n. Its input word list (Instance.encode) mirrors Cslib.Algorithms.Dijkstra.ListInstance.encode: n, source, the n + 1 cumulative edge offsets offset 0, …, offset n, and the m edges in vertex order as pairs (target, Signed.encode weight) using the width-independent sign-and-magnitude code Cslib.WordRAM.Signed.encode.

Because Signed.encode z is never 1 (encode_ne_one), tentative distances in WithTop ℤ are stored in one word each (encodeDist): 1 for ⊤ and Signed.encode z for a finite weight z : ℤ. Signed addition and comparison are executed by Cslib.WordRAM.Signed.add and Cslib.WordRAM.Signed.less using ordinary unsigned word instructions.

The program (Cslib.Algorithms.WordRAM.BellmanFord.program) writes the tentative distance array at addresses [outBase + 1, outBase + 1 + n) just past the input, runs n - 1 edge-relaxation rounds followed by the negative-cycle check pass, writes the output header (n if no reachable negative cycle exists, 0 otherwise) at outBase, and halts returning outBase. Under the list output convention Cslib.WordRAM.ExecutesList, the output list (answerWords G) is [] when a negative cycle is reachable from source and [encodeDist (d 0), …, encodeDist (d (n - 1))] for the shortest-path distances d otherwise.

Word-size condition #

Across n - 1 sequential rounds of m edge relaxations with edge magnitudes at most M = G.maxAbsWeight, every finite intermediate distance has magnitude at most (n - 1) m M, and every candidate sum a + c has magnitude at most (n m + 1) M. The envelope G.wordBound = 8 * (n + 1) * (m + 2) * (M + 2) covers input length and words, memory addresses, and the two extra sign/overflow bits required by Cslib.WordRAM.Signed.exec_add.

Main definitions #

Main results #

References #

Single-word encoding of WithTop ℤ #

No integer has sign-and-magnitude code 1: nonnegative integers have even codes and negative integers have codes at least 3.

Single-word encoding of WithTop ℤ: 1 for ⊤ and Signed.encode z for (z : ℤ).

Problem instances and word encoding #

A single-source shortest-path instance on Fin n with signed integer edge weights. Parallel edges and self-loops are permitted.

  • n : ℕ

    Number of vertices.

  • adj : Fin self.n → List (Fin self.n × ℤ)

    Adjacency lists of (target, weight) pairs.

  • source : Fin self.n

    Source vertex.

Total number of directed edge entries across all adjacency lists.

Maximum absolute edge weight across all edges (0 when m = 0).

Cumulative number of edges leaving vertices < u.

Word-list encoding of G: n, source, the n + 1 offsets offset 0, …, offset n, and then all edges in vertex order as two words (target, Signed.encode weight).

First memory address past the input in State.init G.encode, used as the output-list header.

Word-size envelope covering input length and words, memory addresses, and intermediate signed path weights of magnitude at most (n * m + 1) * maxAbsWeight.

The encoded input has n + 2 * m + 3 words.

theorem Cslib.Algorithms.WordRAM.BellmanFord.Instance.init_target (G : Instance) {r : ℕ} (u : Fin G.n) {i : ℕ} (hi : i < (G.adj u).length) :
(WordRAM.Data.init G.encode).mem (G.n + 4 + 2 * (G.offset ↑u + i)) = ↑(G.adj u)[i].1
theorem Cslib.Algorithms.WordRAM.BellmanFord.Instance.init_weight (G : Instance) {r : ℕ} (u : Fin G.n) {i : ℕ} (hi : i < (G.adj u).length) :
(WordRAM.Data.init G.encode).mem (G.n + 4 + 2 * (G.offset ↑u + i) + 1) = WordRAM.Signed.encode (G.adj u)[i].2

Whenever G.wordBound fits in w bits, the encoded input words fit in w bits.

Output specification #

Output word list of Bellman–Ford on G: [] when a negative cycle is reachable from G.source, and [encodeDist (d 0), …, encodeDist (d (n - 1))] otherwise.

Decode the output word list of program back to Option (Fin G.n → WithTop ℤ).

When answerWords G ≠ [], entry v decodes to the shortest-path weight from G.source to v.

Magnitude envelope of intermediate Bellman–Ford estimates #

Registers and structured Word-RAM program #

@[reducible, inline]

Output-list header address outAddrBase.

Complete structured Bellman–Ford solver leaving output header address in outBase.

Flat Word-RAM Bellman–Ford program returning answerWords G via ExecutesList.

Machine state invariants and step specifications #

Main Word-RAM theorems #

Structured execution of solver leaves outAddrBase in outBase and answerWords G in outputList y.mem outAddrBase within 64 * G.n * (G.n + G.m) - 1 instructions.

Bellman–Ford on the word RAM (O(|V|(|V| + |E|))). At every word width w with Fits w G.wordBound, program halts within 64 * G.n * (G.n + G.m) instructions (including the final halt) and outputs answerWords G: [] if and only if a negative cycle is reachable from G.source, and the encoded shortest-path distances [encodeDist (d 0), …, encodeDist (d (n - 1))] otherwise.

Fixed-program Program.ComputesListInTime certificate for Word-RAM Bellman–Ford.