# CS345 Solutions to Final Exam

Problems 1-5 were graded by Edwina; 6-10 by Jeff. Unfortunately, Jeff lost the key with error codes. In Problems 6-10, there are lists of common errors, some without codes. From the finals that were not picked up, we were able to reconstruct some but not all of the codes' meanings. If you are not sure what the error on your exam is, show us the exam, and help reestablish the code.
1. (10 pts.)
a)
{a(0,1)} is a counterexample database.

-2 A: if database for Q1 was given

b)
{X}{Y}{Z}, {X,Y}{Z}, {X}{Y,Z}, {X,Z},{Y}

-1 per missing partition

c)
partition {X,Z}{Y} gives that database {a(0,1), a(1,0)}. Q2 contains its own frozen head. Q1(D) = {}.

2. (10 pts.) The diagram should have the following arcs:

• qr -> r
• r -> r
• p -> pq
• pq -> pq
• pqr -> {}
• pr -> {}
• {} -> pqr
• q -> pqr

A: {} -> qr, instead of {} -> pqr.

3. (15 pts.)
a)
The rule/goal graph should have nodes for p^bf, a^bf, q^bf, b^ff, p^bb, a^bb, q^bb, b^fb.

The rule/goal graph should have the following arcs:

p^bf -> r1.0[X|Y]
r1.0[X|Y] -> a^bf
p^bf -> r2.0[X|YZ]
r2.0[X|YZ] -> a^bf
r2.0[X|YZ] -> r2.1[XZ|Y]
r2.1[XZ|Y] -> q^bf

q^bf -> r3.0[X|YZ]
r3.0[X|YZ] -> b^ff
r3.0[X|YZ] -> r3.1[XYZ|]
r3.1[XYZ|] -> p^bb

p^bb -> r1.0[XY|]
r1.0[XY|] -> a^bb
p^bb -> r2.0[XY|Z]
r2.0[XY|Z] -> a^bf
r2.0[XY|Z] -> r2.1[XYZ|]
r2.1[XYZ|] -> q^bb

q^bb -> r3.0[XY|Z]
r3.0[XY|Z] -> b^fb
r3.0[XY|Z] -> r3.1[XYZ|]
r3.1[XYZ|] -> p^bb

b)
``` p_bf(X,Y) :- a(X,Y) p_bb(X,Y) :- a(X,Y) p_bf(X,Y) :- a(X,Z) & q_bf(Z,Y) p_bb(X,Y) :- a(X,Z) & q_bb(Z,Y) q_bf(X,Y) :- b(Z,Y) & p_bb(X,Z) q_bb(X,Y) :- b(Z,Y) & p_bb(X,Z) ```

c)
r1 and r2 remain the same. Change r3 to: ` q(X,Y) :- p(X,Z) & b(Z,Y)`

The rule/goal graph should have nodes for p^bf, a^bf, q^bf, b^bf.

The rule/goal graph should have the following arcs:

p^bf -> r1.0[X|Y]
r1.0[X|Y] -> a^bf
p^bf -> r2.0[X|YZ]
r2.0[X|YZ] -> a^bf
r2.0[X|YZ] -> r2.1[XZ|Y]
r2.1[XZ|Y] -> q^bf

q^bf -> r3.0[X|YZ]
r3.0[X|YZ] -> p^bf
r3.0[X|YZ] -> r3.1[XZ|Y|]
r3.1[XYZ|] -> b^bf

4. (15 pts.)
a)
``` ans^bff(X,Y,Z) :- par(X,Z) & par(Z,A) & par(Z,W) & par(W,B) & par(W,Y) & par(Y,C) ```

b)
Yes. All three subgoals use v2^bf, so the first parameter must be bound for all three. For the first subgoal, X is bound by the head. For the second, Z is bound by the first subgoal. For the third, W is bound by the second subgoal.

c)
Yes, E(S) is contained in Q. Containment mapping: X->X, Z->Z, W->W, Y->Y.

d)
No, Q is not contained in E(S). A counterexample database is {par(0,1), par(1,2), par(2,3)}. the frozen head of Q is ans^bff(0,3,1). E(S)(D) = {}; E(S)(D) doesn't contain the frozen head of Q.

-1 A: no counterexample database, but showed there was no containment mapping.

-2 B: wrong database.

e)
R: `ans^bff(X,Y,Z) :- v2^bf(X,Z) & v1^bf(Z,Y)`

E(R): ``` ans^bff(X,Y,Z) :- par(X,Z) & par(Z,B) & par(Z,C) & par(C,Y) ```

E(R) is contained in Q. Containment mapping: X->X, Y->Y, Z->Z, W->C.

Q is contained in E(R). Containment mapping: X->X, Y->Y, Z->Z, B->W, C->W.

Thus, E(R) is equivalent to Q.

5. (15 pts.)
a)
r1: subl(nil,nil)
r3: subl(nil, cons(1,nil))
r2: subl(cons(0,nil),cons(0,cons(1,nil))

-1 A: forgetting subl(nil, nil).

b)
Group I;
``` m_subl(Ys) :- sup2.1(X,Ys) m_subl(Ys) :- sup3.1(Y,Ys) ```

