Cohesive Systems logoCOHESIVE SYSTEMS

Search Cohesive Systems

Ready

Search Cohesive Systems

Find product pages, building blocks, technical articles, and graph definitions.

Shortest Paths and Matrix Multiplication

All-pairs shortest paths can be computed by matrix multiplication in which addition is replaced by min⁡\min and multiplication by ++. Replacing those two operations with others yields transitive closure, regular expressions for automata, and matrix inversion from the same algorithm. The value set that parametrizes the algorithm plays the same role as the base of enrichment for a Lawvere distance. Computer science and category theory arrived at this structure independently and at about the same time.

Shortest Paths as Matrix Products

Represent a weighted directed graph on nn vertices as a matrix WW, where W(i,j)W(i, j) is the weight of the edge from ii to jj, ∞\infty when there is no edge, and 00 on the diagonal. The best path from ii to jj with at most two edges chooses the best intermediate vertex:

W2(i,j)=min⁡k(W(i,k)+W(k,j))W^{2}(i, j) = \min_{k} \big( W(i, k) + W(k, j) \big)

This is the ordinary matrix product

(AB)(i,j)=∑kA(i,k)⋅B(k,j)(AB)(i, j) = \sum_{k} A(i, k) \cdot B(k, j)

with ∑\sum read as min⁡\min and ⋅\cdot read as ++. The structure ([0,∞],min⁡,+)([0, \infty], \min, +) is called the min-plus or tropical semiring.

Because the diagonal is 00, the power WmW^{m} gives the best path with at most mm edges. With nonnegative weights, no shortest path needs more than n−1n - 1 edges, so Wn−1W^{n-1} holds all-pairs shortest distances. Repeated squaring computes it with about log⁡2n\log_2 n matrix products. In general the result is the closure

W∗=I⊕W⊕W2⊕W3⊕⋯W^{*} = I \oplus W \oplus W^{2} \oplus W^{3} \oplus \cdots

where ⊕\oplus is entrywise min⁡\min and the identity II has 00 on the diagonal and ∞\infty elsewhere.

Floyd-Warshall

The Floyd-Warshall algorithm reaches the same closure in n3n^{3} steps by inducting on which vertices may serve as intermediates rather than on path length. After round kk, d(i,j)d(i, j) is the best path whose intermediate vertices are among the first kk:

d(i,j)←min⁡(d(i,j),  d(i,k)+d(k,j))for k=1,…,nd(i, j) \leftarrow \min\big( d(i, j),\; d(i, k) + d(k, j) \big) \qquad \text{for } k = 1, \dots, n

Single-Source Iteration

Bellman-Ford computes distances from one source by multiplying a vector by the matrix until it stops changing:

d←d⊕(d⊗W)d \leftarrow d \oplus (d \otimes W)

Distance-vector routing protocols run this iteration in a distributed way. Each router repeatedly combines its neighbors' advertised distances with the cost of the link to each neighbor.

The Semiring Generalization

Nothing in the algorithm depends on min⁡\min and ++ beyond their algebraic laws. Any semiring, a set with an associative choice ⊕\oplus and a composition ⊗\otimes that distributes over it, supports the same matrix product and the same elimination:

d(i,j)←d(i,j)⊕(d(i,k)⊗d(k,k)∗⊗d(k,j))d(i, j) \leftarrow d(i, j) \oplus \big( d(i, k) \otimes d(k, k)^{*} \otimes d(k, j) \big)

The star d(k,k)∗d(k, k)^{*} accounts for paths that go around kk any number of times. When staying put is already the best value, as with nonnegative costs, the star is the unit and drops out.

Semiring⊕\oplus⊗\otimesClosure computesAlgorithm
Booleans∨\lor∧\landreachability, transitive closureRoy 1959, Warshall 1962
Min-plusmin⁡\min++all-pairs shortest pathsFloyd 1962
Languages∪\cupconcatenationa regular expression for the paths of an automatonKleene 1956
Real numbers++×\times(I−A)−1=I+A+A2+⋯(I - A)^{-1} = I + A + A^{2} + \cdotsGauss

Robert and Ferland first stated the algorithm uniformly in terms of semirings in 1968. The same closure evaluates recursive queries over semirings in relational and logic programming, and in every case the result is a fixed point: no further path improves any entry.

Relation to Lawvere Distance

