isl_map_transitive_closure: handle existentials
[platform/upstream/isl.git] / doc / implementation.tex
1 \section{Sets and Relations}
2
3 \begin{definition}[Polyhedral Set]
4 A {\em polyhedral set}\index{polyhedral set} $S$ is a finite union of basic sets
5 $S = \bigcup_i S_i$, each of which can be represented using affine
6 constraints
7 $$
8 S_i : \Q^n \to 2^{\Q^d} : \vec s \mapsto
9 S_i(\vec s) =
10 \{\, \vec x \in \Z^d \mid \exists \vec z \in \Z^e :
11 A \vec x + B \vec s + D \vec z + \vec c \geq \vec 0 \,\}
12 ,
13 $$
14 with $A \in \Z^{m \times d}$,
15 $B \in \Z^{m \times n}$,
16 $D \in \Z^{m \times e}$
17 and $\vec c \in \Z^m$.
18 \end{definition}
19
20 \begin{definition}[Parameter Domain of a Set]
21 Let $S \in \Q^n \to 2^{\Q^d}$ be a set.
22 The {\em parameter domain} of $S$ is the set
23 $$\pdom S \coloneqq \{\, \vec s \in \Z^n \mid S(\vec s) \ne \emptyset \,\}.$$
24 \end{definition}
25
26 \begin{definition}[Polyhedral Relation]
27 A {\em polyhedral relation}\index{polyhedral relation}
28 $R$ is a finite union of basic relations
29 $R = \bigcup_i R_i$ of type
30 $\Q^n \to 2^{\Q^{d_1+d_2}}$,
31 each of which can be represented using affine
32 constraints
33 $$
34 R_i = \vec s \mapsto
35 R_i(\vec s) =
36 \{\, \vec x_1 \to \vec x_2 \in \Z^{d_1} \times \Z^{d_2}
37 \mid \exists \vec z \in \Z^e :
38 A_1 \vec x_1 + A_2 \vec x_2 + B \vec s + D \vec z + \vec c \geq \vec 0 \,\}
39 ,
40 $$
41 with $A_i \in \Z^{m \times d_i}$,
42 $B \in \Z^{m \times n}$,
43 $D \in \Z^{m \times e}$
44 and $\vec c \in \Z^m$.
45 \end{definition}
46
47 \begin{definition}[Parameter Domain of a Relation]
48 Let $R \in \Q^n \to 2^{\Q^{d+d}}$ be a relation.
49 The {\em parameter domain} of $R$ is the set
50 $$\pdom R \coloneqq \{\, \vec s \in \Z^n \mid R(\vec s) \ne \emptyset \,\}.$$
51 \end{definition}
52
53 \begin{definition}[Domain of a Relation]
54 Let $R \in \Q^n \to 2^{\Q^{d+d}}$ be a relation.
55 The {\em domain} of $R$ is the polyhedral set
56 $$\domain R \coloneqq \vec s \mapsto
57 \{\, \vec x_1 \in \Z^{d_1} \mid \exists \vec x_2 \in \Z^{d_2} :
58 (\vec x_1, \vec x_2) \in R(\vec s) \,\}
59 .
60 $$
61 \end{definition}
62
63 \begin{definition}[Range of a Relation]
64 Let $R \in \Q^n \to 2^{\Q^{d+d}}$ be a relation.
65 The {\em range} of $R$ is the polyhedral set
66 $$
67 \range R \coloneqq \vec s \mapsto
68 \{\, \vec x_2 \in \Z^{d_2} \mid \exists \vec x_1 \in \Z^{d_1} :
69 (\vec x_1, \vec x_2) \in R(\vec s) \,\}
70 .
71 $$
72 \end{definition}
73
74 \begin{definition}[Composition of Relations]
75 Let $R \in \Q^n \to 2^{\Q^{d_1+d_2}}$ and
76 $S \in \Q^n \to 2^{\Q^{d_2+d_3}}$ be two relations,
77 then the composition of
78 $R$ and $S$ is defined as
79 $$
80 S \circ R \coloneqq
81 \vec s \mapsto
82 \{\, \vec x_1 \to \vec x_3 \in \Z^{d_1} \times \Z^{d_3}
83 \mid \exists \vec x_2 \in \Z^{d_2} :
84 \vec x_1 \to \vec x_2 \in R(\vec s) \wedge
85 \vec x_2 \to \vec x_3 \in S(\vec s)
86 \,\}
87 .
88 $$
89 \end{definition}
90
91 \begin{definition}[Difference Set of a Relation]
92 Let $R \in \Q^n \to 2^{\Q^{d+d}}$ be a relation.
93 The difference set ($\Delta \, R$) of $R$ is the set
94 of differences between image elements and the corresponding
95 domain elements,
96 $$
97 \Delta \, R \coloneqq
98 \vec s \mapsto
99 \{\, \vec \delta \in \Z^{d} \mid \exists \vec x \to \vec y \in R :
100 \vec \delta = \vec y - \vec x
101 \,\}
102 $$
103 \end{definition}
104
105 \section{Coalescing}\label{s:coalescing}
106
107 See \shortciteN{Verdoolaege2009isl}, for now.
108 More details will be added later.
109
110 \section{Transitive Closure}
111
112 \subsection{Introduction}
113
114 \begin{definition}[Power of a Relation]
115 Let $R \in \Q^n \to 2^{\Q^{d+d}}$ be a relation and
116 $k \in \Z_{\ge 1}$
117 a positive number, then power $k$ of relation $R$ is defined as
118 \begin{equation}
119 \label{eq:transitive:power}
120 R^k \coloneqq
121 \begin{cases}
122 R & \text{if $k = 1$}
123 \\
124 R \circ R^{k-1} & \text{if $k \ge 2$}
125 .
126 \end{cases}
127 \end{equation}
128 \end{definition}
129
130 \begin{definition}[Transitive Closure of a Relation]
131 Let $R \in \Q^n \to 2^{\Q^{d+d}}$ be a relation,
132 then the transitive closure $R^+$ of $R$ is the union
133 of all positive powers of $R$,
134 $$
135 R^+ \coloneqq \bigcup_{k \ge 1} R^k
136 .
137 $$
138 \end{definition}
139 Alternatively, the transitive closure may be defined
140 inductively as
141 \begin{equation}
142 \label{eq:transitive:inductive}
143 R^+ \coloneqq R \cup \left(R \circ R^+\right)
144 .
145 \end{equation}
146
147 Since the transitive closure of a polyhedral relation
148 may no longer be a polyhedral relation \shortcite{Kelly1996closure},
149 we can, in the general case, only compute an approximation
150 of the transitive closure.
151 Whereas \shortciteN{Kelly1996closure} compute underapproximations,
152 we, like \shortciteN{Beletska2009}, compute overapproximations.
153 That is, given a relation $R$, we will compute a relation $T$
154 such that $R^+ \subseteq T$.  Of course, we want this approximation
155 to be as close as possible to the actual transitive closure
156 $R^+$ and we want to detect the cases where the approximation is
157 exact, i.e., where $T = R^+$.
158
159 For computing an approximation of the transitive closure of $R$,
160 we follow the same general strategy as \shortciteN{Beletska2009}
161 and first compute an approximation of $R^k$ for $k \ge 1$ and then project
162 out the parameter $k$ from the resulting relation.
163
164 \begin{example}
165 As a trivial example, consider the relation
166 $R = \{\, x \to x + 1 \,\}$.  The $k$th power of this map
167 for arbitrary $k$ is
168 $$
169 R^k = k \mapsto \{\, x \to x + k \mid k \ge 1 \,\}
170 .
171 $$
172 The transitive closure is then
173 $$
174 \begin{aligned}
175 R^+ & = \{\, x \to y \mid \exists k \in \Z_{\ge 1} : y = x + k \,\}
176 \\
177 & = \{\, x \to y \mid y \ge x + 1 \,\}
178 .
179 \end{aligned}
180 $$
181 \end{example}
182
183 \subsection{Computing an Approximation of $R^k$}
184
185 There are some special cases where the computation of $R^k$ is very easy.
186 One such case is that where $R$ does not compose with itself,
187 i.e., $R \circ R = \emptyset$ or $\domain R \cap \range R = \emptyset$.
188 In this case, $R^k$ is only non-empty for $k=1$ where it is equal
189 to $R$ itself.
190
191 In general, it is impossible to construct a closed form
192 of $R^k$ as a polyhedral relation.
193 We will therefore need to make some approximations.
194 As a first approximations, we will consider each of the basic
195 relations in $R$ as simply adding one or more offsets to a domain element
196 to arrive at an image element and ignore the fact that some of these
197 offsets may only be applied to some of the domain elements.
198 That is, we will only consider the difference set $\Delta\,R$ of the relation.
199 In particular, we will first construct a collection $P$ of paths
200 that move through
201 a total of $k$ offsets and then intersect domain and range of this
202 collection with those of $R$.
203 That is, 
204 \begin{equation}
205 \label{eq:transitive:approx}
206 K = P \cap \left(\domain R \to \range R\right)
207 ,
208 \end{equation}
209 with
210 \begin{equation}
211 \label{eq:transitive:path}
212 P = \vec s \mapsto \{\, \vec x \to \vec y \mid
213 \exists k_i \in \Z_{\ge 0} :
214 \vec y = \vec x + \sum_i k_i \, \Delta_i(\vec s)
215 \wedge
216 \sum_i k_i = k > 0
217 \,\}
218 \end{equation}
219 and with $\Delta_i$ the basic sets that compose
220 the difference set $\Delta\,R$.
221 Note that the number of basic sets $\Delta_i$ need not be
222 the same as the number of basic relations in $R$.
223 Also note that since addition is commutative, it does not
224 matter in which order we add the offsets and so we are allowed
225 to group them as we did in \eqref{eq:transitive:path}.
226
227 If all the $\Delta_i$s are singleton sets
228 $\Delta_i = \{\, \vec \delta_i \,\}$ with $\vec \delta_i \in \Z^d$,
229 then \eqref{eq:transitive:path} simplifies to
230 \begin{equation}
231 \label{eq:transitive:singleton}
232 P = \{\, \vec x \to \vec y \mid
233 \exists k_i \in \Z_{\ge 0} :
234 \vec y = \vec x + \sum_i k_i \, \vec \delta_i
235 \wedge
236 \sum_i k_i = k > 0
237 \,\}
238 \end{equation}
239 and then the approximation computed in \eqref{eq:transitive:approx}
240 is essentially the same as that of \shortciteN{Beletska2009}.
241 If some of $\Delta_i$s are not singleton sets or if
242 some of $\vec \delta_i$s are parametric, then we need
243 to resort to further approximations.
244
245 To ease both the exposition and the implementation, we will for
246 the remainder of this section work with extended offsets
247 $\Delta_i' = \Delta_i \times \{\, 1 \,\}$.
248 That is, each offset is extended with an extra coordinate that is
249 set equal to one.  The paths constructed by summing such extended
250 offsets have the length encoded as the difference of their
251 final coordinates.  The path $P'$ can then be decomposed into
252 paths $P_i'$, one for each $\Delta_i$,
253 \begin{equation}
254 \label{eq:transitive:decompose}
255 P' = \left(
256 (P_m' \cup \identity) \circ \cdots \circ
257 (P_2' \cup \identity) \circ
258 (P_1' \cup \identity)
259 \right) \cap
260 \{\,
261 \vec x' \to \vec y' \mid y_{d+1} - x_{d+1} = k > 0
262 \,\}
263 ,
264 \end{equation}
265 with
266 $$
267 P_i' = \vec s \mapsto \{\, \vec x' \to \vec y' \mid
268 \exists k \in \Z_{\ge 1} :
269 \vec y' = \vec x' + k \, \Delta_i'(\vec s)
270 \,\}
271 .
272 $$
273 Note that each $P_i'$ contains paths of length at least one.
274 We therefore need to take the union with the identity relation
275 when composing the $P_i'$s to allow for paths that do not contain
276 any offsets from one or more $\Delta_i'$.
277 The path that consists of only identity relations is removed
278 by imposing the constraint $y_{d+1} - x_{d+1} > 0$.
279 Taking the union with the identity relation means that
280 that the relations we compose in \eqref{eq:transitive:decompose}
281 each consist of two basic relations.  If there are $m$
282 disjuncts in the input relation, then a direct application
283 of the composition operation may therefore result in a relation
284 with $2^m$ disjuncts, which is prohibitively expensive.
285 It is therefore crucial to apply coalescing (\autoref{s:coalescing})
286 after each composition.
287
288 Let us now consider how to compute an overapproximation of $P_i'$.
289 Those that correspond to singleton $\Delta_i$s are grouped together
290 and handled as in \eqref{eq:transitive:singleton}.
291 Note that this is just an optimization.  The procedure described
292 below would produce results that are at least as accurate.
293 For simplicity, we first assume that no constraint in $\Delta_i'$
294 involves any existentially quantified variables.
295 We will return to existentially quantified variables at the end
296 of this section.
297 Without existentially quantified variables, we can classify
298 the constraints of $\Delta_i'$ as follows
299 \begin{enumerate}
300 \item non-parametric constraints
301 \begin{equation}
302 \label{eq:transitive:non-parametric}
303 A_1 \vec x + \vec c_1 \geq \vec 0
304 \end{equation}
305 \item purely parametric constraints
306 \begin{equation}
307 \label{eq:transitive:parametric}
308 B_2 \vec s + \vec c_2 \geq \vec 0
309 \end{equation}
310 \item negative mixed constraints
311 \begin{equation}
312 \label{eq:transitive:mixed}
313 A_3 \vec x + B_3 \vec s + \vec c_3 \geq \vec 0
314 \end{equation}
315 such that for each row $j$ and for all $\vec s$,
316 $$
317 \Delta_i'(\vec s) \cap
318 \{\, \vec x' \to \vec y' \mid B_{3,j} \vec s + c_{3,j} > 0 \,\}
319 = \emptyset
320 $$
321 \item positive mixed constraints
322 $$
323 A_4 \vec x + B_4 \vec s + \vec c_4 \geq \vec 0
324 $$
325 such that for each row $j$, there is at least one $\vec s$ such that
326 $$
327 \Delta_i'(\vec s) \cap
328 \{\, \vec x' \to \vec y' \mid B_{4,j} \vec s + c_{4,j} > 0 \,\}
329 \ne \emptyset
330 $$
331 \end{enumerate}
332 We will use the following approximation $Q_i$ for $P_i'$:
333 \begin{equation}
334 \label{eq:transitive:Q}
335 \begin{aligned}
336 Q_i = \vec s \mapsto
337 \{\,
338 \vec x' \to \vec y'
339 \mid {} & \exists k \in \Z_{\ge 1}, \vec f \in \Z^d :
340 \vec y' = \vec x' + (\vec f, k)
341 \wedge {}
342 \\
343 &
344 A_1 \vec f + k \vec c_1 \geq \vec 0
345 \wedge
346 B_2 \vec s + \vec c_2 \geq \vec 0
347 \wedge
348 A_3 \vec f + B_3 \vec s + \vec c_3 \geq \vec 0
349 \,\}
350 .
351 \end{aligned}
352 \end{equation}
353 To prove that $Q_i$ is indeed an overapproximation of $P_i'$,
354 we need to show that for every $\vec s \in \Z^n$, for every
355 $k \in \Z_{\ge 1}$ and for every $\vec f \in k \, \Delta_i(\vec s)$
356 we have that
357 $(\vec f, k)$ satisfies the constraints in \eqref{eq:transitive:Q}.
358 If $\Delta_i(\vec s)$ is non-empty, then $\vec s$ must satisfy
359 the constraints in \eqref{eq:transitive:parametric}.
360 Each element $(\vec f, k) \in k \, \Delta_i'(\vec s)$ is a sum
361 of $k$ elements $(\vec f_j, 1)$ in $\Delta_i'(\vec s)$.
362 Each of these elements satisfies the constraints in
363 \eqref{eq:transitive:non-parametric}, i.e.,
364 $$
365 \left[
366 \begin{matrix}
367 A_1 & \vec c_1
368 \end{matrix}
369 \right]
370 \left[
371 \begin{matrix}
372 \vec f_j \\ 1
373 \end{matrix}
374 \right]
375 \ge \vec 0
376 .
377 $$
378 The sum of these elements therefore satisfies the same set of inequalities,
379 i.e., $A_1 \vec f + k \vec c_1 \geq \vec 0$.
380 Finally, the constraints in \eqref{eq:transitive:mixed} are such
381 that for any $\vec s$ in the parameter domain of $\Delta$,
382 we have $-\vec r(\vec s) \coloneqq B_3 \vec s + \vec c_3 \le \vec 0$,
383 i.e., $A_3 \vec f_j \ge \vec r(\vec s) \ge \vec 0$
384 and therefore also $A_3 \vec f \ge \vec r(\vec s)$.
385 Note that if there are no mixed constraints and if the
386 rational relaxation of $\Delta_i(\vec s)$, i.e.,
387 $\{\, \vec x \in \Q^d \mid A_1 \vec x + \vec c_1 \ge \vec 0\,\}$,
388 has integer vertices, then the approximation is exact, i.e.,
389 $Q_i = P_i'$.  In this case, the vertices of $\Delta'_i(\vec s)$
390 generate the rational cone
391 $\{\, \vec x' \in \Q^{d+1} \mid \left[
392 \begin{matrix}
393 A_1 & \vec c_1
394 \end{matrix}
395 \right] \vec x' \,\}$ and therefore $\Delta'_i(\vec s)$ is
396 a Hilbert basis of this cone \shortcite[Theorem~16.4]{Schrijver1986}.
397
398 Existentially quantified variables can be handled by
399 classifying them into variables that are uniquely
400 determined by the parameters, variables that are independent
401 of the parameters and others.  The first set can be treated
402 as parameters and the second as variables.  Constraints involving
403 the other existentially quantified variables are removed.
404
405 \begin{example}
406 Consider the relation
407 $$
408 R =
409 n \to \{\, x \to y \mid \exists \, \alpha_0, \alpha_1: 7\alpha_0 = -2 + n \wedge 5\alpha_1 = -1 - x + y \wedge y \ge 6 + x \,\}
410 .
411 $$
412 The difference set of this relation is
413 $$
414 \Delta = \Delta \, R =
415 n \to \{\, x \mid \exists \, \alpha_0, \alpha_1: 7\alpha_0 = -2 + n \wedge 5\alpha_1 = -1 + x \wedge x \ge 6 \,\}
416 .
417 $$
418 The existentially quantified variables can be defined in terms
419 of the parameters and variables as
420 $$
421 \alpha_0 = \floor{\frac{-2 + n}7}
422 \qquad
423 \text{and}
424 \qquad
425 \alpha_1 = \floor{\frac{-1 + x}5}
426 .
427 $$
428 $\alpha_0$ can therefore be treated as a parameter,
429 while $\alpha_1$ can be treated as a variable.
430 This in turn means that $7\alpha_0 = -2 + n$ can be treated as
431 a purely parametric constraint, while the other two constraints are
432 non-parametric.
433 The corresponding $Q$~\eqref{eq:transitive:Q} is therefore
434 $$
435 \begin{aligned}
436 n \to \{\, (x,z) \to (y,w) \mid
437 \exists\, \alpha_0, \alpha_1, k, f : {} &
438 k \ge 1 \wedge
439 y = x + f \wedge
440 w = z + k \wedge {} \\
441 &
442 7\alpha_0 = -2 + n \wedge
443 5\alpha_1 = -k + x \wedge
444 x \ge 6 k
445 \,\}
446 .
447 \end{aligned}
448 $$
449 Projecting out the final coordinates encoding the length of the paths,
450 results in the exact transitive closure
451 $$
452 R^+ =
453 n \to \{\, x \to y \mid \exists \, \alpha_0, \alpha_1: 7\alpha_1 = -2 + n \wedge 6\alpha_0 \ge -x + y \wedge 5\alpha_0 \le -1 - x + y \,\}
454 .
455 $$
456 \end{example}
457
458 \subsection{Checking Exactness}
459
460 The approximation $T$ for the transitive closure $R^+$ can be obtained
461 by projecting out the parameter $k$ from the approximation $K$
462 \eqref{eq:transitive:approx} of the power $R^k$.
463 Since $K$ is an overapproximation of $R^k$, $T$ will also be an
464 overapproximation of $R^+$.
465 To check whether the results are exact, we need to consider two
466 cases depending on whether $R$ is {\em cyclic}, where $R$ is defined
467 to be cyclic if $R^+$ maps any element to itself, i.e.,
468 $R^+ \cap \identity \ne \emptyset$.
469 If $R$ is acyclic, then the inductive definition of
470 \eqref{eq:transitive:inductive} is equivalent to its completion,
471 i.e.,
472 $$
473 R^+ = R \cup \left(R \circ R^+\right)
474 $$
475 is a defining property.
476 Since $T$ is known to be an overapproximation, we only need to check
477 whether
478 $$
479 T \subseteq R \cup \left(R \circ T\right)
480 .
481 $$
482 This is essentially Theorem~5 of \shortciteN{Kelly1996closure}.
483 The only difference is that they only consider lexicographically
484 forward relations, a special case of acyclic relation.
485
486 If, on the other hand, $R$ is cyclic, then we have to resort
487 to checking whether the approximation $K$ of the power is exact.
488 Note that $T$ may be exact even if $K$ is not exact, so the check
489 is sound, but incomplete.
490 To check exactness of the power, we simply need to check
491 \eqref{eq:transitive:power}.  Since again $K$ is known
492 to be an overapproximation, we only need to check whether
493 $$
494 \begin{aligned}
495 K'|_{y_{d+1} - x_{d+1} = 1} & \subseteq R'
496 \\
497 K'|_{y_{d+1} - x_{d+1} \ge 2} & \subseteq R' \circ K'|_{y_{d+1} - x_{d+1} \ge 1}
498 ,
499 \end{aligned}
500 $$
501 where $R' = \{\, \vec x' \to \vec y' \mid \vec x \to \vec y \in R
502 \wedge y_{d+1} - x_{d+1} = 1\,\}$, i.e., $R$ extended with path
503 lengths equal to 1.
504
505 All that remains is to explain how to check the cyclicity of $R$.
506 Note that the exactness on the power is always sound, even
507 in the acyclic case, so we only need to be careful that we find
508 all cyclic cases.  Now, if $R$ is cyclic, i.e.,
509 $R^+ \cap \identity \ne \emptyset$, then, since $T$ is
510 an overapproximation of $R^+$, also
511 $T \cap \identity \ne \emptyset$.  This in turn means
512 that $\Delta \, K'$ contains a point whose first $d$ coordinates
513 are zero and whose final coordinate is positive.
514 In the implementation we currently perform this test on $P'$ instead of $K'$.
515 Note that if $R^+$ is acyclic and $T$ is not, then the approximation
516 is clearly not exact and the approximation of the power $K$
517 will not be exact either.
518
519 \subsection{Decomposing $R$ into strongly connected components}
520
521 If the input relation $R$ is a union of several basic relations
522 that can be partially ordered
523 then the accuracy of the approximation may be improved by computing
524 an approximation of each strongly connected components separately.
525 For example, if $R = R_1 \cup R_2$ and $R_1 \circ R_2 = \emptyset$,
526 then we know that any path that passes through $R_2$ cannot later
527 pass through $R_1$, i.e.,
528 $$
529 R^+ = R_1^+ \cup R_2^+ \cup \left(R_2^+ \circ R_1^+\right)
530 .
531 $$
532 We can therefore compute (approximations of) transitive closures
533 of $R_1$ and $R_2$ separately.
534 Note, however, that the condition $R_1 \circ R_2 = \emptyset$
535 is actually too strong.
536 If $R_1 \circ R_2$ is a subset of $R_2 \circ R_1$
537 then we can reorder the segments
538 in any path that moves through both $R_1$ and $R_2$ to
539 first move through $R_1$ and then through $R_2$.
540
541 This idea can be generalized to relations that are unions
542 of more than two basic relations by constructing the
543 strongly connected components in the graph with as vertices
544 the basic relations and an edge between two basic relations
545 $R_i$ and $R_j$ if $R_i$ needs to follow $R_j$ in some paths.
546 That is, there is an edge from $R_i$ to $R_j$ iff
547 \begin{equation}
548 \label{eq:transitive:edge}
549 R_i \circ R_j
550 \not\subseteq
551 R_j \circ R_i
552 .
553 \end{equation}
554 The components can be obtained from the graph by applying
555 Tarjan's algorithm \shortcite{Tarjan1972}.
556
557 In practice, we compute the (extended) powers $K_i'$ of each component
558 separately and then compose them as in \eqref{eq:transitive:decompose}.
559 Note, however, that in this case the order in which we apply them is
560 important and should correspond to a topological ordering of the
561 strongly connected components.  Simply applying Tarjan's
562 algorithm will produce topologically sorted strongly connected components.
563 The graph on which Tarjan's algorithm is applied is constructed on-the-fly.
564 That is, whenever the algorithm checks if there is an edge between
565 two vertices, we evaluate \eqref{eq:transitive:edge}.
566 The exactness check is performed on each component separately.
567 If the approximation turns out to be inexact for any of the components,
568 then the entire result is marked inexact and the exactness check
569 is skipped on the components that still need to be handled.
570
571 \begin{figure}
572 \begin{center}
573 \begin{tikzpicture}[x=0.5cm,y=0.5cm,>=stealth,shorten >=1pt]
574 \foreach \x in {1,...,10}{
575     \foreach \y in {1,...,10}{
576         \draw[->] (\x,\y) -- (\x,\y+1);
577     }
578 }
579 \foreach \x in {1,...,20}{
580     \foreach \y in {5,...,15}{
581         \draw[->] (\x,\y) -- (\x+1,\y);
582     }
583 }
584 \end{tikzpicture}
585 \end{center}
586 \caption{The relation from \autoref{ex:closure4}}
587 \label{f:closure4}
588 \end{figure}
589 \begin{example}
590 \label{ex:closure4}
591 Consider the relation in example {\tt closure4} that comes with
592 the Omega calculator~\shortcite{Omega_calc}, $R = R_1 \cup R_2$,
593 with
594 $$
595 \begin{aligned}
596 R_1 & = \{\, (x,y) \to (x,y+1) \mid 1 \le x,y \le 10 \,\}
597 \\
598 R_2 & = \{\, (x,y) \to (x+1,y) \mid 1 \le x \le 20 \wedge 5 \le y \le 15 \,\}
599 .
600 \end{aligned}
601 $$
602 This relation is shown graphically in \autoref{f:closure4}.
603 We have
604 $$
605 \begin{aligned}
606 R_1 \circ R_2 &=
607 \{\, (x,y) \to (x+1,y+1) \mid 1 \le x \le 9 \wedge 5 \le y \le 10 \,\}
608 \\
609 R_2 \circ R_1 &=
610 \{\, (x,y) \to (x+1,y+1) \mid 1 \le x \le 10 \wedge 4 \le y \le 10 \,\}
611 .
612 \end{aligned}
613 $$
614 Clearly, $R_1 \circ R_2 \subseteq R_2 \circ R_1$ and so
615 $$
616 \left(
617 R_1 \cup R_2
618 \right)^+
619 =
620 \left(R_2^+ \circ R_1^+\right)
621 \cup R_1^+
622 \cup R_2^+
623 .
624 $$
625 \end{example}
626
627 \begin{figure}
628 \newcounter{n}
629 \newcounter{t1}
630 \newcounter{t2}
631 \newcounter{t3}
632 \newcounter{t4}
633 \begin{center}
634 \begin{tikzpicture}[>=stealth,shorten >=1pt]
635 \setcounter{n}{7}
636 \foreach \i in {1,...,\value{n}}{
637     \foreach \j in {1,...,\value{n}}{
638         \setcounter{t1}{2 * \j - 4 - \i + 1}
639         \setcounter{t2}{\value{n} - 3 - \i + 1}
640         \setcounter{t3}{2 * \i - 1 - \j + 1}
641         \setcounter{t4}{\value{n} - \j + 1}
642         \ifnum\value{t1}>0\ifnum\value{t2}>0
643         \ifnum\value{t3}>0\ifnum\value{t4}>0
644             \draw[thick,->] (\i,\j) to[out=20] (\i+3,\j);
645         \fi\fi\fi\fi
646         \setcounter{t1}{2 * \j - 1 - \i + 1}
647         \setcounter{t2}{\value{n} - \i + 1}
648         \setcounter{t3}{2 * \i - 4 - \j + 1}
649         \setcounter{t4}{\value{n} - 3 - \j + 1}
650         \ifnum\value{t1}>0\ifnum\value{t2}>0
651         \ifnum\value{t3}>0\ifnum\value{t4}>0
652             \draw[thick,->] (\i,\j) to[in=-20,out=20] (\i,\j+3);
653         \fi\fi\fi\fi
654         \setcounter{t1}{2 * \j - 1 - \i + 1}
655         \setcounter{t2}{\value{n} - 1 - \i + 1}
656         \setcounter{t3}{2 * \i - 1 - \j + 1}
657         \setcounter{t4}{\value{n} - 1 - \j + 1}
658         \ifnum\value{t1}>0\ifnum\value{t2}>0
659         \ifnum\value{t3}>0\ifnum\value{t4}>0
660             \draw[thick,->] (\i,\j) to (\i+1,\j+1);
661         \fi\fi\fi\fi
662     }
663 }
664 \end{tikzpicture}
665 \end{center}
666 \caption{The relation from \autoref{ex:decomposition}}
667 \label{f:decomposition}
668 \end{figure}
669 \begin{example}
670 \label{ex:decomposition}
671 Consider the relation on the right of \shortciteN[Figure~2]{Beletska2009},
672 reproduced in \autoref{f:decomposition}.
673 The relation can be described as $R = R_1 \cup R_2 \cup R_3$,
674 with
675 $$
676 \begin{aligned}
677 R_1 &= n \mapsto \{\, (i,j) \to (i+3,j) \mid
678 i \le 2 j - 4 \wedge
679 i \le n - 3 \wedge
680 j \le 2 i - 1 \wedge
681 j \le n \,\}
682 \\
683 R_2 &= n \mapsto \{\, (i,j) \to (i,j+3) \mid
684 i \le 2 j - 1 \wedge
685 i \le n \wedge
686 j \le 2 i - 4 \wedge
687 j \le n - 3 \,\}
688 \\
689 R_3 &= n \mapsto \{\, (i,j) \to (i+1,j+1) \mid
690 i \le 2 j - 1 \wedge
691 i \le n - 1 \wedge
692 j \le 2 i - 1 \wedge
693 j \le n - 1\,\}
694 .
695 \end{aligned}
696 $$
697 The figure shows this relation for $n = 7$.
698 Both
699 $R_3 \circ R_1 \subseteq R_1 \circ R_3$
700 and
701 $R_3 \circ R_2 \subseteq R_2 \circ R_3$,
702 which the reader can verify using the {\tt iscc} calculator:
703 \begin{verbatim}
704 R1 := [n] -> { [i,j] -> [i+3,j] : i <= 2 j - 4 and i <= n - 3 and
705                                   j <= 2 i - 1 and j <= n };
706 R2 := [n] -> { [i,j] -> [i,j+3] : i <= 2 j - 1 and i <= n and
707                                   j <= 2 i - 4 and j <= n - 3 };
708 R3 := [n] -> { [i,j] -> [i+1,j+1] : i <= 2 j - 1 and i <= n - 1 and
709                                     j <= 2 i - 1 and j <= n - 1 };
710 (R1 . R3) - (R3 . R1);
711 (R2 . R3) - (R3 . R2);
712 \end{verbatim}
713 $R_3$ can therefore be moved forward in any path.
714 For the other two basic relations, we have both
715 $R_2 \circ R_1 \not\subseteq R_1 \circ R_2$
716 and
717 $R_1 \circ R_2 \not\subseteq R_2 \circ R_1$
718 and so $R_1$ and $R_2$ form a strongly connected component.
719 By computing the power of $R_3$ and $R_1 \cup R_2$ separately
720 and composing the results, the power of $R$ can be computed exactly
721 using \eqref{eq:transitive:singleton}.
722 As explained by \shortciteN{Beletska2009}, applying the same formula
723 to $R$ directly, without a decomposition, would result in
724 an overapproximation of the power.
725 \end{example}