CSCE 411 Design and Analysis of Algorithms

Similar documents
CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Divide-and-Conquer. Dr. Yingwu Zhu

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

CSCE 411 Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

Geometric Algorithms. 1. Objects

Divide and Conquer. Algorithm Fall Semester

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Planar convex hulls (I) Computational Geometry [csci 3250] Laura Toma Bowdoin College

Convex Hull Algorithms

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Module 2: Classical Algorithm Design Techniques

COMP 3403 Algorithm Analysis Part 2 Chapters 4 5. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

MA/CSSE 473 Day 17. Divide-and-conquer Convex Hull. Strassen's Algorithm: Matrix Multiplication. (if time, Shell's Sort)

CS473 - Algorithms I

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Solutions to the Second Midterm Exam

CPS 616 TRANSFORM-AND-CONQUER 7-1

CS Data Structures and Algorithm Analysis

Algorithms and Data Structures, or

Computational Geometry

Computational geometry

Last time. Today: Convex Hull Brute Force Inside(q,abc) Intersect(ab,cd) Orient(a,b,c).

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

The divide and conquer strategy has three basic parts. For a given problem of size n,

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted.

1 Elementary number theory

Sieving Interior Collinear Points for Convex Hull Algorithms

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Algorithm Design and Analysis

II (Sorting and) Order Statistics

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures

Lecture 8: Mergesort / Quicksort Steven Skiena

CS 410/584, Algorithm Design & Analysis, Lecture Notes 8

Midterm solutions. n f 3 (n) = 3

Advanced Algorithm Homework 4 Results and Solutions

1/60. Geometric Algorithms. Lecture 1: Introduction. Convex Hulls

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

CS 410/584, Algorithm Design & Analysis, Lecture Notes 8!

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

CSE 421 Algorithms: Divide and Conquer

CS Divide and Conquer

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

FINAL EXAM SOLUTIONS

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Algorithms and Data Structures

Lecture 2: Divide and Conquer

Computer Science Spring 2005 Final Examination, May 12, 2005

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

COMP 250 Fall recurrences 2 Oct. 13, 2017

Lecture 4 CS781 February 3, 2011

CS303 (Spring 2008) Solutions to Assignment 7

Caltech Harvey Mudd Mathematics Competition March 3, 2012

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

6.001 Notes: Section 4.1

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

Test 1 Review Questions with Solutions

Unit-2 Divide and conquer 2016

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

Advanced Algorithms Computational Geometry Prof. Karen Daniels. Spring, 2010

Computational Geometry Overview from Cormen, et al.

Recursion Chapter 3.5

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

CS 532: 3D Computer Vision 14 th Set of Notes

Basic Data Structures (Version 7) Name:

COMP 251 Winter 2017 Online quizzes with answers

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

BMO Round 1 Problem 6 Solutions

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Lecture 15 : Review DRAFT

CSE Winter 2015 Quiz 2 Solutions

Algorithms - Ch2 Sorting

Lecture 7. Transform-and-Conquer

Divide and Conquer 1

Copyright 2000, Kevin Wayne 1

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

Multiple-choice (35 pt.)

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

1. To reduce the probability of having any collisions to < 0.5 when hashing n keys, the table should have at least this number of elements.

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Rec 8 Notes. 1 Divide-and-Conquer Convex Hull. 1.1 Algorithm Idea. 1.2 Algorithm. Here s another random Divide-And-Conquer algorithm.

5. DIVIDE AND CONQUER I

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

Simple Sorting Algorithms

CSE 373: Data Structures and Algorithms

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

CS Divide and Conquer

Divide and Conquer 4-0

Transcription:

CSCE 411 Design and Analysis of Algorithms Set 3: Divide and Conquer Slides by Prof. Jennifer Welch Spring 2014 CSCE 411, Spring 2014: Set 3 1

General Idea of Divide & Conquer 1. Take your problem and divide it up into smaller pieces 2. Solve one or more of the smaller problems 3. Combine solutions to subproblems CSCE 411, Spring 2014: Set 3 2

