A Mathematical Preliminaries and Notation

The third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including full text).

A.3 Relations and the Relational Algebra

Relations are common in AI and database systems. The relational algebra defines operations on relations and is the basis of relational databases.

A scope S is a set of variables. A tuple t on scope S, has a value on each variable in its scope. A variable can be seen as a function on tuples; one that returns the value for that variable for that tuple. We write X⁢(t) to be the value of tuple t on variable X. The value of X⁢(t) must be in d⁢o⁢m⁢(X). This is like the mathematical notion of tuple, except the index is given by a variable, not by an integer.

A relation is a set of tuples, all with the same scope. A relation is often given a name. The scope of the tuples is often called the relation scheme. A relational database is a set of relations. A scheme of a relational database is the set of pairs of relation names and relation schemes.

A relation with scope X1,…,Xn can be seen as a Boolean factor on X1,…,Xn, where the true elements are represented as tuples.

Often a relation is written as a table.

Example A.2.

The following is a tabular depiction of a relation, e⁢n⁢r⁢o⁢l⁢l⁢e⁢d:

C⁢o⁢u⁢r⁢s⁢eY⁢e⁢a⁢rS⁢t⁢u⁢d⁢e⁢n⁢tG⁢r⁢a⁢d⁢ec⁢s⁢3222008f⁢r⁢a⁢n77c⁢s⁢1112009b⁢i⁢l⁢l⁢i⁢e88c⁢s⁢1112009j⁢e⁢s⁢s78c⁢s⁢4442008f⁢r⁢a⁢n83c⁢s⁢3222009j⁢o⁢r⁢d⁢a⁢n92

The heading gives the scheme, namely {C⁢o⁢u⁢r⁢s⁢e,Y⁢e⁢a⁢r,S⁢t⁢u⁢d⁢e⁢n⁢t,G⁢r⁢a⁢d⁢e}, and every other row is a tuple. The first tuple, call it t1 is defined by C⁢o⁢u⁢r⁢s⁢e⁢(t1)=c⁢s⁢322, Y⁢e⁢a⁢r⁢(t1)=2008, S⁢t⁢u⁢d⁢e⁢n⁢t⁢(t1)=f⁢r⁢a⁢n, G⁢r⁢a⁢d⁢e⁢(t1)=77.

The order of the columns and the order of the rows is not significant.

If r a relation with scheme S, and c is a condition on the variables in S, the selection of c in r, written σc⁢(r), is the set of tuples in r for which c holds. The selection has the same scheme as r.

If r is a relation with scheme S, and S0⊆S, the projection of r onto S0, written πS0⁢(r), is the set of tuples of r where the scope is restricted to S0.

Example A.3.

Suppose e⁢n⁢r⁢o⁢l⁢l⁢e⁢d is the relation given in Example A.2.

The relation σG⁢r⁢a⁢d⁢e>79⁢(e⁢n⁢r⁢o⁢l⁢l⁢e⁢d) selects those tuples in e⁢n⁢r⁢o⁢l⁢l⁢e⁢d where the grade is over 79. This is the relation:

C⁢o⁢u⁢r⁢s⁢eY⁢e⁢a⁢rS⁢t⁢u⁢d⁢e⁢n⁢tG⁢r⁢a⁢d⁢ec⁢s⁢1112009b⁢i⁢l⁢l⁢i⁢e88c⁢s⁢4442008f⁢r⁢a⁢n83c⁢s⁢3222009j⁢o⁢r⁢d⁢a⁢n92

The relation π{S⁢t⁢u⁢d⁢e⁢n⁢t,Y⁢e⁢a⁢r}⁢(e⁢n⁢r⁢o⁢l⁢l⁢e⁢d) specifies what years students were enrolled:

S⁢t⁢u⁢d⁢e⁢n⁢tY⁢e⁢a⁢rf⁢r⁢a⁢n2008b⁢i⁢l⁢l⁢i⁢e2009j⁢e⁢s⁢s2009j⁢o⁢r⁢d⁢a⁢n2009

Notice how the first and the fourth tuple of e⁢n⁢r⁢o⁢l⁢l⁢e⁢d become the same tuple in the projection; they represent the same function on {S⁢t⁢u⁢d⁢e⁢n⁢t,Y⁢e⁢a⁢r}.

If two relations on the same scheme, the union, intersection and set difference of these are defined as the corresponding operations on the set of tuples.

If r1 and r2 are two relations, the natural join of r1 and r2, written r1⋈r2 is a relation where

  • •

    the scheme of the join is the union of the scheme of r1 and the scheme of r2,

  • •

    a tuple is in the join, if the tuple restricted to the scope of r1 is in the relation r1 and the tuple restricted to the scope of r2 is in the relation r2.

Example A.4.

Consider the relation a⁢s⁢s⁢i⁢s⁢t⁢e⁢d:

C⁢o⁢u⁢r⁢s⁢eY⁢e⁢a⁢rT⁢Ac⁢s⁢3222008y⁢u⁢k⁢ic⁢s⁢1112009s⁢a⁢mc⁢s⁢1112009c⁢h⁢r⁢i⁢sc⁢s⁢3222009y⁢u⁢k⁢i

The join of e⁢n⁢r⁢o⁢l⁢l⁢e⁢d and a⁢s⁢s⁢i⁢s⁢t⁢e⁢d, written e⁢n⁢r⁢o⁢l⁢l⁢e⁢d⋈a⁢s⁢s⁢i⁢s⁢t⁢e⁢d is the relation:

C⁢o⁢u⁢r⁢s⁢eY⁢e⁢a⁢rS⁢t⁢u⁢d⁢e⁢n⁢tG⁢r⁢a⁢d⁢eT⁢Ac⁢s⁢3222008f⁢r⁢a⁢n77y⁢u⁢k⁢ic⁢s⁢1112009b⁢i⁢l⁢l⁢i⁢e88s⁢a⁢mc⁢s⁢1112009j⁢e⁢s⁢s78s⁢a⁢mc⁢s⁢1112009b⁢i⁢l⁢l⁢i⁢e88c⁢h⁢r⁢i⁢sc⁢s⁢1112009j⁢e⁢s⁢s78c⁢h⁢r⁢i⁢sc⁢s⁢3222009j⁢o⁢r⁢d⁢a⁢n92y⁢u⁢k⁢i

Note how in the join, the information about c⁢s⁢444 was lost, as there was no TA in that course.