Group II;

``` sup2.0(X,Ys) :- m_subl(cons(X,Ys)) sup3.0(Y,Ys) :- m_subl(cons(Y,Ys)) ```

Group III;

``` sup2.1(X,Ys) :- sup2.0(X,Ys) & elt(X) sup3.1(Y,Ys) :- sup3.0(Y,Ys) & elt(Y) ```

Group IV;

``` subl(nil,nil) :- subl(cons(X,Xs),cons(X,Ys)) :- sup2.1(X,Ys) & subl(Xs,Ys) subl(Xs,cons(Y,Ys)) :- sup3.1(Y,Ys) & subl(Xs,Ys) ```

Group V;

``` m_subl(cons(0,cons(1,nil))) ```

c)
``` m_subl(Ys) :- sup2.1(X,Ys) m_subl(Ys) :- sup3.1(Y,Ys) sup2.1(X,Ys) :- m_subl(cons(X,Ys)) & elt(X) sup3.1(Y,Ys) :- m_subl(cons(Y,Ys)) & elt(Y) subl(nil,nil) :- subl(cons(X,Xs),cons(X,Ys)) :- sup2.1(X,Ys) & subl(Xs,Ys) subl(Xs,cons(Y,Ys)) :- sup3.1(Y,Ys) & subl(Xs,Ys) m_subl(cons(0,cons(1,nil))) ```

6. Part (a):
```     doctor(D) :- Admits(P,I,D)
```

Part (b):

```     ans(D) :- Admits(P1,I1,D) & Admits(P,I2,D2) & Admits(P3,mumps,D3) &
```

Part (c):

```     ans(D) :- Admits(P,I4,D) & Admits(P,mumps,D5)
```

Common error: 6A: mapping the first of the subgoals above to the second, and winding up with only one subgoal. You can't do the mapping, because D is a distinguished variable and can't map to D5. You lost 3 points, and if you made a similar error on part (e), you lost another 2 points.

Part (d):

```     doctor(D) :- Assigned(D,R)
patient(P) :- Beds(P,R)
illness(I) :- Wards(R,I)
treats(D,P) :- Beds(P,R) & Assigned(D,R)
has(P,I) :- Beds(P,R) & Wards(R,I)
```

Part (e):

```     Ans(D) :- Assigned(D,R1) & Beds(P,R2) & Wards(R3,mumps) &
Beds(P,R4) & Assigned(D,R4) & Beds(P,R5) & Wards(R5,mumps)
```

Part (f):

```     Ans(D) :- Beds(P,R4) & Assigned(D,R4) & Beds(P,R5) & Wards(R5,mumps)
```

Notice that neither of the two Beds subgoals maps to the other because either R4 or R5 gets in the way.

7. Part (a): Add subobjects with label age and an appropriate integer value.

Part (b): Patients should have zero or more history subobjects. Those objects will have atomic date subobjects and atomic diagnosis subobjects. They will also have subobjects that are doctor objects. In the diagram on p. 8, the arrow should go to the doctor object, not the name of the doctor.

Common errors included pointing to the name of the doctor rather than the doctor object itself (7B -1), and creating an addition level of object, called ``histories'' or something, of which each individual history was a subobject. I didn't take off for the latter, but it is wasted effort and serves no purpose.

Part (c):

```     <patient{<name N> <phone P>}> @ medMed :-
<doctor{<patient{<name N> <phone P>}>}> @ docSource
```

A common error was excluding doctor from the path in the doctor source (7C -2). Since there apparently are no patients at the top level at that source, you need to specify doctor in the path.

Part (d): This part was flawed. I thought it was harder than it was, but you can make a simple modification to (c) and avoid using function symbols as OID's.

```     <patient{<name N> <phone P> <doctor D>}> @ medMed :-
<doctor{<name D> <patient{<name N> <phone P>}>}> @ docSource
```

8. Part (a):
```     p(1) :- NOT q(1)
p(3) :- NOT q(3)
q(1) :- p(2)
q(2) :- p(3)
q(3) :-
```

Part (b): No; there is recursive negation involving p and q.

Part (c): Yes, it is locally stratified, as the graph of the six instantiated predicates q(i) and p(i) for i = 1, 2, 3 will show. Stratum 0 consists of {q(3), p(2), q(1)}, and stratum 1 is the other three ground atoms.

Part (d): Only q(3) is inferred.

Part (e): The largest unfounded set is {p(2), p(3), q(1), q(2)}.

Common error (8A -3): Many people failed to discover one or more of these and lost roughly one point per.

Part (f): The table looks like:

```     p(1) 0 1 1 1
p(2) 0 0 0 0
p(3) 0 1 0 0
q(1) 0 0 0 0
q(2) 0 1 0 0
q(3) 0 1 1 1
```

