How to Split a Logic Program

Answer Set Programming (ASP) is a successful method for solving a range of real-world applications. Despite the availability of fast ASP solvers, computing answer sets demands a very large computational power, since the problem tackled is in the second level of the polynomial hierarchy. A speed-up in answer set computation may be attained, if the program can be split into two disjoint parts, bottom and top. Thus, the bottom part is evaluated independently of the top part, and the results of the bottom part evaluation are used to simplify the top part. Lifschitz and Turner have introduced the concept of a splitting set, i.e., a set of atoms that defines the splitting. In this paper, We show that the problem of computing a splitting set with some desirable properties can be reduced to a classic Search Problem and solved in polynomial time. This allows us to conduct experiments on the size of the splitting set in various programs and lead to an interesting discovery of a source of complication in stable model computation. We also show that for Head-Cycle-Free programs, the definition of splitting sets can be adjusted to allow splitting of a broader class of programs.


Introduction
Answer Set Programming (ASP) is a successful method for solving a range of real-world applications. Despite the availability of fast ASP solvers, the task of computing answer sets demands extensive computational power, because the problem tackled is in the second level of the polynomial hierarchy. A speed-up in answer set computation may be gained, if the program can be divided into several modules in which each module is computed separately [14,12,9]. Lifschitz and Turner propose to split a logic program into two disjoint parts, bottom and top, such that the bottom part is evaluated independently from the top part, and the results of the bottom part evaluation are used to simplify the top part. They have introduced the concept of a splitting set, i.e., a set of atoms that defines the splitting [14]. In addition to inspiring incremental ASP solvers [10], splitting sets are shown to be useful also in investigating answer set semantics [4,15,9].
In this paper we raise and answer two questions regarding splitting sets. The first question is, how do we compute a splitting set? We show that if we are looking for a splitting set having a desirable property that can be tested efficiently, we can find it in polynomial time. Examples of desirable splitting sets can be minimum-size splitting sets, splitting sets that include certain atoms, or splitting sets that define a bottom part with minimum number of rules or bottom that are easy to compute, for example, a bottom which is an HCF program [3]. Once we have an effcient algorithm for computing splitting sets, we can use it to investigate the size of a minimal noempty splitting set and discover some interesting results that explain the source of complexity in computing stable models.
Second, we ask if it is possible to relax the definition of splitting sets such that we can now split programs that could not be split using the original definition. We answer affirmatively to the second question as well, and we present a more general and relaxed definition of a splitting set.

Disjunctive Logic Programs and Stable Models
A propositional Disjunctive Logic Program (DLP) is a collection of rules of the form A 1 | . . . |A k ←− A k+1 , . . . , A m , not A m+1 , . . . , not A n , n ≥ m ≥ k ≥ 0, where the symbol "not " denotes negation by default, and each A i is an atom (or variable). For k + 1 ≤ i ≤ m, we will say that A i appears positive in the body of the rule, while for m + 1 ≤ i ≤ n, we shall say that A i appears negative in the body of the rule. If k = 0, then the rule is called an integrity rule. If k > 1, then the rule is called a disjunctive rule. The expression to the left of ←− is called the head of the rule, while the expression to the right of ←− is called the body of the rule. Given a rule r, head(r) denotes the set of atoms in the head of r, and body(r) denotes the set of atoms in the body of r. We shall shall sometimes denote a rule by H ←− B pos , B neg , where B pos is the set of positive atoms in the body of the rule (A k+1 , ..., A m ), B neg is the set of negated atoms in the body of the rule (A m+1 , ..., A n ), and H the set of atoms in its head. Given a program P, Lett(P) is the set of all atoms that appear in P. From now, when we refer to a program, it is a DLP.
Stable Models [11] of a program P are defined as Follows: Let Lett(P) denote the set of all atoms occurring in P. Let a context be any subset of Lett(P). Let P be a negation-by-default-free program. Call a context S closed under P iff for each rule A 1 | . . . |A k ← A k+1 , . . . , A m in P, if A k+1 , . . . , A m ∈ S, then for some i = 1, . . . , k, A i ∈ S. A Stable Model of P is any minimal context S, such that S is closed under P . A stable model of a general DLP is defined as follows: Let the reduct of P w.r.t. P and the context S be the DLP obtained from P by deleting (i) each rule that has not A in its body for some A ∈ S, and (ii) all subformulae of the form not A of the bodies of the remaining rules. Any context S which is a stable model of the reduct of P w.r.t. P and the context S is a stable model of P.
HCF-Head Cycle Free programs [3] are DLPs such that in the associated dependency graph there is no cycle including two atoms occurring in the head of the same rule.
Definition 2.1 A set of atoms S satisfies the body of a rule r if all the atoms that appear positive in the body of r are in S and all the atoms that appear negative in r are not in S. A set of atoms S satisfies a rule if it does not satisfy the body of the rule r or if one of the atoms in head(r) is in S.
According to [3], a proof of a atom is a sequence of rules that can be used to derive the atom from the program. Definition 2.2 ( [3]) An atom L has a proof w.r.t. a set of atoms S and a logic program P if and only if there is a sequence of rules r 1 , ..., r n from P such that: 1. for all 1 ≤ i ≤ n there is one and only one atom in the head of r i that belongs to S.
2. L is the head of r n .
3. for all 1 ≤ i ≤ n, the body of r i is satisfied by S. 4. r 1 has no atoms that appear positive in its body, and for each 1 < i ≤ n, each atom that appears positive in the body of r i is in the head of some r j for some 1 ≤ j < i.
Note that given a proof r 1 , ..., r n of some atom L w.r.t. a set of atoms S and a logic program P, for every 1 ≤ i ≤ n r 1 , ..., r i is also a proof of some atom L w.r.t. S and P.

