Skip to main content

NP-Completeness

Introduction

So far, we have designed efficient algorithms for a wide range of problems. But not all problems are so accomodating. In this section, we examine a class of problems for which no polynomial time algorithm is known (and for which there is strong theoretical reason to believe none exists).

Definitions

We study NP-completeness in the setting of decision problems, or problems with a yes/no answer. This is not much of a restriction, since most optimization problems that we have previously studied have a natural decision version (e.g. "for a graph G=(V,E)G=(V,E), does there exist a traveling salesman tour of cost at most kk?").

We say a (decision) problem AA is in NP if a proposed solution can be verified in polynomial time. More precisely, there exists a polynomial-time algorithm that, given an instance xx of AA and a certificate cc, correctly determines whether cc proves that xx is a yes-instance of problem AA. For example, a certificate (proposed solution) for a SAT instance is a truth assignment to its variables; we can check in polynomial time whether this assignment satisfies the SAT formula.

A polynomial reduction reduction from problem AA to problem BB, written as APBA \le_P B, is a polynomial-time computable function ff such that for any instance xx of AA, xx is a yes-instance of AA if and only if f(x)f(x) is a yes-instance of BB. Intuitively, this means AA is "no harder" than BB; given an efficient algorithm for BB, we obtain an efficient algorithm for AA by transforming instances via ff and running BB's algorithm on the result. Note the direction; APBA \le_P B means BB is at least as hard as AA, a fact we will use when proving NP-hardness.

We say a problem BB is NP-hard if APBA \le_P B for every problem ANPA \in \mathrm{NP}. Intuitively, BB is at least as hard as every problem in NP; solving BB efficiently would let us solve all of NPNP efficiently.

We say AA is NP-Complete if and only if the following conditions are true:

  • ANPA \in \mathrm{NP}: there exists a verifier that can check proposed solutions to an instance xx of AA in polynomial time.
  • AA is NP-hard. To show this, we produce a polynomial-time reduction BPAB \le_P A from some problem BB already known to be NP-hard. By transitivity of P\le_P, since every problem in NP reduces to B (as B is NP-hard) and BPAB \le_P A, every problem in NP also reduces to AA; so AA is NP-hard.

General Framework

NP-completeness proofs are an important part of CS4820, and they are sure to show up on exams. If you want to show that some decision problem AA is NP-complete, the proof generally has 4 steps, as follows:

  1. ANPA \in \mathrm{NP}. Show that there is a polynomial-time verifier for AA. It should take in an instance of the problem Foo and a certificate (possible solution) to that instance, answering 'yes' if certificate is a correct solution and 'no' otherwise. This is usually straightforward and should almost always be done first. Don't forget to argue why the verifier runs in polynomial time!
  2. Reduce from some NP-complete problem XX to AA (i.e. showing XPAX \le_P A). You should pick a known NP-complete problem XX. Your goal is to solve XX using an algorithm that solves AA. You should do this by "reducing" or translating an arbitrary instance of XX into an equivalent instance of AA. This way, if you have an algorithm that solves AA, you can solve XX by reducing the problem to AA and running the algorithm for AA.
  3. Show that the reduction takes polynomial time. Make sure you aren't creating exponentially many gadgets/nodes!
  4. Show that your reduction is equivalent. In other words, xx is a yes-instance of XX iff f(x)f(x) is a yes-instance of AA (where ff is our reduction function). This has two parts.
  • ()(\Rightarrow) If xx is a yes-instance of XX, then f(x)f(x) is a yes-instance of AA. Typically, you show this by taking an arbitrary solution to xx and explicitly constructing a corresponding solution to f(x)f(x).
  • ()(\Leftarrow) If f(x)f(x) is a yes-instance of AA, then xx is a yes-instance of XX. You show this by taking a solution to f(x)f(x) and explicitly constructing a corresponding solution to xx. This is often the harder direction, since you need to consider all possible solutions to f(x)f(x) (not just the ones you had in mind while making the reduction construction).

