0% found this document useful (0 votes)
11 views72 pages

5 FFT NP Randomized

The document covers advanced topics in algorithm design, including Fast Fourier Transform (FFT) for polynomial multiplication, the theory of NP-completeness, and various algorithmic strategies like approximation and randomized algorithms. It explains polynomial representation, convolution, and the efficiency of FFT in converting between coefficient and point-value forms. Additionally, it discusses the classification of computational problems into P, NP, NP-complete, and NP-hard categories, emphasizing the significance of the P versus NP problem in computer science.

Uploaded by

xech.170
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views72 pages

5 FFT NP Randomized

The document covers advanced topics in algorithm design, including Fast Fourier Transform (FFT) for polynomial multiplication, the theory of NP-completeness, and various algorithmic strategies like approximation and randomized algorithms. It explains polynomial representation, convolution, and the efficiency of FFT in converting between coefficient and point-value forms. Additionally, it discusses the classification of computational problems into P, NP, NP-complete, and NP-hard categories, emphasizing the significance of the P versus NP problem in computer science.

Uploaded by

xech.170
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Unit-5

Design and Analysis of Algorithm


KCS-503
Topic to be covered
➢Fast Fourier Transform and Algebraic computation
➢Theory of NP-completeness
➢Approximation algorithms
➢Randomized algorithms
➢Global Data Flow Analysis
Overview to Algebraic computation
• Given two polynomial A(x) and B(x), find the product C(x) =
A(x)*B(x).

• There is already an O(n2) naive approach to solve this


problem. This approach uses the coefficient form of the
polynomial to calculate the product.

• To do better, represent polynomial in another form called point-


value form, and we use FFT algorithm for the conversion.
Polynomial Multiplication Using Co-efficient Form
▪ A coefficient representation of a polynomial is
a = a0, a1, …, an-1.
• Example:
A(x) = 6x3 + 7x2 - 10x + 9, B(x) = -2x3 + 4x - 5
– `Coefficient representation of A(x) = (9, -10, 7, 6)
– `Coefficient representation of B(x) = (-5, 4, 0, -2)
✓ Input:
A[] = {9, -10, 7, 6}, B[] = {-5, 4, 0, -2}
✓ Output:
C(x) = a0*bi + a1*bi-1 + ... + ai*b0
= -12x6 - 14x5 + 44x4 - 20x3 - 75x2 + 86x - 45
Fourier Transform and Convolution Theorem
• If we think of A and B as vectors, then C vector is called 'Convolution' of A and B
(represented as {A ⊗B}).
• Fourier analysis is the decomposition of any mathematical function into a series of
sine and cosine waves (sinusoids). We generally represent a function in its Fourier
Transform form, as it is easier to work with.
• The Convolution Theorm states that under suitable conditions the Fourier transform
of a convolution (denoted by ‘ ⊗’) is the point-wise product of Fourier transforms.
• Let f and g be two functions with convolution {f ⊗ g}.
• Fourier transform be denoted by the operator ‘ζ’. So ζ(f) and ζ(g) are the Fourier
transforms of f and g, respectively.
• Therefore, ζ(f ⊗g) = ζ(f).ζ(g)
• The convolution operation is converted to point-wise multiplication
Point Value Representation of Polynomials
• A polynomial of degree n-1 can be uniquely represented by its values at at-least n
different points.
• This is a result of the Fundamental Theorm of Algebra that states: “A degree n-1
polynomial A(x) is uniquely specified by its evaluation at n distinct values of x”.

• A polynomial A(x) =a0+a1x+a2x2+… anxn can be represented as:


A set of n pairs {(x0, y0),(x1, y1),...,(xn, yn)} such that:
✓for all i != j, xi != xj
i.e., points are unique
✓for every k, yk = A(xk)
• In point-value form, multiplication C(x) = A(x)B(x) is given by C(xk) =
A(xk).B(xk) for any point (xk).

