Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper.

Slides:



Advertisements
Similar presentations
Model Checking Lecture 3. Specification Automata Syntax, given a set A of atomic observations: Sfinite set of states S 0 Sset of initial states S S transition.
Advertisements

NP and NP Complete. Definitions A problem is in the class P if there is a polynomial time solution to the problem A problem is in the class NP if there.
The Theory of NP-Completeness
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Complexity 12-1 Complexity Andrei Bulatov Non-Deterministic Space.
Computability and Complexity 15-1 Computability and Complexity Andrei Bulatov NP-Completeness.
Graphs 4/16/2017 8:41 PM NP-Completeness.
Testing for Connectedness and Cycles
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
NP-Complete Problems Reading Material: Chapter 10 Sections 1, 2, 3, and 4 only.
The Theory of NP-Completeness
NP-Complete Problems Problems in Computer Science are classified into
Analysis of Algorithms CS 477/677
Knoweldge Representation & Reasoning
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
NP-Completeness (2) NP-Completeness Graphs 4/17/2017 6:25 AM x x x x x
Improving Code Generation Honors Compilers April 16 th 2002.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
The Theory of NP-Completeness 1. What is NP-completeness? Consider the circuit satisfiability problem Difficult to answer the decision problem in polynomial.
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
1 The Theory of NP-Completeness 2012/11/6 P: the class of problems which can be solved by a deterministic polynomial algorithm. NP : the class of decision.
February 18, 2015CS21 Lecture 181 CS21 Decidability and Tractability Lecture 18 February 18, 2015.
Lecture 22 More NPC problems
Theory of Computation, Feodor F. Dragan, Kent State University 1 NP-Completeness P: is the set of decision problems (or languages) that are solvable in.
MCS 312: NP Completeness and Approximation algorthms Instructor Neelima Gupta
Week 10Complexity of Algorithms1 Hard Computational Problems Some computational problems are hard Despite a numerous attempts we do not know any efficient.
EMIS 8373: Integer Programming NP-Complete Problems updated 21 April 2009.
CSC401 – Analysis of Algorithms Chapter 13 NP-Completeness Objectives: Introduce the definitions of P and NP problems Introduce the definitions of NP-hard.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
NP-Complete Problems. Running Time v.s. Input Size Concern with problems whose complexity may be described by exponential functions. Tractable problems.
NP-COMPLETE PROBLEMS. Admin  Two more assignments…  No office hours on tomorrow.
Advanced Algorithm Design and Analysis (Lecture 14) SW5 fall 2004 Simonas Šaltenis E1-215b
NP-Complete problems.
CS 3343: Analysis of Algorithms Lecture 25: P and NP Some slides courtesy of Carola Wenk.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Graph Theory By: Maciej Kicinski Chiu Ming Luk. Extract Triple words.
CS 461 – Nov. 30 Section 7.5 How to show a problem is NP-complete –Show it’s in NP. –Show that it corresponds to another problem already known to be NP-complete.
“One ring to rule them all” Analogy (sort of) Lord of The Rings Computational Complexity “One problem to solve them all” “my preciousss…”
28.
Chapter 11 Introduction to Computational Complexity Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
NPC.
CSC 413/513: Intro to Algorithms
COMPLEXITY. Satisfiability(SAT) problem Conjunctive normal form(CNF): Let S be a Boolean expression in CNF. That is, S is the product(and) of several.
SAT Solving As implemented in - DPLL solvers: GRASP, Chaff and
Complexity ©D.Moshkovits 1 2-Satisfiability NOTE: These slides were created by Muli Safra, from OPICS/sat/)
CSCI 2670 Introduction to Theory of Computing December 2, 2004.
CSCI 2670 Introduction to Theory of Computing December 7, 2005.
CSE 421 Algorithms Richard Anderson Lecture 27 NP-Completeness Proofs.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);
COSC 3101A - Design and Analysis of Algorithms 14 NP-Completeness.
COMPLEXITY. Satisfiability(SAT) problem Conjunctive normal form(CNF): Let S be a Boolean expression in CNF. That is, S is the product(and) of several.
Computability Examples. Reducibility. NP completeness. Homework: Find other examples of NP complete problems.
The NP class. NP-completeness Lecture2. The NP-class The NP class is a class that contains all the problems that can be decided by a Non-Deterministic.
Honors Track: Competitive Programming & Problem Solving Fun with Graphs I Kevin Verbeek.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Breadth-First Search (BFS)
NP-Completeness (2) NP-Completeness Graphs 4/13/2018 5:22 AM x x x x x
Richard Anderson Lecture 26 NP-Completeness
NP-Completeness (2) NP-Completeness Graphs 7/23/ :02 PM x x x x
NP-Completeness (2) NP-Completeness Graphs 7/23/ :02 PM x x x x
NP-Completeness Proofs
Richard Anderson Lecture 26 NP-Completeness
(xy)(yz)(xz)(zy)
NP-Completeness (2) NP-Completeness Graphs 11/23/2018 2:12 PM x x x x
NP-Completeness Proofs
Search Related Algorithms
NP-Completeness (2) NP-Completeness Graphs 7/9/2019 6:12 AM x x x x x
Presentation transcript:

Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper

2-Satisfiability Contents What is the problem? How to solve it? Implementation Another solution: Limited backtracking Example problems: (line) labeling Similar problems to 2SAT

