Presentation is loading. Please wait.

Presentation is loading. Please wait.

Disjunctive Normal Form

Similar presentations


Presentation on theme: "Disjunctive Normal Form"— Presentation transcript:

1 Disjunctive Normal Form
CS 270: Math Foundations of CS Jeremy Johnson

2 Boolean Expressions A Boolean expression is a Boolean function
September 4, 1997 Boolean Expressions A Boolean expression is a Boolean function Any Boolean function can be written as a Boolean expression Disjunctive normal form (sums of products) For each row in the truth table where the output is true, write a product such that the corresponding input is the only input combination that is true Not unique E.G. (multiplexor function) s x0 x1 f 2 2

3 Disjunctive Normal Form
Theorem. Any boolean expression can be converted to an equivalent boolean exprssion in DNF. Proof. Any boolean expression defines a boolean function. Construct the truth table for the boolean expression and use the procedure on the previous slide to construct a boolean expression in DNF for the function defined by the truth table.

4 Alternative Proof A recursive conversion algorithm with corresponding inductive proof of correctness. Assume that implications and equivalences have been removed Assume that constants have been eliminated First convert to Negative Normal Form (NNF) (not expr) only occurs when expr is a variable Use DeMorgan’s Law and Double Negation Law Then convert to DNF Distribute and over or

5 DeMorgan’s Law (E  F)  E  F E F E F

6 Double Negation E  E E E

7 NNF Example 𝑎 ⋀(𝑏∨𝑐) a b c

8 NNF Example 𝑎 ⋀(𝑏∨𝑐) ≡ 𝑎 ∨ 𝑏∨𝑐 [DeMorgan’s Law] a b c

9 NNF Example 𝑎 ⋀(𝑏∨𝑐) ≡ 𝑎 ∨ 𝑏∨𝑐 [DeMorgan’s Law] Recursively convert operands a b c

10 NNF Example 𝑎 ⋀(𝑏∨𝑐) ≡ 𝑎 ∨ 𝑏∨𝑐 [DeMorgan’s Law] ≡𝑎 ∨ 𝑏∨𝑐 [Double Negation Law] a b c

11 NNF Example 𝑎 ⋀(𝑏∨𝑐) ≡ 𝑎 ∨ 𝑏∨𝑐 [DeMorgan’s Law] ≡𝑎 ∨ 𝑏∨𝑐 [Double Negation Law] ≡𝑎 ∨( 𝑏 ∧ 𝑐 ) [DeMorgan’s Law] a b c

