Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Algorithms Piyush Kumar (Lecture 11: Div & Conquer) Welcome to COT5405 Source: Kevin Wayne, Harold Prokop.

Similar presentations


Presentation on theme: "Advanced Algorithms Piyush Kumar (Lecture 11: Div & Conquer) Welcome to COT5405 Source: Kevin Wayne, Harold Prokop."— Presentation transcript:

1 Advanced Algorithms Piyush Kumar (Lecture 11: Div & Conquer) Welcome to COT5405 Source: Kevin Wayne, Harold Prokop.

2 Divide and Conquer Inversions Closest Point problems Integer multiplication Matrix Multiplication Cache Aware algorithms/ Cache oblivious Algorithms Static Van Emde Boas Layout

3 5.3 Counting Inversions

4 Music site tries to match your song preferences with others. –You rank n songs. –Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings. –My rank: 1, 2, …, n. –Your rank: a 1, a 2, …, a n. –Songs i and j inverted if i a j. Brute force: check all  (n 2 ) pairs i and j. You Me 14325 13245 ABCDE Songs Counting Inversions Inversions 3-2, 4-2

5 Applications. –Voting theory. –Collaborative filtering. –Measuring the "sortedness" of an array. –Sensitivity analysis of Google's ranking function. –Rank aggregation for meta-searching on the Web. –Nonparametric statistics (e.g., Kendall's Tau distance).

6 Counting Inversions: Divide-and-Conquer Divide-and-conquer. 481021512113769

7 Counting Inversions: Divide-and-Conquer Divide-and-conquer. –Divide: separate list into two pieces. 481021512113769 481021512113769 Divide: O(1).

8 Counting Inversions: Divide-and-Conquer Divide-and-conquer. –Divide: separate list into two pieces. –Conquer: recursively count inversions in each half. 481021512113769 481021512113769 5 blue-blue inversions8 green-green inversions Divide: O(1). Conquer: 2T(n / 2) 5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7

9 Counting Inversions: Divide-and-Conquer Divide-and-conquer. –Divide: separate list into two pieces. –Conquer: recursively count inversions in each half. –Combine: count inversions where a i and a j are in different halves, and return sum of three quantities. 481021512113769 481021512113769 5 blue-blue inversions8 green-green inversions Divide: O(1). Conquer: 2T(n / 2) Combine: ??? 9 blue-green inversions 5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7 Total = 5 + 8 + 9 = 22.

10 13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0 Counting Inversions: Combine Combine: count blue-green inversions –Assume each half is sorted. –Count inversions where a i and a j are in different halves. –Merge two sorted halves into sorted whole. Count: O(n) Merge: O(n) 101418193716172325211 710111423181923251617 6322 0 0 to maintain sorted invariant

11 Counting Inversions: Implementation Pre-condition. [Merge-and-Count] A and B are sorted. Post-condition. [Sort-and-Count] L is sorted. Sort-and-Count(L) { if list L has one element return 0 and the list L Divide the list into two halves A and B (r A, A)  Sort-and-Count(A) (r B, B)  Sort-and-Count(B) (r B, L)  Merge-and-Count(A, B) return r = r A + r B + r and the sorted list L }

12 12 5.4 Closest Pair of Points

13 Closest Pair of Points Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive. –Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. –Special case of nearest neighbor, Euclidean MST, Voronoi. Brute force. Check all pairs of points p and q with  (n 2 ) comparisons. 1-D version. O(n log n) easy if points are on a line. Assumption. No two points have same x coordinate. to make presentation cleaner fast closest pair inspired fast algorithms for these problems

14 Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. L

15 Closest Pair of Points: First Attempt Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece. L

16 Closest Pair of Points Algorithm. –Divide: draw vertical line L so that roughly ½n points on each side. L

17 Closest Pair of Points Algorithm. –Divide: draw vertical line L so that roughly ½n points on each side. –Conquer: find closest pair in each side recursively. 12 21 L

