In Chapter 6, we delved into the realm of complexity theory, classifying problems based on the computational resources required to solve them. We identified NP-hard problems as those that are at least as difficult as any problem in NP, and NP-complete problems as the hardest problems in NP. Assuming the widely believed conjecture that , these problems cannot be solved by any polynomial-time algorithm.
This presents a significant challenge: many NP-hard problems, such as the Traveling Salesperson Problem (TSP), the Knapsack Problem, and various scheduling and routing problems, are of immense practical importance. If we cannot solve them efficiently in the worst case, what can we do in practice? This is where the trade-off between finding an absolutely optimal solution and finding a sufficiently good solution efficiently becomes critical.
This chapter explores various algorithmic strategies designed to tackle NP-hard problems. The core idea is to transition from computationally infeasible to manageable complexity by weakening the requirements for the solution. This might involve:
- Restricting the Problem Instances: Focusing on subclasses of inputs that are efficiently solvable, or on “typical” instances that are easier than the worst case.
- Accepting Superpolynomial Algorithms: Designing algorithms that are exponential, but with a base small enough to be practical for realistic input sizes (e.g., instead of ).
- Weakening Solution Quality: Instead of an optimal solution, accepting a “good enough” solution (approximation algorithms).
- Accepting Probabilistic Guarantees: Using randomization to achieve efficiency, with a small, controlled probability of error (randomized algorithms, covered in Chapter 8).
We will explore these concepts, demonstrating how clever algorithmic design can yield practical solutions even for theoretically “hard” problems.
7.1 Aims
By the end of this chapter, you will be able to:
- Understand Pseudopolynomial Algorithms: Recognize a class of algorithms that are efficient for number problems with small numerical values, and understand the concept of strongly NP-hard problems.
- Design Approximation Algorithms: Develop and analyze algorithms that find near-optimal solutions for optimization problems with provable quality guarantees, such as for Vertex Cover and Metric TSP.
- Apply Local Search Techniques: Understand the principles of local search, how neighborhoods are defined, and how it can be used to find local optima for problems like MAX-CUT.
- Grasp Simulated Annealing: Learn about a metaheuristic that extends local search to escape local optima and find better solutions, inspired by a physical process.
7.2 Pseudopolynomial Algorithms
Some NP-hard problems, particularly those involving numbers, can be solved by algorithms whose running time is polynomial in the numerical value of the input numbers, but exponential with respect to the length of their binary representation. Such algorithms are called pseudopolynomial.
We consider number problems, where inputs can be interpreted as sequences of numbers. For an input , where are binary representations of numbers, let .
Definition 7.1 (Pseudopolynomial Algorithm)
Let be a number problem and an algorithm that solves . is a pseudopolynomial algorithm for if there exists a bivariate polynomial such that for all problem instances of .
If is bounded by a polynomial in (e.g., if the numbers in the input are small), then a pseudopolynomial algorithm runs in polynomial time with respect to the input length. This means that for “light” problem instances (where numbers are small), these algorithms are efficient.
The Knapsack Problem
The Knapsack Problem is a classic NP-hard optimization problem:
- Input: objects, each with a weight and a benefit , and a knapsack capacity . All are positive integers.
- Goal: Select a subset of objects such that their total weight does not exceed , and their total benefit is maximized.
We can design a pseudopolynomial algorithm for the Knapsack Problem using dynamic programming. The algorithm DPR (Dynamic Programming for Knapsack) computes, for each object and each possible total benefit , the minimum weight required to achieve benefit using a subset of the first objects.
Algorithm DPR (Dynamic Programming for Knapsack)
Input: An instance of the Knapsack Problem. Output: A subset of indices maximizing total benefit within weight .
- Initialize
TRIPLE(0) = {(0, 0, $\emptyset$)}. (Represents: (Current Benefit, Current Weight, Items Included))- For to : (Consider adding the -th object) a.
SET(i) = TRIPLE(i-1)(Solutions without object ) b. For each : If : (If object can be added without exceeding capacity) Add toSET(i). (Solutions including object ) c.TRIPLE(i)is formed fromSET(i)by keeping, for each unique benefit , only the triple with the minimum weight . (If multiple ways to achieve benefit , keep the most efficient one).- Find the triple with the maximum benefit .
- Output: .
Theorem 7.2
For every instance of the Knapsack Problem, , making
DPRa pseudopolynomial algorithm.Proof
The maximum possible total benefit is . In the worst case, each could be up to , so the maximum total benefit is at most .
In each step (from to ), the set
TRIPLE(i)stores information for each reachable benefit. The number of distinct benefit values is at most . Therefore, is at most .Each step involves iterating through
TRIPLE(i-1)and updatingSET(i), which takes time.Since there are such steps (for to ), the total time complexity for Phase 2 is .
Phase 1 and 3 take negligible time. Since (the length of the input encoding all objects and ), the overall time complexity is .
This algorithm is pseudopolynomial because its runtime depends polynomially on the numerical value of the input numbers (), not just their length.
Strongly NP-Hard Problems
Some NP-hard problems are so difficult that they don’t even admit pseudopolynomial algorithms (unless P=NP). These are called strongly NP-hard.
Definition 7.3 (Strongly NP-Hard)
A number problem is strongly NP-hard if there exists a polynomial such that the problem restricted to instances where is NP-hard.
Theorem 7.3
If is a strongly NP-hard number problem, then (assuming ) there exists no pseudopolynomial algorithm for .
Proof
Assume for contradiction that is strongly NP-hard and has a pseudopolynomial algorithm . By definition of strongly NP-hard, there exists a polynomial such that the problem restricted to instances where is NP-hard. Let this restricted problem be .
Since is a pseudopolynomial algorithm for , its time complexity is for some bivariate polynomial . For instances of , we have . Substituting this into the runtime of : . Since and are polynomials, is also a polynomial in . This means that solves in polynomial time.
However, is NP-hard. If can be solved in polynomial time, then by definition of NP-hardness, every problem in NP can be solved in polynomial time, implying P = NP. This contradicts our assumption that . Therefore, no pseudopolynomial algorithm for can exist if is strongly NP-hard and .
This concept helps classify NP-hard problems further. For example, the Traveling Salesperson Problem (TSP) is strongly NP-hard, implying that even if all edge weights are small, it remains NP-hard and thus does not admit a pseudopolynomial algorithm (unless P=NP).
7.3 Approximation Algorithms
For many NP-hard optimization problems, finding an optimal solution is intractable. However, a solution that is “good enough” might be found efficiently. Approximation algorithms aim to find such near-optimal solutions with provable guarantees on their quality.
Definition 7.4 (Approximation Quality)
Let be an optimization problem and an algorithm for . For an instance , let be the cost of the solution found by , and be the cost of an optimal solution. The approximation quality of on is: For a positive number , is a -approximation algorithm for if for all instances . The definition ensures that , with values closer to 1 indicating better approximation.
Minimal Vertex Cover (MIN-VC)
The Vertex Cover problem is to find a minimum set of vertices in a graph such that every edge is incident to at least one vertex in the set.
Algorithm VCA (Vertex Cover Approximation)
Input: Graph Output: A vertex cover for .
- Initialize .
- Initialize (set of uncovered edges).
- While : a. Pick an arbitrary edge from . b. Add and to . c. Remove all edges incident to or from .
- Output: .
Theorem 7.4
The algorithm
VCAis a polynomial-time 2-approximation algorithm for MIN-VC.Proof
- Polynomial Time: The algorithm iterates as long as there are uncovered edges. In each iteration, one edge is chosen, and its two endpoints are added to the cover. All edges incident to these two vertices are removed. Since at least one edge is removed in each iteration, the loop runs at most times. Operations within the loop (picking an edge, adding to , removing edges) can be implemented efficiently (e.g., using adjacency lists). Thus, , which is polynomial.
- Vertex Cover: When the algorithm terminates, is empty, meaning all edges in the original graph are covered by at least one vertex in . So is indeed a vertex cover.
- Approximation Quality: Let be the set of edges chosen in step 3a throughout the algorithm’s execution. By construction, all edges in are disjoint (they form a matching). For each edge , both and are added to . Therefore, . Now, consider any optimal vertex cover . To cover all edges in , must include at least one endpoint from each edge in . Since the edges in are disjoint, must contain at least vertices. So, . Combining these, the approximation ratio is: Thus,
VCAis a 2-approximation algorithm.
Traveling Salesperson Problem (TSP)
For the general TSP, it can be proven that (assuming ) no polynomial -approximation algorithm exists for any constant . This means TSP is “too hard” for approximation in the general case.
However, for the Metric TSP (-TSP), where edge costs satisfy the triangle inequality ( for all vertices ), approximation algorithms exist.
Algorithm SB (Spanning Tree-Based for -TSP)
Input: Complete graph with cost function satisfying triangle inequality. Output: A Hamiltonian cycle .
- Compute a Minimum Spanning Tree (MST) of with respect to the edge costs .
- Perform a depth-first search (DFS) traversal of , starting from an arbitrary node . Let be the sequence of nodes visited in the order they are first encountered. This traversal effectively creates an Eulerian tour of where each edge is traversed twice.
- Construct a Hamiltonian cycle by traversing the nodes in and adding edges to connect the last node back to the first. When a node is encountered that has already been visited in the current path, it is skipped (a “shortcut” is taken).
- Output: .
Theorem 7.5
The algorithm
SBis a polynomial-time 2-approximation algorithm for -TSP.Proof
- Polynomial Time: An MST can be computed in polynomial time (e.g., Kruskal’s or Prim’s algorithm). A DFS traversal also takes polynomial time. Constructing the Hamiltonian cycle from the DFS traversal is also polynomial. Thus,
SBis a polynomial-time algorithm.- Approximation Quality: Let be an optimal Hamiltonian cycle for . Its cost is .
- If we remove any edge from , we get a spanning tree. Therefore, the cost of any spanning tree is less than or equal to the cost of . Since is a Minimum Spanning Tree, .
- The DFS traversal of (before shortcuts) effectively traverses every edge of twice (once in each direction). The total cost of this traversal, let’s call it , is .
- From (1) and (2), .
- The Hamiltonian cycle is constructed from by taking “shortcuts.” For example, if visits nodes in sequence, and is already visited, we might skip and go directly from to . Due to the triangle inequality, . This means that taking shortcuts never increases the total cost. Therefore, .
- Combining these inequalities: . Thus,
SBis a 2-approximation algorithm for -TSP.
7.4 Local Search
Local search is a general algorithmic paradigm for optimization problems. It starts with an initial feasible solution and iteratively tries to improve it by making small, local changes. The process continues until no further improvement can be made within the defined “neighborhood” of the current solution.
Definition 7.5 (Neighborhood)
For an optimization problem and an instance , a neighborhood defines, for each feasible solution , a set of “neighboring” solutions . A solution is a neighbor of . A neighborhood function typically satisfies:
- Reflexivity: (a solution is its own neighbor, or a variant where it’s excluded if an improvement is found).
- Symmetry: If , then .
- Reachability: For any two solutions , there exists a path of neighbors connecting them.
An admissible solution is a local optimum with respect to if no neighbor in is better than .
Algorithm LS (Local Search)
Input: Instance of optimization problem , neighborhood function . Output: A local optimum .
- Compute an initial admissible solution .
- While is not a local optimum with respect to : a. Find a such that is better than (e.g., for minimization, or for maximization). b. Set .
- Output: .
Example: MAX-CUT
The MAX-CUT problem is to partition the vertices of a graph into two sets such that the number of edges between the sets is maximized.
A simple neighborhood for MAX-CUT: for a given partition , a neighbor is any partition obtained by moving a single vertex from one set to the other.
Algorithm LS-CUT (Local Search for MAX-CUT)
Input: Graph . Output: A cut .
- Initialize . (Initial cut is ).
- While there exists a vertex such that moving to the other side of the cut improves the cut value: a. Move to the side that improves the cut.
- Output: The final cut .
Theorem 7.6
LS-CUTis a polynomial-time 2-approximation algorithm for MAX-CUT.Proof
- Polynomial Time: In each iteration, the algorithm checks all vertices to see if moving one improves the cut. Checking a single vertex takes time. So, finding an improving move takes time. Each time a vertex is moved, the cut value increases. The maximum possible cut value is . Therefore, the loop can run at most times. The total time complexity is , which is polynomial.
- Approximation Quality: Let be the local optimum found by
LS-CUT. This means that for any vertex , moving to would not increase the cut value. This implies that the number of edges connecting to is less than or equal to the number of edges connecting to . Summing this over all vertices in and , it can be shown that the value of the cut is at least half the total number of edges in the graph. Since the optimal cut value is at most , and we know the local optimumcost, the approximation ratio is .
A major drawback of simple local search is that it can get stuck in a local optimum that is far from the global optimum.
7.5 Simulated Annealing
Simulated Annealing (SA) is a metaheuristic inspired by the physical process of annealing metals, designed to overcome the limitation of local search by allowing the search to “jump out” of local optima.
In metallurgy, annealing involves heating a metal to a high temperature and then slowly cooling it. At high temperatures, particles have enough energy to move freely, exploring many configurations. As the temperature slowly decreases, particles settle into a low-energy, stable (optimal) configuration.
Simulated Annealing applies this idea to optimization:
- System states correspond to admissible solutions.
- Energy of a state corresponds to the cost of a solution.
- Optimal state corresponds to the optimal solution.
- Temperature is a control parameter that decreases over time.
Algorithm SA (Simulated Annealing)
Input: Problem instance , initial temperature , cooling schedule . Output: An approximate optimal solution .
- Compute an initial admissible solution .
- Set current temperature .
- While is not “very close to 0” (or a stopping criterion is met): a. Randomly choose a neighbor from . b. Calculate . (Assuming minimization, so is an improvement). c. If ( is better or equal): Set . (Always accept improving moves). d. Else ( is worse): Accept as the new solution with probability . (This is the Metropolis criterion. It allows escaping local optima, as accepting a worse solution occasionally prevents the algorithm from getting stuck in a suboptimal peak. The probability decreases for larger deteriorations and lower temperatures.) e. Update according to the cooling schedule (e.g., , or ).
- Output: .
The key feature of SA is the probabilistic acceptance of worse solutions. This probability decreases as increases (stronger deterioration is less likely) and as decreases (less likely to accept worse solutions at lower temperatures). With a carefully chosen cooling schedule, SA can converge to a global optimum, though often without guarantees on the number of iterations. It is widely used as a robust heuristic for many hard problems.
Summary
- Tackling Intractability: For NP-hard problems, which are theoretically intractable in the worst case (assuming ), practical solutions are sought by relaxing strict requirements for optimality or efficiency.
- Pseudopolynomial Algorithms: These algorithms are efficient when numerical input values are small, even if they are exponential in the input’s bit length. The Knapsack Problem is a prime example. Strongly NP-hard problems, however, do not admit such algorithms (unless P=NP).
- Approximation Algorithms: These provide polynomial-time solutions for optimization problems with a provable guarantee on how close the solution is to the optimum (e.g., a 2-approximation for MIN-VC or Metric TSP). This offers a valuable trade-off between optimality and efficiency.
- Local Search: A heuristic approach that iteratively improves a solution by making small, local changes until a local optimum is reached. While simple and widely applicable, it can get trapped in suboptimal local optima.
- Simulated Annealing: A metaheuristic inspired by thermodynamics that enhances local search by allowing occasional moves to worse solutions. This probabilistic mechanism helps escape local optima, enabling a more thorough exploration of the solution space and often leading to better solutions for complex problems.
- Art of Algorithm Design: This chapter highlights that even for problems deemed “hard” by complexity theory, clever algorithmic design, often involving compromises on optimality or generality, can yield effective and practical solutions.
Previous Chapter: Chapter 6 - Complexity Theory Next Up: Chapter 8 - Randomization
Exercises
Exercise 7.1 (Knapsack Problem Simulation)
Consider the Knapsack Problem instance with weights , benefits , and capacity . Simulate the
DPRalgorithm to find the optimal solution.Solution
Input:
Phase 1: Initialize TRIPLE(0)
TRIPLE(0) = {(0, 0, $\emptyset$)}Phase 2: Iterations
i = 1 (Object 1: )
SET(1) = TRIPLE(0) = {(0, 0, $\emptyset$)}Add object 1:SET(1) = {(0, 0, $\emptyset$), (6, 1, \{1\})}TRIPLE(1)(no duplicates, so same as SET(1)):{(0, 0, $\emptyset$), (6, 1, \{1\})}i = 2 (Object 2: )
SET(2) = TRIPLE(1) = {(0, 0, $\emptyset$), (6, 1, \{1\})}
- From : Add object 2. . New triple:
SET(2) = {(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\})}- From : Add object 2. . New triple:
SET(2) = {(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\}), (13, 4, \{1, 2\})}
TRIPLE(2)(no duplicates in benefit values):{(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\}), (13, 4, \{1, 2\})}i = 3 (Object 3: )
SET(3) = TRIPLE(2) = {(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\}), (13, 4, \{1, 2\})}
- From : Add object 3. . New triple:
SET(3) = {(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\}), (13, 4, \{1, 2\}), (4, 5, \{3\})}- From : Add object 3. . New triple:
SET(3) = {(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\}), (13, 4, \{1, 2\}), (4, 5, \{3\}), (10, 6, \{1, 3\})}- From : Add object 3. . New triple:
SET(3) = {(0, 0, $\emptyset$), (6, 1, \{1\}), (7, 3, \{2\}), (13, 4, \{1, 2\}), (4, 5, \{3\}), (10, 6, \{1, 3\}), (11, 8, \{2, 3\})}- From : Add object 3. . (Cannot add)
TRIPLE(3)(no duplicates in benefit values):{(0, 0, $\emptyset$), (4, 5, \{3\}), (6, 1, \{1\}), (7, 3, \{2\}), (10, 6, \{1, 3\}), (11, 8, \{2, 3\}), (13, 4, \{1, 2\})}Phase 3: Find Max Benefit The maximum benefit in
TRIPLE(3)is 13, corresponding to the triple .Output: The index set {1, 2}. (Objects 1 and 2 give benefit 13 with weight 4, which is ).
Exercise 7.2 (Strongly NP-Hard Proof)
Consider the Weighted Vertex Cover problem: given a graph with weights for each vertex , find a vertex cover such that is minimized. Prove that this problem is strongly NP-hard.
Solution
To prove that Weighted Vertex Cover (WVC) is strongly NP-hard, we need to show that even if the numerical values of the weights are polynomially bounded by the input length, the problem remains NP-hard. This is typically done by reducing a known NP-hard problem (which is not a number problem, or whose numerical values are small) to WVC.
We know that the unweighted Vertex Cover (VC) problem is NP-hard. VC is a special case of WVC where all vertex weights are 1. In this case, (where encodes the graph and weights) would be 1, which is polynomially bounded by the input length (it’s a constant).
Since VC is NP-hard, and it is equivalent to WVC where (a polynomial in ), it means that WVC restricted to instances with polynomially bounded weights is NP-hard.
Therefore, by Definition 7.3, Weighted Vertex Cover is strongly NP-hard.
Exercise 7.3 (2-Exchange Neighborhood for TSP)
The 2-Exchange neighborhood for TSP involves taking a Hamiltonian cycle, removing two non-adjacent edges and , and adding edges and to form a new Hamiltonian cycle. Does this neighborhood satisfy the conditions of Definition 7.5 (reflexivity, symmetry, reachability)?
Solution
Let be the set of all Hamiltonian cycles for a given instance of TSP.
Reflexivity (): This condition is typically interpreted as “a solution is always in its own neighborhood.” If the 2-Exchange operation is defined such that it must result in a different cycle, then it’s not reflexive. However, if we allow the “new” edges to be the same as the “removed” edges (i.e., no actual change to the cycle), then it would be reflexive. In practice, local search algorithms usually only consider neighbors that are strictly better, so the current solution is implicitly considered its own neighbor if no better one exists. For formal definition, it’s usually assumed to be reflexive.
Symmetry (if then ): Yes, this condition is satisfied. If a cycle is obtained from by removing edges and and adding and , then can be obtained from by removing and and adding and . The operation is reversible.
Reachability (any reachable from any ): Yes, this condition is satisfied. It is a known result in graph theory that any Hamiltonian cycle in a complete graph can be transformed into any other Hamiltonian cycle using a sequence of 2-Exchange operations. This means that the 2-Exchange neighborhood connects all possible Hamiltonian cycles, allowing the local search to explore the entire solution space.
Exercise 7.4 (LS-CUT Polynomial Time Proof)
Prove that
LS-CUT(Algorithm 7.6) is a polynomial algorithm.Solution
The
LS-CUTalgorithm works by iteratively moving a vertex from one partition to another if such a move improves the cut value.
- Initialization: Initializing takes time.
- Loop Iterations: The
whileloop continues as long as there exists a vertex whose move improves the cut. Each time a vertex is moved, the cut value (number of edges between and ) strictly increases. Since the maximum possible cut value is (the total number of edges in the graph), the loop can run at most times.- Finding an Improving Move: In each iteration of the
whileloop, the algorithm needs to check if an improving move exists. This involves iterating through all vertices . For each vertex , we calculate the change in the cut value if is moved to the other partition. This calculation involves summing the number of edges connecting to vertices within its current partition and to vertices in the other partition. This takes time. Summing over all vertices, finding the best improving move (or just any improving move) takes time.Total Time Complexity: The total time complexity is the product of the maximum number of iterations and the time taken per iteration: . Since for a simple graph, this simplifies to , which is a polynomial in the number of vertices.
Therefore,
LS-CUTis a polynomial algorithm.