Varieties of Divide & Conquer [Levitin]! Option 1: Only use one of the subproblems! Ex: binary search! Levitin calls this decrease & conquer! Option 2: Use all of the subproblems! Ex: mergesort! Running time for these algorithms can often be stated as a recurrence and solved with the master theorem CSCE 411, Spring 2014: Set 3 3

Varieties of Decrease & Conquer! Decrease by a constant! Ex: insertion sort (subtract 1) Θ(n 2 )! incrementally build up longer and longer prefix of the array of keys that is in sorted order (unsorted suffix shrinks by 1)! take the current key, find correct place in sorted prefix, and shift to make room to insert it! Ex: another algorithm for topological sorting (subtract 1)! identify a source (node with no incoming edges) in the DAG! add this node to the list of nodes and remove all its outgoing edges! repeat until all nodes are removed CSCE 411, Spring 2014: Set 3 4

Varieties of Decrease & Conquer! Decrease by a constant factor Θ(log n)! Ex: binary search (divide by 2)! divide sequence into two halves by comparing search key to midpoint! recursively search in one of the two halves! combine step is empty! Ex: fake coin problem Θ(log n)! Given a set of n coins (n-1 are real and have same weight, 1 is fake and is lighter), find the fake coin! divide set of coins into two piles of floor(n/2) each; if n is odd, there is 1 left over! if the piles weigh the same, the leftover coin is the fake coin! otherwise continue recursively with the lighter pile CSCE 411, Spring 2014: Set 3 5

Varieties of Decrease & Conquer! Decrease by a variable amount! Ex: searching (and inserting) in a binary search tree! compare search key to key in current node and decide to continue search in either left subtree or right subtree, not necessarily same size! Ex: Euclid s algorithm for computing GCD (greatest common divisor)! From about 300 B.C.! Cf. Chapter 31, Section 2 http://etc.usf.edu/clipart CSCE 411, Spring 2014: Set 3 6

Greatest Common Divisor! gcd(a,b) is the largest integer that divides both a and b! Ex: gcd(24,36) = 12! First try: factor a and b into primes and then choose the common ones:! 24 = 2 3 x 3 and 36 = 2 2 x 3 2, so gcd(24,36) = 2 2 x 3 = 12! But factoring is not so easy CSCE 411, Spring 2014: Set 3 7

Euclid s Algorithm! Key insight: gcd(a,b) = gcd(b, a mod b)! a mod b means the remainder when dividing a by b! Ex: gcd(36, 24) = gcd(24, 36 mod 24) = gcd(24, 12) = gcd(12, 24 mod 12) = gcd(12,0) = 12! Why? Next slide CSCE 411, Spring 2014: Set 3 8

GCD Recursion Theorem Proof! Strategy is to show that! gcd(a, b) divides gcd(b, a mod b), and that! gcd(b, a mod b) divides gcd(a, b). So they must be equal.! To show gcd(a,b) divides gcd(b, a mod b):! a mod b = a floor(a/b)*b (remainder after dividing a by b)! gcd(a, b) divides a and b, and so it divides a floor(a,b)*b, which is (a mod b)! since gcd(a, b) divides b and (a mod b), it divides gcd(b, a mod b)! To show gcd(b, a mod b) divides gcd(a, b), use similar argument CSCE 411, Spring 2014: Set 3 9

Euclid s Algorithm! Euclid(a,b) // a and b nonnegative integers! if b == 0 return a! else return Euclid(b, a mod b)! Correct because of previous observation. Also, no infinite loop (why?) CSCE 411, Spring 2014: Set 3 10

Running Time of Euclid s Algorithm! Running time is proportional to the number of recursive calls made! WLOG, assume a > b initially. Then first argument is larger than second in each recursive call.! Show if k 1 recursive calls are done, then a F k+2 and b F k+1.! Fibonacci numbers: F 0 = 0, F 1 = 1, F i = F i-1 + F i-2 for i 2.! Basis: k = 1. Then b 1 = F 2 (since there is at least one recursive call), and a 2 = F 3 (since a > b). CSCE 411, Spring 2014: Set 3 11