18 Closest Pair of Points Algorithm. –Divide: draw vertical line L so that roughly ½n points on each side. –Conquer: find closest pair in each side recursively. –Combine: find closest pair with one point in each side. –Return best of 3 solutions. 12 21 8 L seems like  (n 2 )

19 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . 12 21  = min(12, 21) L

20 Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . –Observation: only need to consider points within  of line L.  12 21 L  = min(12, 21)

21 12 21 1 2 3 4 5 6 7  Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . –Observation: only need to consider points within  of line L. –Sort points in 2  -strip by their y coordinate. L  = min(12, 21)

22 12 21 1 2 3 4 5 6 7  Closest Pair of Points Find closest pair with one point in each side, assuming that distance < . –Observation: only need to consider points within  of line L. –Sort points in 2  -strip by their y coordinate. –Only check distances of those within 11 positions in sorted list! L  = min(12, 21)

23 Closest Pair of Points Def. Let s i be the point in the 2  -strip, with the i th smallest y-coordinate. Claim. If |i – j|  12, then the distance between s i and s j is at least . Pf. –No two points lie in same ½  -by-½  box. –Two points at least 2 rows apart have distance  2(½  ). ▪ Fact. Still true if we replace 12 with 7.  27 29 30 31 28 26 25  ½½ 2 rows ½½ ½½ 39 i j

24 Closest Pair Algorithm Closest-Pair(p 1, …, p n ) { Compute separation line L such that half the points are on one side and half on the other side.  1 = Closest-Pair(left half)  2 = Closest-Pair(right half)  = min(  1,  2 ) Delete all points further than  from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than , update . return . } O(n log n) 2T(n / 2) O(n) O(n log n) O(n)

25 Closest Pair of Points: Analysis Running time. Q. Can we achieve O(n log n)? A. Yes. Don't sort points in strip from scratch each time. –Each recursive returns two lists: all points sorted by y coordinate, and all points sorted by x coordinate. –Sort by merging two pre-sorted lists.

26 26 5.5 Integer Multiplication

27 Integer Arithmetic Add. Given two n-digit integers a and b, compute a + b. –O(n) bit operations. Multiply. Given two n-digit integers a and b, compute a × b. –Brute force solution:  (n 2 ) bit operations. 1 0111 1101+ 0101 111 0101 0111 1000 10111 Add Multiply

28 To multiply two n-digit integers: –Multiply four ½n-digit integers. –Add two ½n-digit integers, and shift to obtain result. Divide-and-Conquer Multiplication: Warmup assumes n is a power of 2

29 To multiply two n-digit integers: –Add two ½n digit integers. –Multiply three ½n-digit integers. –Add, subtract, and shift ½n-digit integers to obtain result. Theorem. [Karatsuba-Ofman, 1962] Can multiply two n-digit integers in O(n 1.585 ) bit operations. Karatsuba Multiplication AB C A C

30 Karatsuba: Recursion Tree n 3(n/2) 9(n/4) 3 k (n / 2 k ) 3 lg n (2)... T(n) T(n/2) T(n/4) T(2) T(n / 2 k ) T(n/4) T(n/2) T(n/4) T(n/2) T(n/4)...

31 31 Matrix Multiplication

32 Matrix Multiplication (MM) ∑   n k kjikij bac 1 CBA = × Courtesy Harold Prokop

33 Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB. Brute force.  (n 3 ) arithmetic operations. Fundamental question. Can we improve upon brute force? Matrix Multiplication

34

35

36 Matrix Multiplication: Warmup Divide-and-conquer. –Divide: partition A and B into ½n-by-½n blocks. –Conquer: multiply 8 ½n-by-½n recursively. –Combine: add appropriate products using 4 matrix additions.

37 Matrix Multiplication: Key Idea Key idea. multiply 2-by-2 block matrices with only 7 multiplications. –7 multiplications. –18 = 10 + 8 additions (or subtractions).

