Constraint Satisfaction

Complexity

and

Logic

Phokion G. Kolaitis

IBM Almaden Research Center & UC Santa Cruz

1 Goals

Goals:

• Show that Constraint Satisfaction Problems (CSP) form a broad class of algorithmic problems that arise naturally in several different areas.

• Present an overview of CSP with emphasis on the computational complexity aspects of CSP and on the connections with , univeral algebra, and logic

2 Course Outline

Constraint Satisfaction Problems (CSP)

• Definition & Examples of CSP • CSP & the Homomorphism Problem • CSP & Database Theory • Computational Complexity of CSP – Uniform & Non-Uniform CSP – Schaefer’s Dichotomy Theorem for Boolean CSP – The Feder-Vardi Dichotomy Conjecture for general CSP

3 Course Outline

• The Pursuit of Tractable Cases of CSP – Universal Algebra and Closure Properties of Constraints – , Finite-variable Logics, and Pebble Games

– Bounded Treewidth and Finite-variable Log- ics

4 Algorithmic Problems

• k-SAT:(k ≥ 2 is fixed) Given a k-CNF formula, is it satisfiable?

• k-Colorability:(k ≥ 2 is fixed) Given a graph H = (V,E), is it k-colorable?

• Clique: Given a graph H = (V,E) and a positive inte- ger k, does H contain a k-clique?

• Hamiltonian Cycle: Given a graph H = (V,E), does it contain a Hamiltonian cycle?

5 Algorithmic Problems

Latin Squares: . Euler – 1783 A Latin square of order n is an n × n matrix of n symbols such that each symbol occurs exactly once in each row and each column of the matrix.

A Latin Square of Order 4

2 3 1 4 3 4 2 1 4 1 3 2 1 2 4 3

Latin Square Completion: given a n × n grid and a partial assignment of n symbols, can this partial assignment be completed to a Latin square of order n?

6 Latin Square Completion

• There is a completion to a Latin square

2 4 4 1 4 3 1

• There is no completion to a Latin square

3 4 1 2 2

7 Latin Square Completion

Is there a completion to a Latin square?

3 4 6 5 1 3 2 5 7 3 7 2 4 3 2 5 3

8 Algorithmic Problems

Problems:

• k-SAT (k ≥ 2) • k-Colorability (k ≥ 2) • Clique • Hamiltonian Cycle • Latin Square Completion

Question: What do these have in common?

9 Constraint Satisfaction Problems

Problems:

• k-SAT (k ≥ 2) • k-Colorability (k ≥ 2) • Clique • Hamiltonian Cycle • Latin Square Completion

Question: What do these have in common?

Answer: Each of them is a Constraint Satisfaction Problem.

10 Constraint Satisfaction Problem

U. Montanari – 1974

CSP – Informal Definition Input: A set V of variables, a domain D of values for the variables, and a set C of constraints.

Question: Is there an assignment of values to variables such that all constraints in C are satis- fied?

Example: k-SAT as a CSP k-CNF ϕ: variables x1, . . . , xn; clauses c1, . . . , cm

• Variables: x1, . . . , xn • Values: 0, 1 • Constraints: Given by the clauses of ϕ.

11 Constraint Satisfaction Problem

Input: (V,D,C) • A finite set V of variables • A finite domain D of values for the variables • A set C of constraints (t, R) restricting the values that tuples of variables can take.

– t: a tuple t = (x1, . . . , xm) of variables – R: a on D of arity |t| = m

Question: Does (V,D,C) have a solution?

Solution: A mapping h : V → D such that

h(t) = (h(x1), . . . , h(xm)) ∈ R, for every constraint (t, R) ∈ C.

An assignment of values to the variables such that all constraints are satisfied.

12 Example

• 3-Sat:

Given a 3CNF-formula ϕ with variables x1, . . . , xn

and clauses c1, . . . , cm, is ϕ satisfiable?

• 3-Sat as a CSP:

– Variables x1, . . . , xn – Domain D = {0, 1}

– Constraints ((x, y, z),Ri), i = 0, 1, 2, 3

Clause Relation 3 (x ∨ y ∨ z) R0 = {0, 1} − {(0, 0, 0)} 3 (¬x ∨ y ∨ z) R1 = {0, 1} − {(1, 0, 0)} 3 (¬x ∨ ¬y ∨ z) R2 = {0, 1} − {(1, 1, 0)} 3 (¬x ∨ ¬y ∨ ¬z) R3 = {0, 1} − {(1, 1, 1)}

13 Example

• 3-Colorability: Given a graph H = (V,E), is it 3-colorable?

• 3-Colorability as a CSP: – The variables are the nodes in V – The domain is the set D = {R, G, B} of three colors. – The constraints are the pairs ((u, v),Q), where (u, v) ∈ E and Q is the relation

{(R, G), (G, R), (R, B), (B, R), (B, G), (G, B)}.

14 More Examples

Homework Assignment #1:

Express the following problems as CSPs:

• 4-Colorability

• Clique

• Hamiltonian Cycle

• Latin Square Completion

15 Constraint Satisfaction Problems

Fundamental & ubiquitous problems in AI and CS:

• Boolean Satisfiability, Graph Colorability, ...

• Database Query Processing

• Planning and Scheduling

• Belief Maintenance

• Machine Vision • ···

• Linguistics

16 Homomorphisms

Definition: Consider two relational structures A A B B A = (A, R1 ,...,Rm) and B = (B,R1 ,...,Rm). h : A → B is a homomorphism if for every i ≤ m n and every tuple (a1, . . . , an) ∈ A ,

A B Ri (a1, . . . , an) =⇒ Ri (h(a1), . . . , h(an)).

Example: Clique K4 vs. Clique K5 v LQ Q  Q v v  L Q @  L Q v Qv @ b L " @ B b " ¢ b L " ¢ @ B b "L ¢ B b " @ "b L ¢ @ B " b " b L ¢ @@vv BB v" bL v¢

• There is a homomorphism from K4 to K5;

• There is no homomorphism from K5 to K4. 17 The Homomorphism Problem

Definition: The Homomorphism Problem

Given two relational structures A and B, is there a homomorphism h : A → B?

Fact: Feder & Vardi – 1993 CSP can be identified with the Homomorphism Problem.

18 Example

3-Sat as a Homomorphism Problem:

Given 3-CNF formula ϕ with variables x1, . . . , xn:

ϕ ϕ ϕ ϕ ϕ • A = ({x1, . . . , xn},R0 ,R1 ,R2 ,R3 ), where

ϕ R0 = {(x, y, z):(x ∨ y ∨ z) is a clause of ϕ} ϕ R1 = {(x, y, z):(¬x ∨ y ∨ z) is a clause of ϕ} ϕ R2 = {(x, y, z):(¬x ∨ ¬y ∨ z) is a clause of ϕ} ϕ R3 = {(x, y, z):(¬x ∨ ¬y ∨ ¬z) is a clause of ϕ}

• B = ({0, 1},R0,R1,R2,R3), where

3 R0 = {0, 1} − {(0, 0, 0)} 3 R1 = {0, 1} − {(1, 0, 0)} 3 R2 = {0, 1} − {(1, 1, 0)} 3 R3 = {0, 1} − {(1, 1, 1)}

Fact: ϕ is satisfiable iff there is hom. h : Aϕ → B.

19 Examples

3-Colorability as a Homomorphism Problem:

Fact: A graph H = (V,E) is 3-colorable ⇐⇒ there is a homomorphism h : H → K3, where K3 is the 3-clique, i.e., K3 = ({R, G, B},E3), where

E3 = {(R, G), (G, R), (R, B), (B, R), (B, G), (G, B)}.

k-Colorability as a Homomorphism Problem:

Fact: A graph H = (V,E) is k-colorable ⇐⇒ there is a homomorphism h : H → Kk, where Kk is the k-clique.

20 More Examples

Homework Assignment #2:

Express the following problems as Homomorphism Problems:

• 2-Sat

• Hamiltonian Cycle

• Latin Square Completion

21 Cliques and Homomorphisms

Definition: The Clique Problem: Given a graph H = (V,E) and a positive integer k, does H contain a k-clique?

Fact: The following are equivalent for a graph H and a positive integer k: • H contains a k-clique.

• There is a homomorphism from Kk to H.

Thus, the Clique Problem can be viewed as a Homomorphism Problem.

22 Homomorphisms and Cliques

Definition: Given two graphs G = (V,E) and H = (V 0,E0) the homomorphism product G ◦ H is the graph with • V × V 0 as nodes; • ((s, t), (s0, t0)) is an edge of G ◦ H if s 6= s0 and whenever (s, s0) ∈ E, then (t, t0) ∈ E0.

Proposition: (Baˇc´ıkand Mahajan) The following are equivalent for two graphs G and H: • There is a homomorphism from G to H. • G ◦ H contains a clique of size |V |. • The clique number of G ◦ H is |V |.

Thus, the Homomorphism Problem has a direct reduction to the Clique Problem.

23 From CSP to the Homomorphism Problem

Given a CSP-instance (V,D,C), let R1,...,Rm be the distinct relations on D occurring in C.

Define the instance (A, B) of the Homomorphism Problem, where

A = (V, {t :(t, R1) ∈ C},..., {t :(t, Rm) ∈ C}) and

B = (D,R1,...,Rm).

Fact: The CSP instance (V,D,C) has a solution

⇐⇒ there is a homomorphism from A to B.

24 From the Homomorphism Problem to CSP

