Propositional Logic Combinatorial Problm Solving (CPS) Albrt Olivras Enric Rodríguz-Carbonll May 17, 2018
Ovrviw of th sssion Dfinition of Propositional Logic Gnral Concpts in Logic Rduction to SAT CNFs and DNFs Tsitin Transformation Problm Solving with SAT Rsolution 1 / 18
Dfinition of Propositional Logic SYNTAX (what is a formula?): Vocabulary consists of a st P of propositional variabls, usually dnotd by (subscriptd) p,q,r,... Th st of propositional formulas ovr P is dfind as: Evry propositional variabl is a formula If F is a formula, F is also a formula If F and G ar formulas, (F G) is also a formula If F and G ar formulas, (F G) is also a formula Nothing ls is a formula Formulas ar usually dnotd by (subscriptd) F,G,H,... Exampls: p p (p q) (p q) (p ( p q)) ((p q) (r q))... 2 / 18
Dfinition of Propositional Logic SEMANTICS (what is an intrprtation I, whn I satisfis F?): An intrprtation I ovr P is a function I : P {0,1}. val I : Formulas {0,1} is a function dfind as follows: val I (p) = I(p) val I ( F) = 1 val I (F) val I ( (F G) ) = min{val I (F),val I (G)} val I ( (F G) ) = max{val I (F),val I (G)} I satisfis F (writtn I = F) if and only if val I (F) = 1. If I = F w say that I is a modl of F or, quivalntly F is tru in I. 3 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } = min{ val I (p), max{ val I (q),1 val I (r)} } Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } = min{ val I (p), max{ val I (q),1 val I (r)} } = min{ I(p), max{ I(q),1 I(r)} } Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } = min{ val I (p), max{ val I (q),1 val I (r)} } = min{ I(p), max{ I(q),1 I(r)} } = min{ 1, max{ 0,1 1} } Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } = min{ val I (p), max{ val I (q),1 val I (r)} } = min{ I(p), max{ I(q),1 I(r)} } = min{ 1, max{ 0,1 1} } = 0 Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } = min{ val I (p), max{ val I (q),1 val I (r)} } = min{ I(p), max{ I(q),1 I(r)} } = min{ 1, max{ 0,1 1} } = 0 Is thr any I such that I = F? 4 / 18
Dfinition of Propositional Logic EXAMPLE: Lt F b th formula (p (q r)). Lt I b such that I(p) = I(r) = 1 and I(q) = 0. Lt us comput val I (F) (us your intuition first!) val I ((p (q r))) = min{ val I (p), val I ((q r)) } = min{ val I (p), max{ val I (q),val I ( r)} } = min{ val I (p), max{ val I (q),1 val I (r)} } = min{ I(p), max{ I(q),1 I(r)} } = min{ 1, max{ 0,1 1} } = 0 Is thr any I such that I = F? YES, I(p) = I(q) = I(r) = 1 is a possibl modl. 4 / 18
Dfinition of Propositional Logic EXAMPLE W hav 3 pigons and 2 hols. If ach hol can hav at most on pigon, is it possibl to plac all pigons in th hols? Vocabulary: p i,j mans i-th pigon is in j-th hol Each pigon is placd in at last on hol: (p 1,1 p 1,2 ) (p 2,1 p 2,2 ) (p 3,1 p 3,2 ) Each hol can hold at most on pigon: (p 1,1 p 2,1 ) (p 1,1 p 3,1 ) (p 2,1 p 3,1 ) (p 1,2 p 2,2 ) (p 1,2 p 3,2 ) (p 2,2 p 3,2 ) Rsulting formula has no modl 5 / 18
Dfinition of Propositional Logic A small syntax xtnsion: W will writ (F G) as an abbrviation for ( F G) Similarly, (F G) is an abbrviation of ((F G) (G F)) 6 / 18
Ovrviw of th sssion Dfinition of Propositional Logic Gnral Concpts in Logic Rduction to SAT CNFs and DNFs Tsitin Transformation Problm Solving with SAT Rsolution 6 / 18
Gnral Concpts in Logic Lt F and G b arbitrary formulas. Thn: F is satisfiabl if it has at last on modl F is unsatisfiabl (also a contradiction) if it has no modl F is a tautology if vry intrprtation is a modl of F G is a logical consqunc of F, dnotd F = G, if vry modl of F is a modl of G F and G ar logically quivalnt, dnotd F G, if F and G hav th sam modls Not that: All dfinitions ar only basd on th concpt of modl. Hnc thy ar indpndnt of th logic. 7 / 18
Gnral Concpts in Logic p Circuit corrsponds to formula ( p p) Formula unsatisfiabl amounts to circuit output is always 0 p Circuit corrsponds to formula ( p p) Formula is a tautology amounts to circuit output is always 1 8 / 18
Gnral Concpts in Logic p p q q Circuit on th lft corrsponds to formula F := (p q) Circuit on th right corrsponds to formula G := ( p q) Thy ar functionally quivalnt, i.. sam inputs produc sam output That corrsponds to saying F G Chapst / fastst / lss powr-consuming circuit is thn chosn 9 / 18
Gnral Concpts in Logic p r 1 q 3 4 s 2 p q 6 5 7 1 5 in th circuit amounts to 1 (p q) 2 (r s) 3 ( 1 2 ) 4 ( 3 5 ) 5 ( 6 7 ) 6 ( p) 7 ( q) Is 1 always diffrnt from 5? = 1 5 10 / 18
Rduction to SAT Assum w hav a black box SAT that givn a formula F: SAT(F) = YES iff F is satisfiabl SAT(F) = NO iff F is unsatisfiabl How to rus SAT for dtcting tautology, logical consquncs,...? F tautology iff SAT( F) = NO F = G iff SAT(F G) = NO F G iff SAT((F G) ( F G)) = NO 11 / 18
Rduction to SAT Assum w hav a black box SAT that givn a formula F: SAT(F) = YES iff F is satisfiabl SAT(F) = NO iff F is unsatisfiabl How to rus SAT for dtcting tautology, logical consquncs,...? F not taut. iff SAT( F) = YES F = G iff SAT(F G) = YES F G iff SAT((F G) ( F G)) = YES 11 / 18
Rduction to SAT Assum w hav a black box SAT that givn a formula F: SAT(F) = YES iff F is satisfiabl SAT(F) = NO iff F is unsatisfiabl How to rus SAT for dtcting tautology, logical consquncs,...? F tautology iff SAT( F) = NO F = G iff SAT(F G) = NO F G iff SAT((F G) ( F G)) = NO Hnc, a singl tool suffics: all problms can b rducd to SAT (propositional SATisfiability) Th black box SAT will b calld a SAT solvr GOAL: larn how to build a SAT solvr 11 / 18
Ovrviw of th sssion Dfinition of Propositional Logic Gnral Concpts in Logic Rduction to SAT CNFs and DNFs Tsitin Transformation Problm Solving with SAT Rsolution 11 / 18
CNFs and DNFs In ordr to construct our SAT solvr it will simplify our job to assum that th formula F has a givn format. A litral is a propositional variabl (p) or a ngation of on ( p) A claus is a disjunction of zro or mor litrals (l 1...l n ) Th mpty claus (zro lits.) is dnotd and is unsatisfiabl A formula is in Conjunctiv Normal Form (CNF) if it is a conjunction of zro or mor disjunctions of litrals (i.., clauss) A formula is in Disjunctiv Normal Form (DNF) if it is a disjunction of zro or mor conjunctions of litrals Exampls: p (q r) (q p r) is in CNF p (q r) (q p r) is in DNF 12 / 18
CNFs and DNFs Givn a formula F thr xist formulas G in CNF with F G and (G is said to b a CNF of F) H in DNF with F H (H is said to b a DNF of F) Which is th complxity of dciding whthr F is satisfiabl...... if F is an arbitrary formula?... if F is in CNF?... if F is in DNF? 13 / 18
CNFs and DNFs Givn a formula F thr xist formulas G in CNF with F G and (G is said to b a CNF of F) H in DNF with F H (H is said to b a DNF of F) Which is th complxity of dciding whthr F is satisfiabl...... if F is an arbitrary formula? NP-complt (Cook s Thorm)... if F is in CNF?... if F is in DNF? 13 / 18
CNFs and DNFs Givn a formula F thr xist formulas G in CNF with F G and (G is said to b a CNF of F) H in DNF with F H (H is said to b a DNF of F) Which is th complxity of dciding whthr F is satisfiabl...... if F is an arbitrary formula? NP-complt (Cook s Thorm)... if F is in CNF? NP-complt (vn if clauss hav 3 litrals!)... if F is in DNF? 13 / 18
CNFs and DNFs Givn a formula F thr xist formulas G in CNF with F G and (G is said to b a CNF of F) H in DNF with F H (H is said to b a DNF of F) Which is th complxity of dciding whthr F is satisfiabl...... if F is an arbitrary formula? NP-complt (Cook s Thorm)... if F is in CNF? NP-complt (vn if clauss hav 3 litrals!)... if F is in DNF? linar Procdur SAT(F) Input: formula F in DNF Output: YES if thr xists I such that I = F, NO othrwis 1. If th DNF is mpty thn rturn NO. Els tak a conjunction of litrals C of th DNF 2. If thr is a variabl p such that both p, p appar in C, thn C cannot b mad tru: rmov it and go to stp 1. Els dfin I to mak C tru and rturn YES. 13 / 18
CNFs and DNFs Ida: givn F, find a DNF of F and apply th linar-tim algorithm Why this dos not work? 14 / 18
CNFs and DNFs Ida: givn F, find a DNF of F and apply th linar-tim algorithm Why this dos not work? Finding a DNF of F may tak xponntial tim In fact thr ar formulas for which CNFs/DNFs hav xponntial siz 14 / 18
CNFs and DNFs Ida: givn F, find a DNF of F and apply th linar-tim algorithm Why this dos not work? Finding a DNF of F may tak xponntial tim In fact thr ar formulas for which CNFs/DNFs hav xponntial siz Considr xor dfind as xor(x 1 ) = x 1 and if n > 0: xor(x 1,...,x n ) = (xor(x 1,...,x n 2 ) xor(x n 2 +1,...,x n )) ( xor(x 1,...,x n 2 ) xor(x n 2 +1,...,x n )) Th siz of xor(x 1,...,x n ) is Θ(n 2 ) Cubs (conjunctions of litrals) of a DNF of xor(x 1,...,x n ) hav n litrals Any DNF of xor(x 1,...,x n ) has at last 2 n 1 cubs (on for ach of th assignmnts with an odd numbr of 1s) Any CNF of xor(x 1,...,x n ) also has an xponntial numbr of cubs 14 / 18
CNFs and DNFs Ida: givn F, find a DNF of F and apply th linar-tim algorithm Why this dos not work? Finding a DNF of F may tak xponntial tim In fact thr ar formulas for which CNFs/DNFs hav xponntial siz Considr xor dfind as xor(x 1 ) = x 1 and if n > 0: xor(x 1,...,x n ) = (xor(x 1,...,x n 2 ) xor(x n 2 +1,...,x n )) ( xor(x 1,...,x n 2 ) xor(x n 2 +1,...,x n )) Th siz of xor(x 1,...,x n ) is Θ(n 2 ) Cubs (conjunctions of litrals) of a DNF of xor(x 1,...,x n ) hav n litrals Any DNF of xor(x 1,...,x n ) has at last 2 n 1 cubs (on for ach of th assignmnts with an odd numbr of 1s) Any CNF of xor(x 1,...,x n ) also has an xponntial numbr of cubs Nxt w ll s a workaround 14 / 18
Tsitin Transformation Lt F b (p q) ( p (q r) ) ^ p q ^ p q r 15 / 18
Tsitin Transformation Lt F b (p q) ( p (q r) ) 1 ^ 2 3 p q ^ 4 5 6 p q 7 r 15 / 18
Tsitin Transformation Lt F b (p q) ( p (q r) ) 1 1 ^ 2 3 1 2 3 2 p q 3 4 p q 5 p ^ q 4 6 7 4 5 6 5 p 6 q 7 7 r r 15 / 18
Tsitin Transformation Lt F b (p q) ( p (q r) ) 1 ^ 2 3 1 1 2 3 1 2 3 2 1 3 1 2 p q p q 5 ^ 4 6 3 4 4 5 6 5 p p q 7 6 q 7 7 r r 15 / 18
Tsitin Transformation Lt F b (p q) ( p (q r) ) 1 p 1 ^ q 2 3 5 ^ p q 4 6 7 1 2 3 1 2 3 2 1 3 1 2 p q p q 2 2 p 2 q 3 4 4 5 6 5 p 6 q 7 7 r r 15 / 18
Tsitin Transformation Lt F b (p q) ( p (q r) ) 1 1 1 2 3 1 2 3 2 1 3 1 p ^ q 2 3 5 ^ 4 6 2 p q p q 2 2 p 2 q 3 4 3 4 3 4 p q 7 4 5 6 5 p 6 q 7 r 7 r 15 / 18
Tsitin Transformation Variations of Tsitin transformation ar th ons usd in practic Tsitin transformation dos not produc an quivalnt CNF: for xampl, th Tsitin transformation of F = p is G = ( p) ( p), and p F G 0 0 1 0 0 1 0 0 1 0 1 1 1 1 0 0 Still, CNF obtaind from F via Tsitin transformation has nic proprtis: It is quisatisfiabl to F Any modl of CNF projctd to th variabls in F givs a modl of F Any modl of F can b compltd to a modl of th CNF Can b computd in linar tim in th siz of F Hnc no modl is lost nor addd in th transformation 16 / 18
Ovrviw of th sssion Dfinition of Propositional Logic Gnral Concpts in Logic Rduction to SAT CNFs and DNFs Tsitin Transformation Problm Solving with SAT Rsolution 16 / 18
Problm Solving with SAT Ys + modl Problm Formula P F Solutions P = Modls F CNF SAT solvr No This is th standard flow whn solving problms with SAT Transformation from P to F is calld th ncoding into SAT Alrady sn som xampls: pigon-hol problm Othr xampls will b sn in th nxt classs CNF transformation alrady xplaind Lt us s now how to dsign fficint SAT solvrs 17 / 18
Ovrviw of th sssion Dfinition of Propositional Logic Gnral Concpts in Logic Rduction to SAT CNFs and DNFs Tsitin Transformation Problm Solving with SAT Rsolution 17 / 18
Rsolution Th rsolution rul is p C p D C D Rs(S) = closur of st of clauss S undr rsolution = = clauss infrrd in zro or mor stps of rsolution from S Proprtis: Rsolution is corrct: Rs(S) only contains logical consquncs Rsolution is rfutationally complt: if S is unsatisfiabl, thn Rs(S) If S is a finit st of clauss, thn Rs(S) is also finit So, givn a st of clauss S, its satisfiability can b chckd by: 1. Computing Rs(S) 2. If Rs(S) Thn UNSAT ; Els SAT 18 / 18