Instructor Neelima Gupta Table of Contents Divide and Conquer.

Slides:



Advertisements
Similar presentations
Fast Fourier Transform for speeding up the multiplication of polynomials an Algorithm Visualization Alexandru Cioaca.
Advertisements

A simple example finding the maximum of a set S of n numbers.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Fast Fourier Transform Lecture 6 Spoken Language Processing Prof. Andrew Rosenberg.
Matrices: Inverse Matrix
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
6 6.1 © 2012 Pearson Education, Inc. Orthogonality and Least Squares INNER PRODUCT, LENGTH, AND ORTHOGONALITY.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
CSE 421 Algorithms Richard Anderson Lecture 15 Fast Fourier Transform.
FFT1 The Fast Fourier Transform by Jorge M. Trabal.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fast Fourier Transform Jean Baptiste Joseph Fourier ( ) These lecture.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Reconfigurable Computing S. Reda, Brown University Reconfigurable Computing (EN2911X, Fall07) Lecture 16: Application-Driven Hardware Acceleration (1/4)
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
Linear Equations in Linear Algebra
Introduction to Algorithms
6 6.1 © 2012 Pearson Education, Inc. Orthogonality and Least Squares INNER PRODUCT, LENGTH, AND ORTHOGONALITY.
The Fourier series A large class of phenomena can be described as periodic in nature: waves, sounds, light, radio, water waves etc. It is natural to attempt.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 14 Instructor: Paul Beame.
VECTORS AND THE GEOMETRY OF SPACE Vectors VECTORS AND THE GEOMETRY OF SPACE In this section, we will learn about: Vectors and their applications.
Copyright © Cengage Learning. All rights reserved. 1.8 Coordinate Geometry.
Fast Fourier Transform Irina Bobkova. Overview I. Polynomials II. The DFT and FFT III. Efficient implementations IV. Some problems.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Applied Discrete Mathematics Week 9: Relations
Systems and Matrices (Chapter5)
Chapter 8 Review Quadratic Functions.
ME 1202: Linear Algebra & Ordinary Differential Equations (ODEs)
MCA 202: Discrete Mathematics Instructor Neelima Gupta
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Vectors and the Geometry of Space 9. Vectors 9.2.
Copyright © Cengage Learning. All rights reserved. 12 Vectors and the Geometry of Space.
5 -1 Chapter 5 The Divide-and-Conquer Strategy A simple example finding the maximum of a set S of n numbers.
FFT1 The Fast Fourier Transform. FFT2 Outline and Reading Polynomial Multiplication Problem Primitive Roots of Unity (§10.4.1) The Discrete Fourier Transform.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
5.6 Convolution and FFT. 2 Fast Fourier Transform: Applications Applications. n Optics, acoustics, quantum physics, telecommunications, control systems,
The Fast Fourier Transform
MCA 202: Discrete Structures Instructor Neelima Gupta
Karatsuba’s Algorithm for Integer Multiplication
Chapter 5 Eigenvalues and Eigenvectors 大葉大學 資訊工程系 黃鈴玲 Linear Algebra.
Precalculus Fifth Edition Mathematics for Calculus James Stewart Lothar Redlin Saleem Watson.
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Applied Symbolic Computation1 Applied Symbolic Computation (CS 300) Karatsuba’s Algorithm for Integer Multiplication Jeremy R. Johnson.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
1 The Coordinate Plane Just as points on a line can be identified with real numbers to form the coordinate line, points in a plane can be identified with.
Applied Symbolic Computation1 Applied Symbolic Computation (CS 567) The Fast Fourier Transform (FFT) and Convolution Jeremy R. Johnson TexPoint fonts used.
May 9, 2001Applied Symbolic Computation1 Applied Symbolic Computation (CS 680/480) Lecture 6: Multiplication, Interpolation, and the Chinese Remainder.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
CS 361 – Chapter 11 Divide and Conquer ! Examples: –Merge sort √ –Ranking inversions –Matrix multiplication –Closest pair of points Master theorem –A shortcut.
Advanced Algorithms Analysis and Design
Modeling with Recurrence Relations
Chapter 2 Divide-and-Conquer algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Polynomial + Fast Fourier Transform
Applied Symbolic Computation
CSCE 411 Design and Analysis of Algorithms
DFT and FFT By using the complex roots of unity, we can evaluate and interpolate a polynomial in O(n lg n) An example, here are the solutions to 8 =
Punya Biswas Lecture 15 Closest Pair, Multiplication
Advanced Algorithms Analysis and Design
Applied Symbolic Computation
Topic: Divide and Conquer
Copyright © Cengage Learning. All rights reserved.
CSCI 256 Data Structures and Algorithm Analysis Lecture 12
Lecture 15, Winter 2019 Closest Pair, Multiplication
Applied Symbolic Computation
Lecture 15 Closest Pair, Multiplication
Presentation transcript:

Instructor Neelima Gupta

Table of Contents Divide and Conquer

Divide the problem into smaller sub-problems. Solve the sub-problems recursively Combine the solution of the smaller sub-problems to obtain the solution of the bigger problem. Size of subproblems must reduce There must be some initial conditions The two together ensures that the algorithm terminates

Familiar Egs of divide and conquer Quick sort Merge sort (John von Neumann, 1945)

Time If T(n) is the time to sort ‘n’ numbers :- T(n)=T(n/2)+T(n/2)+ Ө (n) =2T(n/2) + cn = Ө (nlog n) Where Ө (n )= time required to merge two sorted lists, each of size at most n/2

Multiplying 2 large integers Suppose for simplicity, numbers are of equal length Eg- suppose the 2 number are 2345 and 3854 O(n 2 ) time by the usual successive add algorithm. We can reduce this time using divide and conquer strategy.

Multiplying 2 large integers split the numbers into roughly 2 halves Here x 0 =45; x 1 =23; y 0 =54; y 1 =38;

Mathematically x= x 1.10 n/2 + x 0 (1) y= y 1.10 n/2 + y 0 (2) xy=x 1 y 1.10 n +(x 1 y 0 +y 1 x 0 ).10 n/2 +x 0 y 0 (3) In our eg n=4 x=23* y=38*

x 1 y 1 =874;x 1 y 0 =1242;x 0 y 1 =1710;x 0 y 0 =2430; Using (1),(2),(3) answer came out to be = 874*10 4 +(2952)* adding these numbers take linear time.

Time Analysis Computing x 1 y 1,(x 1 y 0 +y 1 x 0 ),x 0 y 0 T(n)=4T(n/2)+cn = Ө (n 2 ) So, no improvement. We’ll use a smarter way to compute the middle term.

(x 0 +x 1 )(y 0 +y 1 )=x 1 y 1 +(x 1 y 0 +y 1 x 0 )+x 0 y 0 Thus, x 1 y 0 +y 1 x 0 = (x 0 +x 1 )(y 0 +y 1 ) - x 0 y 0 - x 1 y 1 Thus, only 3 multiplications suffice

So T(n)is reduced to T(n)=3T(n/2)+cn = Ө (n log 2 3 )< Ө (n 2 ) where 3T(n/2) is the time required to multiply x 1 y 1, (x 0 +x 1 )(y 0 +y 1 ), x 0 y 0

Complex Roots of Unity Consider x n = 1 It has n distinct complex roots as follows: ω j,n = e (2 π ij)/n, j = 1 to n – 1 where i = √-1 These numbers are called n th roots of unity

n th roots of unity For example, for n = 2 x 2 = 1 => x = +1, -1 e (2 π ij)/n, j = 0,1 j = 0 e (2 π ij)/n = e 0 = cos 0 + i sin 0 = 1 j = 1 e (2 π i)/2 = e π i = cos π + i sin π = -1 Thanks to : Megha and Mitul

