A NOTE ON RECURSION

Consider the following edited extract from Exercise 5.16 in Chapter 5:

The well known bill of materials application involves a relvar—PP, say—showing which parts contain which parts as immediate components. Of course, immediate components are themselves parts, and they can have further immediate components of their own.

Figure 7-5 below shows (a) a sample value for that relvar PP and (b) the corresponding transitive closure TC.[108] The predicates are as follows:

  • PP: Part PX contains part PY as an immediate component.

  • TC: Part PX contains part PY as a component at some level (not necessarily immediate).

Relvars PP and TC (sample values)

Figure 7-5. Relvars PP and TC (sample values)

Given a (relation) value pp for relvar PP, the relation tc that’s the transitive closure of pp can be defined as follows:

Definition: The pair (px,py) appears in tc if and only if:

  1. It appears in pp, or

  2. There exists some pz such that the pair (px,pz) appears in pp and the pair (pz,py) appears in tc.

In other words, if we think of pp as representing a directed graph, with a node for each part and an arc from each node to each corresponding immediate component node, then (px,py) appears in the transitive closure if and only if there’s a path in that graph from node px to node py. Observe that the definition involves a recursive reference to relation tc.

Aside: In practice relvar PP would probably have a QTY attribute as well (showing ...

Get SQL and Relational Theory, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.