{
  "id": "transitive-closure",
  "title": "Transitive Closure",
  "lang": "en",
  "source_lang": "en",
  "summary": "The transitive closure of a binary relation is the smallest transitive relation containing it, representing reachability through one or more successive steps.",
  "category": "mathematics",
  "subcategory": "discrete-logic",
  "type": "concept",
  "aliases": [
    "Positive transitive closure"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/transitive-closure/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/transitive-closure/",
      "json": "https://aiwiki.page/zh/transitive-closure.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The transitive closure of a binary relation \\(R\\) on a set \\(X\\) is the smallest transitive relation on \\(X\\) that contains \\(R\\). It adds exactly the pairs required by repeated applications of transitivity: whenever one element can be connected to another by a finite sequence of steps in \\(R\\), the corresponding pair belongs to the closure. In a directed graph, this means adding an edge from \\(u\\) to \\(v\\) whenever a directed walk of positive length leads from \\(u\\) to \\(v\\). Transitive closure is fundamental to reachability, dependency analysis, recursive database queries, and the study of relations."
    },
    {
      "heading": "Definition",
      "text": "A relation \\(T\\) on \\(X\\) is transitive if\n\n\\[(x,y)\\in T \\text{ and } (y,z)\\in T \\quad\\Longrightarrow\\quad (x,z)\\in T\\]\n\nfor all \\(x,y,z\\in X\\).\n\nThe transitive closure of \\(R\\), conventionally written \\(R^{+}\\), is characterized by three conditions:\n\n- \\(R\\subseteq R^{+}\\).\n- \\(R^{+}\\) is transitive.\n- If \\(T\\) is any transitive relation on \\(X\\) containing \\(R\\), then \\(R^{+}\\subseteq T\\).\n\nHere “smallest” means smallest under inclusion, not merely having the fewest pairs. These conditions uniquely determine the closure.\n\nAn equivalent definition uses powers of a relation. Set \\(R^1=R\\), and define\n\n\\[R^{k+1} = \\{(x,z)\\in X\\times X: \\text{there exists }y\\in X \\text{ with }(x,y)\\in R^k \\text{ and }(y,z)\\in R\\}.\\]\n\nThe notation \\(X\\times X\\) denotes the Cartesian product. Then\n\n\\[R^{+}=\\bigcup_{k=1}^{\\infty}R^k.\\]\n\nThus \\((x,y)\\in R^{+}\\) exactly when there is a finite sequence\n\n\\[x=x_0,x_1,\\ldots,x_k=y,\\qquad k\\geq 1,\\]\n\nsuch that \\((x_{i-1},x_i)\\in R\\) for every \\(i\\).\n\nThis construction works for infinite as well as finite sets. Only finite sequences are used; the definition does not add connections that would require infinitely many successive steps.\n\nWhy the construction is minimal\n\nThe union of the positive powers contains \\(R\\). If \\((x,y)\\in R^p\\) and \\((y,z)\\in R^q\\), concatenating the two sequences gives \\((x,z)\\in R^{p+q}\\), so the union is transitive.\n\nConversely, any transitive relation containing \\(R\\) must contain every \\(R^k\\), by mathematical induction. It therefore contains their union."
    },
    {
      "heading": "Transitive and reflexive-transitive closure",
      "text": "The reflexive-transitive closure, usually written \\(R^{*}\\), also includes every identity pair:\n\n\\[R^{*}=I_X\\cup R^{+}, \\qquad I_X=\\{(x,x):x\\in X\\}.\\]\n\nEquivalently, taking \\(R^0=I_X\\),\n\n\\[R^{*}=\\bigcup_{k=0}^{\\infty}R^k.\\]\n\nThe distinction is whether zero steps are allowed:\n\n- \\(R^{+}\\) represents connection by one or more steps.\n- \\(R^{*}\\) represents connection by zero or more steps.\n\nA diagonal pair \\((x,x)\\) belongs to \\(R^{+}\\) only if there is a positive-length sequence returning to \\(x\\). It always belongs to \\(R^{*}\\).\n\nSome texts and software use “transitive closure” for the reflexive-transitive version. Consequently, the treatment of diagonal pairs must be checked rather than inferred from the name alone."
    },
    {
      "heading": "Examples",
      "text": "Let\n\n\\[X=\\{a,b,c,d\\}, \\qquad R=\\{(a,b),(b,c),(c,d)\\}.\\]\n\nThen\n\n\\[R^{+} = \\{(a,b),(b,c),(c,d),(a,c),(b,d),(a,d)\\}.\\]\n\nThe three added pairs represent connections taking two or three steps. No diagonal pair is added.\n\nFor a cycle,\n\n\\[R=\\{(a,b),(b,a)\\},\\]\n\nthe transitive closure is\n\n\\[R^{+} = \\{(a,a),(a,b),(b,a),(b,b)\\}.\\]\n\nAlthough the original relation has no identity pairs, each vertex can return to itself in two steps.\n\nOn the integers, let \\(xRy\\) mean \\(y=x+1\\). Then \\(xR^{+}y\\) means \\(x<y\\), whereas \\(xR^{*}y\\) means \\(x\\leq y\\)."
    },
    {
      "heading": "Graph interpretation",
      "text": "A relation \\(R\\) on \\(X\\) can be regarded as a directed graph with vertex set \\(X\\) and edge set \\(R\\). Its transitive closure records all ordered pairs connected by a directed walk of positive length. This interpretation connects the operation directly to graph theory.\n\nFor distinct vertices, any reachable pair has a path without repeated vertices. For a vertex to reach itself in positive length, a directed cycle is required. This distinction matters because some definitions reserve “path” for walks with no repeated vertices.\n\nFor a finite graph with \\(n\\geq 1\\) vertices,\n\n\\[R^{+}=\\bigcup_{k=1}^{n}R^k.\\]\n\nA connection between distinct vertices can be witnessed using at most \\(n-1\\) edges. A positive-length return to the starting vertex can be witnessed by a cycle of at most \\(n\\) edges.\n\nFor a directed acyclic graph, the positive transitive closure contains no diagonal pairs and is a strict partial order. Adding the identity relation gives a partial order.\n\nMore generally, define mutual reachability by\n\n\\[x\\sim y \\quad\\Longleftrightarrow\\quad xR^{*}y \\text{ and } yR^{*}x.\\]\n\nThis is an equivalence relation. Its classes are the graph’s strongly connected components. Collapsing each component to a single vertex produces an acyclic graph."
    },
    {
      "heading": "Matrix representation",
      "text": "For a finite graph, let \\(A\\) be its Boolean adjacency matrix, with\n\n\\[A_{ij}=1 \\quad\\Longleftrightarrow\\quad (i,j)\\in R.\\]\n\nUse Boolean matrix multiplication, in which logical AND replaces multiplication and logical OR replaces addition:\n\n\\[(B\\odot C)_{ij} = \\bigvee_{k=1}^{n} (B_{ik}\\land C_{kj}).\\]\n\nThen the Boolean power \\(A^{\\odot k}\\) records whether a walk of length exactly \\(k\\) exists. The transitive-closure matrix is\n\n\\[A^{+} = A\\lor A^{\\odot 2}\\lor\\cdots\\lor A^{\\odot n},\\]\n\nwhere OR is applied entrywise.\n\nFor reflexive-transitive closure, the Boolean identity matrix is included:\n\n\\[A^{*}=I\\lor A^{+}.\\]\n\nThis is an operation over Boolean algebra, not ordinary numerical matrix addition. With ordinary arithmetic, entries of \\(A^k\\) count walks of length \\(k\\); Boolean powers record only their existence."
    },
    {
      "heading": "Algorithms",
      "text": "Warshall’s algorithm\n\nWarshall’s algorithm computes the closure by progressively allowing additional vertices as intermediate points.\n\nInitialize a Boolean matrix \\(T\\) with the adjacency matrix. For \\(k=1,\\ldots,n\\), update all pairs \\(i,j\\) by\n\n\\[T_{ij} \\leftarrow T_{ij}\\lor(T_{ik}\\land T_{kj}).\\]\n\nThe update records that \\(i\\) can reach \\(j\\) through \\(k\\) whenever \\(i\\) can reach \\(k\\) and \\(k\\) can reach \\(j\\). The intermediate-vertex loop over \\(k\\) must be outermost in this standard formulation.\n\nInitializing \\(T=A\\) computes positive transitive closure. Initializing \\(T=A\\lor I\\) computes reflexive-transitive closure.\n\nUsing asymptotic notation, the standard implementation takes \\(O(n^3)\\) time and \\(O(n^2)\\) space. Bit-vector implementations can accelerate the Boolean operations by processing multiple entries together.\n\nRepeated graph searches\n\nAnother method runs a depth-first or breadth-first search from each vertex. With adjacency lists, the total running time is\n\n\\[O\\bigl(n(n+m)\\bigr),\\]\n\nwhere \\(m\\) is the number of edges. This approach is often suitable for sparse graphs.\n\nFor positive closure, the source vertex should be included in its own result only when a positive-length cycle returns to it. A search that automatically marks its source as reachable instead produces reflexive reachability unless this convention is handled separately.\n\nRepeated squaring\n\nBoolean matrix multiplication also gives a repeated-squaring method. Starting from \\(B=I\\lor A\\), repeatedly replace\n\n\\[B\\leftarrow B\\odot B.\\]\n\nEach squaring doubles the maximum walk length represented. After \\(O(\\log n)\\) squarings, \\(B\\) represents reflexive-transitive closure.\n\nAn explicitly stored closure can contain \\(n^2\\) pairs even when the original graph has only \\(O(n)\\) edges. Thus producing the full relation can require quadratic output space, independently of the chosen algorithm."
    },
    {
      "heading": "Algebraic properties",
      "text": "On the collection of relations over a fixed set, transitive closure is a closure operator:\n\n- Extensive: \\(R\\subseteq R^{+}\\).\n- Monotone: if \\(R\\subseteq S\\), then \\(R^{+}\\subseteq S^{+}\\).\n- Idempotent: \\((R^{+})^{+}=R^{+}\\).\n\nThe last property is an instance of idempotence: applying the operation again adds nothing.\n\nThe closure can also be defined as the intersection of all transitive relations containing \\(R\\):\n\n\\[R^{+} = \\bigcap \\{T\\subseteq X\\times X: R\\subseteq T \\text{ and }T\\text{ is transitive}\\}.\\]\n\nThis family is nonempty because \\(X\\times X\\) is transitive.\n\nA further description uses a fixed point. With relation composition defined by\n\n\\[S\\circ T = \\{(x,z):\\exists y,\\ (x,y)\\in S \\text{ and }(y,z)\\in T\\},\\]\n\nthe relation \\(R^{+}\\) is the least fixed point, under inclusion, of\n\n\\[F(S)=R\\cup(S\\circ R).\\]\n\nThis formulation underlies recursive computations of reachability."
    },
    {
      "heading": "Applications and related operations",
      "text": "In databases, transitive closure expresses recursive queries such as finding all descendants in a hierarchy, all indirectly required components, or all destinations reachable through connecting routes.\n\nIn program analysis and formal verification, a transition relation describes individual execution steps. Its reflexive-transitive closure describes states reachable after any finite number of steps, including none. If the state space is infinite, an explicit closure need not be finitely representable or effectively computable.\n\nIn logic, reachability illustrates a limitation of first-order logic: no single ordinary first-order formula over the edge relation defines reachability on all finite directed graphs. Logics equipped with transitive-closure operators can express this unbounded iteration directly.\n\nThe transitive reduction addresses a complementary problem: removing edges while preserving reachability. For a finite directed acyclic graph, the reduction is unique and retains only edges not implied by longer paths. For general directed graphs, a reachability-preserving reduction need not be unique.\n\nA separate use occurs in set theory. A transitive set is a set containing every element of each of its elements, and the transitive closure of a set is the least transitive set containing the original set as a subset. This concerns repeated membership rather than adding pairs to an arbitrary binary relation; the two constructions should not be conflated."
    }
  ],
  "links": [
    "binary-relation",
    "directed-graph",
    "cartesian-product",
    "mathematical-induction",
    "integer",
    "graph-theory",
    "directed-acyclic-graph",
    "partial-order",
    "equivalence-relation",
    "adjacency-matrix",
    "matrix-multiplication",
    "identity-matrix",
    "boolean-algebra",
    "big-o-notation",
    "idempotence",
    "fixed-point",
    "formal-verification",
    "first-order-logic",
    "set-theory"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}