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 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 Closest points Integer Multiplication Finding & Solving Recurrences 4

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: Mergesort, Binary Search, Strassen s Algorithm, Quicksort (roughly) 5

4 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++ ; Return C; } L split sort merge 6

5 why balanced subdivision? Alternative "divide & conquer" algorithm: Sort n- Sort last Merge them T(n) = T(n-)+T()+3n for n 2 T() = Solution: 3n + 3(n-) + 3(n-2) = Θ(n 2 ) 7

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

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! 9

8 d&c approach, cont. Moral 3: unbalanced division less good: (.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. () 2 + (n-) 2 + n = n 2-2n n Little improvement here.

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

10 A Divide & Conquer Example: Closest Pair of Points 2

11 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? 3

12 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 4

13 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 5

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

15 closest pair of points: st try Divide. Sub-divide region into 4 quadrants. Obstacle. Impossible to ensure n/4 points in each piece. 7

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

17 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 2 2 9

18 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 )?

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

20 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) δ 22

21 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 δ 23

22 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 δ 24

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

24 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 δ. 26

25 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? 28

26 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 n =& ' ( ( ) + O(n logn) n > ) C(n) = O(n log 2 n) Q. Can we achieve O(n log 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) 29

27 Going From Code to Recurrence 3

28 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) 3

29 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 32

30 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. 33

31 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) 34

32 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 O(n) 35

33 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? 36

34 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) 37

35 Basic operations: comparisons 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) Delete all points further than δ from separation line L Sort remaining points p[] p[m] by y-coordinate. O(n log n) 2C(n / 2) O(n) O(n log n) } 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 δ. O(n) One recursive level 38

36 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 n =& ' ( ( ) + O(n logn) n > ) C(n) = O(n log 2 n) Q. Can we achieve time O(n log 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) 39

37 Integer Multiplication 4

38 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. 4 + Add * Multiply

39 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

40 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 ) y y x x x y ( ) + x y = " x y + " x y + x y 8 x y Same idea works for long integers can split them into 4 half-sized ints x y x y 43

41 divide & conquer multiplication: warmup To multiply two n-bit integers: Multiply four ½n-bit integers. Add two ½n-bit integers, and shift 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 44

42 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 45

43 To multiply two n-bit integers: Add two ½n bit integers. Multiply three ½n-bit integers. Karatsuba multiplication Add, subtract, and shift ½n-bit integers to obtain result. 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 46

44 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 47

45 Idea: Two halves are better than a whole d & c summary if the base algorithm has super-linear complexity. If a little's good, then more's better repeat above, recursively Applications: Many. Binary Search, Merge Sort, (Quicksort), Closest points, Integer multiply, 48

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

47 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 5

48 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) 5

49 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 = 52 (2 k ) 2 = n 2

50 Solve: T() = c T(n) = 3 T(n/2) + cn n = 2 k ; k = log 2 n k Total Work: T(n) =! 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 53

51 a useful identity Theorem: + 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-) 54

52 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= ( ) i = cn 3 2 ( 3 ) k+ " = cn 2 ( ) "

53 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 56

54 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 57

