### Problem 1 (15 pts.)

Consider the conjunctive queries A and B.
``` A: p(X,Y) :- q(X,Z) & q(Z,T) & r(T,Y). B: ```
``` p(X,Z) :- q(X,X) & r(X,Z). ```
Is A contained in B? Is B contained in A? Justify your answers by offering containment mappings or showing that none exists.

### Solution

B is contained in A since we have a containment mapping {X->X, Y->Z, Z->X, T->X} from A to B. A is not contained in B since no containment mapping from B to A exists. Consider the subgoal q(X,X) in B. Any containment mapping from B to A must map it to either q(X,Z) or q(Z,T). Since in both cases X must be mapped to two different variables, no containment mapping exists.

### Problem 2 (20 pts.)

Consider the infinite sequence of conjunctive queries Q1, Q2, ..., where Qi is:
``` ```
``` q(X) :- arc(X,Z1) & arc(Z1,Z2) & ... & arc(Z{i-1},Zi) & arc(Zi,X). ```
Note that Qi represents (not necessarily simple) cycles of length i+1 in a directed graph. Find all containments among the Q's.

### Solution

For all positive i and n, Qi is contained Q{n(i+1)-1} since we have the following containment mapping from Q{n(i+1)-1} to Qi.
• X -> X,
• Z{k(i+1)} -> X for k=1,2...n-1,
• Z{k(i+1)+m} -> Zm for k=0,1...n-1 and m = 1,2...i.

### Problem 3 (15 pts.)

Consider the conjunctive queries A and B.
``` A: p(X) :- q(X,Y,X) & q(X,Z,Y) & r(Y,X). B: ```
``` p(X) :- q(X,Y,Z) & r(Y,Z). ```
Show that A is contained in B by using the method of canonical databases.

### Solution

The canonical database from A is D = {q(0,1,0), q(0,2,1), r(1,0)}. Since the frozen head of A, p(0), is contained in B(D) = {p(0)}, then A is contained in B. The instantiation of B that shows that p(0) is in B(D) is {X->0, Y->1, Z->0}.

### Problem 4 (30 pts.)

Consider the conjunctive queries A and B.
``` A: p(X) :- q(X,Y) & r(Y,Z) & s(Z,W) & s(X,Z). B: ```
``` p(X) :- q(X,Y) & q(X,Z) & r(Y,Z) & r(Z,Y) & s(Y,Z) & s(X,Y). ```
Test whether B is contained in A by using Saraiya's containment test. However, you must start by mapping q(X,Y) to q(X,Y) (this requirement is to make sure that you go through the major steps of the algorithm). Indicate at each step what inferences are made.

### Solution

```(1) Suppose q(X,Y) -> q(X,Y)
(2)	Then X->X, Y->Y
(3)	r(Y,Z) -> r(Y,Z), Z->Z
(4)	Now s(Z,W) -> s(Z,?)
(5)	    Since there is no s(Z,?) in B, fail
(6) Then we must map q(X,Y) to q(X,Z)
(7) 	Then X->X, Y->Z
(8)	r(Y,Z) -> r(Z,Y), Z->Y
(9)	s(Z,W) -> s(Y,Z), W->Z
(10)	s(X,Z) -> s(X,Y)
```
That completes the containment mapping successfully, with X->X, Y->Z, Z->Y, W->Z.

### Problem 5 (20 pts.)

Find all equivalences and containments among the four conjunctive queries below. Note that c is a constant in Q4.
``` Q1: p(X,Y) :- q(X,A) & q(A,B) & q(B,Y). Q2: p(X,Y) :- q(X,A) & q(A,B) & q(B,C) & q(C,Y). Q3: p(X,Y) :- q(X,A) & q(B,C) & q(D,Y) & q(X,B) & q(A,C) & q(C,Y). Q4: ```
``` p(X,Y) :- q(X,A) & q(A,c) & q(c,B) & q(B,Y). ```

### Solution

1. Q1 is contained in Q3 since we have the containment mapping {X->X, A->A, C->B, Y->Y, B->A, D->B} from Q3 to Q1.
2. Q3 is contained in Q1 since we have the containment mapping {X->X, A->A, B->C, Y->Y} from Q1 to Q3.
3. From 1 and 2 we have that Q1 is equivalent to Q3.
4. Q4 is contained in Q2 because we have the containment mapping {X->X, A->A, c->B, B->C, Y->Y} from Q4 to Q2.
5. Since Q4 includes a constant that does not appear in any of the other queries, Q4 does not contain any other query.
6. Q2 does not contain Q1. Suppose there is a containment mapping from Q2 to Q1. Then, from the heads, X->X, Y->Y. Then q(X,A) -> q(X,A) so A->A. Then q(A,B) -> q(A,B) so B->B. Then q(B,C) -> q(B,Y) so C->Y. Then q(C,Y) -> q(Y,Y) but q(Y,Y) is not a subgoal in Q1.
7. Q1 does not contain Q2. Suppose there is a containment mapping from Q1 to Q2. Then, from the heads, X->X, Y->Y. Then q(X,A) -> q(X,A), so A->A. Then q(A,B) -> q(A,B), so B->B. Then q(B,Y) -> q(B,Y) but q(B,Y) is not a subgoal in Q2. Thus, we conclude that Q1 is equivalent to Q3 and Q2 contains Q4.