Theorem 2.3 ([3])
Let P be an HCF DLP. Then a set of atoms S is an answer set of P if and only if S satisfies each rule in P and each a ∈ S has a proof with respect to P and S.

Programs and graphs
With every program P we associate a directed graph, called the dependency graph of P, in which (a) each atom in Lett(P) is a node, and (b) there is an arc directed from a node A to a node B if there is a rule r in P such that A ∈ body(r) and B ∈ head(r).
A super-dependency graph SG is an acyclic graph built from a dependency graph G as follows: For each strongly connected component (SCC) c in G there is a node in SG, and for each arc in G from a node in a strongly connected component c 1 to a node in a strongly connected component c 2 (where c 1 = c 2 ) there is an arc in SG from the node associated with c 1 to the node associated with c 2 . A program P is Head-Cycle-Free (HCF), if there are no two atoms in the head of some rule in P that belong to the same component in the super-dependency graph of P [3]. Let G be a directed graph and SG be a super dependency graph of G. A source in G (or SG) is a node with no incoming edges. By abuse of terminology, we shall sometimes use the term "source" or "SCC" as the set of nodes in a certain source or a certain SCC in SG, respectively, and when there is no possibility of confusion we shall use the term rule for the set of all atoms that appears in the rule. Given a node v in G, scc(v) denotes the set of all nodes in the SCC in SG to which v belongs, and tree(v) denotes the set of all nodes that belongs to any SCC S such that there is a path in SG from S to scc(v). Similarly, when S is a set of nodes, tree(S) is the union of all tree(v) for every v ∈ S. Given a node v in G, scc(v) will be sometimes called the root of tree(v). For example, given the super dependency graph in Figure 1, scc(e) = {e, h}, tree(e) = {a, b, e, h}, A source in a program will serve as a shorthand for "a source in the super dependency graph of the program." Given a source S of a program P, P S denotes the set of rules in P that uses only atoms from S. Example 2.4 (Running Example) Suppose we are given the following program P: Figure 1 the dependency graph of P is illustrated in solid lines. The SG is marked with dotted lines. Note that {a, b} is a source in the SG of P, but it is not a splitting set.

Splitting Sets
The definitions of Splitting Set and the Splitting Set Theorem are adopted from a paper by Lifschitz and Turner [14]. We restate them here using the notation and the limited form of programs discussed in our work.