• If A and B are of degree-bound 'n', then C is of degree-bound '2n'.


– Need to start with “extended” point-value forms for A and B consisting
of 2n point-value pairs each:
✓ A: (x0,y0), (x1,y1), ......, (x2n-1,y2n-1)
✓ B: (x0,y0'), (x1,y1'), ......, (x2n-1,y2n-1')
✓ C: (x0,y0y0'), (x1,y1y1'), ......, (x2n-1,y2n-1y2n-1')
• Example:
– A(x) = x3 − 2x + 1
– B(x) = x3 + x2 + 1
– xk = (−3, −2, −1, 0, 1, 2, 3), we need 7 coefficients!
– A: (−3,−20) ,( −2,−3) ,( −1,1) ,( 0, 1) ,( 1, 0) , (2, 5) , (3, 22)
– B: (−3,−17) ,(−2,−3) , (−1, 2) ,( 0, 1) ,( 1, 3) ,( 2, 13) ,( 3, 37)
– C: (−3,340) , (−2,9) , (−1,2) ,( 0, 1) ,( 1, 0) ,( 2, 65) ,( 3, 814)

• This is point-wise multiplication (Time Complexity: Θ(n)).


• The inverse of evaluation--determining the coefficient form of a polynomial from a point-value
representation is called interpolation.
• This is what the Convolution Theorm states with respect to Fourier Transform of a function.
• Following diagram summarizes what we have discussed so far:

•Now the efficiency of our algorithm depends on the efficiency of conversion between the two
representation:

•This is where we use the FFT algorithm.


Overview of the Algorithm:
Let m = 2n-1. [so degree of C is less than m]
1. Pick m points x0, x1, ..., x_{m-1} chosen cleverly.
2. Evaluate A at each of the points: A(x_0),..., A(x_{m-1}).
3. Same for B.
4. Now compute C(x_0),..., C(x_{m-1}), where C is A(x)*B(x)
5. Interpolate to get the coefficients of C.

• 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:

A(x) = a_0 + a_1 x + a_2 x² + a_3 x³ + a_4 x⁴ + a_5 x⁵ + a_6x⁶ + a_7x⁷


(as a vector, A = [a_0, a_1, …, a_7])
-To evaluate at eight points of our choosing. Split A into two pieces, but instead of
left and right, have them be even and odd.
✓ So, as vectors,
– A_even = [a_0, a_2, a_4, a_6]
– A_odd = [a_1, a_3, a_5, a_7]
✓ or, as polynomials:
– A_even(x) = a_0 + a_2 x + a_4 x² + a_6 x³
– A_odd(x) = a_1 + a_3 x + a_5 x² + a_7 x³.
• Each has degree < m/2. How can we write A(x) in terms of A_even and A_odd?
– A(x) = A_even(x²) + x A_odd(x²).
– A(-x) = A_even(x²) — x A_odd(x²).
▪ Now, instead of calculating the value for 1 polynomial of degree m, we now need to
calculate 2 polynomials of degree (m/2) and then combine them.

▪ 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.

• Once the Inverse Fourier Transform is performed, we get an array of Complex


Numbers with the imaginary part almost equal to 0. Hence, it can be ignored.
• So, we have seen so far, computing A(x) at 1, ω,ω2, …,ω{m-1} can be
represented as:

• In matrix form:
• Let's use F to denote the matrix of primitive roots.

• In order to retrieve the coefficients, we need to get


F^-1(inverse of matrix F), and multiply it with the y-matrix.

• 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:

• Based on the above observation, we can still apply FFT by


replacing a with y, y with a, ωn with ωn^−1 (that is, ωn^n-1 ),
and scaling the result by 1/n.
• So, the final algorithm is:

Let F_A = FFT(A, m, ω) // time O(n log n)


Let F_B = FFT(B, m, ω) // time O(n log n)
For i=1 to m, let F_C[i] = F_A[i]*F_B[i] // time O(n)
Output C = 1/m * FFT(F_C, m, ω-1). // time O(n log n)
Theory of NP-completeness
Overview: Theory of NP-completeness
▪ NP-complete problems are in NP, the set of all decision
problems whose solutions can be verified in polynomial time.
▪ NP may be equivalently defined as the set of decision problems
that can be solved in polynomial time on a non-deterministic
Turing machine.
▪ A problem p in NP is NP-complete if every other problem in NP
can be transformed (or reduced) into p in polynomial time.
Can all computational problems be solved by a computer?
• There are computational problems that can not be solved by algorithms even with
unlimited time.
• Example, Turing Halting problem.
• Alan Turing proved that general algorithm to solve halting problem for all
possible program-input pairs cannot exist.
• A key part of the proof is, Turing machine was used as a mathematical definition
of a computer and program.

• 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:

• The ratio is never less than 1 (perfect performance).

• An algorithm that has an approximation ratio of ρ(n) is called a ρ(n)-


approximation algorithm.

• For example, if the ratio is 2 we have a 2-approximation algorithm,


meaning it gives solutions that never cost more than twice that of optimal if it
is a minimization problem, or never provide less than half the optimal value
if it is a maximization problem.
• An approximation scheme is a parameterized approximation algorithm that
takes an additional input ε > 0. And for any fixed ε, there is a (1+ε)-
approximation algorithm.
• ε is how much slack you will allow away from the optimal 1-"approximation".
• For example, if ε = 1 we have a 2-approximation scheme.
• An approximation scheme is a polynomial approximation scheme if for any
fixed ε > 0 the scheme runs in time polynomial in input size n.
• Examples:-Vertex Cover Approximations, TSP Approximations
Vertex Cover Approximations
• A vertex cover of an undirected graph G = (V, E) is a subset V′ ⊆ V such that if (u, v) ∈ E then
u ∈ V′ or v ∈ V′ or both.
• optimization version of Vertex Cover Problem is to find a vertex cover of minimum size in G.
• corresponding decision problem is NP-Complete, so the optimization problem is NP-Hard.
• Vertex Cover can be approximated by the following surprisingly simple algorithm,
which iterately chooses an edge that is not covered yet and covers it using both
incident vertices:
Example:
• Input Graph:

• 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.

Approximate algorithm produces optimal tour,


but it may not produce optimal tour in all cases.
How is this algorithm 2-approximate?
▪ Cost of the output produced by the above algorithm is never more than twice the cost of
best possible output. Let us see how is this guaranteed by the above algorithm.
▪ Let us define a term full walk to understand this. A full walk is lists all vertices when they
are first visited in preorder, it also list vertices when they are returned after a subtree is
visited in preorder. The full walk of above tree would be 1-2-1-4-1-3-1.
Important facts that prove the 2-approximateness
1) Cost of best possible Travelling Salesman tour is never less than cost of MST.
2) The total cost of full walk is at most twice the cost of MST (Every edge of MST is visited at-most twice)
3) Output of the above algorithm is less than the cost of full walk. In above algorithm, we print preorder walk
as output. In preorder walk, two or more edges of full walk are replaced with a single edge.
For example, 2-1 and 1-4 are replaced by 1 edge 2-4. So if the graph follows triangle inequality, then this is
always true.
• From above three statements, we can conclude that the cost of output produced by the approximate
algorithm is never more than twice the cost of best possible solution.
• We have discussed a very simple 2-approximate algorithm for the travelling salesman problem. There are
other better approximate algorithms for the problem. For example Christofides algorithm is 1.5
approximate algorithm.
Randomized Algorithm
A randomized algorithm is an algorithm that employs a degree
of randomness as part of its logic or procedure.
Deterministic Algorithm Vs Randomized
Algorithm
• The output as well as the running •The output or the running time are
time are functions only of the functions of the input and random
input. bits chosen.
➢ Deterministic Quicksort
• Quicksort(A,p,r)
if p < r then
• q := Partition(A,p,r); // rearrange A[p..r] in place
• Quicksort(A, p,q-1);
• Quicksort(A,p+1,r);

