CSE 421 Algorithms: Divide and Conquer

Size: px
Start display at page:

Download "CSE 421 Algorithms: Divide and Conquer"

Transcription

1 CSE 42 Algorithms: Divide and Conquer Larry Ruzzo Thanks to Richard Anderson, Paul Beame, Kevin Wayne for some slides

2 Outline: General Idea algorithm design paradigms: divide and conquer Review of Merge Sort Why does it work? Importance of balance Importance of super-linear growth Some interesting applications Inversions Closest points Integer Multiplication Finding & Solving Recurrences 2

3 Divide & Conquer algorithm design techniques Reduce problem to one or more sub-problems of the same type Typically, each sub-problem is at most a constant fraction of the size of the original problem Subproblems typically disjoint Often gives significant, usually polynomial, speedup Examples: Binary Search, Mergesort, Quicksort (roughly), Strassen s Algorithm, integer multiplication, powering, FFT, 3

4 Motivating Example: Mergesort 4

5 merge sort MS(A: array[..n]) returns array[..n] { If(n=) return A; New U:array[:n/2] = MS(A[..n/2]); New L:array[:n/2] = MS(A[n/2+..n]); Return(Merge(U,L)); } A U C Merge(U,L: array[..n]) { New C: array[..2n]; a=; b=; For i = to 2n C[i] = smaller of U[a], L[b] and correspondingly a++ or b++, while being careful about running past end of either ; Return C; } Time: Θ(n log n) L split sort merge 5

6 Why does it work? Suppose we ve already invented DumbSort, taking time n 2 Try Just One Level of divide & conquer: DumbSort(first n/2 elements) O((n/2) 2 ) DumbSort(last n/2 elements) O((n/2) 2 ) Merge results divide & conquer the key idea O(n) Time: 2 (n/2) 2 + n = n 2 /2 + n n 2 Almost twice as fast! D&C in a nutshell 6

7 d&c approach, cont. Moral : two halves are better than a whole Two problems of half size are better than one full-size problem, even given O(n) overhead of recombining, since the base algorithm has super-linear complexity. Moral 2: If a little's good, then more's better Two levels of D&C would be almost 4 times faster, 3 levels almost 8, etc., even though overhead is growing. Best is usually full recursion down to some small constant size (balancing "work" vs "overhead"). In the limit: you ve just rediscovered mergesort! 7

8 d&c approach, cont. Moral 3: unbalanced division good, but less so: (.n) 2 + (.9n) 2 + n =.82n 2 + n The 8% savings compounds significantly if you carry recursion to more levels, actually giving O(nlogn), but with a bigger constant. So worth doing if you can t get 5-5 split, but balanced is better if you can. This is intuitively why Quicksort with random splitter is good badly unbalanced splits are rare, and not instantly fatal. Moral 4: but consistent, completely unbalanced division doesn t help much: () 2 + (n-) 2 + n = n 2 - n + 2 Little improvement here. 8

9 mergesort (review) Mergesort: (recursively) sort 2 half-lists, then merge results. T(n) = 2T(n/2)+cn, n 2 T() = Solution: Θ(n log n) (details later) Log n levels O(n) work per level 9

10 Example: Counting Inversions

11 Let a,... a n be a permutation of.. n (a i, a j ) is an inversion if i < j and a i > a j Inversion Problem 4, 6,, 7, 3, 2, 5 Problem: given a permutation, count the number of inversions This can be done easily in O(n 2 ) time Can we do better?

12 Application Counting inversions can be use to measure closeness of ranked preferences People rank 2 movies, based on their rankings you cluster people who like the same types of movies Can also be used to measure nonlinear correlation 2

13 Let a,... a n be a permutation of.. n (a i, a j ) is an inversion if i < j and a i > a j Inversion Problem 4, 6,, 7, 3, 2, 5 Problem: given a permutation, count the number of inversions This can be done easily in O(n 2 ) time Can we do better? 3

14 Counting Inversions Count inversions on left half Count inversions on right half Count the inversions between the halves 4

15 Count the Inversions

16 Can we count inversions between sub-problems in O(n) time? Yes Count inversions while merging Standard merge algorithm add to inversion count when an element is moved from the right array to the solution. (Add how much? Why not left array?) 6

17 Counting inversions while merging Indicate the number of inversions for each element detected when merging 7

18 Inversions Counting inversions between two sorted lists O() per element to count inversions x x x x x x x x y y y y y y y y z z z z z z z z z z z z z z z z Algorithm summary Satisfies the Standard recurrence T(n) = 2 T(n/2) + cn 8

19 A Divide & Conquer Example: Closest Pair of Points 9

20 closest pair of points: non-geometric version Given n points and arbitrary distances between them, find the closest pair. (E.g., think of distance as airfare definitely not Euclidean distance!) ( and all the rest of the ( n ) edges ) 2 Must look at all n choose 2 pairwise distances, else any one you didn t check might be the shortest. Also true for Euclidean distance in -2 dimensions? 2

21 closest pair of points: dimensional version Given n points on the real line, find the closest pair Closest pair is adjacent in ordered list Time O(n log n) to sort, if needed Plus O(n) to scan adjacent pairs Key point: do not need to calc distances between all pairs: exploit geometry + ordering 2

22 closest pair of points: 2 dimensional version 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. fast closest pair inspired fast algorithms for these problems Brute force. Check all pairs of points p and q with Θ(n 2 ) comparisons. -D version. O(n log n) easy if points are on a line. Assumption. No two points have same x coordinate. Just to simplify presentation 22

23 closest pair of points. 2d, Euclidean distance: st try Divide. Sub-divide region into 4 quadrants. 23

24 closest pair of points: st try Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece, so the balanced subdivision goal may be elusive/problematic. 24

25 Algorithm. closest pair of points Divide: draw vertical line L with n/2 points on each side. L 25