These can be pictured as a set of equally spaced points lying on a unit circle as shown in figure for n = 8. Figure by Mitul If ω j,2n ( j = 0, 1 … 2n-1) are (2n) th roots of unity then clearly ω 2 j,2n ( j = 0, 1 … n-1) are n th roots of unity. For j = n … 2n -1, roots repeat. ω j,2n = e (2 π ij)/2n = e (π ij)/n ω j,2n 2 = (e (π ij)/n ) 2 = e (2 π ij)/n It is also visible from the figure below Figure by Mitul

Discrete Fourier Transform DFT of a polynomial with coefficient vector is the vector y = where Θ(n 2 ) time to compute DFT is trivial, each y i can be computed in Θ(n) time. FFT is a method to compute DFT in Θ(n log n) time, It makes use of special properties of complex roots of unity.

Fast Fourier Transform Instead of n, we will deal with 2n Break the polynomial in 2 parts : A even (x) = a 0 + a 2 x + … + a n-2 x (n-2)/2 A odd (x) = a 1 + a 3 x + a 5 x 2 + ….+ a n-1 x (n-2)/2 A(x) = A even (x 2 ) + x.A odd (x 2 ) Let p(x) = A even (x 2 ) = a 0 +a 2 x 2 + a 4 x 4 +….+ a n-2 x n-2 and q(x) = A odd (x 2 ) + a 1 + a 3 x 2 + a 5 x 4 +….+a n-1 x n-2 x.q(x) = a 1 x + a 3 x 3 + a 5 x 5 +…+a n-1 x n-1 Thanks to : Megha and Mitul

Fast Fourier Transform contd… Thus, y j = A(ω j,2n ) = A even (ω 2 j,2n ) + ω j,2n.A odd (ω 2 j,2n ) (2n) th root of unityn th root of unity A even and A odd are polynomials with n terms, thus they can be computed at n th roots of unity, recursively.

FFT contd.. Thus we arrive at the following recurrence to compute the DFT T(n) = 2T(n/2) + O(n) = n log n Thus given a polynomial, its DFT can be computed in O(n log n) time.

Vandermonde Matrix Computing DFT is equivalent to n is only a control variable and can be replaced by 2n Since DFT can be computed in O(n log n) time, this matrix-vector product can be computed in O(n log n) time. Vandermonde matrix

Computing DFT -1 Theorem:For j,k = 0 …2n -1, (j,k) entry of V -1 2n is ω -k j,2n /2n Thus, given y i ‘s, a i ‘s and hence the polynomial can be computed as follows : As before n can be replaced by 2n. This matrix-vector multiplication is similar to the previous one and hence can be computed in O(n log n) time.

Convolution Let A = and B = be 2 vectors C = A o B = ( Length = 2n -1) C k = ∑ i+j = k a i b j AIM : to obtain convolution in (nlog(n)) time Thanks to : Megha and Mitul

Applications: polynomial multiplication Suppose we have two polynomials A(x) = a 0 + a 1 x + ………+ a n-1 x n-1 B(x) = b 0 + b 1 x + ………+ b n-1 x n-1 Then, C(x) = A(x).B(x)

Algortihm for AoB Let A(x) and B(x) be two polynomials with coefficients from the vectors A and B respectively. 1. Compute A and B at (2n)th roots of unity i.e. compute A(ω j,2n ) and B(ω j,2n ), j = 0,1 …2n-1 using FFT. O(n log n) time. 2. Compute C(ω j,2n ) = A(ω j,2n ). B(ω j,2n ) …. O(n) time. 3. Reconstruct C(x) using FFT -1 ….O(n log n) time.

Submitted By: Jewel Pruthi(18) Juhi Jain(19)

Problem : Given a set of points p 1,p 2, p n, our aim is to find out the closest pair of points. Finding Closest Pair in one-dimension Complexity – O(nlogn) How? Thanks to Jewel and Juhi