A A Given an instance A = (A, R1 ,...,Rm) and B = B B (B,R1 ,...,Rm) of the Homomorphism Prob- lem, define the instance (V,D,C) of CSP, where

• V = A (i.e., the elements of A are variables);

• D = B (i.e., the elements of B are values);

B A • C = {(t, Ri ): t ∈ Ri , 1 ≤ i ≤ m}.

Fact: There is a homomorphism from A to B

⇐⇒ the CSP-instance (V,D,C) has a solution.

25 CSP vs. the Homomorphism Problem

Conclusion: Feder & Vardi – 1993

CSP = Homomorphism Problem

Some Advantages:

• Clean, algebraic formulation of CSP.

• Makes it possible to apply methods from universal algebra and logics to the study of CSP.

• Makes the connections between CSP and database theory transparent.

26 CSP vs. the Homomorphism Problem

• CSP = Homomorphism Problem

• “A constraint network consists of a finite set of variables, each associated with a set of discrete values, and a set of constraints. Each of the constraints is expressed as a relation, defined on some subset of variables, whose tuples are all the simultaneous value assignments to the members of this variable subset that, as far as this constraint is concerned, are legala.” Encyclopedia of Artificial Intelligence – 1992

aThis does not mean that the actual representation of any constraint is necessarily in the form of its defining re- lation, but that the relation can, in principle, be generated using the constraints’ specification without the need to con- sult other constraints in the network.

27 Course Outline

Constraint Satisfaction Problems

• Definition & Examples of CSP • CSP & the Homomorphism Problem

• CSP & Database Theory

• Computational Complexity of CSP

• The Pursuit of Tractable Cases of CSP

28 Relational Databases

Definition:

• Relation Schema: R(A1,...,Ak)

R: relation name; A1,...,Ak: attribute names. • Relation conforming with R: A set R0 of k-tuples • Database Schema: A set S of relation schemas • Database over S: A set S of relations conform- ing with the relation schemas in S. Database = Relational Structure

Example: • Relation Schemas: ENROLLS(Student, Course) TEACHES(Faculty, Course) • Database Schema: {ENROLLS, TEACHES}. 29 Relational Databases

Example: (continued) • Relation Schemas: ENROLLS(Student, Course) TEACHES(Faculty, Course) • Relation ENROLLS Student Course Adams CS101 Bing Math10 ··· ···

• Relation TEACHES Faculty Course Euler Math10 Turing CS130 Codd CS180 ··· ··· 30 Relational Join

Notation: Given relations R1,...,Rm,

R1 on R2 on ··· on Rm is the relational or, simply, the join of R1,...,Rm.

Definition: Given

R1(A, B, C),R2(B,C,D),R3(A, D, E),

R1 on R2 on R3 = {(a, b, c, d, e):

R1(a, b, c) ∧ R2(b, c, d) ∧ R3(a, d, e)}

Example: TAUGHT-BY(Student,Course,Faculty)

TAUGHT-BY = ENROLLS on TEACHES

Fact: Computing joins is the most frequent and most costly operation in database systems. (Why?)

31 Relational Joins

• Relation ENROLLS Student Course Adams CS101 Bing Math10 ··· ···

• Relation TEACHES Faculty Course Euler Math10 Turing CS130 Codd CS180 ··· ···

• Relation TAUGHT-BY Student Course Faculty Bing Math10 Euler ··· ··· ··· 32 CSP and Joins

• Consider a CSP instance (V,D,C), where

V = {X1,...,Xn}

C = {C1,...,Cm}

Ci = (ti,Ri)

ti = (Xj1 ,...,Xji ).

• For each (ti,Ri), view the occurrence of Ri as a relation over the relation schema Ri(Xj1 ,...,Xji ).

Fact: Bibel, Gyssens, Jeavons, ...

R1 on ··· on Rm = Solutions((V,D,C)).

In particular,

(V,D,C) has a solution ⇐⇒ R1 on ··· on Rm 6= ∅.

Conclusion: CSP is a database problem.

33 CSP and Databases

Fact: There are strong links between CSP and key problems in database systems:

• Relational Join Evaluation

• Conjunctive Query Evaluation (CQE)

• Conjunctive Query Containment (CQC)

34 Queries

Definitions: • Class C of structures: a collection of relational structures closed under isomorphisms. • k-ary Query Q on C: a mapping Q with domain C and such that – Q(A) is a k-ary relation on A, for A ∈ C; – Q is preserved under isomorphisms, i.e., if h : A → B is an isomorphism, then

Q(B) = h(Q(A)).

• Boolean Query Q on C: a mapping Q : C → {0, 1} preserved under isomorphisms. Thus, Q can be identified with the subclass C0 of C, where

C0 = {A ∈ C : Q(A) = 1}.

35 Examples of Queries

• Path of Length 2 – P 2: Binary query on graphs H = (V,E) such that

P 2(H) = {(a, b) ∈ V 2:(∃c)(E(a, c) ∧ E(c, b))}.

• Articulation Point – AP : Unary query on graphs H = (V,E) such that

AP (H) = {a ∈ V : a is an articulation point of H}.

• Connectivity – CN: Boolean query on graphs H = (V,E) such that   1 if H is connected CN(H) =  0 otherwise.

• k-Colorability, k ≥ 2, Hamiltonian Cycle, ...

36 Definability of Queries

Let L be a logic and C a class of structures

• A k-ary query Q on C is L-definable if there is

an L-formula ϕ(x1, . . . , xk) with x1, . . . , xk as free variables and such that for every A ∈ C

k Q(A) = {(a1, . . . , ak) ∈ A : A |= ϕ(a1, . . . , ak)}.

• A Boolean query Q on C is L-definable if there is an L-sentence ψ such that for every A ∈ C

Q(A) = 1 ⇐⇒ A |= ψ.

37 Conjunctive Queries

Definition: A conjunctive query is a query defin- able by a FO-formula in built from atomic formulas, ∧, and ∃ only.

(∃z1 ... ∃zm)ψ(x1, . . . , xk, z1, . . . , zm), where ψ is a conjunction of atomic formulas. Also, written as a rule:

Q(x1, . . . , xk): − R(y2, x3, x1),S(x1, y3),...,S(y7, x2)

Examples: • Path of Length 2 (Binary query)