Definition 2.5 (Splitting Set)
A Splitting Set for a program P is a set of of atoms U such that for each rule r in P, if one of the atoms in the head of r is in U, then all the atoms in r are in U. We denote by b U (P) the set of all rules in P having only atoms from U.
The empty set is a splitting set for any program. For an example of a nontrivial splitting set, the set {a, b, e, h} is a splitting set for the program P introduced in Example 2.4. The set b {a,b,e,h} (P) is For the Splitting set theorem, we need the a procedure called Reduce, which resembles many reasoning methods in knowledge representation, as, for example, unit propagation in DPLL and other constraint satisfaction algorithms [5,6]. Reduce(P,X,Y ) returns the program obtained from a given program P in which all atoms in X are set to true, and all atoms in Y are set to false. Reduce(P,X,Y ) is shown in Figure Reduce. For example, Reduce(P,{a, e, h},{b}), where P is the program from Example 2.4, is Procedure Reduce(P,X,Y ) Input: A program P and two sets of atoms: X and Y Output: An update of P assuming all the atoms in X are true and all atoms in Y are false 1 foreach atom a ∈ X do 2 foreach rule r in P do 3 If a appears negative in the body of r delete r ; 4 If a is in the head of r delete r; 5 Delete each positive appearance of a in the body of r; 6 foreach atom a ∈ Y do 7 foreach rule r in P do 8 If a appears positive in the body of r, delete r ; 9 If a is in the head of r, delete a from the head of r; 10 Delete each negative appearance of a in the body of r; 11 return P; the following program (the numbers of the rules are the same as the corresponding rules of the program in Example 2.4): Theorem 2.6 (Splitting Set Theorem) (adopted from [14]) Let P be a program, and let U be a splitting set for P. A set of atoms S is a stable model for P if and only if S = X ∪ Y , where X is a stable model of b U (P), and Y is a stable of Reduce(P, X,U − X).
As seen in Example 2.4, a source is not necessarily a splitting set. A slightly different definition of a dependency graph is possible. The nodes are the same as in our definition, but in addition to the edges that we already have, we add a directed arc from a variable A to a variable B whenever A and B are in the head of the same rule. It is clear that a source in this variation of dependency graph must be a splitting set. The problem is that the size of a dependency graph built using this new definition may be exponential in the size of the head of the rules, while we are looking for a polynomial-time algorithm for computing a nontrivial splitting set.

Search Problems
The area of search is one of the most studied and most known areas in AI (see, for example, [16]). In this paper we show how the problem of computing a nontrivial minimum-size splitting set can be expressed as a search problem. We first recall basic definitions in the area of search. A search problem is defined by five elements: set of states, initial state, actions or successor function, goal test, and path cost. A solution is a sequence of actions leading from the initial state to a goal state. Figure 2 provides a basic search algorithm [17].
There are many different strategies to employ when we choose the next leaf node to expand. In this paper we use uniform cost, according to which we expand the leaf node with the lowest path cost.

Between Splitting Sets and Dependency Graphs
In this section we show that a splitting set is actually a tree in the SG of the program P. The first lemma states that if an atom Q is in some splitting set, all the atoms in scc(Q) must be in that splitting set as well.   3.1 Let P be a program, let SP be a Splitting Set in P, let Q ∈ SP, and let S = scc(Q). It must be the case that S ⊆ SP.
Proof: Let R ∈ S. We will show that R ∈ SP. Since Q ∈ S, and S is a strongly connected component, it must be that for each Q ∈ S there is a path in SG -the super dependency graph of P -from Q to Q, such that all the atoms along the path belong to S. The proof goes by induction on i, the number of edges in the shortest path from Q to Q.
Case i = 0. Then Q = Q , and so obviously Q ∈ SP.

Induction
Step. Suppose that for all atoms Q ∈ S, such that the shortest path from Q to Q is of size i, Q belongs to SP. Let R be an atom in S, such that the shortest path from R to Q is of size i + 1. So, there must be an atom R such that there is an edge in SG from R to R , and the shortest path from R to Q is of size i. By the induction hypothesis, R ∈ SP. Since there is an edge from R to R in SG, it must be that there is a rule r in P, such that R ∈ body(r) and R ∈ head(r). Since R ∈ SP and SP is a Splitting Set, it must be the case that R ∈ SP.
Lemma 3.2 Let P be a program, let SP be a Splitting Set in P, let r be a rule in P, and S an SCC in SG -the super dependency graph of P. If head(r) ∩ SP = / 0, then tree(r) ⊆ SP.
Proof: We will show that for every Q ∈ r, tree(Q) ⊆ SP. Let Q ∈ r. The set tree(Q) is a union of SCCs. We shall show that for every SCC S such that S ⊆ tree(Q), S ⊆ SP. Let S be the root of tree(Q). The proof is by induction on the distance i from S to S .
Case i = 0. Then S = S , and S is the root of tree(Q). Since head(r) ∩ SP = / 0, Q ∈ r and SP is a splitting set, Q ∈ SP. So by Lemma 3.1 S ⊆ SP.