Running Time of Euclid s Algorithm! Induction: Euclid(a,b) recursively calls Euclid(b, a mod b), which in turn makes k 1 recursive calls.! By inductive hypothesis, since Euclid(b, a mod b) makes k 1 recursive calls, b F k+1 and (a mod b) F k.! Must show a F k+2, or equivalently F k+2 a:! F k+2 = F k+1 + F k b + (a mod b) = b + (a floor(a/b)*b) a since floor(a/b) is at least 1 CSCE 411, Spring 2014: Set 3 12

Running Time of Euclid s Algorithm! Just showed if it takes k recursive calls, then b F k+1.! Fact: F k+1 is approx. φ k / 5, where φ = (1+ 5)/2 (the golden ratio)! see Ch 3, Sec 2! So b φ k / 5! Solving for k gives: k log φ 5 + log φ b! Thus k = O(log b)! base of logarithm doesn t matter asymptotically and running time is proportional to number of digits in b. CSCE 411, Spring 2014: Set 3 13

Classic Divide & Conquer! Sorting:! mergesort Θ(n log n)! divide sequence in half! recursively sort the two halves! merge the sorted halves! quicksort Θ(n 2 )! divide sequence into two (possibly unequal-sized) parts by comparing pivot to each key! recursively sort the two parts! combine step is empty CSCE 411, Spring 2014: Set 3 14

Classic Divide & Conquer! Graph algorithms: binary tree traversals! Inorder traversal:! traverse left subtree of current vertex! visit current vertex! traverse right subtree of current vertex! Preorder traversal similar, but visit current vertex first! Postorder traversal similar, but visit current vertex last! All three take O(n) time, where n is number of nodes in tree! Note difference from searching in a binary tree CSCE 411, Spring 2014: Set 3 15

D&C Algorithm for Closest Pair! Recall the problem: Given n points in the plane, find two that are the minimum distance apart.! Brute force algorithm took Θ(n 2 ) time.! Try to do better with divide and conquer:! divide points into two disjoint subsets! recursively find closest pairs in the two subsets! somehow combine results to get final answer CSCE 411, Spring 2014: Set 3 16

D&C Algorithm for Closest Pair: Ideas! Separate points into two equal-sized groups on either side of a vertical line! Recursively compute closest pair for left group and for right group! what should base of the recursion be?! Check if there is a smaller distance between two points on opposite sides of the vertical line! This is the tricky part CSCE 411, Spring 2014: Set 3 17

D&C Algorithm for Closest Pair: Ideas! Separate points into two equal-sized groups on either side of a vertical line! Recursively compute closest pair for left group and for right group! what should base of the recursion be?! Check if there is a smaller distance between two points on opposite sides of the vertical line! This is the tricky part CSCE 411, Spring 2014: Set 3 18

D&C Algorithm for Closest Pair: Ideas! d is min. of min. distance on right and min. distance on left! any pair with distance < d must be in this strip of width 2d centered around dividing line! consider points in strip from bottom to top! for each such point, compare it against other points in the strip that could possibly be closer! there are only a constant number of these other points! d d CSCE 411, Spring 2014: Set 3 19

D&C Algorithm for Closest Pair: Ideas Each box is d/2 by d/2 No point in comparing p against points in red area more than d away d Just need to worry about the six blue boxes p Each box contains at most one point, since maximum distance in a box is d/ 2, which is < d d d CSCE 411, Spring 2014: Set 3 20

D&C Algorithm for Closest Pair: Pseudocode! ClosestPairDist(P):! if n is small then return result of brute force algorithm! P l := left half of P w.r.t. x-coordinate! P r := right half of P w.r.t. x-coordinate! d l := ClosestPairDist(P l )! d r := ClosestPairDist(P r )! d := min(d l,d r )! for each point p in S (2d-wide center strip) do! for each point q in one of the six boxes do " d := min(dist(p,q),d)! return d CSCE 411, Spring 2014: Set 3 21