26 Algorithm. closest pair of points Divide: draw vertical line L with n/2 points on each side. Conquer: find closest pair on each side, recursively. L

27 Algorithm. closest pair of points Divide: draw vertical line L with n/2 points on each side. Conquer: find closest pair on each side, recursively. Combine: find closest pair with one point in each side. Return best of 3 solutions. L seems like Θ(n 2 )?

28 closest pair of points Find closest pair with one point in each side, assuming distance < δ. L 2 2 δ = min(2, 2) 28

29 closest pair of points Find closest pair with one point in each side, assuming distance < δ. Observation: suffices to consider points within δ of line L. L 2 2 δ = min(2, 2) 29 δ

30 closest pair of points Find closest pair with one point in each side, assuming distance < δ. Observation: suffices to consider points within δ of line L. Almost the one-d problem again: Sort points in 2δ-strip by their y coordinate. 7 L δ = min(2, 2) 2 δ 3

31 closest pair of points Find closest pair with one point in each side, assuming distance < δ. Observation: suffices to consider points within δ of line L. Almost the one-d problem again: Sort points in 2δ-strip by their y coordinate. Only check pts within 8 in sorted list! 7 L δ = min(2, 2) 2 δ 3

32 Def. Let s i have the i th smallest y-coordinate among points in the 2δ-width-strip. Claim. If j i 8, then the distance between s i and s j is > δ. Pf: No two points lie in the same δ/2-by-δ/2 square: closest pair of points i j δ/2 δ/2 " δ % $ ' # 2& 2 " + δ % $ ' # 2& 2 = 2 2 δ.7δ < δ so 7 points within +δ of y(s i ). 32 δ δ

33 closest pair algorithm Closest-Pair(p,, p n ) { if(n <=??) return?? Compute separation line L such that half the points are on one side and half on the other side. δ = Closest-Pair(left half) δ 2 = Closest-Pair(right half) δ = min(δ, δ 2 ) Delete all points further than δ from separation line L Sort remaining points p[] p[m] by y-coordinate. for i =..m k = while i+k <= m && p[i+k].y < p[i].y + δ δ = min(δ, distance between p[i] and p[i+k]); k++; } return δ. 33

34 closest pair of points: analysis Analysis, I: Let D(n) be the number of pairwise distance calculations in the Closest-Pair Algorithm when run on n points D(n) # n =& $ % 2D( n /2) + 7n n > ' ( D(n) = O(n log n) BUT that s only the number of distance calculations What if we counted comparisons? 34

35 closest pair of points: analysis Analysis, II: Let C(n) be the number of comparisons between coordinates/distances in the Closest-Pair Algorithm when run on n points " $ C(n) # %$ 2C n / 2 for some constant k n = ( ) + kn logn n > Q. Can we achieve O(n log n)? & $ ' ($ C(n) = O(n log 2 n) A. Yes. Don't sort points from scratch each time. Sort by x at top level only. Each recursive call returns δ and list of all points sorted by y Sort by merging two pre-sorted lists. T(n) 2T( n/2) + O(n) T(n) = O(n log n) 35

36 Code is longer & more complex is it worth the effort? O(n log n) vs O(n 2 ) may hide x in constant? How many points? n Speedup: n 2 / ( n log 2 n).3.5,, 75, 62,, 5,7,, 43,4 36

37 Going From Code to Recurrence 37

38 going from code to recurrence Carefully define what you re counting, and write it down! Let C(n) be the number of comparisons between sort keys used by MergeSort when sorting a list of length n In code, clearly separate base case from recursive case, highlight recursive calls, and operations being counted. Write Recurrence(s) 38