On the Direction of the Reduction

The most common way to lose points on an NP-completeness proof is to reduce the wrong way. If the reduction direction is flipped, then the entire content of the argument will be incorrect.

Recall what NP-hardness of AA requires: a reduction XPAX \le_P A from a known NP-hard problem XX into your target AA. The operational meaning is that a fast algorithm for AA would yield a fast algorithm for XX (compute reduction f(x)f(x), then call a solver for AA). Since XX is already known to be hard, this transfers that hardness onto AA.

Reducing the other way, APXA \le_P X, proves the opposite kind of statement. It says a fast algorithm for XX would solve AA; that is, AA is no harder than XX. This is an upper bound on AA's difficulty, not a lower bound. It places no floor on how hard AA is, so it establishes nothing about NP-hardness.

Example Problem: Independent Set

Independent Set: Given an undirected graph G=(V,E)G = (V, E) and integer kk, we say that a set of nodes SVS \subseteq V is independent if no two nodes in SS are joined by an edge. Does there exist an independent set of size at least kk?

Show that Independent Set is NP-Complete.

We first show that Independent Set is in NP. Given a graph instance G=(V,E)G = (V, E) and certificate (proposed solution) SS, we check that the following conditions are true. If any of them do not hold, then we output "no". Otherwise, we output "yes".

  • Check that SS is at least size kk (verify Sk|S| \ge k).
  • For each pair of nodes u,vSu, v \in S with uvu \ne v, ensure (u,v)E(u, v) \notin E. In other words, no node pair in SS should form an edge in GG.

We observe that this verifier runs in polynomial time. Since SVS \subseteq V, checking the first condition takes O(V)O(|V|) time. For the second condition, we consider all pairs of nodes in SS and check membership in EE; there can be at most O(V2)O(|V|^2) pairs and a naive membership check in EE takes O(E)O(|E|) time. Thus, our verifier runs in O(V2E)O(|V|^2 \cdot |E|) time, which is indeed polynomial.

We show that Independent Set is NP-Hard by showing a reduction from 3SAT.

Recall that 3SAT gives a 3CNF (AND of ORs) of clauses with exactly three variables, where the 3CNF is satisfiable if there exists a boolean assignment of variables that satisfy all clauses. For example, the following 3CNF is satisfiable by setting x1x_1 and x3x_3 to be true and x2x_{2} to be false (there are other satisfying sets of variable assignments).

(x1¬x3x2)(¬x1¬x3¬x2)(¬x1x3x2) \begin{align*} (x_1 \lor \lnot x_3 \lor x_2) \land (\lnot x_1 \lor \lnot x_3 \lor \lnot x_2) \land (\lnot x_1 \lor x_3 \lor x_2) \end{align*}

Given a 3CNF ϕ\phi of mm clauses and nn variables, we create a graph GG that we can run an algorithm for Independent Set on. For clause ii, we create 3 nodes xij1x_{ij_1}, xij2x_{i j_2}, and xij3x_{i j_3}, representing the three literals in clause ii. We connect these three nodes with edges forming a triangle. Additionally, for every pair of nodes representing a variable and its negation (i.e. xijx_{ij} and ¬xij\lnot x_{ij}) across all clauses, we add an edge between them. We set k=mk = m.

We argue that this reduction takes polynomial time. For each of the mm clauses, we create 3 nodes and 3 edges (to connect them all together) in our graph. In addition, we connect all pair of nodes representing variables and their negation. There are at most (3m)2=O(m2)(3m)^2 = O(m^2) pairs possible, so we can reduce our 3SAT instance to an Independent Set instance in O(m2+m)=O(m2)O(m^2 + m) = O(m^2) time, which is polynomial.