D&C Algorithm for Closest Pair: Implementation Notes! Before calling recursive code, preprocess:! sort P into array PX by increasing x-coordinate! sort P into array PY by increasing y-coordinate! Use PX to efficiently divide P into half w.r.t. x- coordinates! Use PY to efficiently scan up the 2d-wide center strip CSCE 411, Spring 2014: Set 3 22

D&C Algorithm for Closest Pair: Running Time! Preprocessing takes O(n log n) time! Recursive code, if implemented carefully, has running time described by this recurrence: T(n) = 2T(n/2) + O(n)! I.e., two recursive calls (left half and right half)! rest of the work takes time linear in the number of points being handled! Solution is T(n) = O(n log n)! Total time is O(n log n); beats brute force CSCE 411, Spring 2014: Set 3 23

D&C Algorithm for Convex Hull! Divide points into two halves by x-coordinates! Recursively compute the convex hulls of the two subsets! Combine the two convex hulls into the convex hull for the entire set of points! How to do the combining step? CSCE 411, Spring 2014: Set 3 24

Merging Hulls! Find the upper tangent line and the lower tangent line to the two hulls! Remove the interior points on the two hulls CSCE 411, Spring 2014: Set 3 25

Running Time! Claim: Merging the two hulls can be done in O(n) time.! see Preparata and Hong, CACM 1977 (original paper) and various textbooks and on-line resources for details! Thus running time is T(n) = 2T(n/2) + O(n)! Why?! By master theorem, T(n) = O(n log n) CSCE 411, Spring 2014: Set 3 26

Another Convex Hull Algorithm: Graham s Scan! (Not a divide & conquer algorithm)! Start with lowest point and work your way around the set of points counter-clockwise, deciding whether or not each point is in the convex hull See Fig. 33.7 in [CLRS] for a more involved example CSCE 411, Spring 2014: Set 3 27

Graham s Scan Pseudocode! p 0 := point with minimum y-coordinate! p 1,p 2,,p m := remaining points in counter-clockwise order of polar angle around p 0 // drop collinear points! S := empty stack! S.push(p 0 ); S.push(p 1 ); S.push(p 2 )! for i = 3 to m do! while angle formed by S.second(),S.top(), and p i does not form a left turn do! S.pop()! S.push(p i )! return S // contains CH vertices in CCW order CSCE 411, Spring 2014: Set 3 28

Ordering Points by Polar Angle! Simple approach is to calculate angle that line segment p 0 p i makes w.r.t. horizontal line passing through p 0 (using basic geometry) for each p i, and sort by angle! There is also a way using cross products of vectors to avoid operations that are expensive and prone to round-off error (division and trig functions)! See Ex. 33.1-3 in [CLRS] CSCE 411, Spring 2014: Set 3 29

Determining if an Angle Makes a Left Turn! Given 3 points u, v and w, does angle <uvw turn left or right?! In other words, is line segment uw counterclockwise or clockwise from line segment uv? w v v w u counter-clockwise, left turn u clockwise, right turn CSCE 411, Spring 2014: Set 3 30

Determining if an Angle Makes a Left Turn! Can check this using cross product:! (w u)#(v u) is defined to be: (w.x u.x)(v.y u.y) (v.x u.x)(w.y u.y) using.x and.y to indicate x and y coordinates of u, v and w! Claim: If (w u)#(v u) < 0, then counter-clockwise (left), if it is > 0, then clockwise (right), and if it is 0, then collinear w v v w u counter-clockwise, left turn u clockwise, right turn CSCE 411, Spring 2014: Set 3 31

Running Time of Graham s Scan! Determine point p 0 with smallest y-coordinate: O(1)! Calculate polar angles of remaining points w.r.t. p 0 and sort them: O(n log n)! Each stack operation: O(1)! Total time of for loop, excluding time taken by enclosed while loop: O(n)! m < n iterations and remaining body consists of a single stack push! Total time of while loop, over all iterations of enclosing for loop: O(n)! total number of pops total number of pushes! each point is pushed at most once, so at most n pops! each while loop iteration does one pop! so at most n iterations of while loop! also, while loop test (for left turn) takes O(1) time! Grand total is O(n log n) CSCE 411, Spring 2014: Set 3 32

