Use a dynamic programming state indexed by an edge budget. Put and for , then for compute
Every route using at most edges either already uses at most , or ends with some edge whose preceding route uses at most . Conversely every finite candidate in the displayed equation is attained by such a route. This proves the hop-bounded shortest-route recurrence by induction on , even when weights are negative.
In each round, copy the previous distance row and relax every edge into the new row, always reading from the old row. An in-place pass would allow several new edges in one round and violate the edge budget. Store a predecessor record for every selected candidate, either a carry to or the incoming edge from . Following records backwards from and reversing the collected edges returns the actual optimum route; an infinite final distance means no feasible route exists.
Only vertices incident to an edge, together with , need distance fields. In a graph representation whose edges reference vertex records, initialise those fields by visiting both endpoints of every edge, then ; repeated initialisations are harmless. In each round, first copy old fields to new fields by another complete endpoint pass, and only then begin the relaxation pass. Copying the same endpoint several times before any relaxation also does no harm. Thus each round costs , with no scan of isolated vertices, and for the total is time. An implementation that instead initialises full arrays for all vertices has the conventional bound . For or no edges, only the empty route can succeed.
To realise reconstruction without a dense table, associate a persistent route record with each finite distance. A successful relaxation in round creates a record containing the new edge and a pointer to the old round's route record at its source; a carry shares its record. All reads use old-round records. At most such records are created, taking space, and the returned chain has at most edges. Distances alone need only two fields per relevant vertex.
Here “route” uses the standard edge-budget shortest-path convention permitting repeated vertices; a bounded budget prevents an arbitrarily long negative-cycle traversal. If “path” instead requires vertex simplicity with unrestricted negative weights, this recurrence does not enforce that extra restriction. With nonnegative weights, removing cycles gives a simple optimum within the same budget.

Articles by others on the same topic (0)

There are currently no matching articles.