5 FFT NP Randomized
5 FFT NP Randomized
•Now the efficiency of our algorithm depends on the efficiency of conversion between the two
representation:
• At a glance, it looks like points 2 and 3 from the Algorithm takes O(n2) time.
– However, the FFT will allow us to quickly move from coefficient representation
of polynomial to the point-value representation, and back, for our cleverly
chosen set of m points.
– Doesn’t work for arbitrary m points. The special points will turn out to be the
roots of unity.
Roots of Unity
• The roots of unity of order n are those numbers which, when raised to the n th power, we
get 1 ("unity"). They're also called "n th roots of unity".
• There are exactly n such numbers, one of which is always the number 1 itself.
• When n=2, they are 1 and −1 .
• To get any root of unity, other than 1, we need to delve into Complex Numbers.
• Visualising eight 8th roots of unity:
Some Interesting Properties of Roots of Unity:
1. Principal nth Root of Unity
2. Cancellation Lemma
3. Halving Lemma
Discrete Fourier Transform (DFT)
• Since we have broken up the problem, our goal now is to evaluate a given
polynomial, A (x)(degree <m) at m points of our choosing in total time O(m log
m).
-Assume m is a power of 2.
-Develop it through an example.
-Say m=8, so we have a polynomial:
▪ Let T(m) = time to evaluate a degree-m polynomial at our special set of m points. We’re
doing this by evaluating two degree-m/2 polynomials at m/2 points each (the squares), and
then doing O(m) work to combine the results.
• The recurrence T(m) = 2T(m/2) + O(m) solves to O(m log m).
▪ What’s nice is that the effort spent computing A(x) will give us A(-x) almost for free. So, we
need to specially choose m points that will have the property: the 2nd half of points are the
negative of the 1st half.
• We use complex numbers for this (roots of unity).
Fast Fourier Transform (FFT)
• The problem of evaluating A(x) at ωn^0 , ωn^1 , … , ωn^n−1 reduces to
1. evaluating the degree-bound n/2 polynomials Aeven(x) and Aodd(x) at the points
(ωn^0)^2 ,(ωn^1)^2 , … , (ωn^n−1)^2.
2. combining the results by A(x) = Aeven(x2) + xAodd(x2).
• Why bother?
• The list (ωn^0)^2 ,(ωn^1)^2 , … , (ωn^n−1)^2 does not contain n distinct values, but n/2
complex n/2-th roots of unity.
• Polynomials Aeven and Aodd are recursively evaluated at the n/2 complex n/2-th roots
of unity.
• Subproblems have exactly the same form as the original problem, but are half the size.
• Example:
• Algorithm
Algorithm
▪ ‘m’ should be a power of 2 , Pad extra zeros to the polynomial to ensure that length of the
array is a power of 2.
▪ This algorithm returns an array of complex numbers which is then used for point-wise
multiplication.
▪ Tree of input vectors to recursive calls of the FFT procedure. Initial invocation is for n = 8.
Inverse Fourier Transform:
• Once we perform point-wise multiplication on the Fourier Transforms of A and B and
get C (an array of Complex Numbers), we need to convert it back to coefficient form
to get the final answer.
• We use the Inverse Fourier Transform to get the coefficient form of C (Don’t worry
about the imaginary part of the complex number.
• In matrix form:
• Let's use F to denote the matrix of primitive roots.
• It turns out that F^-1 looks the same. In place of ω, the inverse Fourier
Matrix uses ω^-1.
– if ω = a+ib => ω^-1 = a-ib
– if ω = e^2Πi/k => ω^-1 = e^-2Πi/k
• So, F^-1:
• No polynomial time algorithm has yet been discovered for any NP complete
problem, nor has anybody yet been able to prove that no polynomial-time algorithm
exist for any of them.
• If any one of the NP complete problems can be solved in polynomial time, then all
of them can be solved.
What are NP, P, NP-complete and NP-Hard problems?
•P is set of problems that can be solved by a deterministic Turing machine in
Polynomial time.
• NP is set of decision problems that can be solved by a Non-deterministic Turing
Machine in Polynomial time.
• P is subset of NP
• Informally, NP is set of decision problems which can be solved by a polynomial
time via a “Lucky Algorithm”, a magical algorithm that always makes a right guess
among the given set of choices.
What are NP, P, NP-complete and NP-Hard problems?
• A problem is NP-hard if all problems in NP are polynomial time reducible
to it, even though it may not be in NP itself.
• The circuit-satisfiability problem
• Set Cover
• Vertex Cover
• Travelling Salesman Problem
• A problem is NP-complete if it is both NP-hard and in NP. Therefore, NP-
Complete set is also a subset of NP-Hard set.
• Determining whether a graph has a Hamiltonian cycle
• Determining whether a Boolean formula is satisfiable, etc.
What are NP, P, NP-complete and NP-Hard
problems?
P versus NP problem
• P versus NP problem is a major unsolved problem in computer science. It asks
whether every problem whose solution can be quickly verified can also be solved
quickly.
• It is one of the seven Millennium Prize Problems selected by the Clay Mathematics
Institute, each of which carries a US$1,000,000 prize for the first correct solution.
• An answer to P versus NP question would determine whether problems that can
be verified in polynomial time can also be solved in polynomial time.
• If it turned out that P ≠ NP, which is widely believed, it would mean that there are
problems in NP that are harder to compute than to verify: they could not be
solved in polynomial time, but the answer could be verified in polynomial time.
P versus NP problem
Decision vs Optimization Problems
▪ NP-completeness applies to the realm of decision problems.
▪ It was set up this way because it’s easier to compare the difficulty of decision problems than
that of optimization problems.
▪ In reality, though, discussing the difficulty of decision problems is often really equivalent to
discussing the difficulty of optimization problems.
▪ For example, consider the vertex cover problem (Given a graph, find out the minimum sized
vertex set that covers all edges). It is an optimization problem. Corresponding decision
problem is, given undirected graph G and k, is there a vertex cover of size k?
What is Reduction?
• Let L1 and L2 be two decision problems.
• Suppose algorithm A2 solves L2. That is, if y is an input for L2 then algorithm A2 will
answer Yes or No depending upon whether y belongs to L2 or not.
• The idea is to find a transformation from L1 to L2 so that the algorithm A2 can be part
of an algorithm A1 to solve L1.
How to prove that a given problem is NP complete?
• From definition of NP-complete, it appears impossible to prove that a
problem L is NP-Complete.
• By definition, it requires us to that show every problem in NP is polynomial
time reducible to L.
• The idea is to take a known NP-Complete problem and reduce it to L.
• If polynomial time reduction is possible, we can prove that L is NP-
Complete by transitivity of reduction (If a NP-Complete problem is
reducible to L in polynomial time, then all problems are reducible to L in
polynomial time).
What was the first problem proved as NP-Complete?
• There must be some first NP-Complete problem proved by definition of NP-
Complete problems.
• SAT (Boolean satisfiability problem) is the first NP-Complete problem proved by
Cook.
• Boolean Satisfiability is the problem of determining if a Boolean formula is satisfiable
or unsatisfiable
Approximation algorithm
Approximation Algorithms
• Up to now, the best algorithm for solving an NP-complete problem
requires exponential time in the worst case. It is too time-consuming.
• To reduce the time required for solving a problem, we can relax the
problem, and obtain a feasible solution “close” to an optimal solution.
Definition-
Let C be the cost of a solution found for a problem of size n and C* be the optimal
solution for that problem.
Then we say an algorithm has an approximation ratio of ρ(n) if
C/C* ≤ ρ(n) for minimization problems: the factor by which the actual solution obtained is
larger than the optimal solution.
C*/C ≤ ρ(n) for maximization problems: the factor by which the optimal solution is larger
than the solution obtained.
• Both of these statements can be written together as follows:
• Suppose then that edge {b, c} is chosen. The two incident vertices are added to
the cover and all other incident edges are removed from consideration:
• Iterating now for edges {e, f} and then {d, g}:
• The resulting vertex cover is shown on the left and the optimal vertex on the
right (the lighter colored vertices are in the cover:
Analysis
• How good is the approximation? We can show that the solution is within a
factor of 2 of optimal.
• Theorem: Approx-Vertex-Cover is a polynomial time 2-approximation
algorithm for Vertex Cover.
• Proof: Vertex Cover: The algorithm constructs a vertex cover because it
loops until every edge in E has been covered.
• Polynomial: The algorithm has O(|E|) iterations of the loop, and (using aggregate analysis, Topic
15) across all loop iterations, O(|V|) vertices are added to C. Therefore it is O(E + V), so is
polynomial.
• 2-Approximation: It remains to be shown that the solution is no more than twice the size of the
optimal cover. We'll do so by finding a lower bound on the optimal solution C*.
• Let A be the set of edges chosen in line 4 of the algorithm. Any vertex cover must cover at least
one endpoint of every edge in A.
• No two edges in A share a vertex, because for any two edges one of them must be selected first
(line 4), and all edges sharing vertices with the selected edge are deleted from E′ in line 6, so are
no longer candidates on the next iteration of line 4. Therefore, in order to cover A, the optimal
solution C* must have at least as many vertices as edges in A:
| A | ≤ | C* |
• Since each execution of line 4 picks an edge for which neither endpoint is yet in C and line 5 adds these two
vertices to C, then we know that
|C| = 2|A|
• Therefore:
| C | ≤ 2 | C* |
That is, |C| cannot be larger than twice the optimal, so is a 2-approximation algorithm for Vertex Cover.
• This is a common strategy in approximation proofs: we don't know the size of the optimal solution, but we
can set a lower bound on the optimal solution and relate the obtained solution to this lower bound.
TSP Approximations (Approximate using MST)
• Travelling Salesman Problem has Naive and Dynamic Programming Solutions.
– Both infeasible solutions
– No polynomial time solution
– NP Hard problem
• The approximate algorithms work only if the problem instance satisfies Triangle-
Inequality.
▪ Triangle-Inequality: The least distant path to reach a vertex j from i is always
to reach j directly from i, rather than through some other vertex k (or
vertices), i.e., dis(i, j) is always less than or equal to dis(i, k) + dist(k, j). The
Triangle-Inequality holds in many practical situations.
▪ When cost function satisfies the triangle inequality, we can design an
approximate algorithm for TSP that returns a tour whose cost is never more
than twice the cost of an optimal tour. The idea is to use Minimum Spanning
Tree (MST). Following is the MST based algorithm.
Algorithm
1) Let 1 be the starting and ending point for salesman.
2) Construct MST from with 1 as root using Prim’s Algorithm.
3) List vertices visited in preorder walk of the constructed MST and add 1 at the end.
Example- Second diagram shows MST constructed with 1 as root. The preorder
traversal of MST is 1-2-4-3. Adding 1 at the end gives 1-2-4-3-1 which is the output
of this algorithm.
2 1 3 8 7 5 6 4
p i r
• swap(A[i+1],A[r])
2 1 3 4 7 5 6 8
p i r
• Now recursively sort yellow and red parts.
Worst-Case Partitioning
• The worst-case behavior for quicksort occurs on an input of length n when
partitioning produces just one subproblem with n-1 elements and one
subproblem with 0 elements.
• Therefore the recurrence for the running time T(n) is:
• T(n) = T(n-1) + T(0) + θ(n) = T(n-1) + θ(n) = θ(n2)
Better method for Quicksort and Linear Median
Algorithm
Best-case Partitioning:
• If partition produces two subproblems that are roughly of the same size,
then the recurrence of the running time is
• T(n) <= 2T(n/2) + θ(n)
• so that T(n) = O(n log n)
• Can we achieve this bound?
• Yes, modify the algorithm. Use a linear-time median algorithm to find
median, then partition using median as pivot.
Linear Median Algorithm
Let A[1..n] be an array over a totally ordered domain.
- Partition A into groups of 5 and find the median of each group. [We can do
that with 6 comparisons]
- Make an array U[1..n/5] of the medians and find the median m of U by
recursively calling the algorithm.
- Partition array A using the median-of-medians m to find the rank of m in A.
-If m is of larger rank than median of A, eliminate all elements > m.
- If m is of smaller rank than median of A, eliminate all elements <= m.
-Repeat the search on the smaller array.
Linear-Time Median Finding
How many elements do we eliminate in each round?
The array U contains n/5 elements. Thus, n/10 elements of U are larger
(smaller) than m, since m is the median of U . Since each element in U
is a median itself, there are 3n/10 elements in A that are larger (smaller)
than m.
Therefore, we eliminate (3/10)n elements in each round.
Thus, the time T(n) to find the median is
• Almost the same as Partition, but now pivot element is not rightmost element,
but an element from A[p..r] that is chosen uniformly at random.