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