CS 321. Algorithm Analysis & Design
Lecture 9
Recursion
Satisfiability
The Birthday Problem
The Birthday Problem
CENTRAL EUROPEAN OLYMPIAD IN INFORMATICS
name is a request. This request is satisfied if
and only if John invites name.
—name is a request. This request is satisfied if
and only if John doesn’t invite name.
If R1, . . . , Rk are requests, then
(R1 & ... & Rk) is a request.
This request is satisfied if and only if
all requests R1, . . . , Rk are satisfied.
If R1, . . . , Rk are requests, then
(R1 | ... | Rk) is a request.
This request is satisfied if and only if
at least one of the requests R1, . . . , Rk is satisfied.
If R1, R2 are requests, then (R1 R2) is a request.
This request is not satisfied if and only if
R1 is satisfied and R2 is not satisfied.
Google Code Jam: Round 1A, 2008
n flavours of milkshake, each of two kinds: malted and unmalted.
Google Code Jam: Round 1A, 2008
n flavours of milkshake, each of two kinds: malted and unmalted.
Every customer likes a certain set of milkshakes.
Google Code Jam: Round 1A, 2008
n flavours of milkshake, each of two kinds: malted and unmalted.
Every customer likes a certain set of milkshakes.
Goal: Make n milkshakes so that every customer can get at least
one of the flavours that they like.
Google Code Jam: Round 1A, 2008
Satisfiability
Input: A set of boolean variables X, and a formula F over X using
conjunctions (AND), disjunctions (OR) and negations (NOT).
Satisfiability
Input: A set of boolean variables X, and a formula F over X using
conjunctions (AND), disjunctions (OR) and negations (NOT).
Task: Determine if there is an assignment (t: X ⟼{0,1})
such that F evaluates to one.
Satisfiability
Input: A set of boolean variables X, and a formula F over X using
conjunctions (AND), disjunctions (OR) and negations (NOT).
Task: Determine if there is an assignment (t: X ⟼{0,1})
such that F evaluates to one.
Literal: A variable or it’s negation.
Satisfiability
Input: A set of boolean variables X, and a formula F over X using
conjunctions (AND), disjunctions (OR) and negations (NOT).
Task: Determine if there is an assignment (t: X ⟼{0,1})
such that F evaluates to one.
Literal: A variable or it’s negation.
CNF: Conjuctions of disjunctions of literals.
(x + y + z) . (w + x + (1-z)) . (x + (1-w))
Satisfiability
Input: A set of boolean variables X, and a formula F over X using
conjunctions (AND), disjunctions (OR) and negations (NOT).
Task: Determine if there is an assignment (t: X ⟼{0,1})
such that F evaluates to one.
Literal: A variable or it’s negation.
CNF: Conjuctions of disjunctions of literals.
(x + y + z) . (w + x + (1-z)) . (x + (1-w))
DNF: Disjunctions of conjunctions of literals.
(xz(1-z)) + (wx(1-z)) + (xwyz) + (y(1-y))
Satisfiability
It is widely believed that there is no polynomial time algorithm for
determining if a 3-CNF formula over n variables is satisfiable.
It is widely believed that there is no polynomial time algorithm for
determining if a 3-CNF formula over n variables is satisfiable.
That doesn’t stop us from thinking about algorithms anyway!
3CNF - WHAT’S THE BEST WE CAN DO?
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
O(2nn3)
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
O(2nn3)
Recursion?
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
O(2nn3)
Recursion?
Pick a clause: (x + y + (1-z))
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
O(2nn3)
Recursion?
Pick a clause: (x + y + (1-z))
SAT(F(x=1)), SAT(F(y=1)), SAT(F(z=0))
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
O(2nn3)
Recursion?
Pick a clause: (x + y + (1-z))
SAT(F(x=1)), SAT(F(y=1)), SAT(F(z=0))
T(n) < 3T(n-1) + poly(n)
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
O(2nn3)
Recursion?
Pick a clause: (x + y + (1-z))
SAT(F(x=1)), SAT(F(y=1)), SAT(F(z=0))
T(n) < 3T(n-1) + poly(n)
O(3nn3) - this is even worse!
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
Recursion?
O(2nn3)
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
Recursion?
Pick a clause: (x + y + (1-z))
O(2nn3)
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
Recursion?
Pick a clause: (x + y + (1-z))
SAT(F(x=1)), SAT(F(x = 0, y=1)), SAT(F(x = 0, y = 0, z=0))
O(2nn3)
3CNF - WHAT’S THE BEST WE CAN DO?
Trivial Algorithm: Try all possible assignments.
Recursion?
Pick a clause: (x + y + (1-z))
SAT(F(x=1)), SAT(F(x = 0, y=1)), SAT(F(x = 0, y = 0, z=0))
T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n)
O(2nn3)
If T(n) was bounded by O(cn),
for what c will an inductive proof work?
Let’s reverse-engineer it!
T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n)
If T(n) was bounded by O(cn),
for what c will an inductive proof work?
Let’s reverse-engineer it!
T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n)
c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n)
If T(n) was bounded by O(cn),
for what c will an inductive proof work?
Let’s reverse-engineer it!
T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n)
c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n)
c3 < c2 + c1 + 1 + poly(n)
If T(n) was bounded by O(cn),
for what c will an inductive proof work?
Let’s reverse-engineer it!
T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n)
c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n)
c3 < c2 + c1 + 1 + poly(n)
Solving for c gives us c = 1.8-ish.
If T(n) was bounded by O(cn),
for what c will an inductive proof work?
Let’s reverse-engineer it!
T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n)
c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n)
c3 < c2 + c1 + 1 + poly(n)
Solving for c gives us c = 1.8-ish.
More details for the proof of correctness of the algorithm
are in the next set of slides.
If T(n) was bounded by O(cn),
for what c will an inductive proof work?
Let’s reverse-engineer it!
Note that the algorithm we discussed
is meant for 3-CNF formulas.
Can be easily generalised to d-CNF formulas.
For a general CNF formula
(where the size of the clauses are not bounded)
the number of recursive sub-instances is not bounded!