(∃z)(E(x1, z) ∧ E(z, x2)

P 2(x1, x2): − E(x1, z),E(z, x2)

• Cycle of Length 3 (Boolean query)

(∃x1∃x2∃x3)(E(x1, x2)∧E(x2, x3)∧E(x3, x1))

Q : − E(x1, x2),E(x2, x3),E(x3, x1) 38 Conjunctive Queries

• Every relational join is a conjunctive query

R1(A, B, C),R2(B,C,D),

R1 on R2(x, y, z, w): − R1(x, y, z),R2(y, z, w)

• Conjunctive Queries are the most frequently asked queries in databases (a.k.a. SPJ queries)

• The main construct of SQL expresses conjunc- tive queries

SELECT R1.A, R2.D

FROM R1,R2

WHERE R1.B = R2.B AND R1.C = R2.C

39 Conjunctive Query Evaluation

A fundamental problem about conjunctive queries

Definition: Conjunctive Query Evaluation CQE • Given a CQ Q and a structure A, find

Q(A) = {(a1, . . . ak): A |= Q(a1, . . . , ak)}

• For Boolean queries Q, this becomes: Given Q and A, does A |= Q? (is Q(A) = 1?)

Examples: • Given a graph H, find all pairs of nodes con- nected by a path of length 4.

• Given a graph H, does it contain a triangle?

40 Conjunctive Query Containment

A fundamental problem about conjunctive queries

Definition: Conjunctive Query Containment CQC

• Given two k-ary CQs Q1 and Q2, is it true that for every structure A,

Q1(A) ⊆ Q2(A)?

• For Boolean queries, this becomes:

Given two Boolean queries Q1 and Q2, does

Q1 |= Q2? (does Q1 logically imply Q2?)

Examples: • Is it true that if two nodes of a graph H are connected by a path of length 4, then they are also connected by a path of length 3?

• It is true that if a graph H contains a K4, then

it also contains a K3? 41 CQE, CQC, CSP

Theorem: Chandra & Merlin – 1977 CQE and CQC are the same problem.

Fact: K ... & Vardi – 1998 CSP and CQC are the same problem

Question: What is the common link?

42 CQE, CQC, CSP

Theorem: Chandra & Merlin – 1977 CQE and CQC are the same problem.

Fact: K ... & Vardi – 1998 CSP and CQC are the same problem

Question: What is the common link?

Answer: The Homomorphism Problem

43 Canonical CQs and Canonical Structures

Definition: Canonical Conjunctive Query A A Given A = (A, R1 ,...,Rm), the canonical CQ of A is the Boolean CQ QA with the elements of A as variables and the “facts” of A as conjuncts: ^m ^ A A Q : − Ri (t) i=1 t

Definition: Canonical Structure Given a Boolean conjunctive query Q, let AQ be the structure with the variables of Q as elements and the conjuncts of Q as “facts”.

Example: • A = ({a, b, c}, {(a, b), (b, c), (c, a)} QA : − E(x, y) ∧ E(y, z) ∧ E(z, x)

• Q : − E(x, y) ∧ E(x, z) AQ = ({a, b, c), {(a, b), (a, c)} 44 Homomorphisms, CQC and CQE

Theorem: Chandra & Merlin – 1977 For relational structures A and B, TFAE • There is a homomorphism h : A → B

• B |= QA (i.e., QA(B) = 1)

• QB ⊆ QA

Theorem: Chandra & Merlin – 1977

For conjunctive queries Q1 and Q2, TFAE

• Q1 ⊆ Q2

• There is a homomorphism h : AQ2 → AQ1

Q1 Q1 • A |= Q2 (i.e., Q2(A ) = 1)

45 Homomorphisms, CQC, and CQE

Example: 3-Colorability For a graph H, the following are equivalent: 1. H is 3-colorable

2. There is a homomorphism h : H → K3

H 3. K3 |= Q

4. QK3 ⊆ QH Proof:

(2) =⇒ (3): A homomorphism h : H → K3 pro- vides witnesses in K3 for the existential quantifiers in QH.

H K3 (3) =⇒ (4): If K3 |= Q and A |= Q , then there is a homomorphism h : H → K3 and a ho- ∗ momorphism and h : K3 → A. The composition h∗ ◦h : H → A provides witnesses in A for the existential quantifiers in QH. 46 CSP, CQE, and CQC

Theorem: Chandra & Merlin – 1977 For relational structures A and B, TFAE • There is a homomorphism h : A → B

• B |= QA (i.e., QA(B) = 1)

• QB ⊆ QA

Conclusion: CSP, CQE, and CQC are the same problem.

47 Homework Assignment #3

• Complete the proof of the Chandra-Merlin The- orem

• Express the Clique problem as a Conjunctive Query Evaluation problem.

• Express the Clique problem as a Conjunctive Query Contaiment Problem.

• Express the Latin Square Completion prob- lem as a Conjunctive Query Evaluation Prob- lem.

48 Course Outline

Constraint Satisfaction Problems

• Definition & Examples of CSP • CSP & the Homomorphism Problem • CSP & Database Theory • Computational Complexity of CSP – Uniform & Non-Uniform CSP – Schaefer’s Dichotomy Theorem for Boolean CSP – The Feder-Vardi Dichotomy Conjecture for general CSP • The Pursuit of Tractable Cases of CSP – Datalog & Pebble Games – Consistency Properties & Bounded Treewidth

49 Goals and Prerequisites

Goals:

• Show that Constraint Satisfaction Problems (CSP) form a broad class of algorithmic problems that arise naturally in several different areas.

• Present an overview of CSP with emphasis on the computational complexity aspects of CSP and on the connections with database theory and logic

Prerequisites:

• Familiarity with the basics of first-order logic.

• Familiarity with the basics of computational complexity (PTIME and NP-completeness).

50 Computational Complexity

Computational Complexity: • The quantitative study of solvability Y. Hartmanis – 1993 • Solvable decision problems are placed into classes according to the time/space resources required to solve them.

Some Major Complexity Classes:

P polynomial time NP nondeterministic polynomial time EXPTIME exponential time

51 Complexity Classes

Fact: The following containments hold:

P ⊆ NP ⊆ EXPTIME

Theorem: Hartmanis & Stearns – 1965

P ⊂ EXPTIME

Open Problems: • Is P 6= NP?

• Is NP 6= EXPTIME?

Note: It is generally believed that P 6= NP.

52 NP-Complete Problems

Definition: Let Q be a decision problem (a Boolean query). We say that Q is NP-complete if 1. Q ∈ NP. 2. Q is NP-hard, i.e., for every Q0 ∈ NP, there a polynomial-time many-one reduction f of Q0 to Q: x ∈ Q0 ⇐⇒ f(x) ∈ Q.

Theorem: S. Cook – 1971, R. Karp 1972, ... The following problems are NP-complete: • k-Sat, k ≥ 3 • k-Colorability, k ≥ 3 • Clique • Hamiltonian Cycle • Latin Square Completion 53 NP-Complete Problems

Fact: Let Q be an NP-complete problem. The following are equivalent:

• P = NP

• Q ∈ P.

Remark:

• NP-complete problems hold the secret of whether or not P = NP.

• Establishing that Q is NP-complete is viewed as evidence that Q 6∈ P and, hence, it is an intractable algorithmic problem.

54 Complexity of CSP

Definition: Uniform CSP

{(A, B): ∃ homomorphism h : A → B}

(Homomorphism Problem)

Theorem: Uniform CSP is NP-complete

Proof: • Uniform CSP is in NP: Given A, B, “guess” h : A → B and verify it is a homomorphism. • Uniform CSP is NP-hard: It contains NP-hard problems as special cases (e.g., 3-SAT)

55 Complexity of CQC

Definition: CQC Problem

Given Boolean queries Q1, Q2, does Q1 |= Q2?

Theorem: CQC is NP-complete

Proof: Same as Uniform CSP

Remark: • Recall that Boolean CQs are FO-sentences built from atomic formulas, ∧, and ∃ only. So, for such sentences, logical implication is decidable.

• In contrast, for arbitrary FO-sentences, logical implication is undecidable.

56 Coping with NP-Completeness

Question: How to cope with NP-completeness?

Answer: Garey & Johnson – 1979 • Design heuristic algorithms that may work well in practice.

• Identify polynomial-time solvable cases of the problem, the so-called islands of tractability, by imposing restrictions on the inputs.

Example 1: – 3-Sat is NP-complete. – Horn 3-Sat is in P. Example 2: – 3-Colorability is NP-complete. – 3-Colorability on graphs of bounded treewidth is in P.

57 Restricting Uniform CSP

Research Program: Identify all islands of tractabil- ity of Uniform CSP.

Definition: Let A, B be two classes of structures.

CSP(A, B) = {(A, B): A ∈ A, B ∈ B and

∃ homomorphism h : A → B}

Research Program: Identify all A, B such that CSP(A, B) is in P.

58 Non-Uniform CSP

Definition: Fix a structure A. CSP({A}, All) = {B : ∃ homomorphism h : A → B}

Fact: For every fixed A, CSP({A}, All) ∈ P Proof: Exercise.

Definition: Fix a structure B: CSP(All, {B}) = {A : ∃ homomorphism h : A → B}

Fact: The complexity of CSP(All, {B}) depends on B. Proof:

• CSP(All, {K3}) is NP-complete (3-Colorability)

• CSP(All, {K2}) is in P. (2-Colorability) 59 Complexity of Non-Uniform CSP

Notation: For simplicity, for every fixed struc- ture B, we put

CSP(B) = CSP(All, {B})

Research Program: • Identify the islands of tractability of CSP(B).

• In other words, characterize the structures B for which CSP(B) is solvable in polynomial time.

60 Boolean Non-Uniform CSP

Definition: Boolean Non-Uniform CSP B B CSP(B) with B = ({0, 1},R1 ,...,R1 ) Fact: Boolean Non-Uniform CSP problems are Generalized Satisfiability Problems, i.e., variants of Boolean satisfiability in CNF.

Examples: Each of the following problems is a CSP(B) problem for a suitable Boolean B.

• 3-SAT: B = ({0, 1},R0,R1,R2,R3) 0 0 0 • 2-SAT: B = ({0, 1},R0,R1,R2) • POSITIVE 1-IN-3-SAT: 3CNF-formulas with clauses (x ∨ y ∨ z); exactly one variable is true in each clause.

B = ({0, 1},R1/3), where

R1/3 = {(1, 0, 0), (0, 1, 0), (0, 0, 1). 61 Complexity of Boolean Non-Uniform CSP

Schaefer’s Dichotomy Theorem – 1978

• If B is Boolean structure, then CSP(B) is in P or it is NP-complete.

• Moreover, there is a polynomial-time algorithm to decide, given a Boolean structure B, whether CSP(B) is in P or it is NP-complete.

Question: Why is this a dichotomy theorem?

62 The Fine Structure of NP

Theorem: Ladner - 1975 If P 6= NP, then there is a problem Q such that • Q ∈ NP − P; • Q is not NP-complete.

NP-complete NP − P not NP-complete P

Remark: Consequently, it is conceivable that a family of NP-problems contains problems of such intermediate complexity problems. This possibility cannot be ruled out a priori.

63 Dichotomy of Boolean Non-Uniform CSP

Schaefer’s Dichotomy Theorem – 1978

• If B is Boolean structure, then CSP(B) is in P or it is NP-complete.

% NP-complete CSP(B) NP − P, not NP-complete & P

• Moreover, there is a polynomial-time algorithm to decide, given a Boolean structure B, whether CSP(B) is in P or it is NP-complete.

Question: What is the boundary in this dichotomy?

64 Dichotomy of Boolean Non-Uniform CSP

Proof Architecture:

B B B = ({0, 1},R1 ,...,Rm) • Identify a number of cases for which CSP(B) is in P. Each such case will be described in terms of B B conditions on the relations R1 ,...,Rm of B.

• Show that CSP(B) is NP-complete in all other cases.

• Show that there is a polynomial-time algorithm for checking whether the conditions describing the tractable cases hold.

65 Tractable Cases of Boolean CSP(B)

Definition: R ⊆ {0, 1}k, for some k ≥ 1.

• R is 0-valid if it contains the k-tuple (0,..., 0).

• R is 1-valid if it contains the k-tuple (1,..., 1).

B B Fact: B = ({0, 1},R1 ,...,Rm) B • If every Ri is 0-valid, then CSP(B) is in P.

B • If every Ri is 1-valid, then CSP(B) is in P.

Proof: Trivial • The constant function h(x) = 0 is a homomor- phism. • The constant function h(x) = 1 is a homomor- phism.

66 Relations and Solutions

Notation: Let ϕ be a Boolean formula with k variables x1, . . . , xk.

Sol(ϕ) is the set of all k-tuples (a1, . . . , ak) such that if s is a truth assignment with s(xi) = ai, 1 ≤ i ≤ k, then s(ϕ) = 1.

Example: If ϕ is (¬x ∨ y), then

Sol(ϕ) = {(0, 0), (0, 1), (1, 1)}.

Remark: For all remaining tractable cases, the condition on the relations of B will assert that each B relation Ri is equal to Sol(ϕ), where ϕ belongs to a certain class of “well-behaved” Boolean formulas (the class of formulas will change from tractable case to tractable case).

67 Tractable Cases of Boolean CSP(B)

Definition: R ⊆ {0, 1}k, for some k ≥ 1. R is bijunctive if there is a 2-CNF formula ϕ such that R = Sol(ϕ).

B B Theorem: B = ({0, 1},R1 ,...,Rm) B If every Ri is bijunctive, then CSP(B) is in P. Proof: 2-SAT is in P. The resolution algorithm runs in polynomial time on 2-CNF formulas, because • Every resolvent of two 2-clauses is a 2-clause:

{l1, l2}{l1, l3} &.

{l2, l3}

• Given n variables, there is a polynomial num- ber of 2-clauses involving these variables. 68 Tractable Cases of Boolean CSP(B)

Definition: R ⊆ {0, 1}k, k ≥ 1. • R is Horn if R = Sol(ϕ) for some Horn formula ϕ, i.e., a CNF-formula in which each conjunct has at most one positive literal.

x ∧ (¬x ∨ z) ∧ (¬y ∨ ¬z ∨ w)

• R is dual Horn R = Sol(ϕ) for some dual Horn formula ϕ, i.e., a CNF-formula in which each conjunct has at most one negative literal.

¬x ∧ (x ∨ ¬z) ∧ (y ∨ z ∨ ¬w)

B B Theorem: B = ({0, 1},R1 ,...,Rm) B • If every Ri is Horn, then CSP(B) is in P.

B • If every Ri is dual Horn, then CSP(B) is in P.

Proof: Unit Propagation algorithm. 69 Tractable Cases of Boolean CSP(B)

Definition: R ⊆ {0, 1}k, k ≥ 1. R is affine if it is the set of solutions of a system of linear equations over the 2-element field.

Example: R = {(0, 1), (1, 0)} is affine, because

R = Sol(x ⊕ y = 1).

B B Theorem: B = ({0, 1},R1 ,...,Rm) B If every Ri is affine, then CSP(B) is in P. Proof: Use Gaussian Elimination algorithm.

70 Homework Assignment #4

• Show that 2-SAT can be solved in polynomial time using resolution.

• Show that Horn SAT and Dual Horn SAT can be solved in polynomial time using suitable unit propagation algorithms.

• Show that Affine SAT can be solved in poly- nomial time using Gaussian elimination.

• Show that the formula (x ∨ y) is not logically equivalent to any Horn formula.

• Show that the formula (x ∨ y) ∧ (¬x ∨ ¬y) is not logically equivalent to any (dual) Horn for- mula.

71 Schaefer Structures

B B Definition: B = ({0, 1},R1 ,...,Rm) B is Schaefer if at least one of the following six conditions holds:

B • Every relation Ri is 0-valid.

B • Every relation Ri is 1-valid.

B • Every relation Ri is bijunctive.

B • Every relation Ri is Horn.

B • Every relation Ri is dual Horn.

B • Every relation Ri is affine.

Otherwise, we say that B is non-Schaefer.

72 Schaefer’s Dichotomy Theorem

B B Theorem: Let B = ({0, 1},R1 ,...,Rm). • If B is Schaefer, then CSP(B) is in P.

• If B is non-Schaefer, then CSP(B) is NP-complete.

Proof: • The first part has already been established.

• The second part requires much more work. The key ingredient is a theorem about the ex- pressive power of conjunctive queries on non- Schaefer structures.

73 Schaefer’s Expressibility Theorem

Note: Recall that a conjunctive query is a FO- formula built from atomic formulas, ∧, and ∃ only.

B B Theorem: Let B = ({0, 1},R1 ,...,Rm). If B is non-Schaefer, then every Boolean relation R ⊂ {0, 1}k, k ≥ 1, is definable on B by some conjunctive query over B with constants 0 and 1. This means that for every k ≥ 1 and for every k R ⊂ {0, 1} , there is a formula ψ(x1, . . . , xk, y, z) B B that is built from R1 ,...,Rm, ∧, and ∃, and has k the property that for every (a1, . . . , ak) ∈ {0, 1}

(a1, . . . , ak) ∈ R ⇐⇒ B |= ψ(a1, . . . , ak, 0, 1).

Remark: Intutitively, this result asserts that on non-Schaefer structures, conjunctive queries have maximum expressive power (they can define all Boolean relations). 74 Schaefer’s Dichotomy Theorem

B B Theorem: Let B = ({0, 1},R1 ,...,Rm). • If B is Schaefer, then CSP(B) is in P.

• If B is non-Schaefer, then CSP(B) is NP-complete.

• Moreover, there are simple polynomial-time cri- teria to test if B is Schaefer.

Remark: These criteria involve closure properties of the relations of B.

75 Closure Properties

Theorem: Let R ⊆ {0, 1}k, k ≥ 1. • R is bijunctive iff it is closed under the 3-ary majority function maj(x, y, z).

• S is Horn iff it is closed under g(x, y) = x ∧ y.

• S is dual Horn iff it is is closed under g0(x, y) = x ∨ y.

• S is affine iff it is closed under h(x, y, z) = x ⊕ y ⊕ z.

76 Closure Properties

Example: Let R = {(0, 1), (1, 0), (1, 1)} • R is not Horn, because

(0, 1) ∧ (1, 0) = (0, 0) 6∈ R.

• R is not affine, because

(0, 1) ⊕ (1, 0) ⊕ (1, 1) = (0, 0) 6∈ R.

Example: Let R = {(0, 0), (0, 1), (1, 0)} • R is not dual Horn, because

(0, 1) ∨ (1, 0) = (1, 1) 6∈ R.

• R is not affine, because

(0, 0) ⊕ (0, 1) ⊕ (1, 0) = (1, 1) 6∈ R.

77 Positive 1-in-3 SAT

• 3-CNF formula with clauses (x ∨ y ∨ z); exactly one variable/clause is true.

• Positive-1-In-3-SAT = CSP(({0, 1},R1/3))

R1/3 = {(1, 0, 0), (0, 1, 0), (0, 0, 1).

Fact: Positive 1-In-3-SAT is NP-complete Proof: Apply Schaefer’s Dichotomy Theorem

• R1/3 is not bijunctive, because maj((1, 0, 0), (0, 1, 0), (0, 0, 1)) = (0, 0, 0) 6∈ R.

• R1/3 is neither Horn nor dual Horn, since

(1, 0, 0) ∧ (0, 1, 0) = (0, 0, 0) 6∈ R1/3.

(1, 0, 0) ∨ (0, 1, 0) = (1, 1, 0) 6∈ R1/3.

• R1/3 is not affine, since

(1, 0, 0) ⊕ (0, 1, 0) ⊕ (0, 0, 1) = (1, 1, 1) 6∈ R1/3.

78 Homework Assignment #5

• Given an example of a Boolean structure B such that CSP(B) is NP-complete, but every relation in B is Horn or dual Horn. Repeat for the other combinations of tractable cases in Schaefer’s Dichotomy Theorem.

• Use Schaefer’s Dichotomy Theorem to show that 2-Colorability is in P.

• Use Schaefer’s Dichotomy Theorem to show that the following are NP-complete: – Not-All-Equal 3-SAT

– Monotone 3-SAT

79 Schaefer’s Dichotomy Theorem

B B Theorem: Let B = ({0, 1},R1 ,...,Rm). • If B is Schaefer, then CSP(B) is in P.

• If B is non-Schaefer, then CSP(B) is NP-complete.

• Moreover, there are simple polynomial-time cri- teria to test if B is Schaefer.

Remark: Schaefer’s Dichotomy Theorem is the mother of many other dichotomy results for fami- lies of decision problems.

80 Other Dichotomy Theorems

Theorem: Lewis – 1979 Let T be a finite set of Boolean functions and let SAT(T ) be the satisfiability problem for formulas built from the functions in T only.

• If S1 ⊆ [T ], then SAT[T ] is NP-complete. Equivalently, if (x ∧ ¬y) ∈ [T ], then SAT[T ] is NP-complete. • Otherwise, SAT[T ] is in P.

Theorem: Fortune, Hopcroft and Wiley – 1980 Dichotomy for the complexity of the Graph Home- omorphism Problem.

81 More Dichotomy Theorems

• Dichotomy for MAX SAT in Schaefer’s frame- work Creignou – 1995

• Dichotomy for #P in Schaefer’s framework Creignou and Hermann – 1996

• Dichotomy for LEXMINSAT in Lewis’ frame- work Reith and Vollmer – 2000

• Dichotomy for MINSAT in Schaefer’s frame- work Kirousis and K .... – 2001

• Dichotomy for CIRC INF in Schaefer’s frame- p work (Π2 vs. coNP) Kirousis and K ... – 2001

82 Complexity of CSP(B)

Conclusion: Schaefer’s Dichotomy Theorem yields a complete classification of the complexity of CSP(B) for Boolean structures B.

Question: What about arbitrary structures B?

83 Complexity of CSP(B)

Conclusion: Schaefer’s Dichotomy Theorem yields a complete classification of the complexity of CSP(B) for Boolean structures B.

Question: What about arbitrary structures B?

Dichotomy Conjecture: Feder & Vardi –1993 For every structure B, one of the following holds: • CSP(B) is in P.

• CSP(B) is NP-complete.

84 Towards the Dichotomy Conjecture

Theorem: Hell & Neˇsetril– 1990 Let B be an undirected graph. • If B is 2-Colorable, then CSP(B) is in P.

• If B not 2-Colorable, then CSP(B) is NP-complete.

Theorem: A. Bulatov – 2002 The Dichotomy Conjecture is true for CSP(B), where B = {0, 1, 2}.

Note: The Dichotomy Conjecture remains open for CSP(B) with |B| ≥ 4.

85 Islands of Tractability of CSP(B)

Fact: An extensive pursuit of tractable cases of CSP(B) has taken place over the years.

First Stage: Numerous particular results.

Second Stage: Broad sufficient conditions for tractability of CSP(B).

Fact: Unifying explanations for many tractability results have been discovered: • Expressibility in Datalog Feder & Vardi – 1993

• Closure Properties of Constraints Connections with universal algebra Feder & Vardi – 1993 Jeavons & collaborators – 1995 to present

86 Course Outline

Constraint Satisfaction Problems (CSP)

• Definition & Examples of CSP • CSP & the Homomorphism Problem • CSP & Database Theory • Computational Complexity of CSP – Uniform & Non-Uniform CSP – Schaefer’s Dichotomy Theorem for Boolean CSP – The Feder-Vardi Dichotomy Conjecture for general CSP • The Pursuit of Tractable Cases of CSP – Datalog & Pebble Games – Consistency Properties & Bounded Treewidth

87 Datalog

Note: Recall that CQs can be written as rules:

Path of Length 2 - P 2: (∃z)(E(x1, z)∧E(z, x2)

P 2(x1, x2): − E(x1, z),E(z, x2)

Definition: • Datalog = Conjunctive Queries + Recursion Logic Programming without function symbols • A Datalog program is a finite set of rules given by conjunctive queries

T (x): − S1(y1),...,Sr(yr). – Some relation symbols may occur both in the heads and the bodies of rules. These are the recursive relation symbols or intensional database predicates (IDBs). – The remaining relation symbols are the extensional database predicates (EDBs). 88 Datalog Examples

Definition: Transitive Closure Query TC Given graph H = (V,E),

TC(H) = {(a, b) ∈ V 2 : there is a path from a to b}.

Example 1: Datalog program for TC ¯ ¯ ¯ S(x, y): − E(x, y) ¯ ¯ ¯ S(x, y): − E(x, z) ∧ S(z, y)

Example 2: Another Datalog program for TC ¯ ¯ ¯ S(x, y): − E(x, y) ¯ ¯ ¯ S(x, y): − S(x, z) ∧ S(z, y)

Note: • E is the EDB. • S is the IDB; it defines TC. 89 Datalog Examples

Definition: S. Cook – 1974 Path Systems S = (F, A, R) Given a finite set of formulas F , a set of axioms A ⊆ F , and a rule of inference R ⊆ F 3, compute the theorems of this system.

Example: Datalog program for Path Systems: ¯ ¯ ¯ T (x): − A(x) ¯ ¯ ¯ T (x): − T (y),T (z),R(x, y, z)

Note: • A and R are the EDBs. • T is the IDB; it defines the theorems of S.

90 Tractability of Datalog

Fact: • If a query Q is defined by a Datalog program, then Q is in P.

Proof: • Datalog programs can be evaluated “bottom- up” in a polynomial number of iterations.

91 Evaluation of Datalog Programs

Example : Datalog program for TC ¯ ¯ ¯ S(x, y): − E(x, y) ¯ ¯ ¯ S(x, y): − E(x, z) ∧ S(z, y)

Bottom-up Evaluation ¯ ¯ ¯ S1 = ∅ ¯ ¯ ¯ Sm+1 = {(a, b)) : ∃z(E(a, z) ∧ Sm(z, b)}

Fact: S m TC = m S TC = S|V |.

92 Datalog(6=)

Definition: Datalog(6=) is the extension of Datalog in which 6= are allowed in the rules.

Example: w-Avoiding Paths: Let Q be the query: given G = (V,E) and nodes x, y, w, is there a path from x to y avoiding w? • Q is definable by the Datalog(6=) program:

T (x, y, w): − E(x, y) ∧ w 6= x ∧ w 6= y T (x, y, w): − E(x, z) ∧ T (z, y, w) ∧ w 6= x.

93 Even Simple Path

Definition: Let Even Simple Path be the query: given a graph G = (V,E) and two nodes x, y, is there a simple path of even length from x to y?

Fact: Even Simple Path is NP-complete. (Fortune, Hopcroft, Wiley - 1980)

Fact: Even Simple Path on undirected graphs is definable by a Datalog(6=) program (so, is in P). (Yannakakis - 1990)

94 Datalog(6=)-Program for Even Simple Path

¯ ¯ ¯ T (x, y, w): − E(x, y) ∧ w 6= x ∧ w 6= y ¯ ¯ ¯ T (x, y, w): − E(x, z) ∧ T (z, y, w) ∧ w 6= x ¯ ¯ ¯ P (x, y): − E(x, y) ¯ ¯ ¯ P (x, y): − Q(x, w),E(w, y),T (x, w, y) ¯ ¯ Q(x, y): − P (x, w),P (w, y),T (x, w, y).

Fact: • T defines w-Avoiding Path; • P defines Odd Simple Path • Q defines Even Simple Path Proof: Exercise – Use Menger’s Theorem.

95 Preservation Properties

Fact: Preservation Properties of Datalog. • Datalog queries are preserved under homomorphisms. Similarly, if a query Q is defined by a Datalog program, then Q is monotone, i.e., the query is preserved if new tuples are added to the EDBs. • Datalog(6=) queries are preserverd under one-to-one homomorphisms. Proof: Exercise - use preservation properties of positive existential formulas.

Fact: On the class G of all finite graphs:

Datalog[G] $ Datalog(6=)[G] $ PTIME[G].

96 Datalog and CSP

B B Fact: Let B = (B,R1 ,...,Rm). • In general, CSP(B) is not monotone. (Why?)

• Hence, CSP(B) is not expressible in Datalog.

However,

• CSP(B) is monotone, where

CSP(B) = {A : 6 ∃ homomorphism h : A → B}.

• Hence, it is conceivable that CSP(B) is expressible in Datalog (and, thus, it is in P).

97 Datalog and CSP

Fact: Feder & Vardi – 1993 Expressibility of CSP(B) in Datalog is a unify- ing explanation for many tractability results about CSP(B).

Example: 2-Colorability = CSP(K2) Datalog program for Non 2-Colorability ¯ ¯ ¯ O(X,Y ): − E(X,Y ) ¯ ¯ ¯ O(X,Y ): − O(X,Z),E(Z,W ),E(W, Y ) ¯ ¯ Q : − O(X,X)

Example: Let B be Boolean structure. If B is bijunctive, Horn, or dual Horn, then CSP(B) is expressible in Datalog.

98 Datalog and CSP

Theorem: Feder & Vardi – 1993

• If B = (B,R1,...,Rk) is such that

Pol({R1,...,Rk}) contains a near-unanimity function, then CSP(B) is expressible in Dat- alog.

• If B = (B,R1,...,Rk) is such that

Pol({R1,...,Rk}) contains an ACI function, then CSP(B) is expressible in Datalog.

99 Datalog and CSP

Open Problem: Is there an algorithm to decide whether, given B, we have that CSP(B) is express- ible in Datalog?

B B Question: Fix B = (B,R1 ,...,Rm).

When is CSP(B) expressible in Datalog?

Remark: Feder & Vardi have shown that, for every structure B, the following are equivalent:

• CSP(B) expressible in Datalog.

• CSP(B) expressible in Datalog(6=).

100 Datalog and CSP

B B Question: Fix B = (B,R1 ,...,Rm).

When is CSP(B) expressible in Datalog?

Answer: K ... & Vardi – 1998, 2000

Expressibility of CSP(B) in Datalog can be characterized in terms of

• Finite-Variable Logics and Pebble Games;

• Consistency Properties.

101 ω The Infinitary Logic L∞ω

Definition: Barwise (1977) • Infinitary logic with k variables, k ≥ 1. k L∞ω is the collection of L∞ω-formulas with at most k distinct variables. • Infinitary logic with finitely many variables [ ω k L∞ω = L∞ω. k≥1

ω Fact: L∞ω is a useful tool in analyzing the expres- sive power of least fixed-point logic.

Theorem: Barwise – 1977, Immerman – 1981

ω L∞ω-definability can be characterized in terms of pebble games.

102 ω Existential Positive Fragments of L∞ω

Definition: K . . . & Vardi – 1995 • ∃Lk is the fragment of Lk that contains all ∞ω ∞ω W V atomic formulas and is closed under ∃, , . • Existential infinitary logic with finitely many variables [ ω k ∃L∞ω = ∃L∞ω. k≥1

k ω • ∃L∞ω(6=) and ∃L∞ω(6=)

Fact:

ω • Datalog ⊆ ∃L∞ω

ω • ∃L∞ω is a useful tool for analyzing the expres- sive power of Datalog.

ω • ∃L∞ω-definability can be characterized in terms of existential pebble games. 103 Existential k-Pebble Games

Spoiler and Duplicator play on two structures A and B. Each player uses k pebbles. In each move,

• Spoiler places a pebble on or removes a pebble from an element of A.

• Duplicator tries to duplicate the move on B.

A : a1 a2 . . . al ↓ ↓ · · · ↓

B : b1 b2 . . . bl l ≤ k

• Spoiler wins the (∃, k)-pebble game if at some

point the mapping ai 7→ bi, 1 ≤ i ≤ l, is not a partial homomorphism.

• Duplicator wins the (∃, k)-pebble game if the above never happens. 104 Example

Cliques of Different Size v LQ Q  Q v v  L Q @  L Q v Qv @ b L " @ B b " ¢ b L " ¢ @ B b "L ¢ B b " @ "b L ¢ @ B " b " b L ¢ @@vv BB v" bL v¢

K4 K5

Fact: Let Kk be the k-clique

• Duplicator wins the (∃, k)-pebble game on Kk

and Kk+1.

• Spoiler wins the (∃, k)-pebble game on Kk and

Kk−1. 105 Linear Orders of Different Size w w

w w

w w

w w

w w

w w

w

Lm Ln

• Spoiler wins the (∃, 2)-pebble game

on Lm and Ln, where m > n. • Duplicator wins the (∃, 2)-pebble game

on Ln and Lm, where m > n. 106 Winning Strategies in the (∃, k)-Pebble Game

Definition: A winning strategy for the Duplicator in the (∃, k)-pebble game is a non-empty family I of partial homomorphisms from A to B such that • If f ∈ I and h ⊆ f, then h ∈ I (I is closed under subfunctions).

• If f ∈ I and |f| < k, then for every a ∈ A, there is g ∈ I so that f ⊆ g and a ∈ dom(g). (I has the forth property up to k)

Note: Similar definition of winning strategy for the Duplicator in the (∃, 6=, k)-pebble game with partial one-to-one homomorphims in place of par- tial homomorphisms.

107 Uses of Existential k-Pebble Games

• Existential k-pebble games can be used to study k k the expressive power of ∃L∞ω and ∃L∞ω(6=).

• In particular, existential k-pebble games can be used to obtain lower bounds for definability in Datalog and Datalog(6=).

108 k (∃, k)-Pebble Games and ∃L∞ω-Definability

Definition: Let A, B be two σ-structures.

k k A ¹∞ω B, if every ∃L∞ω-sentence that is true on A is also true on B.

Theorem: K . . . & Vardi – 1995 The following statements are equivalent.

k • A ¹∞ω B. • Duplicator wins the (∃, k)-pebble game on A and B.

Remark: An analogous result holds for (∃, 6=, k)-pebble games k and ∃L∞ω(6=).

109 k Lower Bounds for ∃L∞ω-Definability

Corollary: Let Q be a Boolean query on C. The following statements are equivalent:

ω • Q is ∃L∞ω-definable on C. • There is a k such that for every A, B in C, if A |= Q and B 6|= Q, then the Spoiler wins the (∃, k)-pebble game on A, B.

ω A Methodology for ∃L∞ω-Definability To show that a Boolean query Q is not expressible ω in ∃L∞ω, suffices to show that for every k there are structures Ak and Bk such that

• Ak |= Q and Bk 6|= Q. • Duplicator wins the (∃, k)-pebble game on A, B.

Fact: This method is also complete.

ω Similar method for ∃L∞ω(6=)-definability. 110 FIxed Subgraph Homeomorphism (FISH)

Definition: Let H and G be directed graphs. Homeomorphism h : H Ã G • h maps 1 − 1 the nodes of H to nodes of G;

• h maps the edges of H to pairwise node-disjoint simple paths of G.

Definition: Let H be a fixed directed graph. FISH Query with Pattern H asks:

• Given a directed graph G and a 1 − 1 mapping from the nodes of H to the nodes of G, is there a homeomorphism h : H Ã G extending this mapping?

111 Two Disjoint Paths Query

Pattern H

s1 t v- 1 v s t 2v- 2 v

s0 t0 v1 - v - b b b - v1 s0 t0 2v - v - b b b - v2

FISH Query with Pattern H: Does G contain two node-disjoint simple paths 0 0 0 0 from s1 to t1 and from s2 to t2?

112 Computational Complexity of FISH Queries

Star Graphs: single source or a single sink v v v 6 v v v @I  @ v@ v - v v -@R v? © v b b b  6 b v © b v b v? v

Hopcroft, Fortune, Wiley (1980)

Dichotomy Theorem: • If H is a star graph, then the FISH query with pattern H is in P. • If H is not a star graph, then the FISH query with pattern H is NP-complete.

Note: This dichotomy is proper only if P 6= NP.

113 Dichotomy Theorem for FISH Queries

Theorem: K ... & Vardi – 1995 • If H is a star graph, then the FISH query with ω pattern H is ∃L∞ω(6=)-definable; in fact, it is definable in Datalog(6=).

• If H is not a star graph, then the FISH query ω with pattern H is not ∃L∞ω(6=)-definable.

Note: This dichotomy is indeed proper. No complexity-theoretic assumptions are made.

114 Dichotomy Theorem for FISH Queries

Hint of Proof: Two Disjoint Paths query 2DP is the key case of the negative results.

Show that for every k ≥ 1 there are directed graphs

Ak and Bk such that

• Ak |= 2DP and Bk 6|= 2DP

• Duplic. wins the (∃, k)-pebble game on A, B.

How do we find such directed graphs Ak and Bk?

• Ak consists of two disjoint long paths;

• Bk is extracted from the NP-completeness proof. Duplicator’s winning strategy is quite complicated.

Moral: NP-completeness proofs can also yield interesting structures to play combinatorial games. 115 k-Datalog

Definition: A k-Datalog program is a Datalog program in which each rule

t0 : − t1, . . . , tm has at most k distinct variables in the head t0 and at most k distinct variables in the body t1, . . . , tm.

Example: Non 2-Colorability revisited ¯ ¯ ¯ O(X,Y ): − E(X,Y ) ¯ ¯ ¯ O(X,Y ): − O(X,Z),E(Z,W ),E(W, Y ) ¯ ¯ Q : − O(X,X)

Therefore, Non 2-Colorability is expressible in 4-Datalog.

116 k-Datalog and (∃, k)-Pebble Games

Theorem: K ... & Vardi • On the class of all finite structures,

k k-Datalog ⊂ ∃L∞ω.

• There is a polynomial-time algorithm to decide whether, given two finite structures A and B, whether the Spoiler or the Duplicator wins the (∃, k)-pebble game on A and B.

• For each finite structure B, there is a k-Datalog

program πB that expresses the query: given a finite σ-structure A, does the Spoiler win the (∃, k)-pebble game on A and B?

117 Datalog and CSP

Theorem: K ... & Vardi – 1998 Let k be a positive integer and B a finite structure. Then the following are equivalent:

• CSP(B) is expressible in k-Datalog

k • CSP(B) is expressible in ∃L∞ω

• CSP(B) = {A : Duplicator wins the (∃, k)-pebble game on A and B}.

Note: It is always the case that

CSP(B) ⊆ {A : Duplicator wins the

(∃, k)-pebble game on A and B}.

118 Islands of Tractability of CSP

Non-Uniform CSP(B) Problems • Definability of CSP(B) in Datalog

ω • Definability of CSP(B) in ∃L∞ω.

• Single canonical polynomial-time algorithm: determine who wins the (∃, k)-pebble game.

119 Consistency Properties

Remark: • Many heuristic algorithms for CSP involve con- straint propagation, i.e., deriving new constraints that are implied from the given constraints. (recall unit propagation for Horn SAT.)

• This is formalized using the notion of consis- tency properties, such as node/arc/path consis- tency, and, generally, strong k-consistency.

120 Consistency Properties & Pebble Games

• It turns out that there are tight connections between consistency properties and (∃, k)-pebble games.

• These connections provide a different perspec- tive on consistency properties and help identify additional tractable cases of CSP.

121 CSP and Consistency Properties

Definition: An instance (V,D,C) of CSP is • i-consistent if for every partial solution on i−1

variables v1, . . . , vi−1 and for every variable vi,

there is some partial solution on v1, . . . , vi−1, vi extending that solution.

• strongly k-consistent if for every i ≤ k, it is i-consistent.

• globally consistent if every partial solution can be extended to a solution.

Note: Strong k-consistency is a key concept in designing heuristics for CSP and identifying tractable cases.

122 Consistency Properties and Pebble Games

Definition: A winning strategy for the Duplicator in the (∃, k)-pebble game on A, B is a non-empty family I of k-partial homomorphisms from A to B such that: • I is closed under subfunctions.

• I has has the k-forth property: For every f ∈ I with |f| < k and every a ∈ A, there is g ∈ I such that f ⊆ g and a ∈ dom(g).

Observation: Let A, B be the instance of the Homomorphism Problem associated with (V,D,C). Then the following are equivalent: • (V,D,C) is strongly k-consistent.

• The family of all k-partial homomorphisms from A to B is a winning strategy for the Duplicator in the (∃, k)-pebble game on A and B. 123 Establishing Strong k-Consistency

Definition: Let (V,D,C) be an instance of CSP. Establishing strong k-consistency for (V,D,C) means finding a strongly k-consistent instance (V 0,D0,C0) of CSP such that • V = V 0 and D = D0; • (V 0,D0,C0) is at least as constrained as (V,D,C); • (V,D,C) and (V 0,D0,C0) have the same space of solutions.

Note: Establishing strong k-consistency is a key step in constraint propagation and in identifying tractable cases in which a solution can be found using a backtrack-free search.

124 When can strong k-consistency be established?

Theorem: K ... & Vardi – 2000 The following are equivalent for a CSP-instance (V,D,C). • It is possible to establish strong k-consistency for (V,D,C).

• The Duplicator wins the (∃, k)-pebble game on A and B, where A, B form the instance of the Homomorphism Problem associated with (V,D,C).

Furthermore, when this happens, the set Wk(A, B) of all winning configurations for the Duplicator gives rise to the largest (and, hence, least constrained) CSP-instance that establishes strong k-consistency for (V,D,C).

Intuition: Computing winning strategies for the Duplicator amounts to constraint propagation. 125 Consistency Properties and Datalog

Corollary: Let B be a fixed finite σ-structure. Then the following are equivalent. • CSP(B) is expressible in k-Datalog

• For every finite σ-structure A, establishing strong k-consistency for A and B implies that there is a homomorphism from A to B.

Note: Moreover, in this case CSP(B) is solvable in polynomial-time.

126 Tractable Cases of CSP

CSP(B) is solvable in polynomial time, if • Establishing strong k-consistency for A and B implies that that there is a homomorphism from A to B. (CSP(B) is expressible in k-Datalog)

– Advantage: Wide applicability – Disadvantage: No information on how to construct a homomorphism

• Establishing strong k-consistency for A and B implies that every partial homomorphism from A to B can be extended to a homomorphism from A to B. (Equivalent to Pol(S) being closed under a near unanimity operation.)

– Disadvantage Limited applicability – Advantage A homomorphism can be constructed in a backtrack-free manner. 127 Another Sufficient Condition for Tractability

Definition: CSP(B) is k-local if CSP(B∗) is in k-Datalog, where B∗ is the expansion of B with all elements of B as constants (each element of B is encoded by a unary predicate).

Theorem: Let B be a finite σ-structure. • If CSP(B) is k-local, then there is a polynomial- time algorith such that, given A, constructs a homomorphism from A to B or tells that no homomorphism exists.

• k-locality is in between the previous two tractable cases of CSP. In particular, Horn k-Sat, k ≥ 3, is k-local, but is not is not closed under any near una- nimity operation.

128 Islands of Tractability of CSP

Non-Uniform CSP(B) Problems • Definability of CSP(B) in Datalog

ω • Definability of CSP(B) in ∃L∞ω. • CSP(B) = {A : Duplicator wins the (∃, k)-pebble game on A and B}.

• Single canonical polynomial-time algorithm: determine whether the Spoiler or the Duplica- tor wins the (∃, k)-pebble game.

129 From Non-Uniform CSP to Uniform CSP

Note: So far, we have focused on tractable cases of non-uniform constraint satisfaction problems.

CSP(B) = CSP(All, {B})

Fact: Many applications in AI and in database systems involve uniform CSP(A, B), where A and B are classes of structures. For example, usually, conjunctive query contain- ment and conjunctive query evaluation appear as uniform CSPs.

Question: Can we obtain tractability results for uniform CSP from tractability results for non-uniform CSP?

130 From Non-Uniform CSP to Uniform CSP

Note: Let A and B be two classes of structures. It is possible that CSP(A, {B}) is in P, for every B in B, but CSP(A, B) is NP-complete.

Example: K = all cliques and G = all graphs: • CSP(K, {H}) is in P, for every graph H; • CSP(K, G) in NP-complete. (it is the Clique problem)

Conclusion: Tractability results for non-uniform CSP do not necessarily yield tractability results for uniform CSP.

131 k-Datalog and CSP

However, certain non-uniform tractable cases do uniformize!

Definition: For every positive integer k, let

Dk = {B : CSP(B) is expressible in k-Datalog}.

Remark: Recall that if B ∈ Dk, then CSP(B) is in P.

Theorem:

CSP(All, Dk) is in P, for every positive integer k. Proof: An easy consequence of the following: • Characterization of when CSP(B) is express- ible in k-Datalog (using (∃, k)-pebble games).

• Polynomial-time algorithm for determining the winner in the (∃, k)-pebble game. 132 Datalog and CSP

Conclusions: • Expressibility in Datalog provides a unifying explanation for many tractability results about non-uniform CSP.

• Expressibility of non-uniform CSPs in k-Datalog

uniformizes, which implies that CSP(All, Dk) is a large island of tractability for uniform CSP.

However,

Open Problem: Let k be a positive integer. Is there an algorithm to test, given a structure B, whether or not CSP(B) is expressible in k-Datalog?

133 Bounded Treewidth: Motivation

Observation: Many algorithmic problems that are “hard” on arbitrary graphs become “easy” on trees.

Question: Can the concept of tree be weakened while maintaining “good” computational proper- ties?

134 Bounded Treewidth: Motivation

Observation: Many algorithmic problems that are “hard” on arbitrary graphs become “easy” on trees.

Question: Can the concept of tree be weakened while maintaining “good” computational proper- ties?

Answer: Robertson & Seymour – 1985–1995 Yes, the concept of bounded treewidth achieves this.

Note: Part of their work on graph minors.

(Downey & Fellows: “Parametrized Complexity”)

135 k-Trees, Partial k-Trees, Treewidth

Definition: Let k be a positive integer. • A graph H is a k-tree if

1. H is a Kk+1-clique, or

2. There are a k-tree G, nodes a1, . . . , ak in

G forming a Kk-clique, and a node b in H − G such that H is obtained from G by

connecting b to each of the nodes a1, . . . , ak

(thus, forming a Kk+1-clique).

• A graph is a partial k-tree if it is a subgraph (not necessarily induced) of a k-tree.

• The treewidth of a graph H, denoted by tw(H), is the smallest k such that H is a partial k-tree.

136 Treewidth: Examples

Fact: • If T is a tree, then tw(T) = 1.

• If C is a cycle, then tw(C) = 2.

• tw(on) = 2.

• tw(¯) = 2.

• ...

• tw(Kk) = k − 1, for every k ≥ 2.

137 Treewidth: Characterizations

Fact: The concept of treewidth can be character- ized in two different ways:

• Using tree decompositions of graphs.

• Using induced widths of orderings of the nodes.

Note: All three descriptions of treewidth are use- ful, and are needed in different applications.

138 Treewidth: Complexity

Fact: The following problem is NP-complete: Given graph H and integer k ≥ 1, is tw(H) ≤ k?

However,

Theorem: Bodlaender – 1993 For every integer k ≥ 1, there is a linear-time al- gorithm such that, given a graph H, it determines whether or not tw(H) ≤ k.

139 Bounded Treewidth

Definition: For every integer k ≥ 1,

T (k) = {H : tw(H) < k}.

Fact: Fix an integer k ≥ 2. Many problems that are NP-complete on arbitrary graphs become tractable on T (k). 3-Colorability, Monochromatic Triangle, ...

140 Treewidth & Monadic Second-Order Logic

Definition: Monadic Second-Order Logic

(∃X1∀X2 · · · ∃Xm)ψ, where X1,...,Xm are set variables and ψ is first- order.

Fact: For every m ≥ 1, monadic second-order logic can express problems that are complete for P the m-th level Σm of the polynomial hierarchy PH.

Theorem: Courcelle – 1990 Let Φ be a sentence of monadic second-order logic. For every integer k ≥ 2, there is a polynomial-time algorithm such that, given a graph H ∈ T (k), it determines whether or not H |= Φ.

141 CSP & Monadic Second-Order Logic

B B Fact: If B = (B,R1 ,...,Rm) is a finite struc- ture, then CSP(B) is definable by a sentence of existential monadic second-order logic with a uni- versal first-order part, i.e., by a sentece of the form

(∃X1 · · · ∃Xn)(∀y1 · · · ∀ys)θ, where θ is quantifier-free.

Proof: Use one Xi for each element of B.

Example: 3-Colorability

(∃R∃G∃B)(∀y1∀y2)θ.

Corollary: (to Courcelle’s Theorem) If k ≥ 2, then CSP(T (k), {B}) is in P.

142 Uniform CSP and Bounded Treewidth

Note: The notion of treewidth can be defined for A A arbitrary finite structures A = (A, R1 ,...,Rm). tw(A) = tw(G(A)), where G(A) = (A, E) is the Gaifman graph of A, i.e.,

0 0 A E(a, a ) ⇐⇒ a, a occur in a tuple of Ri , for some i.

Theorem: Dechter & Pearl - 1989, Freuder - 1990 If k ≥ 2, then CSP(T (k), All) is in P.

Note: Thus, CSP(T (k), All) is a large island of tractability for uniform CSP. Moreover, Bodlaender’s Theorem implies that it is an easily “accessible” island.

143 Logical Aspects of Bounded Treewidth

Fact: Fix an integer k ≥ 2. • The tractability of CSP(T (k), All) can be ex- plained in terms of expressibility in k-Datalog.

• There are tight connections between having tw(A) < k and the canonical query QA be- ing definable in a fragment of first-order logic with k variables.

K ... & Vardi – 1998 Dalmau, K ..., Vardi – 2002

144 Finite-Variable Logics

Definition: Fix an integer k ≥ 2. • FOk is the collection of all first-order formulas with k distinct variables.

• Lk is the collection of all FOk-formulas built using atomic formulas, ∧, and ∃ only.

Example: Let Cn be the n-element cycle, n ≥ 3.

The canonical conjunctive query QCn is expressible in L3.

For instance, QC4 is logically equivalent to

(∃x∃y∃z)(E(x, y)∧E(y, z)∧(∃y)(E(z, y)∧E(y, x))).

It is no accident that tw(Cn) = 2 ...

145 Bounded Treewidth & Finite-Variable Logics

Theorem: Fix an integer k ≥ 2. • If A ∈ T (k), then the canonical conjunctive query QA is expressible in Lk.

• If B is a fixed structure, then CSP(T (k), {B}) is expressible in k-Datalog.

Proof: • Use the construction of A as a partial k-tree.

• Combine the above with the characterization of expressibility of non-uniform CSP in k-Datalog in terms of the (∃, k)-pebble game.

Corollary: CSP(T (k), All) is in P.

Algorithm: Determine whether the Spoiler or the Duplicator wins the (∃, k)-pebble game.

146 Bounded Treewidth & Finite-Variable Logics

Question: When is QA definable in Lk?

Definition: A and B are homomorphically equiv- alent, denoted A ∼h B, if there are homomor- phisms h : A → B and h0 : B → A.

Theorem: Fix a k and a finite structure A. Then the following are equivalent: • QA is definable in Lk.

• There is some B ∈ T (k) such that A ∼h B.

• core(A) ∈ T (k).

147 Cores

Definition: We say that a structure B is the core of a structure A if • B is a submodel of A • There is no homomorphism h : B → B0 from B to a proper submodel B0 of B.

Examples:

• core(Kk) = Kk

• core(Cn) = Cn

• core(on) = K3

• If H is 2-colorable, then core(H) = K2.

Note: Cores play an important role in database query processing and optimization.

148 Bounded Treewidth & Finite-Variable Logics

Definition: Fix an integer k ≥ 2. H(T (k)) is the class of all structures that are homomorphically equivalent to a structure in T (k).

Remark: T (k) is properly contained in H(T (k)):

There are 2-colorable graphs of arbitrarily large treewidth (for instance, k-grids)

Theorem : Let k ≥ 2 be an integer. • If B is a fixed structure, then CSP(H(T (k)), {B}) is definable in k-Datalog.

• CSP(H(T (k)), All) is definable in LFP2k, Least Fixed-Point Logic with 2k variables.

Algorithm: Determine whether the Spoiler or the Duplicator wins the (∃, k)-pebble game. 149 New Islands of Tractability

Theorem: Fix an integer k ≥ 2. • CSP(H(T (k)), All) is in P. Good News!

• Determining membership in H(T (k)) is an NP- complete problem. Bad News!

Conclusion: • We have discovered new islands of tractability that are larger than T (k). • Unfortunately, these islands are “inaccessible”.

150 Bounded Treewidth & Finite-Variable Logics

Question: Is there a more precise relationship between T (k) and Lk?

Theorem: Fix a k and a finite structure A. Then the following are equivalent: • A ∈ T (k)

• QA can be transformed to an Lk-sentence by repeatedly applying the following rewrite rules to subformulas of QA: – Associativity and Commutativity of ∧ – (∃x)(ϕ ∧ ψ) 7→ (∃x)ϕ ∧ ψ, provided x is not free in ψ. – (∃x)ϕ 7→ (∃y)ϕ[x/y], provided y is not free in θ.

Note: The above rewrite rules are routinely used in database query processing and optimization. 151 Summary

• The tractability of CSP(T (k), All) can be ex- plained in terms of definability in Datalog.

• CSP(H(T (k)), All) is a new and large island of tractability, but this expansion of the tractabil- ity landscape comes at a price.

• The combinatorial concept of bounded treewidth has a logical reconstruction via definability in finite-variable logics.

152 Concluding Remarks

Constraint Satisfaction is a meeting point of

• Computational Complexity

• Database Theory

• Logic

• Universal Algebra.

153 Solutions to Homework Assignment # 2

Exercise: Express the Clique problem as a Homomorphism Problem.

Given a graph H = (V,E) and a positive integer k ≥ 2, does H contain a k-clique?

Solution:

Fact: H contains a k-clique

⇐⇒ there is a homomorphism h : Kk → H.

154 Solutions to Homework Assignment # 2

Exercise: Express the Hamiltonian Cycle prob- lem as a Homomorphism Problem.

Given a graph H = (V,E), is there a cycle in which each node of H occurs exactly once?

Solution:

Given a graph H = (V,E) with |V | = n, let

Cn = ({1, . . . , n},En) be a cycle of length n.

Fact:

H contains a Hamiltonian cycle

⇐⇒ there is a homomorphism h :({1, . . . , n},En, 6=) → (V,E, 6=).

155 Solutions to Homework Assignment #5

Exercise: Use Schaefer’s Dichotomy Theorem to show that Monotone 3-SAT is NP-complete.

Solution: Monotone 3-Sat is CSP({0, 1},R0,R3), where

3 3 R0 = {0, 1} −{(0, 0, 0}) and R1 = {0, 1} −{(1, 1, 1}).

• R0 is not 0-valid, R1 is not 1-valid.

• R0 is neither bijunctive nor Horn, since

maj((1, 0, 0), (0, 1, 0), (0, 0, 1)) = (0, 0, 0) 6∈ R0.

(1, 0, 0) ∧ (0, 1, 0) = (0, 0, 0) 6∈ R0.

• R3 is neither dual Horn nor affince, since

(1, 1, 0) ∨ (1, 0, 1) = (1, 1, 1) 6∈ R3

(1, 0, 0) ⊕ (0, 1, 0) ⊕ (0, 0, 1) = (1, 1, 1) 6∈ R3.

156 A Very Partial Bibliography

References

[Bod93] H.L. Bodlaender. A linear-time algorithm for finding tree- decompositions of small treewidth. In Proc. 25th ACM Symp. on Theory of Computing, pages 226–234, 1993.

[CM77] A.K. Chandra and P.M. Merlin. Optimal implementation of conjunctive queries in relational databases. In Proc. 9th ACM Symp. on Theory of Computing, pages 77–90, 1977.

[DKV02] V. Dalmau, Ph.G. Kolaitis, and M.Y. Vardi. Constraint satis- faction, bounded treewidth, and finite-variable logics. In Proc. of 8th International Conference on Principles and Practice of Constraint Programming, Lecture Notes in Computer Science, 2002 (to ap- pear).

[Dec92] R. Dechter. From local to global consistency. Artificial Intelli- gence, 55(1):87–107, May 1992.

[Dec99] R. Dechter. Bucket elimination: a unifying framework for rea- soning. Artificial Intelligence, 113(1–2):41–85, 1999.

[DF99] R.G. Downey and M.R. Fellows. Parametrized Complexity. Springer-Verlag, 1999.

[DP89] R. Dechter and J. Pearl. Tree clustering for constraint net- works. Artificial Intelligence, pages 353–366, 1989.

[Fre90] E.C Freuder. Complexity of k-tree structured constraint sat- isfaction problems. In Proc. AAAI-90, pages 4–9, 1990.

[FV98] T. Feder and M.Y. Vardi. The computational structure of monotone monadic SNP and constraint satisfaction: a study through Datalog and group theory. SIAM J. on Computing, 28:57–104, 1998. Preliminary version in Proc. 25th ACM Symp. on Theory of Computing, May 1993, pp. 612–622.

[GJ79] M. R. Garey and D. S. Johnson. Computers and Intractability - A Guide to the Theory of NP-Completeness. W. H. Freeman and Co., 1979. 157 [HN90] P. Hell and J. Neˇsetˇril.On the complexity of H-coloring. Jour- nal of Combinatorial Theory, Series B, 48:92–110, 1990.

[HN92] P. Hell and J. Neˇsetˇril.The core of a graph. Discrete Mathemat- ics, 109:117–126, 1992.

[HNZ96] P. Hell, J. Nesetril, and X. Zhu. Complexity of Tree Homo- morphism. Discrete Appl. Math., 70:23–36, 1996.

[Hoo97] J.N. Hooker. Constraint satisfaction methods for generating valid cuts. In Advances in computational and stochastic optimization, logic programming, and heuristic studies, pages 1–30. Kluwer, 1997.

[Imm99] N. Immerman. Descriptive Complexity. Springer, 1999.

[Jea98] P. Jeavons. On the algebraic structure of combinatorial prob- lems. Theoretical Computer Science, 200(1–2):185–204, 1998.

[KV95] Ph. G. Kolaitis and M. Y. Vardi. On the expressive power of Datalog: tools and a case study. Journal of Computer and System Sciences, 51(1):110–134, August 1995.

[KV00a] Ph.G. Kolaitis and M.Y. Vardi. Conjunctive-query contain- ment and constraint satisfaction. Journal of Computer and System Sciences, pages 302–332, 2000. Earlier version in: Proc. 17th ACM Symp. on Principles of Database Systems (PODS ’98).

[KV00b] Ph.G. Kolaitis and M.Y. Vardi. A game-theoretic approach to constraint satisfaction. In Proc. of the 17th National Conference on Artificial Intelligence (AAAI 2000), pages 175–181, 2000.

[PJ97] J. Pearson and P. Jeavons. A survey of tractable constraint satisfaction problems. Technical Report CSD-TR-97-15, Royal Holloway University of London, 1997.

[Sch78] T.J. Schaefer. The complexity of satisfiability problems. In Proc. 10th ACM Symp. on Theory of Computing, pages 216–226, 1978.

[Ull89] J. D. Ullman. Database and Knowledge-Base Systems, Volumes I and II. Computer Science Press, 1989.

[Var82] M. Y. Vardi. The complexity of relational query languages. In Proc. 14th ACM Symp. on Theory of Computing, pages 137–146, 1982.

158