# CMPSCI 311: Theory of Algorithms

### Polynomial-Time Reductions

Questions are in black, answers are in blue.

The most important definition in the theory of NP-completeness is the polynomial-time reduction. Let A and B be two sets of inputs, which we can think of as two problems with boolean answers. A reduction from A to B is a function f such that (1) f is easily computable (computable in polynomial time), and (2) for any input x, x is in A if and only if f(x) is in B.

A reduction from A to B gives us an easy way to answer ``is x in A'' if we know an easy way to answer questions about B -- we compute f(x) and decide whether f(x) is in B.

In this exercise we will construct some reductions. In each case B will be the REACHABILITY problem, the set {(G,s,t): G is a directed graph and there is a path form vertex s to vertex t in G}.

• Question 1: Reduce COMPLETE to REACHABILITY, where COMPLETE is the set {G: G is an undirected graph that has all possible edges in it}. Your reduction does not have to solve COMPLETE, though this is easy -- it should create a graph H and vertices s and t in H such that H has a path from s to t if and only if G is complete.

(Note: "(n choose 2)" below refers to the number of edges in a complete n-node graph, which is n(n-1)/2.)

My solution is to make H a graph of (n choose 2) + 1 vertices: s = v0, v1, ..., v(n choose 2 - 1, t = v(n choose 2). Using two loops, consider each of the (n choose 2) possible edges of G. If the i'th edge is there, make an edge from vi-1 to vi in H. If all the possible edges are present in G, H will be a single long chain and will have a path from s to t. If any edge is missing, the chain is broken and there is no path from s to t.

Many people in the discussion had the following correct solution, which has the reduction do more work but produces a simpler graph H. This time H has n+1 vertices and up to n edges. There is an edge from vi-1 to vi if and only if vertex i of G has n-1 edges adjacent to it. If G is complete, then H has all n possible edges and has a path from its first to last node. If G is not complete, at least one (actually at least two) nodes of G fail to have degree n-1, the chain in H is not complete, and H has no path from s to t.

This second solution is in a sense halfway between mine and the "trivial" reduction, which has H consist of only two nodes s and t, and has the reduction decide on its own whether G is in COMPLETE and puts an edge from s to t if and only if it is. By a similar reduction, any problem in P has a poly-time reduction to REACHABILITY. But we are looking for more interesting, direct reductions here.

• Question 2: Reduce the problem MAJORITY to REACHABILITY, where MAJORITY is the set {w: w is a string of 0's and 1's and at least half the letters of w are 1's}.

The simplest solution is probably the following: let H have ceiling(n/2) + 1 nodes, called v0 through vceiling(n/2), where s is v0 and t is vceiling(n/2). The reduction reads the string left to right, keeping track of the number of ones seen. When it sees the i'th one it makes an edge from vi-1 to vi. Clearly it completes the path from s to t if and only if the string is in MAJORITY.

Here is the reduction I had in mind, which again has the reduction do less thinking at the cost of making a larger and perhaps more complicted graph H. The nodes of H will be a grid, with ceiling(n/2) + 1 rows and n+1 columns, so each node can be called vi,j where i is in the range from 0 through ceiling(n/2) and j is in the range from 0 through n. There is always an edge from vi,j-1 to vi,j. If the i'th character of the input string is a one, we also have edges from vi-1,j-1 to vi-1,j for every j. The vertex s is v0,0 and t is vceiling(n/2),n.

To reach t, we have to go down ceiling(n/2) times, and each time we go down must be associated with a different one in the string. So we can reach t if and only if the input string is in MAJORITY.

• Question 3: Reduce STRONGLY-CONNECTED to REACHABILITY, whree STRONGLY-CONNECTED is the set {G: G is a directed graph such that for every pair of vertices u and v in G, there is an path from u to v in G}.

As in Question 1, I will have vertices s = v0 through t = v(n-1)2, corresponding to the (n-1)2 paths that must exist, according to the definition, for G to be strongly connected. (One path for each pair of vertices u and v with u ≠ v.)

I want H to have a path from vi-1 to vi if and only if a particular path exists in G, from some vertex si to some vertex ti.

One correct but less satisfying solution is to have the reduction do a DFS or BFS of G, starting from si, and insert an edge from vi-1 to vi if and only if ti comes up in the search.

My solution again makes the reduction simpler and the graph H larger and more complicated. What I do is insert an entire copy of G between vi-1 and vi. I put an edge from vi-1 to the copy of si, and from the copy of ti to vi. There is clearly a path through this copy from vi-1 to vi if and only if the appropriate path exists in G. So we can get from s to t in H if and only if all the appropriate paths exist in G.

One can simplify this graph a bit by noting that a directed graph is in STRONGLY-CONNECTED if and only if there is a path from vertex 1 to vertex 2, from 2 to 3, from 3 to 4, ..., from n-1 to n, and finally from n to 1. Thus we need check the existence of only n paths rather than the (n-1)2 paths checked above, leading to a graph with Θ(n2) rather than Θ(n3) total nodes.