Why is Graham s Scan Correct?! Intuition is that as we move counterclockwise, we have in the stack exactly the points that form the convex hull of the points we have processed so far, and the points are in the stack (from bottom to top) in counterclockwise order.! We can formalize this argument using induction on the number of iterations of the for loop. CSCE 411, Spring 2014: Set 3 33

Proof that Graham s Scan is Correct! Claim: For all i = 3 to n+1, at start of iteration i of for loop, stack S equals the points of CH(Q i-1 ) in CCW order (Q i-1 is p 0, p 1,...,p i-1 ).! When i = n+1 (i.e., last check of the for-loop condition), this will imply that S equals the CH of all the points.! Show this is true by induction in i.! Basis: When starting the for loop, S equals p 0, p 1, p 2, which is the CH of these 3 points. CSCE 411, Spring 2014: Set 3 34

Proof that Graham s Scan is Correct! Claim: For all i = 3 to n+1, at start of iteration i of for loop, stack S equals the points of CH(Q i 1 ) in CCW order (Q i 1 is p 0, p 1,...,p i 1 ).! Induction: Assume claim is true for all iterations 3, 4,..., i. Show claim is true for iteration i+1.! During iteration i, p i is under consideration, and some points might be popped off S in the while loop due to nonleft-turn check.! Let p j be top of S after all the popping: j i 1.! S contains exactly what it contained at end of iteration j, and thus start of iteration j+1.! Since j+1 i, inductive hypothesis states that S contains CH(Q j ).! At end of iteration i (and start of iteration i+1), S contains CH(Q j ) U {p i }. Must show this is same as CH(Q i ). CSCE 411, Spring 2014: Set 3 35

Proof that Graham s Scan is Correct p i p i p i-1 p j check for non-left p turns and perhaps 0 pop some points p 0 off S CSCE 411, Spring 2014: Set 3 36

Proof that Graham s Scan is Correct p i p j p r p t p 0 No point popped off S during iteration i can belong to CH(Q i ). Suppose p t is popped and p r is its predecessor in S. Then p t is inside triangle p 0 p r p i and is not part of CH(Q i ). CSCE 411, Spring 2014: Set 3 37

Additional Convex Hull Algorithms! Quickhull: also divide & conquer, similar to quicksort! O(n 2 ) worst case time, but if points are distributed uniformly at random in a convex region, then average case time is O(n)! Jarvis march:! O(nh) time, where h is number of points on the hull! ranges from O(n 2 ) to O(n)! Asymptotically optimal algorithm has time O(n log h)! ranges from O(n log n) to O(n) CSCE 411, Spring 2014: Set 3 38

D&C Algorithm to Multiply Large Integers! Cryptographic applications require manipulating very large integers! 100 decimal digits or more! Too long to fit into a computer word! How can we efficiently manipulate them?! in particular, multiply them! What is the time of the brute force algorithm for multiplying two n-digit integers? CSCE 411, Spring 2014: Set 3 39

D&C Algorithm to Multiply Large Integers! The answer is Θ(n 2 ): each digit of one number must be multiplied times each digit of the other number, and then some additions done! Can this be done faster?! Although it may be counter-intuitive, it turns out it can be!! Key idea is to reuse multiplications of some digits! Homework. CSCE 411, Spring 2014: Set 3 40

D&C Algorithm to Multiply Matrices! Now let s consider the problem of multiplying two matrices.! Matrices are used throughout mathematics, science, engineering, business, economics,! Many applications for multiplying matrices (e.g., determining existence of paths from one vertex to another in a graph/network)! What is the running time of the brute force algorithm for matrix multiplication? CSCE 411, Spring 2014: Set 3 41

D&C Algorithm to Multiply Matrices! Following the definition of matrix multiplication gives us an algorithm with Θ(n 3 ) running time.! Can we do better?! It might seem counter-intuitive, but the answer is yes.! Key is to reuse some multiplications of the matrix elements! sound familiar? CSCE 411, Spring 2014: Set 3 42