Sort the points.(takes O(nlogn) time) Find distance between every pair of consecutive points. In n comparisons,we will find the distance between every pair of consecutive points. In another n comparisons,we will find minimum of the distances found. Thanks to Jewel and Juhi

Finding Closest Pair in two- dimension Proposed algorithm:Sort the points p 1,p 2,------p n say on increasing order of x-coordinates. where p i =(x i,y i ) Distance between 2 consecutive points d=sqrt((y 2 -y 1 ) 2 + (x 2 -x 1 ) 2 ) st x 1 ≤x 2 ≤x ≤x n Ques: Will this algorithm work? Thanks to Jewel and Juhi

No Consider p 1,p 2,p 3 st d(p 1 p 2 ) > d(p 2 p 3 ) after sorting p 1,p 2,p 3 on x-coordinates. Algorithm returns p 2 p 3 but we can see that p 1 p 3 are closer. Thanks to Jewel and Juhi p1p1 p2 p2 p3p3

Note : For minimum distance,the two points need not be consecutive on x/y-axis. Brute Force Approach Calculate distance between every possible pair of points. Time Complexity:- O(n 2 ) Can we improve on the time complexity? Thanks to Jewel and Juhi

Divide and Conquer Approach Arrange the points in increasing order of x-coordinates say P x & increasing order of y-coordinates say P y. Divide the set of n points into 2 halves Q and R (breaking on middle of P x ). Compute the closest pair in Q and in R recursively. Thanks to Jewel and Juhi Solve recursively Q R

Let (q 1,q 2 ) and (r 1,r 2 ) be the closest pairs obtained in Q and R respectively. Let δ=min { d(q 1,q 2 ), d(r 1,r 2 ) } Thanks to Jewel and Juhi Solve recursively Q R (q 1,q 2 )(r 1,r 2 )

Ques: Does Ǝ a pair of points say (s 1,s 2 ) such that d(s 1,s 2 ) < δ ? Soln: Let p* be the point with maximum x-coordinate in Q and let x* be its x-coordinate. Draw a line through p* described by the equation L : x=x* Thanks to Jewel and Juhi

Thanks to Jewel and Juhi p* δ δ L x* q(q x ) r(r x ) Q R This distance is x*-q x Now in the highlighted triangle we can see that the length of horizontal line (x*-q x ) < hypotenuse according to Pythagoras theorem.  x*-q x <hypotenuse<d(q,r)< δ Similarly we can prove that r x -x* < d(q,r) < δ

Consider square boxes each of side δ/2 in this vertical strip. Thanks to Jewel and Juhi δ/2 S qyqy ryry L Square of side δ/2

Claim : No box contains more than 1 point. Proof: If the 2 farthest points in the box are the 2 diagonal points of the square, then 1. Distance between the 2 points = √( (δ/2) 2 + (δ/2) 2 ) = (δ/√2) < δ 2. Both the points are within Q or both are within R This implies that we have two points within Q (/R) with distance < δ which is a contradiction to the definition of δ. Thanks to Jewel and Juhi

Claim : Between any pair of 2 points q and r in S with d(q,r) < δ, there can be no more than 12 points. Thanks to Jewel and Juhi No of points ≤ 12 ṕ 1 ṕ 2 ṕ k ṕ r

Proof: Let P y1 … P yt be the points of S in the increasing order of y co-ordinates. If there are more than 12 points between q and r in the above list then there will be at least 3 rows between the y co-ordinate of q and y co-ordinate of r. Then, the vertical distance between q and r is > 3 × (δ/2) > δ Thus, the actual distance which is > vertical distance (show through fig.) > δ ---Contradiction to the definition of points q and r. Thanks to Jewel and Juhi

For i = 1 to t For j = i+1 to i + 12 Compute d(P yi, P yj ) Compute the minimum of the 12t pairs above --- O(n) time.

THANKYOU