A weighted graph is a matrix of values in VV, and matrix multiplication over VV composes such matrices: one step through the first matrix followed by one step through the second. A Lawvere metric space is a matrix dd that is reflexive and transitive in this algebra:

I≤dd⊗d≤dI \le d \qquad d \otimes d \le d

The first law is d(x,x)=0d(x, x) = 0. The second, read entrywise, is the triangle inequality: composing two steps never beats the direct distance. Together they say that dd equals its own closure, d=d∗d = d^{*}.

All-pairs shortest paths therefore computes the Lawvere metric space generated by a weighted graph. The closure W∗W^{*} is the smallest metric consistent with the edge weights, and the triangle inequality is the statement that no shortcut remains. The same holds for every semiring in the table. Reachability generates a preorder, languages generate a category enriched in languages, and when VV is a quantale the infinite join in W∗W^{*} always exists.

Parallel Development

The two lines of work abstracted different things.

  • Algorithms. Kleene (1956), Roy (1959), Warshall (1962), and Floyd (1962) each solved a path problem. Floyd observed that Warshall's transitive-closure procedure computes shortest paths when Booleans become distances, disjunction becomes min⁡\min, and conjunction becomes addition. Robert and Ferland (1968) then parametrized the algorithm by a semiring.
  • Categories. Eilenberg and Kelly (1966) defined categories whose hom-sets are replaced by objects of a closed monoidal category VV. Lawvere (1973) showed that metric spaces are such categories for V=[0,∞]V = [0, \infty], with the triangle inequality as composition.

Computer science abstracted the algorithm by parametrizing it over a semiring. Category theory abstracted the object by parametrizing hom-sets over a base of enrichment. The semiring plays for the algorithm the role that VV plays for enrichment. Pratt drew the connection in 1989 and later noted that the two had developed independently. He used it in event-state duality to treat time and information as distances computed this way.

Systems Uses

  • Dependency and reachability. The transitive closure of a dependency graph answers which components are affected by a change. The closure of message and program-order edges gives happened-before.
  • Latency and cost budgets. Min-plus closure gives the best end-to-end latency or cost between every pair of services.
  • Routing. Distance-vector protocols are distributed Bellman-Ford iterations.
  • Capacity and reliability. Other semirings give widest paths and most reliable paths, as listed under Lawvere distance examples.
  • Recursive queries. Datalog-style recursion over a semiring computes the same closures.

Modeling Checks

  • Which semiring matches the question: reachability, cost, capacity, reliability, or the set of paths itself?
  • Does ⊗\otimes distribute over ⊕\oplus, so that the elimination order does not matter?
  • Can a loop improve a value, as with negative costs, and if so does the star exist?
  • Is the closure computed centrally, by elimination, or by distributed iteration?

Formal relations

  • corresponds_to: Lawvere Distance — The closure of a weighted graph over a semiring is the enriched category it generates, so all-pairs shortest paths computes the Lawvere metric space determined by the edge weights.

External References

  • S. C. Kleene, Representation of Events in Nerve Nets and Finite Automata, in Automata Studies, Princeton University Press, 1956, 3-42.
  • B. Roy, Transitivité et connexité, Comptes Rendus de l'Académie des Sciences 249, 1959, 216-218.
  • Stephen Warshall, A Theorem on Boolean Matrices, Journal of the ACM 9(1):11-12, 1962.
  • Robert W. Floyd, Algorithm 97: Shortest Path, Communications of the ACM 5(6):345, 1962.
  • Samuel Eilenberg and G. Max Kelly, Closed Categories, Proceedings of the Conference on Categorical Algebra, Springer, 1966, 421-562.
  • P. Robert and J. Ferland, Généralisation de l'algorithme de Warshall, Revue Française d'Informatique et de Recherche Opérationnelle 2, 1968, 13-25.
  • F. William Lawvere, Metric Spaces, Generalized Logic, and Closed Categories, Rendiconti del Seminario Matematico e Fisico di Milano 43, 1973.
  • Vaughan R. Pratt, Enriched Categories and the Floyd-Warshall Connection, Proceedings of the First International Conference on Algebraic Methodology and Software Technology, Iowa City, 1989, 177-180.
  • Vaughan R. Pratt, Event-State Duality: The Enriched Case, CONCUR 2002, Section 4.