Shortest Paths and Matrix Multiplication
All-pairs shortest paths can be computed by matrix multiplication in which addition is replaced by 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 vertices as a matrix , where is the weight of the edge from to , when there is no edge, and on the diagonal. The best path from to with at most two edges chooses the best intermediate vertex:
This is the ordinary matrix product
with read as and read as . The structure is called the min-plus or tropical semiring.
Because the diagonal is , the power gives the best path with at most edges. With nonnegative weights, no shortest path needs more than edges, so holds all-pairs shortest distances. Repeated squaring computes it with about matrix products. In general the result is the closure
where is entrywise and the identity has on the diagonal and elsewhere.
Floyd-Warshall
The Floyd-Warshall algorithm reaches the same closure in steps by inducting on which vertices may serve as intermediates rather than on path length. After round , is the best path whose intermediate vertices are among the first :
Single-Source Iteration
Bellman-Ford computes distances from one source by multiplying a vector by the matrix until it stops changing:
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 and beyond their algebraic laws. Any semiring, a set with an associative choice and a composition that distributes over it, supports the same matrix product and the same elimination:
The star accounts for paths that go around 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 | Closure computes | Algorithm | ||
|---|---|---|---|---|
| Booleans | reachability, transitive closure | Roy 1959, Warshall 1962 | ||
| Min-plus | all-pairs shortest paths | Floyd 1962 | ||
| Languages | concatenation | a regular expression for the paths of an automaton | Kleene 1956 | |
| Real numbers | Gauss |
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 , and matrix multiplication over composes such matrices: one step through the first matrix followed by one step through the second. A Lawvere metric space is a matrix that is reflexive and transitive in this algebra:
The first law is . The second, read entrywise, is the triangle inequality: composing two steps never beats the direct distance. Together they say that equals its own closure, .
All-pairs shortest paths therefore computes the Lawvere metric space generated by a weighted graph. The closure 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 is a quantale the infinite join in 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 , 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 . Lawvere (1973) showed that metric spaces are such categories for , 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 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 distribute over , 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.