The design of Quicksort is based on the divide-and-conquer paradigm.


a) Divide: Partition the array A[p..r] into two (possibly empty) subarrays A[p..q-1]
and A[q+1,r] such that
- A[x] <= A[q] for all x in [p..q-1]
- A[x] > A[q] for all x in [q+1,r]
b) Conquer: Recursively sort A[p..q-1] and A[q+1,r]
c) Combine: nothing to do here
Partition
2 1 3 4 7 5 6 8
p i r

• Select pivot (orange element) and rearrange:


• larger elements to the left of the pivot (red)
• elements not exceeding the pivot to the right (yellow)
•Partition(A,p,r)
• x := A[r]; // select rightmost element as pivot
• i := p-1;
• for j = p to r-1 do
• if A[j] <= x then i := i+1; swap(A[i], A[j]); fi;
• od;
• swap(A[i+1],A[r])
• return i+1; Throughout the for loop:
•If p <= k <= i then A[k]<= x
• If i+1<=k <= j-1 then A[k] > x
• If k=r, then A[k] = x
• A[j..r-1] is unstructured
Partition - Loop - Example
• After the loop, the partition routine swaps the leftmost element of the
right partition with the pivot element:

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

T(n) <= T(n/5) + T(7n/10) + 6n/5.

// median of U, recursive call, and finding medians of groups


