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 #
Cslib.Algorithms.WordRAM.BellmanFord.Instance: weighted digraph onFin nwith integer edge weights and source vertex.Cslib.Algorithms.WordRAM.BellmanFord.Instance.encode,Cslib.Algorithms.WordRAM.BellmanFord.Instance.wordBound: input word list and word-size envelope.Cslib.Algorithms.WordRAM.BellmanFord.encodeDist,Cslib.Algorithms.WordRAM.BellmanFord.decodeDist: single-word encoding ofWithTop ℤ.Cslib.Algorithms.WordRAM.BellmanFord.answerWords,Cslib.Algorithms.WordRAM.BellmanFord.decodeAnswer: output word list and decoder back toOption (Fin G.n → WithTop ℤ).Cslib.Algorithms.WordRAM.BellmanFord.Reg.outBase: output-list header register.Cslib.Algorithms.WordRAM.BellmanFord.solver,Cslib.Algorithms.WordRAM.BellmanFord.program: structured and flat word-RAM programs.
Main results #
Cslib.Algorithms.WordRAM.BellmanFord.decodeDist_encodeDist,Cslib.Algorithms.WordRAM.BellmanFord.decodeAnswer_answerWords: lossless decoding of distances and outputs.Cslib.Algorithms.WordRAM.BellmanFord.answerWords_eq_nil_iff,Cslib.Algorithms.WordRAM.BellmanFord.isShortestDist_of_answerWords_ne_nil: negative-cycle characterization and shortest-path correctness ofanswerWords.Cslib.Algorithms.WordRAM.BellmanFord.solver_spec,Cslib.Algorithms.WordRAM.BellmanFord.program_spec,Cslib.Algorithms.WordRAM.BellmanFord.computesListInTime_program: execution within64 * n * (n + m)instructions at every word widthwwithFits w G.wordBound.Cslib.Algorithms.WordRAM.BellmanFord.not_readsWidth_program,Cslib.Algorithms.WordRAM.BellmanFord.isRandFree_program: width-independence and determinism certificates forprogram.
References #
No integer has sign-and-magnitude code 1: nonnegative integers have even codes and negative
integers have codes at least 3.
Decode a word produced by encodeDist back to WithTop ℤ.
Problem instances and word encoding #
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.
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.
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.
Decoding answerWords G recovers bellmanFord G.adj (List.finRange G.n) G.source exactly.
answerWords G is empty if and only if a negative cycle is reachable from G.source.
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 #
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.
program does not read the word width (width%).
program is deterministic (rand-free).