Induction
Step. Suppose that for all SCCs S ∈ tree(Q) such that the distance from S to S is of size i S ⊆ SP. Let R be an SCC in tree(r), such that the distance from R to S is of size i + 1. So, there must be an SCC R , such that there is an edge in tree(r) from R to R , and the distance from R to S is of size i. By the induction hypothesis, R ⊆ SP. Since there is an edge from R to R in tree(Q), it must be the case that there is a rule r in P, such that an atom from R, say P, is in body(r), and an atom from R , say P , is in head(r). By induction hypothesis, P ∈ SP, and since SP is a Splitting Set, it must be that P ∈ SP. By Lemma 3.1, R ⊆ SP.

Corollary 3.3 Every Splitting set is a collection of trees.
Note that the converse of Corollary 3.3 does not hold. In our running example, for instance, tree(g) = {c, d, g}, but {c, d, g} is not a splitting set.

Computing a minimum-size Splitting Set as a search problem
We shall now confront the problem of computing a splitting set with a desirable property. We shall focus on computing a nontrivial minimum-size splitting set. Given a program P, this is how we view the task of computing a nontrivial minimum-size splitting set as a search problem. We assume that there is an order over the rules in the program.
State Space. The state space is a collection of forests which are subgraphs of the super dependency graph of P.
Initial State. The empty set.
Actions. 1. The initial state can unite with one of the sources in the super dependency graph of P. 2. A state S, other than the initial state, has only one possible action, which is: (a) Find the lowest rule r (recall that the rules are ordered) such that head(r) ∩ S = / 0 and Lett(r) ⊆ S; (b) Unite S with tree(r).
Transition Model The result of applying an action on a state S is a state S that is a superset of S as the actions describe.
Goal Test A state S is a goal state, if there is no rule r ∈ P such that head(r) ∩ S = / 0 and Lett(r) ⊆ S. (In other words, a goal state is a state that represents a splitting set.); Path Cost The cost of moving from a state S to a state S is |S | − |S|, that is the number of atoms added to S when it was transformed to S . So, the path cost is actually the number of atoms in the final state of the path.
Once the problem is formulated as a search problem, we can use any of the search algorithms developed in the AI community to solve it. We do claim here, however, that the computation of a nontrivial minimum-size splitting set can be done in time that is polynomial in the size of the program. This search problem can be solved, for example, by a search algorithm called Uniform Cost. Algorithm Uniform Cost [17] is a variation of Dijkstra's single-source shortest path algorithm [7,8]. Algorithm Uniform Cost is optimal, that is, it returns a shortest path to a goal state. Since the search problem is formulated so that the length of the path to a goal state is the size of the splitting set that the goal state represents, Uniform Cost will find a minimum-size splitting set.
The time complexity of this algorithm is O(b m ), where b is the branching factor of the search tree generated, and m is the depth of the optimal solution. It is easy to see that m cannot be larger than the number of rules in the program, because once we use a rule for computing the next state, this rule cannot be used any longer in any sequel state. As for b, the branching factor, except for the initial state, each state can have at most one child; to generate a child we apply the lowest rule that demonstrates that the current state is not a splitting set. In a given a specific state, the time that required to calculate its child is polynomial in the size of the program. Therefore, this search problem can be solved in polynomial time. This claim is summarized in the following proposition. The following example demonstrates how the search algorithm works, assuming that we are looking for the smallest non-empty splitting set, and we are using uniform cost search.
Example. Suppose we are given the program P of Example 2.4, and we want to apply the search procedure to compute a nontrivial minimum-size splitting set. The search tree is shown in Figure 3. Our initial state is the empty set. By the definition of the search problem, the successors of the empty set are the sources of the super dependency graph of the program, which in this case are {a, b} and {c, d}, both of which with action cost 2. Since both current leaves have the same path cost, we shall choose randomly one of them, say {c, d}, and check whether it is a goal state, or in other words, a splitting set. It turns out {c, d} is not a splitting set, and the lowest rule that proves it is rule No. 4 that requires a splitting set that includes d to have also c and g. So, we make the leaf {c, d, g} the son of {c, d} with action cost 1 (only one atom, g, was added to {c, d}). Now we have two leaves in the search tree. The leaf {a, b} with path cost 2, that was there before, and the leaf {c, d, g}, that was just added, with path cost 3. So we go and check whether {a, b} is a splitting set and find out that Rule no. 2 is the lowest rule that proves it is not. W,e add the tree of Rule no. 2 and get the child {a, b, e, h} with a path cost 4. So, we go now and check whether {c, d, g} is a splitting set and find that Rule no. 5 is the lowest rule that proves that it is not. We add the tree of Rule no. 5 and get the child {c, d, g, f , a, b} with a path cost 6. Back to the leaf {a, b, e, h}, the leaf with the shortest path, we find that it is also a splitting set, and we stop the search. 2