The intuition is that each clause is represented by a gadget of "nodes" in our graph GG. Since k=mk = m in our Independent Set instance, we must pick exactly one node from each clause gadget to be in the independent set SS (the triangle edges prevent picking two nodes from the same gadget). Picking a node xijx_{ij} corresponds to setting that literal to true. The consistency edges between contradictory literal pairs ensure we never pick both a variable and its negation (which would violate the independent set property), so the assignment is well-defined and satisfies every clause.

The visualization below walks through this construction on a small 3CNF. Step through it to see the clause gadgets, triangle edges, and consistency edges appear.

3SAT → Independent Set

One gadget per clause

Each clause becomes a gadget of three nodes, one per literal. We have three clauses, so nine nodes in total. Nothing is connected yet.

Target size k3
(x1 ∨ x2 ∨ x3) ∧ (¬x1 ∨ x4 ∨ x5) ∧ (x2 ∨ ¬x4 ∨ x6)
x1x2x3¬x1x4x5x2¬x4x6C1C2C3

()(\Rightarrow) Suppose the 3CNF ϕ\phi is satisfiable. We show that a satisfying assignment of variables to a 3SAT instance implies an independent set solution to our constructed graph. Fix a satisfying assignment. For each clause ii, at least one of its three literals is true under this assignment; pick one such true literal and include its corresponding node xijx_{ij} in SS. This gives S=mk|S| = m \ge k.

We check that SS is independent. No two nodes lie in the same clause gadget, since we picked exactly one per gadget (no triangle edges are present within SS). Also, no two chosen nodes correspond to a variable and its negation: since both nodes would have to present true literals under the same assignment, a variable and its negation cannot both be true. Hence, no consistency edge connects two nodes of SS. So SS is an independent set of size m=km = k.

()(\Leftarrow) Suppose GG has an independent set SS with S=k=m|S| = k = m (we know it cannot go any higher because exactly one node can be picked per clause gadget, and there are mm of them).