12 Is-NNF ;Input: a boolean expression ;Output: a boolean which is true if the input expression is in negative normal ; form. (define (is-nnf? expr) (cond [ (is-constant? expr) #t ] [ (is-variable? expr) #t ] [ (is-not? expr) (is-variable? (op1 expr)) ] [ (is-or? expr) (and (is-nnf? (op1 expr)) (is-nnf? (op2 expr))) ] [ (is-and? expr) (and (is-nnf? (op1 expr)) (is-nnf? (op2 expr))) ] )

13 NNF ; Input: A boolean expression ; Output: A boolean expression that is equivalent to the input and ; is in negative normal form. ; (and (is-nnf? (nnf expr)) (equal? (bool-eval expr env)) ; (equal? (bool-eval (nnf expr) env))) (define (nnf expr) (cond [ (is-constant? expr) expr ] [ (is-variable? expr) expr ] [ (is-not? expr) (nnf-not expr) ] [ (is-or? expr) (list 'or (nnf (op1 expr)) (nnf (op2 expr))) ] [ (is-and? expr) (list 'and (nnf (op1 expr)) (nnf (op2 expr))) ] )

14 NNF ;Input: A boolean expression that is a negation. ;Output: A boolean expression that is in NNF and equivalent to the input. (define (nnf-not expr) (cond [ (is-constant? (op1 expr)) (not expr) ] [ (is-variable? (op1 expr)) expr ] [ (is-not? (op1 expr)) (nnf (op1 (op1 expr))) ] [ (is-or? (op1 expr)) (list 'and (nnf (list 'not (op1 (op1 expr)))) (nnf (list 'not (op2 (op1 expr))))) ] [ (is-and? (op1 expr)) (list 'or (nnf (list 'not (op1 (op1 expr)))) )

15 Correctness (is-nnf? (nnf expr)  (bool-eval (nnf expr) env) = (bool-eval expr env) Structural induction on expr using DeMorgan’s laws and Double Negation law (not (and p q))  (or (not p) (not q)) (not (or p q))  (and (not p) (not q)) (not (not p))  p Base case. expr = var or (not var) Assume true for expr1 and expr2 and show for (not expr), (or expr1 expr2), and (and expr1 expr2) – and and or are trivial

16 Correctness for (not expr)
(is-nnf? (nnf ‘(not expr)) (nnf ‘(not expr)) (nnf-not ‘(not expr)) Case 1. (nnf-not ‘(not (not expr))) = (nnf expr)) which is correct by induction and double negation Case 2. (nnf-not ‘(not (and expr1 expr2))) = (nnf ‘(or (not expr1) (not expr2))) = (list ‘or (nnf ‘(not expr1)) (nnf ‘(not expr2))) which is correct by induction and De Morgan Case 3. (nnf-not ‘(not (or expr1 expr2)))

17 Distribute And over Or E  (F1 F2) (E1  E2)  F (E  F1)  (E  F2)

18 DNF Example (𝑎∨𝑏)∧( 𝑐 ∨ 𝑑 ) 𝑑 a b 𝑐

19 DNF Example (𝑎∨𝑏)∧( 𝑐 ∨ 𝑑 ) ≡ ((𝑎∨𝑏)∧ 𝑐 )∨((𝑎∨𝑏)∧ 𝑑 ) 𝑑 𝑐 a b

20 DNF Example (𝑎∨𝑏)∧( 𝑐 ∨ 𝑑 ) ≡ ((𝑎∨𝑏)∧ 𝑐 )∨((𝑎∨𝑏)∧ 𝑑 ) ≡ ((𝑎∧ 𝑐 ) ∨(𝑏∧ 𝑐 ))∨((𝑎∨𝑏)∧ 𝑑 ) 𝑑 a 𝑐 b

21 DNF Example (𝑎∨𝑏)∧( 𝑐 ∨ 𝑑 ) ≡ ((𝑎∨𝑏)∧ 𝑐 )∨((𝑎∨𝑏)∧ 𝑑 ) ≡ ((𝑎∧ 𝑐 ) ∨(𝑏∧ 𝑐 ))∨((𝑎∨𝑏)∧ 𝑑 ) ≡ ((𝑎∧ 𝑐 ) ∨(𝑏∧ 𝑐 ))∨((𝑎∧ 𝑑 ) ∨(𝑏∧ 𝑑 )) a 𝑐 b 𝑑

22 Is-DNF ;is-dnf? Check to see if a Boolean expression is in DNF. This is true when it ; is in NNF and there are no ands above ors. (define (is-dnf? expr) (and (is-nnf? expr) (no-and-above-or? expr)) ;no-and-above-or? Check to see (define (no-and-above-or? expr) (cond [ (is-constant? expr) #t ] [ (is-variable? expr) #t ] [ (is-not? expr) (no-and-above-or? (op1 expr)) ] [ (is-or? expr) (and (no-and-above-or? (op1 expr)) (no-and-above-or? (op2 expr))) ] [ (is-and? expr) (and (no-or? (op1 expr)) (no-or? (op2 expr))) ] )

23 Is-DNF ;no-or? check if a boolean expression contains any ors. (define (no-or? expr) (cond [ (is-constant? expr) #t ] [ (is-variable? expr) #t ] [ (is-not? expr) (no-or? (op1 expr)) ] [ (is-or? expr) #f ] [ (is-and? expr) (and (no-or? (op1 expr)) (no-or? (op2 expr))) ] )

24 DNF ; Input: A boolean expression ; Output: A boolean expression in DNF that is equivalent to the input expression. (define (dnf expr) (nnf2dnf (nnf expr))) ; Input: A boolean expression in NNF (define (nnf2dnf expr) (cond [ (is-constant? expr) expr ] [ (is-variable? expr) expr ] [ (is-not? expr) expr ] [ (is-and? expr) (distrib-andor (dnf (op1 expr)) (dnf (op2 expr))) ] [ (is-or? expr) (list 'or (dnf (op1 expr)) (dnf (op2 expr))) ] )

25 Distribute And Over Or (and E (or F1 F2)) (and (or E1 E2) F)
(or (and E F1) (and E F2)) (and (or E1 E2) F) (or (and E1 F) (and E2 F))

26 Distribute And Over Or ; Input: Two expressions in DNF ; Output: An expression in DNF equivalent to (and expr1 expr2) ; There are three cases. Let expr1 = E1 and expr2 = E2 ; 1) E1 = E11 \/ E12. (E11 /\ E2) \/ (E12 /\ E2) ; 2) E2 = E21 \/ E22. (E1 /\ E21) \/ (E1 /\ E22) ; 3) E1 and E2 contain no ors. E1 /\ E2 (define (distrib-andor expr1 expr2) (cond [(is-or? expr1) (list 'or (distrib-andor (op1 expr1) expr2) (distrib-andor (op2 expr1) expr2)) ] [(is-or? expr2) (list 'or (distrib-andor expr1 (op1 expr2)) (distrib-andor expr1 (op2 expr2))) ] [ else (list 'and expr1 expr2) ] )

27 Exponential Blowup Converting the following expression shows that an exponential increase in the size of the dnf from of a boolean expression is possible (x1  y1)    (xn  yn) (x1    xn)    (y1    yn)

28 Exercise Prove that (dnf expr) terminates Use induction to prove
Hint show that the size of expr decreases in all recursive calls and that the base case must eventually be reached, where size of an expr is the number of not, and, and or operations. Use induction to prove (is-dnf? (dnf expr)) Modify (dnf expr) to compute the conjunctive normal form (cnf expr). Hint cnf is duel to dnf

29 Proof of Correctness of dnf
Must show the following (bool-eval (dnf expr) env) = (bool-eval expr env) (is-dnf? (dnf expr)) We first prove the following lemmas Lemma 1. (implies (and (is-dnf expr1) (is-dnf? expr2)) (is-dnf? (list ‘or expr1 expr2))) Lemma 2. (implies (and (is-dnf? expr1) (is-dnf? expr2)) (is-dnf? (distrib-andor expr1 expr2))) Lemma 3. (bool-eval (list ‘and expr1 expr2) env) = (bool-eval (distrib-andor exp1 exp2) env)

30 Proof of Lemma 1 (is-dnf (list ‘or expr1 expr2)) = (and (is-nnf? (list ‘or expr1 expr2)) (no-and-above-or? (list ‘or expr1 expr2))) {def} (is-nnf? (list ‘or expr1 expr2)) = (and (is-nnf? expr1) (is-nnf? expr2)) {def} = (and t t) {assumption}

31 Proof of Lemma 1 (no-and-above-or? (list ‘or expr1 expr2)) = (and (no-and-above-or? expr1) (no-and- above-or? expr2)) {def} = (and t t) {assumption}

32 Proof of Lemma 2 (is-dnf? (distrib-andor expr1 expr2)
The proof is by induction on boolean expressions. There are three cases to consider (is-or? expr1) (is-or? expr2) (and (not (is-or? expr1)) (not (is-or? expr2))) In case 3, the base case, we return (and expr1 expr2), and since expr1 and expr2 are in dnf, and are conjunctions, there are no or’s in expr1 and expr2 and consequently (no-and- above-or? (and expr1 expr2)). Similarly (is-nnf? (and expr1 expr2)) is true, thus (is-dnf? (and expr1 expr2)) The proofs for cases 1 and 2 are similar for both cases. We do case 1.

33 Proof of Lemma 2 (is-dnf? (distrib-andor expr1 expr2) = (is-dnf (list 'or (distrib-andor (op1 expr1) expr2) (distrib- andor (op2 expr1) expr2)) ) Since (is-dnf? expr1), (is-dnf? (op1 expr)) and (is-dnf? (op2 expr)). Therefore we can apply the induction hypothesis and conclude that (is-dnf? (distrib-andor (op1 expr1) expr2) and (is-dnf? (distrib-andor (op2 expr1) expr2). Thus, by Lemma 1, the lemma is proved.

34 Proof of Lemma 3 Lemma 3 follows by induction on boolean expressions and uses the distributive property of boolean algebra to prove the result after applying the inductive hypothesis. Show (bool-eval (list ‘and E1 E2) env) = (bool-eval (distrib-andor E1 E2) env)

35 Proof of Lemma 3 Prove by induction on Boolean expressions E = (and E1 E2) Base case. ;3) E1 and E2 contain no ors. E1 /\ E2 (bool-eval (distrib-andor E1 E2) env) = (bool-eval (list ‘and E1 E2) env)

36 Proof of Lemma 3 Inductive case ; 1) E1 = E11 \/ E12. Assume distributive property: E1 /\ E2 = (E11 /\ E2) \/ (E12 /\ E2) Inductive hypothesis: (bool-eval (distrib-andor E1i E2) env) = (bool-eval (list ‘and E1i E2) env) for i=1,2.

37 Proof of Lemma 3 (bool-eval (distrib-andor E1 E2) env) = (bool-eval (‘or (distrib-andor E11 E2) (distrib-andor E12 E2)) env) [Def of distrib-andor] = (or (bool-eval (distrib-andor E11 E2) env) (bool-eval (distrib-andor E11 E2) env)) [Def of bool-eval] = (or (bool-eval (‘and E11 E2) env) (bool-eval (‘and E12 E2) env)) [IH] = (bool-eval (‘or (‘and E11 E2) (‘and E12 E2)) env) [Def of bool- eval] = (bool-eval (‘and (‘or E11 E12) E2) env) [Distrib property] = (bool-eval (‘and E1 E2) env) [Def of E1]

38 Proof of Correctness To show (is-dnf? (dnf expr)) = (is-dnf? (nnf2dnf (nnf expr))) we already showed (is-nnf? (nnf expr)) so we assume that expr is in NNF and show (is-dnf? (nnf2dnf expr)) 1) [base cases] expr = var, expr = const, expr = (not var) In all cases (nn2dnf expr) = expr and (is-dnf? expr) is true.

39 Proof of Correctness [(is-or? expr)] (nn2dnf expr) =
(list 'or (nnf2dnf (op1 expr)) (nnf2dnf (op2 expr))) By induction (is-dnf? (nnf2dnf (op1 expr)) ) and (is-dnf? (nnf2dnf (op2 expr)) ). Therefore by Lemma 1, (is-dnf? (list 'or (nnf2dnf (op1 expr)) (nnf2dnf (op2 expr))))

40 Proof of Correctness 3) [(is-and? expr)] (nn2dnf expr) = (distrib-andor (nnf2dnf (op1 expr)) (nnf2dnf (op2 expr))) By induction (is-dnf? (nnf2dnf (op1 expr)) ) and (is-dnf? (nnf2dnf (op2 expr)) ). Therefore by Lemma 2, (is-dnf? (distrib-andor (nnf2dnf (op1 expr)) (nnf2dnf (op2 expr))))

41 Proof of Correctness To show (bool-eval (dnf expr) env) = (bool-eval (nnf2dnf (nnf expr)) env) = (bool-eval expr env) we again, having shown (bool-eval (nnf expr) env) = (bool-eval expr env), assume (is-nnf expr) and show (bool-eval (nnf2dnf expr) env) = (bool-eval expr env) Just as in the previous proof, we use induction on boolean expressions. The base cases are trivial and the recursive case for or is immediate after applying induction. The recursive case for and requires Lemma 3.


Download ppt "Disjunctive Normal Form"

Similar presentations


Ads by Google