09 - 27 Jan - Recursion Part 1

  • 1.
    CS 321. AlgorithmAnalysis & Design Lecture 9 Recursion Satisfiability
  • 4.
  • 5.
    The Birthday Problem CENTRALEUROPEAN OLYMPIAD IN INFORMATICS
  • 6.
    name is arequest. This request is satisfied if and only if John invites name. —name is a request. This request is satisfied if and only if John doesn’t invite name.
  • 7.
    If R1, .. . , Rk are requests, then (R1 & ... & Rk) is a request. This request is satisfied if and only if all requests R1, . . . , Rk are satisfied.
  • 8.
    If R1, .. . , Rk are requests, then (R1 | ... | Rk) is a request. This request is satisfied if and only if at least one of the requests R1, . . . , Rk is satisfied.
  • 9.
    If R1, R2are requests, then (R1 R2) is a request. This request is not satisfied if and only if R1 is satisfied and R2 is not satisfied.
  • 10.
    Google Code Jam:Round 1A, 2008
  • 11.
    n flavours ofmilkshake, each of two kinds: malted and unmalted. Google Code Jam: Round 1A, 2008
  • 12.
    n flavours ofmilkshake, each of two kinds: malted and unmalted. Every customer likes a certain set of milkshakes. Google Code Jam: Round 1A, 2008
  • 13.
    n flavours ofmilkshake, each of two kinds: malted and unmalted. Every customer likes a certain set of milkshakes. Goal: Make n milkshakes so that every customer can get at least one of the flavours that they like. Google Code Jam: Round 1A, 2008
  • 14.
  • 15.
    Input: A setof boolean variables X, and a formula F over X using conjunctions (AND), disjunctions (OR) and negations (NOT). Satisfiability
  • 16.
    Input: A setof boolean variables X, and a formula F over X using conjunctions (AND), disjunctions (OR) and negations (NOT). Task: Determine if there is an assignment (t: X ⟼{0,1}) such that F evaluates to one. Satisfiability
  • 17.
    Input: A setof boolean variables X, and a formula F over X using conjunctions (AND), disjunctions (OR) and negations (NOT). Task: Determine if there is an assignment (t: X ⟼{0,1}) such that F evaluates to one. Literal: A variable or it’s negation. Satisfiability
  • 18.
    Input: A setof boolean variables X, and a formula F over X using conjunctions (AND), disjunctions (OR) and negations (NOT). Task: Determine if there is an assignment (t: X ⟼{0,1}) such that F evaluates to one. Literal: A variable or it’s negation. CNF: Conjuctions of disjunctions of literals. (x + y + z) . (w + x + (1-z)) . (x + (1-w)) Satisfiability
  • 19.
    Input: A setof boolean variables X, and a formula F over X using conjunctions (AND), disjunctions (OR) and negations (NOT). Task: Determine if there is an assignment (t: X ⟼{0,1}) such that F evaluates to one. Literal: A variable or it’s negation. CNF: Conjuctions of disjunctions of literals. (x + y + z) . (w + x + (1-z)) . (x + (1-w)) DNF: Disjunctions of conjunctions of literals. (xz(1-z)) + (wx(1-z)) + (xwyz) + (y(1-y)) Satisfiability
  • 20.
    It is widelybelieved that there is no polynomial time algorithm for determining if a 3-CNF formula over n variables is satisfiable.
  • 21.
    It is widelybelieved that there is no polynomial time algorithm for determining if a 3-CNF formula over n variables is satisfiable. That doesn’t stop us from thinking about algorithms anyway!
  • 22.
    3CNF - WHAT’STHE BEST WE CAN DO?
  • 23.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments.
  • 24.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. O(2nn3)
  • 25.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. O(2nn3) Recursion?
  • 26.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. O(2nn3) Recursion? Pick a clause: (x + y + (1-z))
  • 27.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. O(2nn3) Recursion? Pick a clause: (x + y + (1-z)) SAT(F(x=1)), SAT(F(y=1)), SAT(F(z=0))
  • 28.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. O(2nn3) Recursion? Pick a clause: (x + y + (1-z)) SAT(F(x=1)), SAT(F(y=1)), SAT(F(z=0)) T(n) < 3T(n-1) + poly(n)
  • 29.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. O(2nn3) Recursion? Pick a clause: (x + y + (1-z)) SAT(F(x=1)), SAT(F(y=1)), SAT(F(z=0)) T(n) < 3T(n-1) + poly(n) O(3nn3) - this is even worse!
  • 30.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. Recursion? O(2nn3)
  • 31.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. Recursion? Pick a clause: (x + y + (1-z)) O(2nn3)
  • 32.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. Recursion? Pick a clause: (x + y + (1-z)) SAT(F(x=1)), SAT(F(x = 0, y=1)), SAT(F(x = 0, y = 0, z=0)) O(2nn3)
  • 33.
    3CNF - WHAT’STHE BEST WE CAN DO? Trivial Algorithm: Try all possible assignments. Recursion? Pick a clause: (x + y + (1-z)) SAT(F(x=1)), SAT(F(x = 0, y=1)), SAT(F(x = 0, y = 0, z=0)) T(n) < T(n-1) + T(n-2) + T(n-3) + poly(n) O(2nn3)
  • 35.
    If T(n) wasbounded by O(cn), for what c will an inductive proof work? Let’s reverse-engineer it!
  • 36.
    T(n) < T(n-1)+ T(n-2) + T(n-3) + poly(n) If T(n) was bounded by O(cn), for what c will an inductive proof work? Let’s reverse-engineer it!
  • 37.
    T(n) < T(n-1)+ T(n-2) + T(n-3) + poly(n) c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n) If T(n) was bounded by O(cn), for what c will an inductive proof work? Let’s reverse-engineer it!
  • 38.
    T(n) < T(n-1)+ T(n-2) + T(n-3) + poly(n) c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n) c3 < c2 + c1 + 1 + poly(n) If T(n) was bounded by O(cn), for what c will an inductive proof work? Let’s reverse-engineer it!
  • 39.
    T(n) < T(n-1)+ T(n-2) + T(n-3) + poly(n) c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n) c3 < c2 + c1 + 1 + poly(n) Solving for c gives us c = 1.8-ish. If T(n) was bounded by O(cn), for what c will an inductive proof work? Let’s reverse-engineer it!
  • 40.
    T(n) < T(n-1)+ T(n-2) + T(n-3) + poly(n) c(n) < c(n-1) + c(n-2) + c(n-3) + poly(n) c3 < c2 + c1 + 1 + poly(n) Solving for c gives us c = 1.8-ish. More details for the proof of correctness of the algorithm are in the next set of slides. If T(n) was bounded by O(cn), for what c will an inductive proof work? Let’s reverse-engineer it!
  • 41.
    Note that thealgorithm we discussed is meant for 3-CNF formulas. Can be easily generalised to d-CNF formulas. For a general CNF formula (where the size of the clauses are not bounded) the number of recursive sub-instances is not bounded!