Common errors: First, many people thought that q(2) was false at the second round (8B; -2). The mistake was probably failing to complete the inferences at that round. Since p(3) is inferred at that round, we can use the fourth rule to infer q(2).

The second mistake was making q(2) true on the third round (8C; -2). The mistake there was probably assuming that we could use p(3), which was true on round 2, to infer q(2) on round 3. Again --- in this or the GL procedure (which are really the same), you can use negative facts from the previous round, but you must infer positive facts anew on the present round.

9. ```     par(X,f(X,Y)) :- v1(X,Y)
par(f(X,Y),Y) :- v1(X,Y)
par(X,g(X,Y)) :- v2(X,Y)
par(g(X,Y),h(X,Y)) :- v2(X,Y)
par(h(X,Y),Y) :- v2(X,Y)
```

A common mistake was not using the three different function symbols that you need, and you lost one point for each lapse (9C -1).

Part (b):

```     par(0,f(0,1))
par(f(0,1),1)
par(1,g(1,2))
par(g(1,2),h(1,2))
par(h(1,2),2)
```

Part (c): There are many possible answers, such as anc(1,h(1,2)), anc(g(1,2),2), and anc(f(0,1),g(1,2)).

Part (d): anc(0,1), anc(1,2), and anc(0,2). You lost one point for each one you missed, usually the third of these.

10. This one was supposed to be hard, and it was, although a few people were able to handle it, for which I am glad. I was surprised, however, at how many people had forgotten their combinatorics.

Part (a): First, you had to realize that every hypergraph with two hyperedges is acyclic. Then, you had to count them right. Many people said there were 2^5-1 = 31 nonempty values of H, and an equal number of values of I. However, multiplying 31 * 31 = 961 is wrong, because it ignores the condition that each node is in at least one edge.

The correct way to count is to let each of the five nodes be assigned to one of three categories: only in H, only in I, or in both. There are thus 3^5 = 243 assignments. That would be the number of hypergraphs that meet the conditions, but at the exam we said that neither hyperedge could be empty. Thus, we must subtract the two assignments in which one is empty, and all five nodes are in the other. The correct answer is 241, but 243 was also accepted.

Part (b): The answer is u AND s AND w. If this condition is satisfied, then it is easy to see that none of the three edges is an ear.

We need to check that if the condition is false, then there is an ear. Once we know there is an ear, then coupled with the fact that any two-edge hypergraph is acyclic, we know that the three-edge hypergraph is also acyclic.

Suppose for example that u is empty. Then hyperedge H consists of regions r, s, and v only. Nodes in r belong only to H, while nodes in the other two regions are shared only with I. Thus, H is an ear.

Part (c): Now to count the number of hypergraphs that satisfy the condition of (b). The easiest way, which I had forgotten, is to use the law of inclusion-exclusion. Start with the total number of hypergraphs, which is 7^5 = 16,807. The justification for this number is that each of the five nodes can go into any of the seven regions. From these, we must subtract the number that do not have a node in u, which is 6^5 = 7776. Likewise, we must subtract the number that do not have a node in s, and those that do not have a node in w --- the same number, 7776, in each case.

However, that calculation double-counts the hypergraphs that are missing nodes in both u and s, or missing nodes in two of the other three regions mentioned in the formula of part (b). Thus, we must add back 3*5^5 = 3*3125 = 9375, the extent of this double counting. However, we are not done, because the hypergraphs that are missing nodes in all three of the part-(b) regions have been subtracted three times and added back three times; they must thus be subtracted again. These are 4^5 = 1024 hypergraphs. The final answer is thus 16807 - 3*7776 + 3*3125 - 1024 = 1830.

If that methodology is unfamiliar, there is a more seat-of-the-pants argument that gets the same answer. There are four ways that the nodes can be distributed:

1. One each to u, s, and w; the other two nodes elsewhere. The number of these assignments is 5*4*3*4*4 = 960. That is, we can choose any of 5 nodes for u, 4 for s, 3 for w, and the remaining two nodes can each go into any of the other four regions.

2. 2 to one of u, s, and w, one to each of the others, and the last node to another region. This count is 3*10*3*2*4 = 720. The 3 is for the choice of which region gets two nodes, 10 is the choice of those two nodes, the next 3 is the choice of node for the next of the shared regions, 2 is the chocie of node for the last of the three shared regions, and 4 is the choice of where else to put the last node.

3. Two nodes to two of the three shared regions, and one to the remaining such region. The count is 3*10*3 = 90; 3 is for which region gets only one node, 10 is the choice of nodes for the first region of size two, and the last three is for which of the remaining three nodes is in a region by itself.

4. Three nodes in one of the shared regions, one each in the other two. This count is 3*10*2 = 60. The 3 is for the choice of which region gets three nodes, the 10 is for the choice of three of the five nodes for that region, and the 2 is for which of the two remaining nodes goes into which of the two remaining regions.

Note that 960 + 720 + 90 + 60 = 1830.

Common errors: (10A -1): not expanding an expression. (10B -2): Failure to account for duplicate counting in part (c).