| Transitive binary relations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
in general (it might, or might not, hold). For example, that every equivalence relation is symmetric, but not necessarily antisymmetric, is indicated by All definitions tacitly require thehomogeneous relation betransitive: for all if and then |
Inmathematics, thetransitive closureR+ of ahomogeneous binary relationR on asetX is the smallestrelation onX that containsR and istransitive. For finite sets, "smallest" can be taken in its usual sense, of having the fewest related pairs; for infinite setsR+ is the uniqueminimal transitivesuperset ofR.
For example, ifX is a set of airports andx R y means "there is a direct flight from airportx to airporty" (forx andy inX), then the transitive closure ofR onX is the relationR+ such thatxR+y means "it is possible to fly fromx toy in one or more flights".
More formally, the transitive closure of a binary relationR on a setX is the smallest (w.r.t. ⊆) transitive relationR+ onX such thatR ⊆R+; seeLidl & Pilz (1998, p. 337). We haveR+ =R if, and only if,R itself is transitive.
Conversely,transitive reduction reduces a minimal relationS from a given relationR such that they have the same closure, that is,S+ =R+; however, many differentS with this property may exist.
Both transitive closure and transitive reduction are also used in the closely related area ofgraph theory.
A relationR on a setX is transitive if, for allx,y,z inX, wheneverx R y andy R z thenx R z. Examples of transitive relations include the equality relation on any set, the "less than or equal" relation on any linearly ordered set, and the relation "x was born beforey" on the set of all people. Symbolically, this can be denoted as: ifx <y andy <z thenx <z.
One example of a non-transitive relation is "cityx can be reached via a direct flight from cityy" on the set of all cities. Simply because there is a direct flight from one city to a second city, and a direct flight from the second city to the third, does not imply there is a direct flight from the first city to the third. The transitive closure of this relation is a different relation, namely "there is a sequence of direct flights that begins at cityx and ends at cityy". Every relation can be extended in a similar way to a transitive relation.
An example of a non-transitive relation with a less meaningful transitive closure is "x is theday of the week aftery". The transitive closure of this relation is "some dayx comes after a dayy on the calendar", which is trivially true for all days of the weekx andy (and thus equivalent to theCartesian square, which is "x andy are both days of the week").
For any relationR, the transitive closure ofR always exists. To see this, note that theintersection of anyfamily of transitive relations is again transitive. Furthermore,there exists at least one transitive relation containingR, namely the trivial one:X ×X. The transitive closure ofR is then given by the intersection of all transitive relations containingR.
For finite sets, we can construct the transitive closure step by step, starting fromR and adding transitive edges.This gives the intuition for a general construction. For any setX, wecan prove that transitive closure is given by the following expression
where is thei-th power ofR, defined inductively by
and, for,
where denotescomposition of relations.
To show that the above definition ofR+ is the least transitive relation containingR, we show that it containsR, that it is transitive, and that it is the smallest set with both of those characteristics.
Theintersection of two transitive relations is transitive.
Theunion of two transitive relations need not be transitive. To preserve transitivity, one must take the transitive closure. This occurs, for example, when taking the union of twoequivalence relations or twopreorders. To obtain a new equivalence relation or preorder one must take the transitive closure (reflexivity and symmetry—in the case of equivalence relations—are automatic).

Incomputer science, the concept of transitive closure can be thought of as constructing a data structure that makes it possible to answerreachability questions. That is, can one get from nodea to noded in one or more hops? A binary relation tells you only that node a is connected to nodeb, and that nodeb is connected to nodec, etc. After the transitive closure is constructed, as depicted in the following figure, in anO(1)-time operation one may determine that noded is reachable from nodea. The data structure is typically stored as a Boolean matrix, so if matrix[1][4] = true, then it is the case that node 1 can reach node 4 through one or more hops.
The transitive closure of the adjacency relation of adirected acyclic graph (DAG) is the reachability relation of the DAG and astrict partial order.

The transitive closure of anundirected graph produces acluster graph, adisjoint union ofcliques. Constructing the transitive closure is an equivalent formulation of the problem of finding thecomponents of the graph.[1]
The transitive closure of a binary relationcannot, in general, be expressed infirst-order logic (FO).This means that one cannot write a formula using predicate symbolsR andT that will be satisfied inany model if and only ifT is the transitive closure ofR.Infinite model theory, first-order logic (FO) extended with a transitive closure operator is usually calledtransitive closure logic, and abbreviated FO(TC) or just TC. TC is a sub-type offixpoint logics. The fact that FO(TC) is strictly more expressive than FO was discovered byRonald Fagin in 1974; the result was then rediscovered byAlfred Aho andJeffrey Ullman in 1979, who proposed to use fixpoint logic as adatabase query language.[2] With more recent concepts of finite model theory, proof that FO(TC) is strictly more expressive than FO follows immediately from the fact that FO(TC) is notGaifman-local.[3]
Incomputational complexity theory, thecomplexity classNL corresponds precisely to the set of logical sentences expressible in TC. This is because the transitive closure property has a close relationship with theNL-complete problemSTCON for findingdirected paths in a graph. Similarly, the classL is first-order logic with the commutative, transitive closure. When transitive closure is added tosecond-order logic instead, we obtainPSPACE.
Since the 1980sOracle Database has implemented a proprietarySQL extensionCONNECT BY... START WITH that allows the computation of a transitive closure as part of a declarative query. TheSQL 3 (1999) standard added a more generalWITH RECURSIVE construct also allowing transitive closures to be computed inside the query processor; as of 2011 the latter is implemented inIBM Db2,Microsoft SQL Server,Oracle,PostgreSQL, andMySQL (v8.0+).SQLite released support for this in 2014.
Datalog also implements transitive closure computations.[4]
MariaDB implements Recursive Common Table Expressions, which can be used to compute transitive closures. This feature was introduced in release 10.2.2 of April 2016.[5]
Efficient algorithms for computing the transitive closure of the adjacency relation of a graph can be found inNuutila (1995). Reducing the problem to multiplications of adjacency matrices achieves the time complexity offast matrix multiplication algorithms,[6]. However, this approach is not practical since both the constant factors and the memory consumption for sparse graphs are high (Nuutila 1995, pp. 22–23, sect.2.3.3). The problem can also be solved by theFloyd–Warshall algorithm in, or by repeatedbreadth-first search ordepth-first search starting from each node of the graph.
For directed graphs,Purdom's algorithm solves the problem by first computing its condensation DAG and its transitive closure, then lifting it to the original graph. Its runtime is, where is the number of edges between itsstrongly connected components.[7][8][9][10]
More recent research has explored efficient ways of computing transitive closure on distributed systems based on theMapReduce paradigm.[11]