Experiments
We have implemented our algorithm and tested it on randomly generated programs, having no negation as failure. Each rule in the program has exactly 3 variables where any subset of them can be in the head. A stable model is actually a minimal model for this type of program. For each program we have computed a nontrivial minimum-size splitting set. The average nontrivial minimum size of a splitting set, and the median of all nontrivial minimum size splitting sets, as a function of the rules to variable number ratio, are shown in Graph 4 and Graph 5, respectively. The average and median were taken over 100 programs generated randomly, starting with a ratio of 2 and generating 100 random programs for each interval of 0.25. It is clear from the graphs that in the transition value of 4.25 (See [18]) the size of the splitting set is maximal, and it is equal to the number of variables in the program. This is a new way of explaining that, programs in the phase transition value of rules to variable are hard to solve 6 Relaxing the splitting set condition As the experiments indicate, in the hard random problems the only nonempty splitting set is the set of all atoms in the program. In such cases splitting is not useful at all. In this section we introduce the concept of generalized splitting set (g-splitting set), which is a relaxation of the concept of a splitting set. Every splitting set is a g-splitting set, but there are g-splitting sets that are not splitting sets. Definition 6.1 (Generalized Splitting Set.) A Generalized Splitting Set (g-splitting set) for a program P is a set of of atoms U such that for each rule r in P, if one of the atoms in the head of r is in U, then all the atoms in the body of r are in U.
Thus, g-splitting sets that are not splitting sets may be found only when there are disjunctive rules in the program.  We next demonstrate the usefulness of g-splitting sets. We show that it is possible to compute a stable model of an HCF program P by computing a stable model of P S for a g-splitting set S of P, and then propagating the values assigned to atoms in S to the rest of the program. Theorem 6.3 (program decomposition.) Let P be a HCF program. For any g-splitting-set S in P, let X be a stable model of P S . Moreover, let P = Reduce(P,X,S-X), where Reduce(P, X, S − X) is the result of propagating the assignments of the model X in the program P. Then, for any stable model M of P , M ∪ X is a stable model of P.
Proof: We denote M ∪ X by M X. The proof has two steps. We prove that (1)-M X is a model of P and (2) -that every a ∈ M X has a proof w.r.t. P and M X.
Note that it must be the case that M ∩ X = / 0 and M ∩ S = / 0. 1. Assume that M X is not a model of P. Then, there is a rule r = H ←− B pos , B neg in P such that M X satisfies the body of r and the head H has empty intersection with M X. Note that r is not in P S . Otherwise it would not be violated by M X, since X is a model of P S , no atom in S is in P and M is a stable model of P .
Since 2. Next we show that every a ∈ M X has a proof w.r.t. P and M X. First, we show that if a ∈ X, then a has a proof w.r.t. P and M X. Since a ∈ X and X is a stable model of P S , a has a proof w.r.t. P S and X. The proof is by induction on the length n of the proof of a w.r.t. P S and X. n = 1 So the proof of a is a single rule r ∈ P S of the form H ←− B neg , where H ∩ X = {a} and B neg ⊆ S − X. Since r ∈ P S and no atom in S is in P it must be the case that B neg ∩ M X = / 0 and H ∩ M X = {a}. In addition, by the definition of P S , since r ∈ P S r ∈ P. So r is the proof of a w.r.t. P and M X. n > 1 We assume that for every k < n, if an atom b has a proof of length k w.r.t. P S and X, then b has a proof w.r.t. P and M X. Assume now that for some atom a ∈ X, a has a proof of length n w.r.t. P S and X. Let r ∈ P S be the last rule in that proof of a. The rule r must be of the form pos has a proof of legth k < n w.r.t. P S and X, and B neg ⊆ S − X. By the induction hypothesis, for every b ∈ B pos there is a proof of b w.r.t. P and M X. Since r ∈ P S and no atom in S is in P it must be the case that B neg ∩ M X = / 0 and H ∩ M X = {a}. In addition, by the definition of P S , since r ∈ P S r ∈ P. So r is the last rule in a proof of a w.r.t. P and M X.
Second, we show that if a ∈ M , then a has a proof w.r.t. P and M X. Since a ∈ M and M is a stable model of P , a has a proof w.r.t. P and M . The proof is by induction on the length n of the proof of a w.r.t. P and M . n > 1 We assume that for every k < n, if an atom b has a proof of length k w.r.t. P and M , then b has a proof w.r.t. P and M X. Assume now that for some atom a ∈ M , a has a proof of length n w.r.t. P and M . Let r ∈ P be the last rule in that proof of a. The rule r must be of the form H ←− B pos , B neg , where H ∩M = {a}, every b ∈ B pos has a proof of legth k < n w.r.t. P and M , and for each d ∈ B neg d / ∈ M , and by the way Reduce works, d / ∈ S . Since r ∈ P , and by the way Reduce works, there must be a rule r ∈ P such that r is of the form H ∪ H S−X ←− B pos , B neg , where: H S−X is the set of atoms in the head of r that belong to S − X, B pos = B pos ∪ B X , where B X is a set of atoms that belong to X, B neg = B neg ∪ B S−X , where B S−X is a set of atoms that belong to S − X.
We note that: 1. By the induction hypothesis, for every b ∈ B pos there is a proof of b w.r.t. P and M X. From all of the above it follows that r is the last rule of a proof of a w.r.t. P and M X.
Consider the program P from Example 6.2, which has two stable models: {a, c} and {b, d}. Let us compute the stable models of P according to Theorem 6.3. We take U = {a, b}, which is a g-splitting set for P . The bottom of P according to U, denoted b {a,b} (P), are Rule 1 and Rule 2, that is:

Related Work
The idea of splitting is discussed in many publications. Here we discuss papers that deal with generating splitting sets and relaxing the definition of a splitting set.
The work in [13] suggests a new way of splitting that introduces a possibly exponential number of new atoms to the program. The authors show that for some typical programs their splitting method is efficient, but clearly it can be quite resource demanding in the worst case.
Baumann [1] discuss splitting sets and graphs, but they do not go all the way in introducing a polynomial algorithm for computing classical splitting sets, as we do here. The authors of [2] suggest quasisplitting, a relaxation of the concept of splitting that requires the introduction of new atoms to the program, and they describe a polynomial algorithm, based on the dependency graph of the program, to efficiently compute a quasi-splitting set. Our algorithm is essentially a search algorithm with fractions of the dependency graph as states in the search space. We do not need the introduction of new atoms to define g-splitting sets.

Conclusions
The concept of splitting has a considerable role in logic programming. This paper has two major contributions. First, we show that the task of looking for an appropriate splitting set can be formulated as a classical search problem and computed in time that is polynomial in the size of the program. Search has been studied extensively in AI, and when we formulate a problem as a search problem, we immediately benefit from the library of search algorithms and strategies that has developed in the past and will be generated in the future. Our second contribution is introducing g-splitting sets, which are a generalization of the definition of splitting sets, as presented by Lifschitz and Turner. This allows for a larger set of programs to be split to non-trivial parts.