Strassen s Matrix Multiplication Algorithm! <board work> CSCE 411, Spring 2014: Set 3 43

Representing Polynomials! Polynomial A(x) = a 0 + a 1 x + a 2 x 2 +... + a n-1 x n 1 has degree n 1 (largest power of x with nonzero coefficient).! Two ways to represent polynomial A(x) :! with the n coefficients: a 0, a 1,..., a n 1! with n point-value pairs (one more than the degree): (x 0,A(x 0 )), (x 1,A(x 1 )),...,(x n-1,a(x n 1 )) where x 0, x 1,..., x n 1 are distinct points! See Theorem 30.1 in [CLRS] for why this works. CSCE 411, Spring 2014: Set 3 44

Operations on Polynomials! evaluate A at some point x 0! add two polynomials A(x) and B(x):! sum is defined to be C(x), where c j = a j + b j, 0 j max(deg(a),deg(b))! multiply two polynomials A(x) and B(x):! product is defined to be C(x), where c j = Σ k a k b j k, 0 j deg(a) + deg(b)! How can we do these operations with the two different representations? CSCE 411, Spring 2014: Set 3 45

Operations with Coefficient Representation! Evaluating A(x 0 ): Use Horner s rule.! rewrite A(x 0 ) as a 0 +x 0 (a 1 +x 0 (a 2 +...+x 0 (a n-2 +x 0 (a n 1 ))...))! Pseudocode: val := a n 1 for i := n 2 downto 0 do val := x*val + a i return val! Running time is O(n) CSCE 411, Spring 2014: Set 3 46

Operations with Coefficient Representation! Adding two polynomials:! add the corresponding coefficients, as in the definition of the sum! O(n) running time! Multiplying two polynomials:! Follow the definition of the product! O(n 2 ) running time CSCE 411, Spring 2014: Set 3 47

Operations with Point-Value Pairs Representation! Evaluation: interpolate (convert to coefficient form) and evaluate! [CLRS] explains how to interpolate in O(n 2 ) time! Thus total time is O(n 2 )! Addition: add the corresponding n values! requires the pairs for the two polynomials to use the same set of points! O(n) time CSCE 411, Spring 2014: Set 3 48

Operations with Point-Value Pairs Representation! Multiplication: multiply the corresponding n values! requires the pairs for the two polynomials to use the same set of points! also requires enough values: since degree of product is deg(a) + deg(b) = 2(n 1), we need 2n 1 points to start with! O(n) running time CSCE 411, Spring 2014: Set 3 49

Comparing Representations Coefficients Point-Value Pairs Evaluation O(n) O(n 2 ) Addition O(n) O(n) Multiplication O(n 2 ) O(n) Can we get the best of both worlds? Yes (almost), using a divide-and-conquer algorithm called the Fast Fourier Transform (FFT)! CSCE 411, Spring 2014: Set 3 50

Efficient Multiplication Using Coefficients: Overview a 0,a 1,...,a n 1 b 0,b 1,...b n 1 2n pairs for A, 2n pairs for B ordinary multiplication Θ(n 2 ) point-wise multiplication Θ(n) c 0,c 1,...,c 2n 2 evaluate at FFT 1 Θ(n log n) carefully interpolate Θ(n log n) chosen FFT points 2n pairs for C CSCE 411, Spring 2014: Set 3 51

FFT Details! <board work> CSCE 411, Spring 2014: Set 3 52

Divide & Conquer Summary! decrease & conquer! insertion sort! topological sort algorithm that successively removes sources! binary search! fake coin algorithm! Euclid s GCD algorithm CSCE 411, Spring 2014: Set 3 53

Divide & Conquer Summary! classic divide & conquer! mergesort, quicksort! binary tree traversals! closest pair algorithm with center strip! convex hull algorithm that merges left and right hulls! also Graham s scan (not D&C)! multiplying large integers! Strassen s matrix multiplication! FFT (applied to polynomial multiplication) CSCE 411, Spring 2014: Set 3 54