## Solutions to CS345 Midterm

### Problem 1

Basis: P = DP = A (the relation for predicate a.

Induction: (Note: we'll use dp as the predicate corresponding to relation DP or "delta-P.")

1.

``` dp(X,Y) := a(X,Z) & a(W,Y) & p(X,W) & p(Z,Y) & dp(Z,W) union a(X,Z) & a(W,Y) & p(X,W) & dp(Z,Y) & p(Z,W) union ```
``` a(X,Z) & a(W,Y) & dp(X,W) & p(Z,Y) & p(Z,W) ```
2. DP = DP - P

3. P = P union DP

### Problem 2

``` f_a(X,Y) :- m(X,Y) f_a(X,Y) :- m(X,Z) & f_a(Z,Y) ```
``` f_z(X,Y) :- f(X,Z) & f_a(Z,Y) ```

### Problem 3

a) a and b are in stratum 0, p in stratum 1, and q and r in stratum 2. The positive arcs of the dependency graph are p->a, q->a, and r->q; the negative arcs are p->b, q->p, and r->b.

b) p(1,2), q(1,2), q(2,3), and r(2,3). c) One possible choice: p(1,2), p(3,2), and q(1,2).

### Problem 4

a)
Atom Round 1 Round 2 Round 3
win(1) 0 1 0
win(2) 0 1 0
win(3) 0 1 1
win(4) 0 0 0

The columns continue to oscillate as above.

b) win(3), NOT win(4)

c) win(1), win(2), win(3)

### Problem 5

a) b)
The rules are not stratified because there is a negative cycle on q.

c)
The relation for p is {(1,2), (2,3), (3,2), (3,3), (2,2), (1,3)}. The instantiations of rule (3) for the given EDB are:
``` q(1,2) :- NOT q(2,1) q(2,3) :- NOT q(3,2) q(3,2) :- NOT q(2,3) q(3,3) :- NOT q(3,3) q(2,2) :- NOT q(2,2) ```
``` q(1,3) :- NOT q(3,1) ```

d) e)
The rules are not modularly stratified for the given EDB because there are negative cycles in the module containing q and thus this module is not locally stratified.

f)
The rules will be modularly stratified if A contains no cycles. If there are no cycles in A then there will be no cycles in the relation for p which in turn leads to no negative cycles in the relation for q.

### Problem 6

a)
D = {b(0), a(0,1)}.

b)
P(D) = {p(0)}.

c)
Q is contained in P since P(D) contains the frozen head of Q.

### Problem 7

a)
Check if the variable in the head of R appears as the second argument of a subgoal in R. If so then return False. Otherwise check if any variable in R appears as both a first and a second argument in subgoals in R. If so return False. Otherwise return True.

b)
Always return True since Q is contained in any "plausible" query R.

### Problem 8

a)
D is contained in Cn for all even n.

b)
For every even n there are two containment mappings:
1. X -> X, X{2i-1} -> Y, X{2i} -> Z, for i=1..n/2
2. X -> X, X{2i-1} -> Z, X{2i} -> Y, for i=1..n/2
c)
The smallest n for which the containment does not hold is 3. Consider the canonical database constructed from D: F = {p(0,1), p(0,2), q(1,2), q(2,1)}. Then D(F) = {s(0)} and C3(F) is empty. Therefore D is not contained in C3.