➢ Randomized Quicksort
Randomized-Quicksort(A,p,r)
if p < r then
q := Randomized-Partition(A,p,r);
Randomized-Quicksort(A, p,q-1);
Randomized-Quicksort(A,p+1,r);
Partition
Randomized-Partition(A,p,r)
i := Random(p,r);
swap(A[i],A[r]);
Partition(A,p,r);

• 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.

▪ Running time of quicksort depends on number of comparisons performed in all


calls to Randomized-Partition routine.
▪ Let X denote random variable counting number of comparisons in all calls to
Randomized-Partition.
Notations
• Let zi denote the i-th smallest element of A[1..n].
• Thus A[1..n] sorted is <z1, z2, ... , zn >.
• Let Zij = {zi, ..., zj} denote the set of elements between zi and
zj, including these elements.

• Xij = I{ zi is compared to zj}.


• Thus, Xij is an indicator random variable for the event that the
i-th smallest and the j-th smallest elements of A are compared in
an execution of quicksort.
Number of Comparisons
– Since each pair of elements is compared at most once by
quicksort, the number X of comparisons is given

– Therefore, the expected number of comparisons is


When do we compare?
• When do we compare zi to zj?

• Suppose we pick a pivot element in Zij = {zi, ..., zj}.


• If zi < x < zj then zi and zj will land in different partitions and
will never be compared afterwards.
• Therefore, zi and zj will be compared if and only if the first
element of Zij to be picked as pivot element is contained in the
set {zi,zj}.
Probability of Comparison
Expected Number of Comparisons
Strength of Randomized quick sort
• It follows that the expected running time of Randomized-Quicksort is
O(n log n).

• It is unlikely that this algorithm will choose a terribly unbalanced


partition each time, so the performance is very good almost all the
time.
Review Question
• Explain the terms Algebraic Computation with suitable example.
• Discuss the problem classes P, NP and NP –complete with class relationship.
• What is FFT (Fast Fourier Transformation)? How recursive FFT procedure works?
Explain.
• Explain the terms Approximation algorithms and Randomized algorithms.
• Discuss Vertex Cover Problem and Travelling Salesman Problem.
• Discuss various applications of FFT.
• What are NP-Complete problems?
• Differentiate NP-complete with NP-Hard.
• What are approximation algorithms? Discuss its significance.
• Show that Vertex-cover problem is 2-approximate.

You might also like