38 Fast Matrix Multiplication Fast matrix multiplication. (Strassen, 1969) –Divide: partition A and B into ½n-by-½n blocks. –Compute: 14 ½n-by-½n matrices via 10 matrix additions. –Conquer: multiply 7 ½n-by-½n matrices recursively. –Combine: 7 products into 4 terms using 8 matrix additions. Analysis. –Assume n is a power of 2. –T(n) = # arithmetic operations.

39 Fast Matrix Multiplication in Practice Implementation issues. –Sparsity. –Caching effects. –Numerical stability. –Odd matrix dimensions. –Crossover to classical algorithm around n = 128. Common misperception: "Strassen is only a theoretical curiosity." –Advanced Computation Group at Apple Computer reports 8x speedup on G4 Velocity Engine when n ~ 2,500. –Range of instances where it's useful is a subject of controversy. Remark. Can "Strassenize" Ax=b, determinant, eigenvalues, and other matrix ops.

40 Fast Matrix Multiplication in Theory Q. Multiply two 2-by-2 matrices with only 7 scalar multiplications? A. Yes! [Strassen, 1969] Q. Multiply two 2-by-2 matrices with only 6 scalar multiplications? A. Impossible. [Hopcroft and Kerr, 1971] Q. Two 3-by-3 matrices with only 21 scalar multiplications? A. Also impossible. Q. Two 70-by-70 matrices with only 143,640 scalar multiplications? A. Yes! [Pan, 1980] Decimal wars. –December, 1979: O(n 2.521813 ). –January, 1980: O(n 2.521801 ).

41 Fast Matrix Multiplication in Theory Best known. O(n 2.376 ) [Coppersmith-Winograd, 1987.] Conjecture. O(n 2+  ) for any  > 0. Caveat. Theoretical improvements to Strassen are progressively less practical.

42 B LOCK -M ULT ( A, B, C, n ) 1 for i  1 to n / s 2 dofor j  1 to n / s 3 dofor k  1 to n / s 4 do O RD -M ULT ( A ik, B kj, C ij, s ) B LOCK -M ULT ( A, B, C, n ) 1 for i  1 to n / s 2 dofor j  1 to n / s 3 dofor k  1 to n / s 4 do O RD -M ULT ( A ik, B kj, C ij, s ) Cache - - Aware MM s s n n [HK81] s s n n

43 Towards faster matrix multiplication… (Blocked version) Advantages –Exploit locality using blocking –Do not assume that each access to memory is O(1) –Can be extended to multiple levels of cache –Usually the fastest algorithm after tuning. Disadvantages –Needs tuning every time it runs on a new machine. –Usually “s” is a voodoo parameter that is unknown.