What is the problem? 2-Satisfiability Mostly expressed in Conjunctive Normal Form (CNF) Conjunction of disjunctions: Terms: variables, possibly negated. Clauses: disjunction of terms, for example: CNF-expression: clause and clause and clause and… The problem Does assignment to values exist such that the expression holds?

Other representations Implicative normal form Rewriting the clauses of a 2-CNF formula to implications: Implication graph A directed graph A vertex for each term An arrow for each implication of the implicative normal form

Example From 2-CNF to Implication graph 2-CNF: (a ∨ b) ∧ (¬b ∨ c) INF: ¬a¬a b ¬ba c ¬c (¬a => b)∧ (¬b => a)∧ (b => c)∧ (¬c => ¬b)

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Construct implication graph Convert to implicative normal form Create vertex for each term Edges are logical implications

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Find strongly connected components Strongly connected component (SCC): Part of the graph in which there is a directed path from every vertex, to all other vertices of that part of the graph. See Fun with Graphs I

Tarjan’s algorithm Perform a DFS Assign number to each node based on DFS order For each node v keep track of lowest index node reachable from v Keep stack of vertices not in SCC yet If v.index = v.low then all nodes on stack until v are SCC Running time O(V + E) A B C D E F A B C D EF Source: Fun with Graphs I, Kevin Verbeek

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Checking for contradictions Variables and their negations can’t be in one SCC Because transitivity would cause: x 0 => ¬x 0 Meaning the expression is not satisfiable Works both ways A 2-CNF formula is satisfiable if and only if there is no variable that belongs to the same strongly connected component as its negation.

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Construct condensation One vertex for each strongly connected component Edges between components as they are in implicative graph Results in directed acyclic graph A B C D EF A B C D E F

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Topological ordering A linear ordering of vertices such that every edge moves forwards Example: If there’s an edge going from u to v, then u appears before v in the topological ordering. Already done Kosaraju’s already sorts components topologically Tarjan’s algorithm in a reverse topological order

How to solve this problem? Solution Linear time Procedure Construct implication graph Find strongly connected components Check for contradictions within those Construct condensation of the graph Topologically order it Set the variables!

How to solve this problem? Setting the variables All terms in a SCC have the same value Implication only false if: True  False Start at the back of topological order Make last component true Make complementary component false Repeat until done Complementary components Skew-symmetry If edge from a to b ➨ edge from ¬b to ¬a

Implementation! Node-class implication graph class Node { ArrayList adj; String name; boolean sign; boolean value = true; Node counter; //complementary term int index; // replaces visited, initially -1 int low; int comp; // initially -1, can be used to check if on stack } Running time O(n+m) Finding SCCs (Tarjan) int index = 0, C = 0; ArrayDeque stack = new ArrayDeque ();

Implementation: Tarjan void tarjan(int i) { // run for every i with V[i].index == -1 V[i].index = index++; V[i].low = V[i].index; stack.push(i); for (Edge e: V[i].adj) { if (V[e.target].index == -1) { tarjan(e.target); V[i].low = Math.min(V[i].low, V[e.target].low); } else if (V[e.target].comp == -1) { // e.target must be on stack V[i].low = Math.min(V[i].low, V[e.target].index); } if (V[i].index == V[i].low) { // SCC is found int j; do { j = stack.pop(); V[j].comp = C; } while(j != i); C++; } Source: Fun with Graphs I, Kevin Verbeek

Implementation: contradictions&condensation Contradiction checking For each vertex v: If (v.comp==v.counter.comp) { break; //Expression is not satisfiable } Running time Constant time for each vertex O(v) Condensation graph Not needed when using Tarjan’s Already in reverse topological order

Implementation: setting variables Setting the variables For each component c Reverse topological order For (Node n: c.list) { if (n.value != false) { n.counter.value = false; } } Running time Constant time for each vertex O(v) V = 2*n  O(n)

Another solution: backtracking Backtracking Uses 2-CNF expression Give a variable a value (starting choice point) Add values to variables this choice determined If contradiction ➨ go back to last choice point If no contradiction ➨ you reach a new choice point Repeat until  All variables have a value, OR  Both choices at a choice point yield a contradiction

Backtracking: example Example of backtracking (¬c ∨ a) ∧ (a ∨ b) ∧ (¬b ∨ c) Limited backtracking Check in parallel Abort when choice point in parallel path is found Linear time, because path length = 2*n This expression is satisfiable with:  A = true  B = true  C = true ¬a b c a a c ¬b ¬c b

Example: Labeling Labeling Problem: Objects need labels, can be placed right or left. Labels may not overlap Say: true=left and false=right INF: (¬a => ¬c) 2-CNF: (a ∨ ¬c) a b c labelR labelL labelR ∧ (c => a)

Example: Line labeling Line labeling Problem: Label can be placed above, on top or below a line. Labels may not overlap (b ∨ ¬a) ∧ (d ∨ ¬c) ∧ (a ∨ ¬c) ∧ (b ∨ ¬d) a ¬a b ¬b c d ¬c ¬d

Similar problems MAX-2-SAT  Given: 2-CNF-expression  Problem: Maximum number of clauses simultaneously true  Complexity: NP-hard W2SAT  Given: 2-SAT instance and integer k  Problem: Satisfying assignment with at most k true variables  Complexity: NP-complete

Questions? Are there any questions?