# Elementary Reductions --- CS 137 // 2021-12-06 ## Administrivia - Exam 2 is returned on Gradescope - Assignment 2 will be returned tomorrow - Assignment 3 due on Wednesday # Questions ## ...about anything? # Exam 2 Debrief # $P$ and $NP$ ## The Class $P$ - $P$ is the set of computational problems that are **solvable** in polynomial time - An algorithm runs in polynomial time if, in the worst case, it takes $O(n^k)$-time for some $k \ge 0$ - Intuitively, problems in $P$ are considered "easy" for a computer to solve ## The Class $NP$ - $NP$ stands for **nondeterministic polynomial time** - Intuitively, $NP$ is the set of computational problems that are **verifiable** in polynomial time ## Finding Versus Checking - **Finding** the two prime factors of a number is hard + $14369648346682547857$ - **Checking** if two numbers are the prime factors of another number is easy: + $1500450271$ and $9576890767$ ## The Satisfiability Problem - The **Boolean satisfiability problem**, $\text{SAT}$, involves checking if a Boolean formula $\Phi$ is satisfiable - $\text{SAT}$ is $NP$-complete, which means: + $\text{SAT} \in NP$ + $\text{SAT} \in P$ if and only if $P = NP$ - This means that if someone finds a fast solution to $\text{SAT}$, then we automatically get a fast solution to factor numbers! # $NP$-Completeness ## $NP$-Completeness - A problem is **$NP$-hard** if every problem in $NP$ is polynomially **reducible** to it + If a polynomial-time algorithm is found for an $NP$-hard problem, then it means that $P = NP$ - A problem is **$NP$-complete** if it is $NP$-hard and also a member of $NP$ + $NP$-complete problems are the "hardest" in the class $NP$ ## Reductions - A problem $A$ is reducible to a problem $B$ if: + An instance of $A$ can be converted to an equivalent instance of $B$ + Thus, if $B$ is solvable, then so is $A$ ## Example: Convex Hull - A common problem in computer vision is finding the **convex hull** of a set of points ![convex hull](../../assets/images/convex-hull-simple.png) - Suppose I wanted to convince you that convex hull is at least as hard as **sorting numbers** - One way to do this is by writing an algorithm that sorts numbers **using convex hull** ## Sorting to Convex Hull - To do this, we need to transform every numeric sorting problem into a convex hull problem - Suppose we're given a list of numbers: + $L = (x_1, x_2, \ldots, x_n)$ - We can convert this to a convex hull problem by creating a point $(x,x^2)$ for each $x\in L$ - Then finding the convex hull necessarily gives us the elements in order ## Sorting to Convex Hull - Why does this work? - Each point is mapped onto the parabola $y = x^2$ which is convex ![convex hull parabola](../../assets/images/convex-hull-parabola.png) ## Sorting to Convex Hull - Thus, an algorithm to solve sorting could be: ```text Given a list of numbers L: 1. Create a list of points (x, x^2) for each x in L 2. Use a convex hull solver to get the path of the hull 3. Extract out the first component of each point to get a sorted list of L 4. Return the sorted list ``` - If someone finds a fast solution to the convex hull problem, this immediately gives us a fast solution to sorting - Thus we've shown that the convex hull problem is **at least as hard as** sorting ## Reductions - **Fact:** If $A$ is an $NP$-hard problem and $A$ is polynomially reducible to $B$, then $B$ is also $NP$-hard - To prove a problem is $NP$-hard, we just need to reduce a well-known $NP$-hard problem to it ![NP reduction diagram](../../assets/images/np-reduction-diagram.png) # Example: Independent Set ## Independent Set to Clique - An **independent set** is a fully disconnected set of vertices of an undirected graph - A **clique** is a fully connected set of vertices of an undirected graph - These problems are "inverses" of each other, so one can easily be reduced to the other ## Independent Set to Clique ![IS to Clique](https://www.researchgate.net/profile/Mikhail-Batsyn-2/publication/235890405/figure/fig4/AS:667066830974979@1536052534398/Maximum-clique-and-maximum-independent-set-problems.png) ## Independent Set to Clique - An algorithm to solve independent set could be: ```text Given an undirected graph G = (V,E): 1. Create a graph G' = (V', E') with V = V' 2. We include an edge (u,v) in E' if and only if the edge (u,v) is not in E 3. Find the largest clique in G' 4. Then that clique must be an independent set in the original graph G, so return it ``` - Converting G to G' takes polynomial time, so it is a polynomial reduction - If independent set is NP-hard, then so is clique # Example: Vertex Cover ## Example: Vertex Cover - A **vertex cover** of an undirected graph is a set of vertices that "touches" every edge in the graph ![Vertex Cover](https://i.stack.imgur.com/7n7bv.jpg) ## Vertex Cover to IS - Notice that every vertex cover (red) induces an independent set (blue) ![VC to IS](../../assets/images/vc-to-is.png) - Thus, finding a small vertex cover is equivalent to finding a large independent set ## Vertex Cover to IS - An algorithm to solve vertex cover could be: ```text Given an undirected graph G = (V,E): 1. Find the largest independent set, I, of of G 2. Let V' be the set of vertices in V but not in I 3. Since all the vertices in I are independent, V' must be a vertex cover 4. Return V' ``` - Now we know that if vertex cover is is NP-hard (and it is), then so is independent set - From our previous reduction, this also shows that clique is NP-hard