44 © Harald Prokop 18 Oct 9910 B LOCK -M ULT ( A, B, C, n ) 1 for i  1 to n / s 2 dofor j  1 to n / s 3 dofor k  1 to n / s 4 do O RD -M ULT ( A ik, B kj, C ij, s ) B LOCK -M ULT ( A, B, C, n ) 1 for i  1 to n / s 2 dofor j  1 to n / s 3 dofor k  1 to n / s 4 do O RD -M ULT ( A ik, B kj, C ij, s ) Voodoo! Cache - - Aware Matrix Multiplication s s n n Tune s so that A ik, B kj, and C ij just fit into cache ?  Zs    . )()( 3 23 ZLn LssnnQ   If n > s, then Optimal[HK81].

45

46 Cache Oblivious

47

48 Algorithms for External Memory

49 P M Computer Secondary Storage (Hard Disks)... C P = Processor C = Cache M = Main Memory

50 Storage Capacity 10 -9 10 -6 10 -3 1 10 3 access time (sec) 10 15 10 13 10 11 10 9 10 7 10 5 10 3 cache electronic main electronic secondary magnetic optical disks online tape nearline tape & optical disks offline tape typical capacity (bytes) from Gray & Reuter (2002)

51 Storage Cost 10 -9 10 -6 10 -3 1 10 3 access time (sec) 10 4 10 2 10 0 10 -2 10 -4 cache electronic main electronic secondary magnetic optical disks online tape nearline tape & optical disks offline tape dollars/MB from Gray & Reuter (2002)

52 Disk Access Time Block X Request  Block in Memory Time = Seek Time + Rotational Delay + Transfer Time + Other Typical Numbers (for random block access) Seek Time = 10ms Rot Delay = 4ms (7200rpm) transfer rate = 50MB/sec Other = CPU time to access delays+ Contention for controllers Contention for Bus Multiple copies of the same data

53 Rules for EM Algorithms Sequential IO is cheap compared to Random IO 1kb block –Seq : 1ms –Random : 20ms The difference becomes smaller and smaller as the block size becomes larger.

54 The DAM Model Count the number of IOs. Explicitly control which Blocks are in memory. Two level memory model. Notation: M = Size of memory. B = size of disk block. N = size of data. Question: How many block transfers for one scan of the data set?

55 Problem Mergesort –How many Block IOs does it need to sort N numbers (when the size of the data is extremely large compared to M) Can we do better?

56 External Memory Sorting Penny Sort Competition 40GB, 433 million records, 1541 seconds on a 614$ Linux/AMD system

57 EM Sorting Two pass external memory merge sort. run 0 input : O(M) chunk 0chunk 1chunk 2chunk 3chunk 4 run 1run 2run n chunk 5chunk 6 … B buffers BBB k=O(M/B) k-merger chunk 7chunk 8 run 0123 B … … …

58 The CO Memory Model Cache-oblivious memory model –Reason about two-level, but prove results for unknown multilevel memory models –Parameters B and M are unknown, thus optimized for all levels of memory hierarchy B = L, M = Z?

59 Matrix Transpose: DAM n CO What is the number of blocks you need to move in a transpose of a large matrix? –In DAM –In CO

60 Static Searches Only for balanced binary trees Assume there are no insertions and deletions Only searches Can we speed up such seaches?

61 What is a layout? Mapping of nodes of a tree to the Memory Different kinds of layouts –In-order –Post-order –Pre-order –Van Emde Boas Main Idea : Store Recursive subtrees in contiguous memory

62 Example of Van Emde Boas

63 Another View

64 Theoretical Guarantees? Cache Complexity Q(n) = O( ) Work Complexity W(n) = O(log n) From Prokop’s Thesis

65 In Practice??

66 In Practice II

67 In Practice! Matrix Operations by Morton Ordering, David S.Wise (Cache oblivious Practical Matrix operation results) Bender, Duan, Wu (Cache oblivious dictionaries) Rahman, Cole, Raman (CO B-Trees)

68 Known Optimal Results Matrix Multiplication Matrix Transpose n-point FFT LUP Decomposition Sorting Searching

69 Other Results Known Priority Q List Ranking Tree Algos Directed BFS/DFS Undirected BFS MSF

70 Introduction to Streaming Algorithms

71 Brain Teaser Let P = { 1...n }. Let P’ = P \ {x} –x in P Paul shows Carole elements from P’ Carole can only use O(log n) bits memory to answer the question in the end. Now what about P’’?

72 Streaming Algorithms Data that computers are being asked to process is growing astronomically. Infeasible to store If I cant store the data I am looking at, how do I compute a summary of this data?

73 Another Brain Teaser Given a set of numbers in a large array. In one pass, decide if some item is in majority (assume you only have constant size memory to work with). 939994679992 N = 12; item 9 is majority Courtesy : Subhash Suri

74 Misra-Gries Algorithm (‘82) A counter and an ID. –If new item is same as stored ID, increment counter. –Otherwise, decrement the counter. –If counter 0, store new item with count = 1. If counter > 0, then its item is the only candidate for majority. 299976499939 ID229999449999 count101210101212

75 Data Stream Algorithms Majority and Frequent are examples of data stream algorithms. Data arrives as an online sequence x 1, x 2, …, potentially infinite. Algorithm processes data in one pass (in given order) Algorithm’s memory is significantly smaller than input data Summarize the data: compute useful patterns

76 Streaming Data Sources –Internet traffic monitoring –New Computer Graphics hardware –Web logs and click streams –Financial and stock market data –Retail and credit card transactions –Telecom calling records –Sensor networks, surveillance –RFID –Instruction profiling in microprocessors –Data warehouses (random access too expensive).

77 FFTFFT

78 Fast Fourier Transform: Applications Applications. –Optics, acoustics, quantum physics, telecommunications, control systems, signal processing, speech recognition, data compression, image processing. –DVD, JPEG, MP3, MRI, CAT scan. –Numerical solutions to Poisson's equation. The FFT is one of the truly great computational developments of this [20th] century. It has changed the face of science and engineering so much that it is not an exaggeration to say that life as we know it would be very different without the FFT. -Charles van Loan

79 Fast Fourier Transform: Brief History Gauss (1805, 1866). Analyzed periodic motion of asteroid Ceres. Runge-König (1924). Laid theoretical groundwork. Danielson-Lanczos (1942). Efficient algorithm. Cooley-Tukey (1965). Monitoring nuclear tests in Soviet Union and tracking submarines. Rediscovered and popularized FFT. Importance not fully realized until advent of digital computers.

80 Polynomials: Coefficient Representation Polynomial. [coefficient representation] Add: O(n) arithmetic operations. Evaluate: O(n) using Horner's method. Multiply (convolve): O(n 2 ) using brute force.

81 Polynomials: Point-Value Representation Fundamental theorem of algebra. [Gauss, PhD thesis] A degree n polynomial with complex coefficients has n complex roots. Corollary. A degree n-1 polynomial A(x) is uniquely specified by its evaluation at n distinct values of x. x y xjxj y j = A(x j )

82 Polynomials: Point-Value Representation Polynomial. [point-value representation] Add: O(n) arithmetic operations. Multiply: O(n), but need 2n-1 points. Evaluate: O(n 2 ) using Lagrange's formula.

83 Converting Between Two Polynomial Representations Tradeoff. Fast evaluation or fast multiplication. We want both! Goal. Make all ops fast by efficiently converting between two representations. Coefficient Representation O(n 2 ) Multiply O(n) Evaluate Point-valueO(n)O(n 2 ) coefficient representation point-value representation

84 Converting Between Two Polynomial Representations: Brute Force Coefficient to point-value. Given a polynomial a 0 + a 1 x +... + a n-1 x n-1, evaluate it at n distinct points x 0,..., x n-1. Point-value to coefficient. Given n distinct points x 0,..., x n-1 and values y 0,..., y n-1, find unique polynomial a 0 + a 1 x +... + a n-1 x n-1 that has given values at given points. Vandermonde matrix is invertible iff x i distinct O(n 3 ) for Gaussian elimination O(n 2 ) for matrix-vector multiply

85 Coefficient to Point-Value Representation: Intuition Coefficient to point-value. Given a polynomial a 0 + a 1 x +... + a n-1 x n- 1, evaluate it at n distinct points x 0,..., x n-1. Divide. Break polynomial up into even and odd powers. –A(x) = a 0 + a 1 x + a 2 x 2 + a 3 x 3 + a 4 x 4 + a 5 x 5 + a 6 x 6 + a 7 x 7. –A even (x) = a 0 + a 2 x + a 4 x 2 + a 6 x 3. –A odd (x) = a 1 + a 3 x + a 5 x 2 + a 7 x 3. –A(-x) = A even (x 2 ) + x A odd (x 2 ). –A(-x) = A even (x 2 ) - x A odd (x 2 ). Intuition. Choose two points to be  1. –A(-1) = A even (1) + 1 A odd (1). –A(-1) = A even (1) - 1 A odd (1). Can evaluate polynomial of degree  n at 2 points by evaluating two polynomials of degree  ½n at 1 point.

86 Coefficient to Point-Value Representation: Intuition Coefficient to point-value. Given a polynomial a 0 + a 1 x +... + a n-1 x n- 1, evaluate it at n distinct points x 0,..., x n-1. Divide. Break polynomial up into even and odd powers. –A(x) = a 0 + a 1 x + a 2 x 2 + a 3 x 3 + a 4 x 4 + a 5 x 5 + a 6 x 6 + a 7 x 7. –A even (x) = a 0 + a 2 x + a 4 x 2 + a 6 x 3. –A odd (x) = a 1 + a 3 x + a 5 x 2 + a 7 x 3. –A(-x) = A even (x 2 ) + x A odd (x 2 ). –A(-x) = A even (x 2 ) - x A odd (x 2 ). Intuition. Choose four points to be  1,  i. –A(-1) = A even (-1) + 1 A odd ( 1). –A(-1) = A even (-1) - 1 A odd (-1). –A(-i) = A even (-1) + i A odd (-1). –A(-i) = A even (-1) - i A odd (-1). Can evaluate polynomial of degree  n at 4 points by evaluating two polynomials of degree  ½n at 2 points.

87 Discrete Fourier Transform Coefficient to point-value. Given a polynomial a 0 + a 1 x +... + a n-1 x n- 1, evaluate it at n distinct points x 0,..., x n-1. Key idea: choose x k =  k where  is principal n th root of unity. Discrete Fourier transform Fourier matrix F n

88 Roots of Unity Def. An n th root of unity is a complex number x such that x n = 1. Fact. The n th roots of unity are:  0,  1, …,  n-1 where  = e 2  i / n. Pf. (  k ) n = (e 2  i k / n ) n = (e  i ) 2k = (-1) 2k = 1. Fact. The ½n th roots of unity are: 0, 1, …, n/2-1 where = e 4  i / n. Fact.  2 = and (  2 ) k = k.  0 = 0 = 1 11  2 = 1 = i 33  4 = 2 = -1 55  6 = 3 = -i 77 n = 8

89 Fast Fourier Transform Goal. Evaluate a degree n-1 polynomial A(x) = a 0 +... + a n-1 x n-1 at its n th roots of unity:  0,  1, …,  n-1. Divide. Break polynomial up into even and odd powers. –A even (x) = a 0 + a 2 x + a 4 x 2 + … + a n/2-2 x (n-1)/2. –A odd (x) = a 1 + a 3 x + a 5 x 2 + … + a n/2-1 x (n-1)/2. –A(x) = A even (x 2 ) + x A odd (x 2 ). Conquer. Evaluate degree A even (x) and A odd (x) at the ½n th roots of unity: 0, 1, …, n/2-1. Combine. –A(  k+n ) = A even ( k ) +  k A odd ( k ), 0  k < n/2 –A(  k+n/2 ) = A even ( k ) -  k A odd ( k ), 0  k < n/2  k+n/2 = -  k k = (  k ) 2 = (  k+n/2 ) 2

90 fft(n, a 0,a 1,…,a n-1 ) { if (n == 1) return a 0 (e 0,e 1,…,e n/2-1 )  FFT(n/2, a 0,a 2,a 4,…,a n-2 ) (d 0,d 1,…,d n/2-1 )  FFT(n/2, a 1,a 3,a 5,…,a n-1 ) for k = 0 to n/2 - 1 {  k  e 2  ik/n y k+n/2  e k +  k d k y k+n/2  e k -  k d k } return (y 0,y 1,…,y n-1 ) } FFT Algorithm

91 FFT Summary Theorem. FFT algorithm evaluates a degree n-1 polynomial at each of the n th roots of unity in O(n log n) steps. Running time. T(2n) = 2T(n) + O(n)  T(n) = O(n log n). assumes n is a power of 2 O(n log n) coefficient representation point-value representation

92 Point-Value to Coefficient Representation: Inverse DFT Goal. Given the values y 0,..., y n-1 of a degree n-1 polynomial at the n points  0,  1, …,  n-1, find unique polynomial a 0 + a 1 x +... + a n-1 x n-1 that has given values at given points. Inverse DFT Fourier matrix inverse (F n ) -1

93 Claim. Inverse of Fourier matrix is given by following formula. Consequence. To compute inverse FFT, apply same algorithm but use  -1 = e -2  i / n as principal n th root of unity (and divide by n). Inverse FFT

94 Inverse FFT: Proof of Correctness Claim. F n and G n are inverses. Pf. Summation lemma. Let  be a principal n th root of unity. Then Pf. –If k is a multiple of n then  k = 1  sums to n. –Each n th root of unity  k is a root of x n - 1 = (x - 1) (1 + x + x 2 +... + x n-1 ). –if  k  1 we have: 1 +  k +  k(2) +... +  k(n-1) = 0  sums to 0. ▪ summation lemma

95 Inverse FFT: Algorithm ifft(n, a 0,a 1,…,a n-1 ) { if (n == 1) return a 0 (e 0,e 1,…,e n/2-1 )  FFT(n/2, a 0,a 2,a 4,…,a n-2 ) (d 0,d 1,…,d n/2-1 )  FFT(n/2, a 1,a 3,a 5,…,a n-1 ) for k = 0 to n/2 - 1 {  k  e -2  ik/n y k+n/2  (e k +  k d k ) / n y k+n/2  (e k -  k d k ) / n } return (y 0,y 1,…,y n-1 ) }

96 Inverse FFT Summary Theorem. Inverse FFT algorithm interpolates a degree n-1 polynomial given values at each of the n th roots of unity in O(n log n) steps. assumes n is a power of 2 O(n log n) coefficient representation O(n log n) point-value representation

97 Polynomial Multiplication Theorem. Can multiply two degree n-1 polynomials in O(n log n) steps. O(n) point-value multiplication O(n log n) FFT inverse FFT O(n log n) coefficient representation

98 FFT in Practice Fastest Fourier transform in the West. [Frigo and Johnson] –Optimized C library. –Features: DFT, DCT, real, complex, any size, any dimension. –Won 1999 Wilkinson Prize for Numerical Software. –Portable, competitive with vendor-tuned code. Implementation details. –Instead of executing predetermined algorithm, it evaluates your hardware and uses a special-purpose compiler to generate an optimized algorithm catered to "shape" of the problem. –Core algorithm is nonrecursive version of Cooley-Tukey radix 2 FFT. –O(n log n), even for prime sizes. Reference: http://www.fftw.org

99 More on FFT 0,4,2,6,1,5,3,7

100 FFT

101 Parallel FFT a 0 y 0 a 1 y 1 a 2 y 2 a 3 y 3 a 4 y 4 a 5 y 5 a 6 y 6 a 7 y 7

102 Integer Multiplication Integer multiplication. Given two n bit integers a = a n-1 … a 1 a 0 and b = b n-1 … b 1 b 0, compute their product c = a  b. Convolution algorithm. –Form two polynomials. –Note: a = A(2), b = B(2). –Compute C(x) = A(x)  B(x). –Evaluate C(2) = a  b. –Running time: O(n log n) complex arithmetic steps. Theory. [Schönhage-Strassen 1971] O(n log n log log n) bit operations. Practice. [GNU Multiple Precision Arithmetic Library] GMP proclaims to be "the fastest bignum library on the planet." It uses brute force, Karatsuba, and FFT, depending on the size of n.


Download ppt "Advanced Algorithms Piyush Kumar (Lecture 11: Div & Conquer) Welcome to COT5405 Source: Kevin Wayne, Harold Prokop."

Similar presentations


Ads by Google