Define a variable assignment by setting each literal corresponding to a node in SS to true (if a variable has neither literal represented in SS, then assign it arbitrarily). This assignment is well defined; if both xijx_{ij} and ¬xij\lnot x_{i'j} were in SS, it would not be an independent set because they are connected by an edge. So no variable is assigned conflicting values. Since SS contains one node per clause gadget, every cluase has at least one literal set to true, so every clause is satisfied. Hence ϕ\phi is satisfiable.

Therefore, our 3CNF ϕ\phi is satisfiable if and only if GG has an independent set of size k=mk = m, completing the reduction. Combined with the polynomial-time verifier shown earlier, Independent Set is NP-complete.

Known NP-Complete Problems

The known NP-complete problems fall into several natural categories. On assignments or exams, you may freely cite NP-complete problems that have appeared in class, previous assignments, or in the textbook.

Packing Problems

If your problem has a parameter kk for which as kk gets larger the problem gets harder, try reducing from:

  • Independent Set: Given an undirected graph G=(V,E)G = (V, E) and integer kk, we say that a set of nodes SVS \subseteq V is independent if no two nodes in SS are joined by an edge. Does there exist an independent set of size at least kk?
  • Set Packing: Given a universe UU, subsets S1,,SmUS_1, \dots, S_m \subseteq U, and an integer kk, does there exists a subcollection of {S1,,Sm}\{S_1, \dots, S_m\} of at least kk pairwise-disjoint subsets?

Covering Problems

If your problem has a parameter kk for which as kk gets larger the problem gets easier, try reducing from:

  • Vertex Cover: Given an undirected graph G=(V,E)G = (V, E) and an integer kk, we say that a set of nodes SES \subseteq E if every edge eEe \in E has at least one endpoint in SS. Does there exist a vertex cover of size at most kk?
  • Set Cover: Given a universe UU, subsets S1,,SmUS_1, \dots, S_m \subseteq U, and an integer kk, does there exist a subcollection of {S1,,Sm}\{S_1, \dots, S_m\} of at most kk sets whose union is all of UU?

Partitioning Problems

If your problem involves dividing elements into disjoint groups, try reducing from:

  • 3D Matching: Given disjoint sets X,Y,ZX, Y, Z each of size nn, a set of triples TX×Y×ZT \subseteq X \times Y \times Z, does there exist a set of nn triples in TT such that every element of XYZX \cup Y \cup Z is contained in exactly one of these triples?
  • 3-Coloring: Given an undirected graph G=(V,E)G = (V, E), does GG have a 3-coloring?

Sequencing Problems

If your problem involves giving an ordering over nn items, try reducing from:

  • Hamiltonian Cycle: Given a graph GG (either directed or undirected), is there a Hamiltonian cycle (a path starts and ends with the same node and visits every other node exactly once)?
  • Hamiltonian Path: Given a graph GG (either directed or undirected), start node ss and end node tt, is there a Hamiltonian path starting at ss and ending at tt?

Other

If your problem involves summing over numeral values to reach a target, try reducing from:

  • Subset Sum: Given nonnegative integers w1,,wnw_1, \dots, w_n and a target integer WW, does there exist I{1,,n}I \subseteq \{1, \dots, n\} such that iIwi=W\sum_{i \in I} w_i = W?

If all else fails, try 3-SAT!

  • 3SAT: Given a CNF with clauses of 3 literals each, does there exist a satisfying assignment?

Practice

Problem 1. [Graph Selection] Suppose we have a graph G=(V,E)G = (V, E). You are given two disjoint sets EgoodE_{good} and EbadE_{bad}, and a vertex set CVC \subseteq V. The Graph-Selection problem is to determine whether there exists a vertex set PP such that:

  • CPC \subseteq P
  • For all vPv \in P, there is a uPu \in P such that (u,v)Egood(u, v) \in E_{good}
  • For all (u,v)Ebad(u, v) \in E_{bad}, either uPu \notin P or vPv \notin P

Prove that Graph-Selection is NP-Complete. (Source: CS 4820 FA25 Prelim 2 Review)

Hint 1

Could we try a reduction from 3-SAT?

Hint 2

With our reduction from 3-SAT, create two nodes for setting a variable xix_i either true or false.

Hint 3

With the previous setup, what should our bad edge set EbadE_{bad} be?

Solution

We first show that Graph Selection is in NP. Given a Graph Selection instance consisting of graph G=(V,E)G = (V, E), edge sets EgoodE_{good} and EbadE_{bad}, and set CVC \subseteq V, we take in a certificate set PP and check the following conditions. If any of them are false, we reject PP (outputting "no"). Otherwise, if all conditions hold, output "yes".

  • CPC \in P. This takes O(V)O(|V|) time.
  • For all vPv \in P, there is a uPu \in P such that (u,v)Pgood(u, v) \in P_{good}. We can check this by fixing each vPv \in P and checking through all uPu \in P with uvu \ne v to see if (u,v)Egood(u, v) \in E_{good}; if no uPu \in P satisfies this, then we can reject PP. We enumerate of all possible pairs in PVP \subseteq V, so we take O(V2)O(|V|^2) time for this check.
  • For all (u,v)Ebad(u, v) \in E_{bad}, either uPu \notin P or vPv \notin P. We check over all edges (u,v)Ebad(u, v) \in E_{bad}. If there exists e=(u,v)e = (u', v') such that uPu' \in P and vPv' \in P, then we reject PP. Since EbadEE_{bad} \subseteq E, this step takes O(E)O(|E|) time.

Our verifier has an overall runtime of O(V2+E)O(|V|^2 + |E|), which is indeed polynomial.

We show that Graph Selection is NP-Hard by showing a reduction from 3SAT.

Given a 3CNF ϕ\phi of mm clauses and nn variables, we create a graph GG that we can run an algorithm for Graph Selection on. We create the following nodes

  • For each variable xix_i, we create two nodes tit_i and fif_i.
  • For each clause CjC_j, create a clause node cjc_j.
  • Let C={cj:1jm}C = \{c_j : 1 \le j \le m \}.

Next, we create the following edges.

  • For every variable xix_i, create an edge (ti,fi)(t_i, f_i). Let EbadE_{bad} be the set of all these edges.
  • For every clause CjC_j with corresponding node vjv_j, we create three edges and place them all in EgoodE_{good}. Let xa,xb,xcx_a, x_b, x_c be the 3 variables in the clause. For each of the three variables: if some literal xix_i is in the clause, we put in edge (cj,ti)(c_j, t_i); otherwise, if some literal ¬xi\lnot x_i is in the clause, we put in edge (cj,fi)(c_j, f_i).

We run Graph-Selection on this input and output whatever it outputs. This reduction runs in polynomial time. We create 2n+m2n + m nodes and n+3mn + 3m edges, which is polynomial in the size of the input 3CNF.

We now show that the original 3SAT instance ϕ\phi has a solution iff this instance of Graph-Selection has a solution.

()(\Rightarrow) Satisfiable ϕ\phi gives a valid PP

Fix a satisfying assignment and let

P0  =  C    {ti:xi TRUE}    {fi:xi FALSE}.P_0 \;=\; C \;\cup\; \{\, t_i : x_i \text{ TRUE} \,\} \;\cup\; \{\, f_i : x_i \text{ FALSE} \,\}.

Condition 1. CP0C \subseteq P_0 by construction.

Condition 2. Let vP0v \in P_0.

  • If v=cjv = c_j: the assignment satisfies CjC_j, so some literal \ell of CjC_j is true. Let ww be its node (tit_i if =xi\ell = x_i, else fif_i). Then (cj,w)Egood(c_j, w) \in E_{good} by construction, and wP0w \in P_0 since \ell is true.
  • If vv is a literal node: after preprocessing its literal occurs in some clause CjC_j, so (cj,v)Egood(c_j, v) \in E_{good} and cjCP0c_j \in C \subseteq P_0.

Condition 3. Ebad={(ti,fi)}i=1nE_{bad} = \{(t_i,f_i)\}_{i=1}^{n}, and the assignment gives each xix_i exactly one value, so at most one of ti,fit_i, f_i lies in P0P_0. No bad edge has both endpoints in P0P_0.

Hence P0P_0 is a valid selection.

()(\Leftarrow) A valid PP gives a satisfying assignment

Given a valid PP, define

xi=TRUE    tiP.x_i = \text{TRUE} \iff t_i \in P.

This is well defined, and it assigns every variable. Observe that fiPf_i \in P forces tiPt_i \notin P, since (ti,fi)Ebad(t_i, f_i) \in E_{bad} and PP satisfies condition 3. So

fiP    xi=FALSE    ¬xi is true.f_i \in P \;\Longrightarrow\; x_i = \text{FALSE} \;\Longrightarrow\; \lnot x_i \text{ is true.}

Fix a clause CjC_j. Since cjCPc_j \in C \subseteq P, condition 2 gives some uPu \in P with (u,cj)Egood(u, c_j) \in E_{good}. Every good edge at cjc_j leads to a node of one of CjC_j's three literals, so uu is such a node.

  • u=tiu = t_i: then xi=TRUEx_i = \text{TRUE}, and the edge exists only because the literal xix_i occurs in CjC_j. Clause satisfied.
  • u=fiu = f_i: then xi=FALSEx_i = \text{FALSE} by the observation, and ¬xi\lnot x_i occurs in CjC_j. Clause satisfied.

Every clause is satisfied, so ϕ\phi is satisfiable.

Problem 2. [Monotone SAT] Consider the following variant of SAT. We say that a boolean formula is monotone if no variable appears negated; i.e. every literal is a positive occurence of some variable xix_i. For example, the following SAT formula is monotone:

(x1x2x4)(x2x23)(x7x10x100)\begin{align*} (x_1 \lor x_2 \lor x_4) \land (x_2 \lor x_{23}) \land (x_7 \lor x_{10} \lor x_{100}) \end{align*}

We take as input a montone CNF formula ϕ=C1C2Cn\phi = C_1 \land C_2 \land \dots \land C_n over variables x1,,xnx_1, \dots, x_n, where each CiC_i consists of ORs of non-negated literals. We also take an input number kk. The Monotone SAT decision problem asks whether ϕ\phi is satisfiable by setting at most kk variables equal to true.

Prove that Monotone SAT is NP-Complete.

Hint

Use a reduction from Vertex Cover.

Solution

We first show that Monotone SAT is in NP. Given a Monotone SAT instance with boolean formula ϕ\phi (made up of clauses of only positive literals) and a parameter kk, we take a variable assignment as our certificate. We check the following conditions: the amount of variables set to true in the assignment is less than or equal to kk, and every clause CiC_i is satisfied. If any of these conditions are false, we reject the certificate. Otherwise, we accept.

We claim that this verifier runs in polynomial time. To check the first condition, we can perform a linear scan through all the variable assignments and count how many variables are assigned to true. For the second condition, checking each clause requires scanning its literals. For a formula with nn variables and mm clauses, this runs in O(mn)O(mn) time, which is polynomial. Thus, Monotone SAT is in NP.

Next, we show that Monotone SAT is NP-Hard by showing a reduction from Vertex Cover.

Recall the Vertex Cover problem: given a graph G=(V,E)G = (V, E) and an integer kk, decide whether there is a set SVS \subseteq V with Sk|S| \le k such that every edge has at least one endpoint in SS.

For our reduction, consider an instance of Vertex Cover with graph G=(V,E)G = (V, E) and parameter kk with V=(v1,,vn)V = (v_1, \dots, v_n). We create a variable xix_i for each vertex viv_i. For each edge e=(vi,vj)Ee = (v_i, v_j) \in E, create the clause (xixj)(x_i \lor x_j). Then, let

ϕ=(vi,vj)E(xixj)\begin{align*} \phi = \bigwedge_{(v_i, v_j) \in E} (x_i \lor x_j) \end{align*}

and keep kk the same. Every literal is a positive occurence of a variable, so ϕ\phi is monotone. The construction produces one variable per vertex and one clause per edge, so it runs in polynomial time.

For correctness, we show that (G,k)(G, k) is a yes-instance of Vertex Cover iff (ϕ,k)(\phi, k) is a yes-instance of Monotone SAT.

()(\Rightarrow) Suppose SS is a vertex cover with Sk|S| \le k. Set xi=x_i = true exactly when viSv_i \in S. This sets at most kk variables true. For any clause (xixj)(x_i \lor x_j) corresponding to edge (vi,vj)(v_i, v_j), since SS covers this edge, at least one of vi,vjSv_i, v_j \in S, so at least one of xi,xjx_i, x_j is true and the clause is satisfied. Thus, ϕ\phi is satisfied with at most kk true variables.

()(\Leftarrow) Suppose ϕ\phi is satisfied by an assignment with at most kk true variables. Let S={vi:xi=true}.soS = \{v_i: x_i = \text{true}\}. so |S| \le k.Foranyedge. For any edge (v_i, v_j),theclause, the clause (x_i \lor x_j)issatisfied,soatleastoneofis satisfied, so at least one ofx_i, x_jistrue,meaningatleastoneofis true, meaning at least one ofv_i, v_j \in S.Hence,. Hence, Scoverseveryedgeisavertexcoverofsizeatmostcovers every edge is a vertex cover of size at mostk$.

Since the reduction runs in polynomial time and is correct, Vertex Cover P\le_P Monotone SAT, so Monotone SAT is NP-Hard.

Monotone SAT is in NP and is NP-Hard, so it is NP-Complete.

Problem 3. [3-Coloring] Given a graph G=(V,E)G = (V, E), the 3-Coloring problem asks if there is an assignment of colors to the nodes of the graph such that no two adjacent vertices have the same color, and at most 3 colors are used.

Prove that 3-Coloring is NP-Complete.

Solution

We first show that 3-Coloring is in NP. Given a 3-Coloring instance of a graph G=(V,E)G = (V, E), we take as input to our verifier a coloring assignment c:V{1,2,3}c: V \rightarrow \{1, 2, 3\}. The verifier iterates over every edge (u,v)E(u, v) \in E and checks that c(u)c(v)c(u) \ne c(v). If every edge has properly colored endpoints, accept; otherwise, reject. This runs in O(E)O(|E|) time, so our verifier is indeed polynomial.

We now show that 3-Coloring is NP-Hard by providing a reduction from 3SAT. Let ϕ\phi be a 3CNF formula.

In progress!

Problem 4. [Partition] The input to the Partition problem is a set of nn large integers w1,,wnw_1, \dots, w_n. The problem is to decide if there is a subset I{1,,n}I \subset \{1, \dots, n\} such that iIwi=iIwi\sum_{i \in I} w_i = \sum_{i \notin I} w_i.

Show that Partition is NP-Complete.

Hint

Reduce from Subset Sum.

Solution

We first show that Partition is in NP. Given a Partition instance of nn large numbers w1,,wnw_1, \dots, w_n, our certificate is a set II. We can simply check if iIwi=iIwi\sum_{i \in I} w_i = \sum_{i \notin I} w_i holds (if yes, then accept; otherwise, reject this certificate). If we use w=maxiwiw = \max_i w_i, then doing O(n)O(n) addition operations and checking if the sums are equivalent takes O(nlogw)O(n \log w) time. This is polynomial in the input size.

We show that Partition is NP-Hard by showing a reduction from Subset Sum.

Let numbers a1,,ana_1, \dots, a_n with target sum AA be a Subset Sum instance. First, we add up all numbers and let A=i=1naiA\overline{A} = \sum_{i = 1}^n a_i - A. We assume without loss of generality that AAA \ge \overline{A}. We create an input to Partition that uses n+1n + 1 numbers: a1,,ana_1, \dots, a_n from Subset Sum and one additional number an+1=AAa_{n + 1} = A - \overline{A}.

Letting a=maxiaia = \max_i a_i, this reduction takes O(nloga)O(n \log a), as we need to do O(n)O(n) addition/subtraction operations to create our new element an+1a_{n + 1} (since these are large numbers, we take O(loga)O(\log a) time for each addition operation). Therefore, our reduction runs in polynomial time.

The intuition for this reduction is as follows: if there exists such a subset II such that iIai=A\sum_{i \in I} a_i = A, then the remaining elements sum to A=i=1naiA\overline{A} = \sum_{i = 1}^n a_i - A. So a yes-instance of Subset Sum splits the numbers into two groups with sums AA and A\overline{A}. This is almost a partition; both sides aren't always balanced, they differ by AAA - \overline{A}. Adding in an extra number an+1=AAa_{n + 1} = A - \overline{A} closes this gap on the A\overline{A} side and makes the two sums equal to AA. In other words, an+1=AAa_{n + 1} = A - \overline A is a "balancing weight" that turns an uneven Subset Sum split into an even partitioning.

()(\Rightarrow) Suppose there is a subset II of the subset sum problem with total sum AA. Then this subset satisfies the parition rule for the input we created. Since we have i=1n+1ai=2A\sum_{i = 1}^{n + 1} a_i = 2A, the remaining items not selected in II will sum to AA. Thus, we have a solution to Partition.

()(\Leftarrow) Suppose our constructed Partition instance has a partition of the n+1n + 1 numbers with equal sums. Note that i=1n+1ai=2A\sum_{i = 1}^{n + 1}a_i = 2A implies that both sides sum to AA. We simply chose subset II to be the side of the partition not containing the newly created item an+1a_{n + 1}, so II is a solution to the Subset Sum problem.

Since we have shown Partition to be in NP and NP-Hard, this problem is NP-Complete as required. \square