As mentioned in a previous post determining the satisfiability of a boolean formula is quite trivial with a brute force algorithm. Here we will look at the brute force algorithm and establish the way that we will talk about solvers 'doing work'.
The most common way to implement a depth first search algorithm is with a stack. For our problem we will be pusing sets of variable assignments onto the stack and pooping them off for processing. We'll illustrate this with a very simple formula
p&q
For formula p&q there are four possible variable assignments.
We begin by selecting a variable, p, at random and pushing it's two possible assignments onto the stack. So our stack looks like:
Each element in the stack will be called a state, because every element in the stack represents a distinct possible (partial) variable assignment for the formula.
Next we will pop the top of the stack, p=true, and select a variable which has not yet been assigned, in this case there is only q. We make two new states with the two possible assignments to q and push them onto the top of the stack. Our stack now looks like:
We pop the next value off the stack, p=false, q=true, this is also a full assignment of the variables in the formula. This state does not make the formula true either. We discard these full assignments and keep searching.
We pop the next value off the stack, p=true, this is a partial assignment and so we make two new states with the two possible assignments to q and push them onto the top of the stack. Our stack now looks like:
The Algorithm In Pseudocode
Note that {x,y,z} is the notation for a set and U is the union operator.
- p = true, q = true
- p= true, q = false
- p = false, q = true
- p = false, q = false
We begin by selecting a variable, p, at random and pushing it's two possible assignments onto the stack. So our stack looks like:
- p=false
- p=true
Next we will pop the top of the stack, p=true, and select a variable which has not yet been assigned, in this case there is only q. We make two new states with the two possible assignments to q and push them onto the top of the stack. Our stack now looks like:
- p=false, q=false
- p=false, q=true
- p=false
false & falsewhich is not true. We must keep searching.
We pop the next value off the stack, p=false, q=true, this is also a full assignment of the variables in the formula. This state does not make the formula true either. We discard these full assignments and keep searching.
We pop the next value off the stack, p=true, this is a partial assignment and so we make two new states with the two possible assignments to q and push them onto the top of the stack. Our stack now looks like:
- p=true, q=false
- p=true, q=true
The Algorithm In Pseudocode
Note that {x,y,z} is the notation for a set and U is the union operator.
F = formula for satisfiability
W = Work stack (initially empty)
S = Current State (set of variable assignments)
Select an unassigned variable V
W.push({V = true})
W.push({V = false})
while (true)
if (W.isEmpty())
return unsatisfiable
else
S = W.pop()
if (isFullAssignment(S,F) and satisfies(S,F))
return satisfiable
else
V = getUnassigned(F,S)
S1 = S U {V = true}
S2 = S U {V = false}
W.push(S1)
W.push(S2)
end
No comments:
Post a Comment