






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







In our discussion of time complexity, we have seen that there are problems that are solvable in principle, but require so much time and/or space to run, that they cannot in practice be solved for any but the smallest input sizes. But what should one do if facing such a problem in practice? Often, there are ways to get around the prohibitive time requirement by either making the problem slightly easier, or by accepting a solution that might be wrong in very few cases. Today we will address both these approaches.
As an early example of an N P-complete problem, we have seen the VERTEX- COVER problem: Given a graph G = (V, E), is there a vertex cover, i. e., a subset of nodes, that touches all edges in E and contains not more than k vertices, where k is a given constant? Posed as a language, this problem becomes
VERTEX-COVER =
〈G, k〉
∣ (^) G has a vertex cover of at most k vertices
Typically, this problem is asked in another form: Instead of asking whether some vertex cover exists, the task is to find the smallest possible vertex cover: Given a graph G = (V, E), find a vertex cover of G having the smallest possible number of vertices. Typically, optimization problems like this one are even more difficult to decide than the related yes/no problems: VERTEX- COVER is N P-complete, but MIN-VERTEX-COVER is N P-hard, i. e., it is not even in N P itself.
But the problem can be solved quite easily if the requirement of finding the optimal solution is relaxed: In practice, it is often sufficient to find a solution that is not much worse than the theoretical best solution. We can formalize this notion by the following definition:
Definition 1 (k-optimal Approximation) If P is a minimization prob- lem, and A is a polynomial-time approximation algorithm for P that always returns a solution that is not more than k times optimal, then A is a k- optimal approximation algorithm for P.
The following algorithm is 2-optimal for MIN-VERTEX-COVER:
2-MIN-VERTEX-COVER On input 〈G〉, where G = (V, E) is a graph,
This algorithm definitely runs in polynomial time, and it also produces a cor- rect vertex cover: If there were any edge in E that is not covered by a selected vertex, it cannot share any vertices with any marked edge. But then it would have been marked by step 2 of the algorithm, and both vertices touched by it would have been included in the vertex cover. That is a contradiction. Let us now look at how many more vertices than necessary this algorithm selects: Let us denote the set of marked edges as M , the set of selected vertices as X, and some minimal vertex cover as C. Then X contains exactly twice as many vertices as M : For every edge in M , the algorithm selects two vertices. The selected vertices for two different edges must be different, because by step 1 no two marked edges share vertices. Thus, |X| = 2 · |M |. Furthermore, the number of vertices in C must be at least as big as the number of edges in M : C is a vertex cover, so its vertices must cover all edges in E, and especially all edges in M. But since edges in M do not share vertices, one vertex in C can only cover one edge in M ; therefore, |C| ≥ |M |. Altogether, this means that |X| ≤ 2 · |C|, which means that the given algorithm is 2-optimal.
a given number p is a prime or not. The brute-force algorithm to decide this checks if any integer between 2 and
p divides p; if this is the case, p cannot be prime; otherwise, it must be prime. Since the amount of integers in the given range is exponential in the length of the encoding of p, this algorithm requires exponential time. To convert this into a probabilistic algorithm with a small probability of failure, we need some tools from number theory:
Theorem 1 (Fermat’s Little Theorem) If p is a prime number, then ap−^1 ≡ 1 (mod p) for any a ∈ { 2 , 3 ,... , p − 1 }.
Theorem 2 If p is an integer, and ap−^1 6 ≡ 1 (mod p) for some integer a ∈ { 2 , 3 ,... , p − 1 }, then ap−^1 6 ≡ 1 (mod p) for at least half of the integers a ∈ { 2 , 3 ,... , p − 1 }.
These two theorems together can be used to probabilistically decide if a number p is prime: An algorithm could pick one number a ∈ { 2 , 3 ,... , p − 1 } at random, and check if ap−^1 ≡ 1 (mod p). If this is not the case, p must be a composite number, and the algorithm can reject. If it is the case, there might be two reasons: First, the number p could be prime, or second, the equivalence just holds by coincidence. But the latter case can only occur with a probability of less than 1/2: The second theorem says, that if one number violates it, at least half the numbers must. But then randomly picking a number that satisfies the equivalence only has a probability of less than 1/2. An error probability of 1/2 is way too large for any practical purpose, but the algorithm can easily reduce it: If the random test is repeated k times, and p is not a prime number, then the probability of picking a “lucky” satisfying number is the product of the probabilities of picking one in each step – the random experiments are independent of each other. Thus, the probability of picking wrong k times is only 2−k. By making k large, the error probability becomes arbitrarily small, and it decreases quickly: By repeating the test only 1000 times, the error probability is less than 2−^1000. The time complexity of each step is polynomial, so repeating it k times for constant k yields total polynomial running-time. This algorithm still has one serious flaw: There are some non-prime num- bers that have the nasty property of passing any modulo test of this kind; they are called Carmichael Numbers. The algorithm as stated will accept all primes, and also all Carmichael Numbers, with a probability of 1. To
filter out the Carmichael Numbers, the algorithm must be changed by using another fact from number theory.
Theorem 3 (Roots of Unity) If a, b and p are integers, then a is said to be a square root of b modulo p, if and only if (a · a) ≡ b (mod p). If p is a prime number, then the number one has exactly two square roots modulo p, namely − 1 and 1.
For many composite numbers, and especially all Carmichael Numbers, one has four or more square roots. To fix the given algorithm, we perform the “Fermat test” first, and if p passes it, we search roots of unity modulo p. This can be easily done: If ap−^1 ≡ 1 (mod p), then p − 1 is an even number (all primes except 2 are odd), and a(p−1)/^2 must be a square root of 1: (a(p−1)/^2 · a(p−1)/^2 ) ≡ ap−^1 ≡ 1 (mod p). If a(p−1)/^2 is itself equal to 1, and (p − 1)/2 is still even, we can create another root of unity by halving it again, repeating this step until the root is no longer one or the exponent becomes odd. Then the number p − 1 can be written as p = s · 2 h, where s is odd and h ≥ 1. We can then compute a sequence of h + 1 numbers by evaluating as·^2 i for i = 0, 1 ,... , h. Every number in this sequence after the last number not equal to one is a root of unity, and, if p is prime, the last number not equal to one must be −1. All numbers before the last occurrence of −1 are not roots of unity, but roots of negative unity and must not be considered. The fixed algorithm using roots of unity works as follows:
Algorithm PRIMES On input 〈p〉, where p ≥ 2 is an integer,
(a) Randomly select a ∈ { 2 , 3 ,... , p − 1 }. (b) Calculate ap−^1 mod p. If this is not equal to 1, reject. (c) Find an odd s and h ≥ 1 such that p − 1 = s · 2 h. (d) Calculate the sequence of numbers as·^2 i for i = 0, 1 ,... , h. (e) If some element of this sequence is not 1, find the last entry un- equal to 1 and reject if that entry is not equal to −1.
f
f(x)
a=x 1 x 2 x 3 b=x 4 x
Figure 2: A piecewise linear approximation a to a function f over the inter- val [a, b] using only four samples.
We judge the quality of an approximation by some error measure; a rea- sonable error measure would be the L^2 -distance between a and f , calculated as E(a) =
∫ (^) b a
a(x)−f (x)
dx. This integral can be approximated by looking at the samples defining f only, without even knowing an analytical represen- tation of f. Using these definitions, the problem becomes: Given a function f as a set of N samples
xi, f (xi)
, find a piecewise linear approximating func- tion a using only M samples, such that the approximation error E(a) is minimal. The brute-force approach to solve this problem would generate all possible subset of M samples, calculate the approximation error for each, and then select the best one found. Since there are
M
different subsets of samples, and all have to be considered, this algorithm needs running-time exponential in N. With N typically being large, that algorithm is not prac- tical. Instead, we use an iterative probabilistic optimization technique as follows:
Optimal Approximation On input 〈f, M 〉, where f is a function defined by a set of samples and M is a positive integer,
(a) Create a new approximation function ai+1 by changing one of the samples selected for ai: Remove one random sample, and insert another random sample from f.
(b) Calculate the approximation error Ei+1 = E(ai+1). (c) If the difference ∆ = Ei+1 − Ei is negative, the change improved the approximation. Accept it. (d) Otherwise, replace ai+1 by ai and Ei+1 by Ei.
This algorithm performs a random walk through the space of approxi- mation functions: It starts with a random approximation, randomly changes the approximation in every step, and accepts a step if it improves the ap- proximation. If this algorithm runs for a large number of steps, the final approximation will be a locally optimal one with a high probability. The behaviour of the algorithm can best be visualized in a one-dimensional op- timization space: If the task is to find a minimum in a function f : R → R, one could start searching at a random position x, move either left or right in each step, and accept the move if it decreased the function value f (x). An illustration of such a walk is depicted in Figure 3.
x 0 x 1 x 2 x 3 x 4 x 5
s 1
s 2 s 3
s 4
s 5 s 6 s 7
Figure 3: A random walk x 0 , x 1 ,... , x 5 to find the minimum of a function. The shaded circles denote rejected steps.
This algorithm has one major problem: It only accepts steps that decrease the error measure. This can lead to the random walk getting stuck in a local minimum that has a higher error measure than the global minimum. As it turns out, the shape of the error function E(a) for function approximations is highly irregular: It typically has local minima in abundance – see Figure 4 for an illustration – and a good approximation algorithm has to move past them and find the true global minimum.
cal constant called Boltzmann’s Constant. This process works extremely well in nature, and its simulated version has been shown to work on optimization problems as well.