55 T(n) = at(n/b)+cn k for n > b then divide and conquer master recurrence a > b k T(n) =!( n logb 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: a ; b > ; c, d, k ; T() = d; n = b t for some t > ; a, b, k, t integers. True even if it is n/b instead of n/b. 58

56 master recurrence: proof sketch Expanding recurrence as in earlier examples, to get T(n) = n g ( d + c S ) where g = log b (a) and S =! log b n j=, where x = b k /a. If c = the sum S is irrelevant, and T(n) = O(n g ): all the work happens in the base cases, of which there are n g, 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(). [S is just the first log n terms of the infinite series with that sum]. if x =, then S = log b (n) = O(log n). [all terms in the sum are and there are that many terms]. if x >, then S = x * (x +log b (n) -)/(x-). After some algebra, n g * S = O(n k ) x j 59

57 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 6

58 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 6

59 Let M(n) be number of multiplies Worst-case recurrence: By master theorem & ( M(n) = ' )( M(n) = O(log n) (a=, b=2, k=) More precise analysis: analysis 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 n! M ("# n / 2$% ) + 2 n > 62

60 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 63

61 d & c summary Idea: 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 Analysis: recursion tree or Master Recurrence Applications: Many. Binary Search, Merge Sort, (Quicksort), Closest points, Integer multiply, exponentiation, 64

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication Shayan Oveis Gharan 1 Finding the Closest Pair of Points Closest Pair of Points (non geometric) Given n points and arbitrary distances

More information

5.4 Closest Pair of Points

5.4 Closest Pair of Points 5.4 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

More information

Divide and Conquer 1

Divide and Conquer 1 Divide and Conquer A Useful Recurrence Relation Def. T(n) = number of comparisons to mergesort an input of size n. Mergesort recurrence. T(n) if n T n/2 T n/2 solve left half solve right half merging n

More information

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg CSE 1 Greedy Alg: Union Find/Dijkstra s Alg Shayan Oveis Gharan 1 Dijkstra s Algorithm Dijkstra(G, c, s) { d s 0 foreach (v V) d[v] //This is the key of node v foreach (v V) insert v onto a priority queue

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 13 Divide and Conquer Closest Pair of Points Convex Hull Strassen Matrix Mult. Adam Smith 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova,

More information

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

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n. Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright 25 Pearson-Addon Wesley. All rights reserved. Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively.

More information

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

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 5 Divide and Conquer 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 each part

More information

Ch5. Divide-and-Conquer

Ch5. Divide-and-Conquer Ch5. Divide-and-Conquer 1 5.1 Mergesort Sorting Sorting. Given n elements, rearrange in ascending order. Applications. Sort a list of names. Organize an MP3 library. obvious applications Display Google

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 Copyright 2013

More information

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego CSE 22 Divide-and-conquer algorithms Fan Chung Graham UC San Diego A useful fact about trees Any tree on n vertices contains a vertex v whose removal separates the remaining graph into two parts, one of

More information

Closest Pair of Points in the Plane. Closest pair of points. Closest Pair of Points. Closest Pair of Points

Closest Pair of Points in the Plane. Closest pair of points. Closest Pair of Points. Closest Pair of Points Closest Pair of Points Closest pair of points. Given n points in the plane, find a pair with smallest euclidean distance between them. Closest Pair of Points in the Plane Inge i Gørtz The slides on the

More information

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points Plan for Today Finish recurrences Inversion Counting Closest Pair of Points Divide and Conquer Divide-and-conquer. Divide problem into several parts. Solve each part recursively. Combine solutions to sub-problems

More information

Divide-Conquer-Glue Algorithms

Divide-Conquer-Glue Algorithms Divide-Conquer-Glue Algorithms Closest Pair Tyler Moore CSE 3353, SMU, Dallas, TX Lecture 12 5. DIVIDE AND CONQUER mergesort counting inversions closest pair of points randomized quicksort median and selection

More information

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego CSE 22 Divide-and-conquer algorithms Fan Chung Graham UC San Diego Announcements Homework due today before the class. About homework, write your own homework, allowing oral discussion with one fixed partner.

More information

Closest Pair of Points. Cormen et.al 33.4

Closest Pair of Points. Cormen et.al 33.4 Closest Pair of Points Cormen et.al 33.4 Closest Pair of Points Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric problem. Graphics,

More information

Lecture 4 CS781 February 3, 2011

Lecture 4 CS781 February 3, 2011 Lecture 4 CS78 February 3, 2 Topics: Data Compression-Huffman Trees Divide-and-Conquer Solving Recurrence Relations Counting Inversions Closest Pair Integer Multiplication Matrix Multiplication Data Compression

More information

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

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

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

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

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 4 Divide-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Divide-and-Conquer The most-well known algorithm design strategy: 2. Divide instance of problem into two or more

More information

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

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original

More information

Divide and conquer algorithms. March 12, 2018 CSCI211 - Sprenkle. What is a recurrence rela&on? How can you compute D&C running &mes?

Divide and conquer algorithms. March 12, 2018 CSCI211 - Sprenkle. What is a recurrence rela&on? How can you compute D&C running &mes? Objec&ves Divide and conquer algorithms Ø Coun&ng inversions Ø Closest pairs of points March 1, 018 CSCI11 - Sprenkle 1 Review What is a recurrence rela&on? How can you compute D&C running &mes? March

More information

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

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול Advanced Algorithms Problem solving Techniques. Divide and Conquer הפרד ומשול 1 Divide and Conquer A method of designing algorithms that (informally) proceeds as follows: Given an instance of the problem

More information

Divide-and-Conquer Algorithms

Divide-and-Conquer Algorithms Divide-and-Conquer Algorithms Divide and Conquer Three main steps Break input into several parts, Solve the problem in each part recursively, and Combine the solutions for the parts Contribution Applicable

More information

Test 1 Review Questions with Solutions

Test 1 Review Questions with Solutions CS3510 Design & Analysis of Algorithms Section A Test 1 Review Questions with Solutions Instructor: Richard Peng Test 1 in class, Wednesday, Sep 13, 2017 Main Topics Asymptotic complexity: O, Ω, and Θ.

More information

Divide and Conquer. Algorithm Fall Semester

Divide and Conquer. Algorithm Fall Semester Divide and Conquer Algorithm 2014 Fall Semester Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances

More information

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides Jana Kosecka Linear Time Sorting, Median, Order Statistics Many slides here are based on E. Demaine, D. Luebke slides Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on

More information

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1 DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same

More information

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

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conuer Algorithm Design Techniue Divide-and-Conuer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

Divide-and-Conquer. Dr. Yingwu Zhu

Divide-and-Conquer. Dr. Yingwu Zhu Divide-and-Conquer Dr. Yingwu Zhu Divide-and-Conquer The most-well known algorithm design technique: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances independently

More information

Recursive Algorithms II

Recursive Algorithms II Recursive Algorithms II Margaret M. Fleck 23 October 2009 This lecture wraps up our discussion of algorithm analysis (section 4.4 and 7.1 of Rosen). 1 Recap Last lecture, we started talking about mergesort,

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Sorting So Far

More information

Randomized algorithms. Inge Li Gørtz

Randomized algorithms. Inge Li Gørtz Randomized algorithms Inge Li Gørtz 1 Randomized algorithms Today What are randomized algorithms? Properties of randomized algorithms Three examples: Median/Select. Quick-sort Closest pair of points 2

More information

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

The Limits of Sorting Divide-and-Conquer Comparison Sorts II The Limits of Sorting Divide-and-Conquer Comparison Sorts II CS 311 Data Structures and Algorithms Lecture Slides Monday, October 12, 2009 Glenn G. Chappell Department of Computer Science University of

More information

SORTING AND SELECTION

SORTING AND SELECTION 2 < > 1 4 8 6 = 9 CHAPTER 12 SORTING AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016)

More information

Algorithms: Lecture 7. Chalmers University of Technology

Algorithms: Lecture 7. Chalmers University of Technology Algorithms: Lecture 7 Chalmers University of Technology Today s Lecture Divide & Conquer Counting Inversions Closest Pair of Points Multiplication of large integers Intro to the forthcoming problems Graphs:

More information

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation Comp2711 S1 2006 Correctness Oheads 1 Efficiency Issues Comp2711 S1 2006 Correctness Oheads 2 Run Times An implementation may be correct with respect to the Specification Pre- and Post-condition, but nevertheless

More information

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

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 4 More Divide and Conquer Binary Search Exponentiation Multiplication Sofya Raskhodnikova and Adam Smith Review questions How long does Merge Sort take on

More information

Union Find. Data Structures and Algorithms Andrei Bulatov

Union Find. Data Structures and Algorithms Andrei Bulatov Union Find Data Structures and Algorithms Andrei Bulatov Algorithms Union Find 6-2 Union Find In a nutshell, Kruskal s algorithm starts with a completely disjoint graph, and adds edges creating a graph

More information

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT. Chapter 3:- Divide and Conquer Compiled By:- Assistant Professor, SVBIT. Outline Introduction Multiplying large Integers Problem Problem Solving using divide and conquer algorithm - Binary Search Sorting

More information

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm Divide and Conquer Chapter 6 Divide and Conquer A divide and conquer algorithm divides the problem instance into a number of subinstances (in most cases 2), recursively solves each subinsance separately,

More information

CS473 - Algorithms I

CS473 - Algorithms I CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1 Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine

More information

Data Structures and Algorithms CMPSC 465

Data Structures and Algorithms CMPSC 465 Data Structures and Algorithms CMPSC 465 LECTURES 7-8 More Divide and Conquer Multiplication Adam Smith S. Raskhodnikova and A. Smith; based on slides by E. Demaine and C. Leiserson John McCarthy (1927

More information

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

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search

More information

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

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide and-conquer is a very common and very powerful algorithm design technique. The general idea:

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

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook by Dr. Y.

More information

Computer Science Approach to problem solving

Computer Science Approach to problem solving Computer Science Approach to problem solving If my boss / supervisor / teacher formulates a problem to be solved urgently, can I write a program to efficiently solve this problem??? Polynomial-Time Brute

More information

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook by Dr. Y.

More information

Divide and Conquer. Divide and Conquer

Divide and Conquer. Divide and Conquer October 6, 2017 Divide and Conquer Chapter 2 of Dasgupta et al. 1 Divide and Conquer Divide: If the input size is too large to deal with in a straightforward manner, divide the data into two or more disjoint

More information

Divide and Conquer 4-0

Divide and Conquer 4-0 Divide and Conquer 4-0 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain

More information

Algorithms with numbers (1) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

Algorithms with numbers (1) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017 Algorithms with numbers (1) CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Spring 2017 Acknowledgement The set of slides have used materials from the following resources Slides for

More information

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic Algorithms Union Find 16- Union Find Union Find Data Structures and Algorithms Andrei Bulatov In a nutshell, Kruskal s algorithm starts with a completely disjoint graph, and adds edges creating a graph

More information

Intro to Algorithms. Professor Kevin Gold

Intro to Algorithms. Professor Kevin Gold Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in

More information

Master Theorem, Introduction to Graphs

Master Theorem, Introduction to Graphs Master Theorem, Introduction to Graphs CSE21 Winter 2017, Day 10 (B00), Day 6-7 (A00) February 1, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Divide & Conquer: General Strategy Divide the problem of

More information

Elementary maths for GMT. Algorithm analysis Part II

Elementary maths for GMT. Algorithm analysis Part II Elementary maths for GMT Algorithm analysis Part II Algorithms, Big-Oh and Big-Omega An algorithm has a O( ) and Ω( ) running time By default, we mean the worst case running time A worst case O running

More information

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

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

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

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Problem solving paradigms

Problem solving paradigms Problem solving paradigms Bjarki Ágúst Guðmundsson Tómas Ken Magnússon Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University Today we re going to cover Problem solving

More information

Unit-2 Divide and conquer 2016

Unit-2 Divide and conquer 2016 2 Divide and conquer Overview, Structure of divide-and-conquer algorithms, binary search, quick sort, Strassen multiplication. 13% 05 Divide-and- conquer The Divide and Conquer Paradigm, is a method of

More information

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

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

n = 1 What problems are interesting when n is just 1?

n = 1 What problems are interesting when n is just 1? What if n=1??? n = 1 What problems are interesting when n is just 1? Sorting? No Median finding? No Addition? How long does it take to add one pair of numbers? Multiplication? How long does it take to

More information

Midterm solutions. n f 3 (n) = 3

Midterm solutions. n f 3 (n) = 3 Introduction to Computer Science 1, SE361 DGIST April 20, 2016 Professors Min-Soo Kim and Taesup Moon Midterm solutions Midterm solutions The midterm is a 1.5 hour exam (4:30pm 6:00pm). This is a closed

More information

Mergesort again. 1. Split the list into two equal parts

Mergesort again. 1. Split the list into two equal parts Quicksort Mergesort again 1. Split the list into two equal parts 5 3 9 2 8 7 3 2 1 4 5 3 9 2 8 7 3 2 1 4 Mergesort again 2. Recursively mergesort the two parts 5 3 9 2 8 7 3 2 1 4 2 3 5 8 9 1 2 3 4 7 Mergesort

More information

Divide & Conquer Design Technique

Divide & Conquer Design Technique Divide & Conquer Design Technique Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY 1 The Divide & Conquer strategy can be described in general terms as follows: A

More information

Lecture 2: Divide and Conquer

Lecture 2: Divide and Conquer Lecture 2: Divide and Conquer Paradigm Convex Hull Median finding Paradigm Given a problem of size n divide it into subproblems of size n, a 1, b >1. Solve each b subproblem recursively. Combine solutions

More information

University of the Western Cape Department of Computer Science

University of the Western Cape Department of Computer Science University of the Western Cape Department of Computer Science Algorithms and Complexity CSC212 Paper II Final Examination 13 November 2015 Time: 90 Minutes. Marks: 100. UWC number Surname, first name Mark

More information

1 Closest Pair Problem

1 Closest Pair Problem 1 Closest Pair Problem Computational Geometry, Lectures 3,4 Closest Pair Problem Scribe Varun Nayyar Given a set of n points determine points a,b such that the interpoint distance ( Euclidean ) is the

More information

! Addition! Multiplication! Bigger Example - RSA cryptography

! Addition! Multiplication! Bigger Example - RSA cryptography ! Addition! Multiplication! Bigger Example - RSA cryptography Modular Arithmetic Modular Exponentiation Primality Testing (Fermat s little theorem) Probabilistic algorithm Euclid s Algorithm for gcd (greatest

More information

Parallel Sorting Algorithms

Parallel Sorting Algorithms CSC 391/691: GPU Programming Fall 015 Parallel Sorting Algorithms Copyright 015 Samuel S. Cho Sorting Algorithms Review Bubble Sort: O(n ) Insertion Sort: O(n ) Quick Sort: O(n log n) Heap Sort: O(n log

More information

Lecture 3: Sorting 1

Lecture 3: Sorting 1 Lecture 3: Sorting 1 Sorting Arranging an unordered collection of elements into monotonically increasing (or decreasing) order. S = a sequence of n elements in arbitrary order After sorting:

More information

CSCE 411 Design and Analysis of Algorithms

CSCE 411 Design and Analysis of Algorithms 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

More information

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS CHAPTER 11 SORTING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND

More information

Lecture 13: Divide and Conquer (1997) Steven Skiena. skiena

Lecture 13: Divide and Conquer (1997) Steven Skiena.   skiena Lecture 13: Divide and Conquer (1997) Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Problem Solving Techniques Most

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Solutions to Exam Data structures (X and NV)

Solutions to Exam Data structures (X and NV) Solutions to Exam Data structures X and NV 2005102. 1. a Insert the keys 9, 6, 2,, 97, 1 into a binary search tree BST. Draw the final tree. See Figure 1. b Add NIL nodes to the tree of 1a and color it

More information

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

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk 8/29/06 CS 6463: AT Computational Geometry 1 Convex Hull Problem Given a set of pins on a pinboard and a rubber band around them.

More information

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University Algorithms (IX) Guoqiang Li School of Software, Shanghai Jiao Tong University Q: What we have learned in Algorithm? Algorithm Design Algorithm Design Basic methodologies: Algorithm Design Algorithm Design

More information

Lecture 8: Mergesort / Quicksort Steven Skiena

Lecture 8: Mergesort / Quicksort Steven Skiena Lecture 8: Mergesort / Quicksort Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Give an efficient

More information

Sorting Pearson Education, Inc. All rights reserved.

Sorting Pearson Education, Inc. All rights reserved. 1 19 Sorting 2 19.1 Introduction (Cont.) Sorting data Place data in order Typically ascending or descending Based on one or more sort keys Algorithms Insertion sort Selection sort Merge sort More efficient,

More information

That is, does the algorithm s output always satisfy the input to output specification that we desire?

That is, does the algorithm s output always satisfy the input to output specification that we desire? CS 125 Course Notes 1 Fall 2016 Welcome to CS 125, a course on algorithms and computational complexity. First, what do these terms means? An algorithm is a recipe or a well-defined procedure for performing

More information

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Module 07 Lecture - 38 Divide and Conquer: Closest Pair of Points We now look at another divide and conquer algorithm,

More information

11. APPROXIMATION ALGORITHMS

11. APPROXIMATION ALGORITHMS 11. APPROXIMATION ALGORITHMS load balancing center selection pricing method: vertex cover LP rounding: vertex cover generalized load balancing knapsack problem Lecture slides by Kevin Wayne Copyright 2005

More information

SORTING, SETS, AND SELECTION

SORTING, SETS, AND SELECTION CHAPTER 11 SORTING, SETS, AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM

More information

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Merge Sort & Quick Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm

More information

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION DESIGN AND ANALYSIS OF ALGORITHMS Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION http://milanvachhani.blogspot.in EXAMPLES FROM THE SORTING WORLD Sorting provides a good set of examples for analyzing

More information

CSE 202: Design and Analysis of Algorithms Lecture 5

CSE 202: Design and Analysis of Algorithms Lecture 5 CSE 202: Design and Analysis of Algorithms Lecture 5 Instructor: Kamalika Chaudhuri Announcements Kamalika s Office hours today moved to tomorrow, 12:15-1:15pm Homework 1 due now Midterm on Feb 14 Algorithm

More information

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism Adam Blank Lecture 2 Winter 2017 CSE 332 Data Structures and Parallelism CSE 332: Data Structures and Parallelism Algorithm Analysis 1 Outline 1 Comparing Algorithms 2 Asymptotic Analysis Comparing Programs

More information

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Comparison Sorts. Chapter 9.4, 12.1, 12.2 Comparison Sorts Chapter 9.4, 12.1, 12.2 Sorting We have seen the advantage of sorted data representations for a number of applications Sparse vectors Maps Dictionaries Here we consider the problem of

More information

7.3 Divide-and-Conquer Algorithm and Recurrence Relations

7.3 Divide-and-Conquer Algorithm and Recurrence Relations 73 Divide-and-Conquer Algorithm and Recurrence Relations Many recursive algorithms take a problem with a given input and divide it into one or more smaller problems This reduction is repeatedly applied

More information

CSE 202: Design and Analysis of Algorithms Lecture 4

CSE 202: Design and Analysis of Algorithms Lecture 4 CSE 202: Design and Analysis of Algorithms Lecture 4 Instructor: Kamalika Chaudhuri Greedy Algorithms Direct argument - MST Exchange argument - Caching Greedy approximation algorithms Greedy Approximation

More information

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

Lecture 18. What we ve done and what s to come

Lecture 18. What we ve done and what s to come Lecture 18 What we ve done and what s to come Announcements Final exam: 3:30 6:30pm, Wednesday 12/13, 320-105. You may bring two double-sided sheets of notes Format: similar to the midterm Resources Practice

More information

CS 171: Introduction to Computer Science II. Quicksort

CS 171: Introduction to Computer Science II. Quicksort CS 171: Introduction to Computer Science II Quicksort Roadmap MergeSort Recursive Algorithm (top-down) Practical Improvements Non-recursive algorithm (bottom-up) Analysis QuickSort Algorithm Analysis Practical

More information

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms Part 2 1 3 Maximum Selection Problem : Given n numbers, x 1, x 2,, x

More information

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

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search

More information

Sorting. Divide-and-Conquer 1

Sorting. Divide-and-Conquer 1 Sorting Divide-and-Conquer 1 Divide-and-Conquer 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conquer 2 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide:

More information

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.

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. Problem 5. Sorting Simple Sorting, Quicksort, Mergesort 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. 98 99 Selection Sort

More information

Searching, Sorting. part 1

Searching, Sorting. part 1 Searching, Sorting part 1 Week 3 Objectives Searching: binary search Comparison-based search: running time bound Sorting: bubble, selection, insertion, merge Sorting: Heapsort Comparison-based sorting

More information

CSC 421: Algorithm Design & Analysis. Spring 2015

CSC 421: Algorithm Design & Analysis. Spring 2015 CSC 421: Algorithm Design & Analysis Spring 2015 Divide & conquer divide-and-conquer approach familiar examples: merge sort, quick sort other examples: closest points, large integer multiplication tree

More information