39 merge sort Base Case MS(A: array[..n]) returns array[..n] { If(n=) return A; New L:array[:n/2] = MS(A[..n/2]); New R:array[:n/2] = MS(A[n/2+..n]); Return(Merge(L,R)); } Merge(A,B: array[..n]) { New C: array[..2n]; a=; b=; For i = to 2n { C[i] = smaller of A[a], B[b] and a++ or b++ ; Return C; } Recursive calls One Recursive Level Operations being counted 39

40 the recurrence Base case C(n) = # if n = $ % 2C(n /2) + (n ) if n > Recursive calls Total time: proportional to C(n) (loops, copying data, parameter passing, etc.) One compare per element added to merged list, except the last. 4

41 going from code to recurrence Carefully define what you re counting, and write it down! Let D(n) be the number of pairwise distance calculations in the Closest-Pair Algorithm when run on n points In code, clearly separate base case from recursive case, highlight recursive calls, and operations being counted. Write Recurrence(s) 4

42 Basic operations: distance calcs closest pair algorithm Closest-Pair(p,, p n ) { if(n <= ) return Base Case Compute separation line L such that half the points are on one side and half on the other side. δ = Closest-Pair(left half) δ 2 = Closest-Pair(right half) δ = min(δ, δ 2 ) Recursive calls (2) 2D(n / 2) } Delete all points further than δ from separation line L Sort remaining points p[] p[m] by y-coordinate. for i =..m k = while i+k <= m && p[i+k].y < p[i].y + δ return δ. Basic operations at this recursive level δ = min(δ, distance between p[i] and p[i+k]); k++; One recursive level 7n 42

43 closest pair of points: analysis Analysis, I: Let D(n) be the number of pairwise distance calculations in the Closest-Pair Algorithm when run on n points D(n) # n =& $ % 2D( n /2) + 7n n > ' ( D(n) = O(n log n) BUT that s only the number of distance calculations What if we counted comparisons? 43

44 going from code to recurrence Carefully define what you re counting, and write it down! Let D(n) be the number of comparisons between coordinates/distances in the Closest-Pair Algorithm when run on n points In code, clearly separate base case from recursive case, highlight recursive calls, and operations being counted. Write Recurrence(s) 44

45 Basic operations: comparisons Base Case closest pair algorithm Closest-Pair(p,, p n ) { if(n <= ) return Recursive calls (2) } Compute separation line L such that half the points are on one side and half on the other side. δ = Closest-Pair(left half) δ 2 = Closest-Pair(right half) δ = min(δ, δ 2 ) Delete all points further than δ from separation line L Sort remaining points p[] p[m] by y-coordinate. for i =..m Basic operations at this recursive level k = while i+k <= m && p[i+k].y < p[i].y + δ δ = min(δ, distance between p[i] and p[i+k]); k++; return δ. k n log n 2C(n / 2) k 2 n k 3 n log n 8n 7n One recursive level 45

46 closest pair of points: analysis Analysis, II: Let C(n) be the number of comparisons of coordinates/distances in the Closest-Pair Algorithm when run on n points C(n) 2C n / 2 for k 4 = k + k 2 + k 3 +6 n = ( ) + k 4 n log 2 n n > Q. Can we achieve time O(n log n)? C(n) = O(n log 2 n) A. Yes. Don't sort points from scratch each time. Sort by x at top level only. Each recursive call returns δ and list of all points sorted by y Sort by merging two pre-sorted lists. T(n) 2T( n/2) + O(n) T(n) = O(n log n) 46

47 Integer Multiplication 47

48 integer arithmetic Add. Given two n-bit integers a and b, compute a + b. O(n) bit operations. Multiply. Given two n-digit integers a and b, compute a b. The grade school method: Θ(n 2 ) bit operations Add * Multiply

49 integer arithmetic Add. Given two n-bit integers a and b, compute a + b. O(n) bit operations. Multiply. Given two n-bit integers a and b, compute a b. The grade school method: Θ(n 2 ) bit operations Add * Multiply Carries

50 divide & conquer multiplication: warmup To multiply two 2-digit integers: Multiply four -digit integers. Add, shift some 2-digit integers to obtain result. x = x + x y = y + y xy = x + x ( ) ( y + y ) ( ) + x y = x y + x y + x y y y x x x y x y Same idea works for long integers can split them into 4 half-sized ints ( becomes k, k = length/2) x y x y 5

51 divide & conquer multiplication: warmup To multiply two n-bit integers: Multiply four ½n-bit integers. Shift/add four n-bit integers to obtain result. x = 2 n / 2 x + x y = 2 n / 2 y + y xy = 2 n / 2 x + x ( ) ( 2 n / 2 y + y ) * y y x x x y ( ) + x y = 2 n x y + 2 n / 2 x y + x y x y x y ( ) T(n) = 4T n /2 + Θ(n) T(n) = Θ(n2 ) recursive calls add, shift x y assumes n is a power of 2 5

52 key trick: 2 multiplies for the price of : x = 2 n / 2 x + x y = 2 n / 2 y + y xy = 2 n / 2 x + x ( ) ( 2 n / 2 y + y ) Well, ok, 4 for 3 is more accurate ( ) + x y = 2 n x y + 2 n / 2 x y + x y α = x + x β = y + y αβ = ( x + x ) ( y + y ) = x y + x y + x y ( x y + x y ) = αβ x y x y ( ) + x y 52

53 To multiply two n-bit integers: Add two pairs of ½n bit integers. Multiply three pairs of ½n-bit integers. Add, subtract, and shift n-bit integers to obtain result. Karatsuba multiplication x = 2 n /2 x + x y = 2 n /2 y + y xy = 2 n x y + 2 n /2 x y + x y ( ) + x y ( ) + x y = 2 n x y + 2 n /2 (x + x )(y + y ) x y x y A B A C C Theorem. [Karatsuba-Ofman, 962] Can multiply two n-digit integers in O(n.585 ) bit operations. ( # $ ) + T % n /2& ( ) + T( + % n /2& ) T(n) T n /2 recursive calls Sloppy version : T(n) 3T(n /2) + O(n) T(n) = O(n log 2 3 ) = O(n.585 ) + Θ(n) add, subtract, shift 53

54 Karatsuba multiplication Theorem. [Karatsuba-Ofman, 962] Can multiply two n-digit integers in O(n.585 ) bit operations. ( # $ ) + T % n /2& ( ) + T( + % n /2& ) T(n) T n /2 recursive calls Sloppy version : T(n) 3T(n /2) + O(n) T(n) = O(n log 2 3 ) = O(n.585 ) + Θ(n) add, subtract, shift Best: Solve the exact recurrence 2 nd best: Solve the sloppy version Almost always gives the right asymptotics Alternatively, you can often change the algorithm to simplify the recurrence so that you can solve it. E.g., sloppy = exact if Get rid of and by padding n to 2 log n 2 Get rid of + n/2 by peeling off one bit: T(n/2)+O(n) 54

55 Karatsuba multiplication Theorem. [Karatsuba-Ofman, 962] Can multiply two n-digit integers in O(n.585 ) bit operations. ( # $ ) + T % n /2& ( ) + T( + % n /2& ) T(n) T n /2 recursive calls Sloppy version : T(n) 3T(n /2) + O(n) T(n) = O(n log 2 3 ) = O(n.585 ) + Θ(n) add, subtract, shift Alternatively, you can often change the algorithm to simplify the recurrence so that you can solve it. E.g., sloppy becomes exact if Kill & : pad n to 2 log 2 n Kill + n/2 : peel off one bit: T(n/2)+O(n) x y x + xy zzz 55

56 multiplication the bottom line Naïve: Θ(n 2 ) Karatsuba: Θ(n.59 ) Amusing exercise: generalize Karatsuba to do 5 size n/3 subproblems Θ(n.46 ) Best known: Θ(n log n loglog n) "Fast Fourier Transform" but mostly unused in practice (unless you need really big numbers - a billion digits of π, say) High precision arithmetic IS important for crypto, among other uses 56

57 Recurrences Above: Where they come from, how to find them Next: how to solve them 57

58 mergesort (review) Mergesort: (recursively) sort 2 half-lists, then merge results. T(n) = 2T(n/2)+cn, n 2 T() = Solution: Θ(n log n) (details later) now! Log n levels O(n) work per level 58

59 Solve: T() = c T(n) = 2 T(n/2) + cn n = 2 k ; k = log 2 n Level Num Size Work = 2 =2 n n cn cn 2 = 2 n/2 2cn/2 2 4 = 2=2 2 2 n/2 n/4 2 c 4cn/4 n/2 2 4=2 2 n/4 4 c n/4 i 2 i n/2 i 2 i c n/2 i i 2 i n/2 i 2 i c n/2 i k- 2 k- n/2 k- 2 k- c n/2 k- Level Num Size Work k- 2 k- n/2 k- 2 k- c n/2 k- k 2 k n/2 k = 2 k T() Total Work: c n (+log 2 n) (add last col) 59

60 Solve: T() = c T(n) = 4 T(n/2) + cn n = 2 k ; k = log 2 n Level Num Size Work = =4 4 n n cn cn 4 = 4=4 4 n/2 4 c 4cn/ =4 = n/4 6 6cn/4 c i i 4 i 4 i n/2 i 4 i c 4 i n/2 c n/2 i k- 4 k- n/2 k- 4 k- c n/2 k- k k 4 k 4 k n/2 n/2 = k = 4 k T() 4 k T() Level Num Size Work k- 4 k- n/2 k- 4 k- c n/2 k- Total Work: T(n) = k 4 i cn / 2 i = O(n 2 ) i = 4 k = (2 2 ) k = 6 (2 k ) 2 = n 2

61 Solve: T() = c T(n) = 3 T(n/2) + cn n = 2 k ; k = log 2 n Total Work: T(n) = k i = Level Num Size Work =3 n cn 3=3 n/2 3 c n/2 2 9=3 2 n/4 9 c n/4 i 3 i n/2 i 3 i c n/2 i k- 3 k- n/2 k- 3 k- c n/2 k- k 3 k n/2 k = 3 k T() Level Num Size Work = 3 n cn 3 = 3 n/2 3cn/2 2 9 = 3 2 n/4 9cn/4 i 3 i n/2 i 3 i c n/2 i k- 3 k- n/2 k- 3 k- c n/2 k- k 3 k n/2 k = 3 k T() 3 i cn / 2 i 6

62 a useful identity Theorem: for x, + x + x 2 + x x k = (x k+ -)/(x-) proof: y = + x + x 2 + x x k xy = x + x 2 + x x k + x k+ xy-y = x k+ - y(x-)= x k+ - y = (x k+ -)/(x-) 62

63 Solve: T() = c T(n) = 3 T(n/2) + cn (cont.) T(n) = k 3 i cn / 2 i i= = cn k 3 i / 2 i i= k i= = cn 3 2 ( ) i ( 2 = cn 3 ) k+ ( )

64 Solve: T() = c T(n) = 3 T(n/2) + cn (cont.) ( 3 ) k+ cn 2 ( ) = 2cn ( 2 3 ) k+ 3 2 ( ) < 2cn( 2 3 ) k+ = 3cn( 2 3 ) k = 3cn 3k 2 k 64

65 Solve: T() = c T(n) = 3 T(n/2) + cn (cont.) 3cn 3k 2 k = 3cn 3log2 n 2 log 2 n = 3cn 3log 2 n n = 3c3 log 2 n = 3c( n log 2 3 ) = O( n ) a log b n = ( b log b a ) log b n = ( b log b n ) log b a = n log b a 65

66 T(n) = at(n/b)+cn k for n > b then divide and conquer master recurrence a > b k T(n) = Θ(n log b a ) [many subprobs leaves dominate] a < b k T(n) = Θ(n k ) [few subprobs top level dominates] a = b k T(n) = Θ (n k log n) [balanced all log n levels contribute] Fine print: T() = d; a ; b > ; c, d, k ; n = b t for some t > ; a, b, k, t integers. True even if it is n/b instead of n/b. 66

67 master recurrence: proof sketch Expand recurrence as in earlier examples, to get T(n) = n h ( d + c S ) where h = log b (a) (and n h = number of tree leaves) and, where x = b k /a. If c = the sum S is irrelevant, and T(n) = O(n h ): all work happens in the base cases, of which there are n h, one for each leaf in the recursion tree. If c >, then the sum matters, and splits into 3 cases (like previous slide): if x <, then S < x/(-x) = O(). if x =, then S = log b (n) = O(log n). S = [S is the first log n terms of the infinite series with that sum.] [All terms in the sum are and there are that many terms.] if x >, then S = x (x +log b (n) -)/(x-). [And after some algebra, n h * S = O(n k ).] log b n x j j= 67

68 Another Example: Exponentiation 68

69 another d&c example: fast exponentiation Power(a,n) Input: integer n and number a Output: a n Obvious algorithm n- multiplications Observation: if n is even, n = 2m, then a n = a m a m 69

70 Power(a,n) if n = then return() if n = then return(a) x Power(a, n/2 ) x x x if n is odd then x a x return(x) divide & conquer algorithm 7

71 Let M(n) be number of multiplies Worst-case recurrence: By master theorem M(n) = O(log n) & ( M(n) = ' )( More precise analysis: (a=, b=2, k=) analysis n M ("# n / 2$% ) + 2 n > M(n) = log 2 n + (# of s in n s binary representation) - Time is O(M(n)) if numbers < word size, else also depends on length, multiply algorithm 7

72 a practical application - RSA Instead of a n want a n mod N a i+j mod N = ((a i mod N) (a j mod N)) mod N same algorithm applies with each x y replaced by ((x mod N) (y mod N)) mod N In RSA cryptosystem (widely used for security) need a n mod N where a, n, N each typically have 24 bits Power: at most 248 multiplies of 24 bit numbers relatively easy for modern machines Naive algorithm: 2 24 multiplies 72

73 Utility: Idea: Correctness often easy; often faster Two halves are better than a whole if the base algorithm has super-linear complexity. If a little's good, then more's better repeat above, recursively d & c summary Analysis: recursion tree or Master Recurrence among others Applications: Many. Binary Search, Merge Sort, (Quicksort), Closest Points, Integer Multiply, Exponentiation, 73

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 Divide-and-Conquer Chapter 5 Divide and Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common

More information

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution. Divide-and-Conquer Chapter 5 Divide and Conquer Divide-and-conquer.! Break up problem into several parts.! Solve each part recursively.! Combine solutions to sub-problems into overall solution. Most common

More information

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 5 Divide and Conquer CLRS 4.3 Slides by Kevin Wayne. Copyright 25 Pearson-Addison Wesley. All rights reserved. Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve

More information

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 5 Divide and Conquer CLRS 4.3 Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve

More information

5. DIVIDE AND CONQUER I

5. DIVIDE AND CONQUER I 5. DIVIDE AND CONQUER I mergesort counting inversions closest pair of points median and selection Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

More information

5. DIVIDE AND CONQUER I

5. DIVIDE AND CONQUER I 5. DIVIDE AND CONQUER I mergesort counting inversions closest pair of points randomized quicksort median and selection Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 4: Divide and Conquer (I) Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Divide and Conquer ( DQ ) First paradigm or framework DQ(S)

More information

CPS 616 DIVIDE-AND-CONQUER 6-1

CPS 616 DIVIDE-AND-CONQUER 6-1 CPS 616 DIVIDE-AND-CONQUER 6-1 DIVIDE-AND-CONQUER Approach 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger)

More information

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer What you really need to know about recurrences Work per level changes geometrically with the level Geometrically increasing (x > 1) The

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 9 Divide and Conquer Merge sort Counting Inversions Binary Search Exponentiation Solving Recurrences Recursion Tree Method Master Theorem Sofya Raskhodnikova S. Raskhodnikova;

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 5: Divide and Conquer (Part 2) Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ A Lower Bound on Convex Hull Lecture 4 Task: sort the

More information

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem CS 577 Introduction to Algorithms: Jin-Yi Cai University of Wisconsin Madison In the last class, we described InsertionSort and showed that its worst-case running time is Θ(n 2 ). Check Figure 2.2 for

More information

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch] Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch] Divide and Conquer Paradigm An important general technique for designing algorithms: divide problem into subproblems recursively

More information

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101 A design paradigm Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/17 112 Multiplying complex numbers (from Jeff Edmonds slides) INPUT: Two pairs of integers, (a,b),

More information

Divide and conquer. Philip II of Macedon

Divide and conquer. Philip II of Macedon Divide and conquer Philip II of Macedon Divide and conquer 1) Divide your problem into subproblems 2) Solve the subproblems recursively, that is, run the same algorithm on the subproblems (when the subproblems

More information

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count Types of formulas for basic operation count Exact formula e.g., C(n) = n(n-1)/2 Algorithms, Design and Analysis Big-Oh analysis, Brute Force, Divide and conquer intro Formula indicating order of growth

More information

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30, Divide and Conquer CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Merging sorted lists: WHAT Given two sorted lists a 1 a 2 a 3 a k b 1 b 2 b 3 b

More information

Divide and Conquer. Arash Rafiey. 27 October, 2016

Divide and Conquer. Arash Rafiey. 27 October, 2016 27 October, 2016 Divide the problem into a number of subproblems Divide the problem into a number of subproblems Conquer the subproblems by solving them recursively or if they are small, there must be

More information

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016 CS125 Lecture 4 Fall 2016 Divide and Conquer We have seen one general paradigm for finding algorithms: the greedy approach. We now consider another general paradigm, known as divide and conquer. We have

More information

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication Integer multiplication Say we have 5 baskets with 8 apples in each How do we determine how many apples we have? Count them all? That would take

More information

COMP 355 Advanced Algorithms

COMP 355 Advanced Algorithms COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background 1 Polynomial Running Time Brute force. For many non-trivial problems, there is a natural brute force search algorithm that

More information

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2 CMPSCI611: Three Divide-and-Conquer Examples Lecture 2 Last lecture we presented and analyzed Mergesort, a simple divide-and-conquer algorithm. We then stated and proved the Master Theorem, which gives

More information

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background 1 Polynomial Time Brute force. For many non-trivial problems, there is a natural brute force search algorithm that checks every

More information

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201 Divide and Conquer: Polynomial Multiplication Version of October 7, 2014 Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201 Outline Outline: Introduction The polynomial multiplication

More information

Divide and Conquer. Andreas Klappenecker

Divide and Conquer. Andreas Klappenecker Divide and Conquer Andreas Klappenecker The Divide and Conquer Paradigm The divide and conquer paradigm is important general technique for designing algorithms. In general, it follows the steps: - divide

More information

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 Algorithm runtime analysis and computational tractability Time Complexity of an Algorithm How do we measure the complexity (time, space requirements) of an algorithm. 1 microsecond? Units of time As soon

More information

Computational Complexity - Pseudocode and Recursions

Computational Complexity - Pseudocode and Recursions Computational Complexity - Pseudocode and Recursions Nicholas Mainardi 1 Dipartimento di Elettronica e Informazione Politecnico di Milano nicholas.mainardi@polimi.it June 6, 2018 1 Partly Based on Alessandro

More information

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation: Data structures Exercise 1 solution Question 1 Let s start by writing all the functions in big O notation: f 1 (n) = 2017 = O(1), f 2 (n) = 2 log 2 n = O(n 2 ), f 3 (n) = 2 n = O(2 n ), f 4 (n) = 1 = O

More information

Data Structures and Algorithms Chapter 3

Data Structures and Algorithms Chapter 3 1 Data Structures and Algorithms Chapter 3 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Introduction to Algorithms

Introduction to Algorithms Lecture 1 Introduction to Algorithms 1.1 Overview The purpose of this lecture is to give a brief overview of the topic of Algorithms and the kind of thinking it involves: why we focus on the subjects that

More information

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 2200 Fall 2017 Divide-and-Conquer Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 1 The divide-and-conquer design paradigm 1. Divide the problem (instance)

More information

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort Xi Chen Columbia University We continue with two more asymptotic notation: o( ) and ω( ). Let f (n) and g(n) are functions that map

More information

Divide-Conquer-Glue Algorithms

Divide-Conquer-Glue Algorithms Divide-Conquer-Glue Algorithms Mergesort and Counting Inversions Tyler Moore CSE 3353, SMU, Dallas, TX Lecture 10 Divide-and-conquer. Divide up problem into several subproblems. Solve each subproblem recursively.

More information

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 MA008 p.1/37 MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 Dr. Markus Hagenbuchner markus@uow.edu.au. MA008 p.2/37 Exercise 1 (from LN 2) Asymptotic Notation When constants appear in exponents

More information

1 Divide and Conquer (September 3)

1 Divide and Conquer (September 3) The control of a large force is the same principle as the control of a few men: it is merely a question of dividing up their numbers. Sun Zi, The Art of War (c. 400 C.E.), translated by Lionel Giles (1910)

More information

Partition and Select

Partition and Select Divide-Conquer-Glue Algorithms Quicksort, Quickselect and the Master Theorem Quickselect algorithm Tyler Moore CSE 3353, SMU, Dallas, TX Lecture 11 Selection Problem: find the kth smallest number of an

More information

Chapter 5. Divide and Conquer. CS 350 Winter 2018

Chapter 5. Divide and Conquer. CS 350 Winter 2018 Chapter 5 Divide and Conquer CS 350 Winter 2018 1 Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall

More information

CSCI 3110 Assignment 6 Solutions

CSCI 3110 Assignment 6 Solutions CSCI 3110 Assignment 6 Solutions December 5, 2012 2.4 (4 pts) Suppose you are choosing between the following three algorithms: 1. Algorithm A solves problems by dividing them into five subproblems of half

More information

Asymptotic Analysis and Recurrences

Asymptotic Analysis and Recurrences Appendix A Asymptotic Analysis and Recurrences A.1 Overview We discuss the notion of asymptotic analysis and introduce O, Ω, Θ, and o notation. We then turn to the topic of recurrences, discussing several

More information

Introduction to Divide and Conquer

Introduction to Divide and Conquer Introduction to Divide and Conquer Sorting with O(n log n) comparisons and integer multiplication faster than O(n 2 ) Periklis A. Papakonstantinou York University Consider a problem that admits a straightforward

More information

Analysis of Algorithms - Using Asymptotic Bounds -

Analysis of Algorithms - Using Asymptotic Bounds - Analysis of Algorithms - Using Asymptotic Bounds - Andreas Ermedahl MRTC (Mälardalens Real-Time Research Center) andreas.ermedahl@mdh.se Autumn 004 Rehersal: Asymptotic bounds Gives running time bounds

More information

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 1: Asymptotics, Recurrences, Elementary Sorting Lecture 1: Asymptotics, Recurrences, Elementary Sorting Instructor: Outline 1 Introduction to Asymptotic Analysis Rate of growth of functions Comparing and bounding functions: O, Θ, Ω Specifying running

More information

CSCI Honor seminar in algorithms Homework 2 Solution

CSCI Honor seminar in algorithms Homework 2 Solution CSCI 493.55 Honor seminar in algorithms Homework 2 Solution Saad Mneimneh Visiting Professor Hunter College of CUNY Problem 1: Rabin-Karp string matching Consider a binary string s of length n and another

More information

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14 Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 14 CSE 101: Design and analysis of algorithms Divide and conquer algorithms Reading: Sections 2.3 and 2.4 Homework 6 will

More information

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes Computational Complexity Lecture 02 - Basic Complexity Analysis Tom Kelsey & Susmit Sarkar School of Computer Science University of St Andrews http://www.cs.st-andrews.ac.uk/~tom/ twk@st-andrews.ac.uk

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Runtime Analysis May 31, 2017 Tong Wang UMass Boston CS 310 May 31, 2017 1 / 37 Topics Weiss chapter 5 What is algorithm analysis Big O, big, big notations

More information

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on Outline Part 5. Computa0onal Complexity (3) Recurrence Relations Divide and Conquer Understanding the Complexity of Algorithms CS 200 Algorithms and Data Structures 1 2 Recurrence Rela0ons: An Overview

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms Divide and Conquer Algorithms Introduction There exist many problems that can be solved using a divide-and-conquer algorithm. A divide-andconquer algorithm A follows these general guidelines. Divide Algorithm

More information

CS173 Running Time and Big-O. Tandy Warnow

CS173 Running Time and Big-O. Tandy Warnow CS173 Running Time and Big-O Tandy Warnow CS 173 Running Times and Big-O analysis Tandy Warnow Today s material We will cover: Running time analysis Review of running time analysis of Bubblesort Review

More information

Divide and Conquer. Recurrence Relations

Divide and Conquer. Recurrence Relations Divide and Conquer Recurrence Relations Divide-and-Conquer Strategy: Break up problem into parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. 2 MergeSort Mergesort.

More information

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide-and-conquer: Order Statistics. Curs: Fall 2017 Divide-and-conquer: Order Statistics Curs: Fall 2017 The divide-and-conquer strategy. 1. Break the problem into smaller subproblems, 2. recursively solve each problem, 3. appropriately combine their answers.

More information

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD Greedy s Greedy s Shortest path Claim 2: Let S be a subset of vertices containing s such that we know the shortest path length l(s, u) from s to any vertex in u S. Let e = (u, v) be an edge such that 1

More information

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019 CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis Ruth Anderson Winter 2019 Today Algorithm Analysis What do we care about? How to compare two algorithms Analyzing Code Asymptotic Analysis

More information

Divide-and-Conquer. a technique for designing algorithms

Divide-and-Conquer. a technique for designing algorithms Divide-and-Conquer a technique for designing algorithms decomposing instance to be solved into subinstances of the same problem solving each subinstance combining subsolutions to obtain the solution to

More information

1 Substitution method

1 Substitution method Recurrence Relations we have discussed asymptotic analysis of algorithms and various properties associated with asymptotic notation. As many algorithms are recursive in nature, it is natural to analyze

More information

Reductions, Recursion and Divide and Conquer

Reductions, Recursion and Divide and Conquer Chapter 5 Reductions, Recursion and Divide and Conquer CS 473: Fundamental Algorithms, Fall 2011 September 13, 2011 5.1 Reductions and Recursion 5.1.0.1 Reduction Reducing problem A to problem B: (A) Algorithm

More information

CSE 421. Dynamic Programming Shortest Paths with Negative Weights Yin Tat Lee

CSE 421. Dynamic Programming Shortest Paths with Negative Weights Yin Tat Lee CSE 421 Dynamic Programming Shortest Paths with Negative Weights Yin Tat Lee 1 Shortest Paths with Neg Edge Weights Given a weighted directed graph G = V, E and a source vertex s, where the weight of edge

More information

Divide-and-conquer. Curs 2015

Divide-and-conquer. Curs 2015 Divide-and-conquer Curs 2015 The divide-and-conquer strategy. 1. Break the problem into smaller subproblems, 2. recursively solve each problem, 3. appropriately combine their answers. Known Examples: Binary

More information

Advanced Counting Techniques. Chapter 8

Advanced Counting Techniques. Chapter 8 Advanced Counting Techniques Chapter 8 Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence Relations Nonhomogeneous Recurrence Relations Divide-and-Conquer

More information

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review: Mathematical Background Mathematical Background CSE 373 Data Structures Today, we will review: Logs and eponents Series Recursion Motivation for Algorithm Analysis 5 January 007 CSE 373 - Math Background

More information

ITEC2620 Introduction to Data Structures

ITEC2620 Introduction to Data Structures ITEC2620 Introduction to Data Structures Lecture 6a Complexity Analysis Recursive Algorithms Complexity Analysis Determine how the processing time of an algorithm grows with input size What if the algorithm

More information

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

Chapter 1 Divide and Conquer Polynomial Multiplication Algorithm Theory WS 2015/16 Fabian Kuhn Chapter 1 Divide and Conquer Polynomial Multiplication Algorithm Theory WS 2015/16 Fabian Kuhn Formulation of the D&C principle Divide-and-conquer method for solving a problem instance of size n: 1. Divide

More information

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example Recurrence Relations Chapter 2 Divide and Conquer Equation or an inequality that describes a function by its values on smaller inputs. Recurrence relations arise when we analyze the running time of iterative

More information

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015 CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015 1 Introduction The divide-and-conquer strategy is a general paradigm for algorithm design. This strategy

More information

COMP 382: Reasoning about algorithms

COMP 382: Reasoning about algorithms Fall 2014 Unit 4: Basics of complexity analysis Correctness and efficiency So far, we have talked about correctness and termination of algorithms What about efficiency? Running time of an algorithm For

More information

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction to Algorithms 6.046J/18.401J/SMA5503 Introduction to Algorithms 6.046J/8.40J/SMA5503 Lecture 3 Prof. Piotr Indyk The divide-and-conquer design paradigm. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving

More information

2. ALGORITHM ANALYSIS

2. ALGORITHM ANALYSIS 2. ALGORITHM ANALYSIS computational tractability asymptotic order of growth survey of common running times Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

More information

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms. Solving recurrences Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms Example: Merge-Sort(A, p, r) 1: if p < r then 2: q (p + r)/2 3: Merge-Sort(A,

More information

Asymptotic Algorithm Analysis & Sorting

Asymptotic Algorithm Analysis & Sorting Asymptotic Algorithm Analysis & Sorting (Version of 5th March 2010) (Based on original slides by John Hamer and Yves Deville) We can analyse an algorithm without needing to run it, and in so doing we can

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17 2.1 Notes Homework 1 will be released today, and is due a week from today by the beginning

More information

Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion

Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion During the past week, we learnt about inductive reasoning, in which we broke down a problem of size n, into one

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms Divide and Conquer Algorithms T. M. Murali February 19, 2013 Divide and Conquer Break up a problem into several parts. Solve each part recursively. Solve base cases by brute force. Efficiently combine

More information

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018 CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis Ruth Anderson Winter 2018 Today Algorithm Analysis What do we care about? How to compare two algorithms Analyzing Code Asymptotic Analysis

More information

CSC373: Algorithm Design, Analysis and Complexity Fall 2017

CSC373: Algorithm Design, Analysis and Complexity Fall 2017 CSC373: Algorithm Design, Analysis and Complexity Fall 2017 Allan Borodin (in collaboration with Rabia Bakhteri and with occasional guest lectures by Nisarg Shah and Denis Pankratov) September 13, 2017

More information

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science Divide & Conquer Jordi Cortadella and Jordi Petit Department of Computer Science Divide-and-conquer algorithms Strategy: Divide the problem into smaller subproblems of the same type of problem Solve the

More information

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n Class Note #14 Date: 03/01/2006 [Overall Information] In this class, we studied an algorithm for integer multiplication, which improved the running time from θ(n 2 ) to θ(n 1.59 ). We then used some of

More information

data structures and algorithms lecture 2

data structures and algorithms lecture 2 data structures and algorithms 2018 09 06 lecture 2 recall: insertion sort Algorithm insertionsort(a, n): for j := 2 to n do key := A[j] i := j 1 while i 1 and A[i] > key do A[i + 1] := A[i] i := i 1 A[i

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 5 Divide and Conquer: Fast Fourier Transform Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms

More information

Chapter 5 Divide and Conquer

Chapter 5 Divide and Conquer CMPT 705: Design and Analysis of Algorithms Spring 008 Chapter 5 Divide and Conquer Lecturer: Binay Bhattacharya Scribe: Chris Nell 5.1 Introduction Given a problem P with input size n, P (n), we define

More information

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version) CS 470/570 Divide-and-Conquer Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively on smaller pieces Combine: Build the

More information

Recurrence Relations

Recurrence Relations Recurrence Relations Winter 2017 Recurrence Relations Recurrence Relations A recurrence relation for the sequence {a n } is an equation that expresses a n in terms of one or more of the previous terms

More information

CS483 Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms CS483 Design and Analysis of Algorithms Chapter 2 Divide and Conquer Algorithms Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: Room 5326, Engineering Building, Thursday 4:30pm -

More information

Divide and Conquer Strategy

Divide and Conquer Strategy Divide and Conquer Strategy Algorithm design is more an art, less so a science. There are a few useful strategies, but no guarantee to succeed. We will discuss: Divide and Conquer, Greedy, Dynamic Programming.

More information

5. DIVIDE AND CONQUER I

5. DIVIDE AND CONQUER I 5. DIVIDE AND CONQUER I mergesort counting inversions randomized quicksort median and selection closest pair of points Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

More information

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm: The maximum-subarray problem Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm: Brute force algorithm: At best, θ(n 2 ) time complexity 129 Can we do divide

More information

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline CS 5321: Advanced Algorithms Analysis Using Recurrence Ali Ebnenasir Department of Computer Science Michigan Technological University Acknowledgement Eric Torng Moon Jung Chung Charles Ofria Outline Motivating

More information

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018 CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis Ruth Anderson Winter 2018 Today Algorithm Analysis What do we care about? How to compare two algorithms Analyzing Code Asymptotic Analysis

More information

Kartsuba s Algorithm and Linear Time Selection

Kartsuba s Algorithm and Linear Time Selection CS 374: Algorithms & Models of Computation, Fall 2015 Kartsuba s Algorithm and Linear Time Selection Lecture 09 September 22, 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 32 Part I Fast Multiplication

More information

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College Why analysis? We want to predict how the algorithm will behave (e.g. running time) on arbitrary inputs, and how it will

More information

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem Chapter 2. Divide-and-conquer algorithms The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem. 2. Recursively

More information

Review Of Topics. Review: Induction

Review Of Topics. Review: Induction Review Of Topics Asymptotic notation Solving recurrences Sorting algorithms Insertion sort Merge sort Heap sort Quick sort Counting sort Radix sort Medians/order statistics Randomized algorithm Worst-case

More information

Objec&ves. Review. Divide and conquer algorithms

Objec&ves. Review. Divide and conquer algorithms Objec&ves Divide and conquer algorithms Ø Recurrence rela&ons Ø Coun&ng inversions March 9, 2018 CSCI211 - Sprenkle 1 Review What approach are we learning to solve problems (as of Wednesday)? What is a

More information

MA/CSSE 473 Day 05. Factors and Primes Recursive division algorithm

MA/CSSE 473 Day 05. Factors and Primes Recursive division algorithm MA/CSSE 473 Day 05 actors and Primes Recursive division algorithm MA/CSSE 473 Day 05 HW 2 due tonight, 3 is due Monday Student Questions Asymptotic Analysis example: summation Review topics I don t plan

More information

1 Closest Pair of Points on the Plane

1 Closest Pair of Points on the Plane CS 31: Algorithms (Spring 2019): Lecture 5 Date: 4th April, 2019 Topic: Divide and Conquer 3: Closest Pair of Points on a Plane Disclaimer: These notes have not gone through scrutiny and in all probability

More information

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 //8 Fast Integer Division Too (!) Schönhage Strassen algorithm CS 8: Algorithm Design and Analysis Integer division. Given two n-bit (or less) integers s and t, compute quotient q = s / t and remainder

More information

Data selection. Lower complexity bound for sorting

Data selection. Lower complexity bound for sorting Data selection. Lower complexity bound for sorting Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 12 1 Data selection: Quickselect 2 Lower complexity bound for sorting 3 The

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms Divide and Conquer Algorithms T. M. Murali March 17, 2014 Divide and Conquer Break up a problem into several parts. Solve each part recursively. Solve base cases by brute force. Efficiently combine solutions

More information

Lecture 2: Divide and conquer and Dynamic programming

Lecture 2: Divide and conquer and Dynamic programming Chapter 2 Lecture 2: Divide and conquer and Dynamic programming 2.1 Divide and Conquer Idea: - divide the problem into subproblems in linear time - solve subproblems recursively - combine the results in

More information

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University CS 5321: Advanced Algorithms - Recurrence Ali Ebnenasir Department of Computer Science Michigan Technological University Acknowledgement Eric Torng Moon Jung Chung Charles Ofria Outline Motivating example:

More information

CS 580: Algorithm Design and Analysis

CS 580: Algorithm Design and Analysis CS 580: Algorithm Design and Analysis Jeremiah Blocki Purdue University Spring 208 Announcement: Homework 3 due February 5 th at :59PM Final Exam (Tentative): Thursday, May 3 @ 8AM (PHYS 203) Recap: Divide

More information