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

Size: px
Start display at page:

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

Transcription

1 CSE 22 Divide-and-conquer algorithms Fan Chung Graham UC San Diego

2 Announcements Homework due today before the class. About homework, write your own homework, allowing oral discussion with one fixed partner. Fan s office hour will be held at CSE226 this week. Olivia s office hour will be changed.

3 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 25 Pearson-Addison Wesley. All rights reserved.

4 4.4 Shortest Paths in a Graph shortest path from Princeton CS department to Einstein's house Shortest path tree in Bay area

5 Coin Changing Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. - Gordon Gecko (Michael Douglas)

6 Trees with at most 4 edges 2

7 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 which is of sizes at most n/2 and the other is at most 2n/3. 3

8 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 which is of sizes at most n/2 and the other is at most 2n/3. Try to write a proof for this! 5

9 A planar graph is a graph that can be drawn in the plane without crossings. 6

10 A planar graph is a graph that can be drawn in the plane without any crossing. Are these planar graphs? 7

11 A planar graph is a graph that can be drawn in the plane without any crossing. Are these planar graphs? 8

12 A useful fact about planar graphs Any planar graph on n vertices contains cn n vertices whose removal separates the remaining graph into two parts, one of which is of sizes at most n/2 and the other is at most 2n/3. Tarjan and Lipton, 977 c = 2 2 9

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

14 Sorting Sorting. Given n elements, rearrange in ascending order. 3, 6, 5, 2,, 4, 2, 3, 4, 5, 6 B, U, S, H B, H, S, U Obvious sorting applications. List files in a directory. Organize an MP3 library. List names in a phone book. Display Google PageRank results. 3

15 Mergesort Mergesort.! Divide array into two halves.! Recursively sort each half.! Merge two halves to make sorted whole. Jon von Neumann (945) A L G O R I T H M S A L G O R I T H M S divide O() A G L O R H I M S T sort 2T(n/2) A G H I L M O R S T merge O(n) 6

16 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently?! Linear number of comparisons.! Use temporary array. A G L O R H I M S T A G H I Challenge for the bored. In-place merge. [Kronrud, 969] using only a constant amount of extra storage 7

17 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A auxiliary array

18 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G auxiliary array 2

19 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G H auxiliary array 3

20 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G H I auxiliary array 4

21 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G H I L auxiliary array 5

22 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G H I L M auxiliary array 6

23 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G H I L M O auxiliary array 7

24 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. smallest smallest A G L O R H I M S T A G H I L M O R auxiliary array 8

25 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. first half exhausted smallest A G L O R H I M S T A G H I L M O R S auxiliary array 9

26 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. first half exhausted smallest A G L O R H I M S T A G H I L M O R S T auxiliary array

27 Merging Merge.! Keep track of smallest element in each sorted half.! Insert smallest of two elements into auxiliary array.! Repeat until done. first half exhausted second half exhausted A G L O R H I M S T A G H I L M O R S T auxiliary array

28 Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently?! Linear number of comparisons.! Use temporary array. A G L O R H I M S T A G H I Challenge for the bored. In-place merge. [Kronrud, 969] using only a constant amount of extra storage 7

29

30 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!#" # $ solve left half ( $ % ) + T n /2!#" # $ solve right half + % n otherwise merging Solution. T(n) = O(n log 2 n). Proof: First try the recurrence T (n) 2T (n / 2) + n Assorted proofs. We describe several ways to prove this recurrence. Prove by induction: Suppose for k < n, T (k) ck log k. Initially we assume n is a power of 2 and replace! with =. T (n) 2c(n / 2)log(n / 2) + n cn(logn ) + n cnlogn 8

31 Solving recurrences: T (n) = 2T (n / 2) + n T (n) = O(n logn)

32 Solving recurrences: T (n) = 2T (n / 2) + n T (n) = O(n logn) T (n) = 2T (n / 2) + n 2

33 Solving recurrences: T (n) = 2T (n / 2) + n T (n) = O(n logn) T (n) = 2T (n / 2) + n 2?

34 Solving recurrences: T (n) = 2T (n / 2) + n T (n) = O(n logn) T (n) = 2T (n / 2) + n 2? T (n) = 3T (n / 2) + n log n

35 Solving recurrences: T (n) = 2T (n / 2) + n T (n) = O(n logn) T (n) = 2T (n / 2) + n 2? T (n) = 3T (n / 2) + n log n?

36 5.3 Counting Inversions

37 Counting Inversions Music site tries to match your song preferences with others.! You rank n songs.! Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings.! My rank:, 2,, n.! Your rank: a, a 2,, a n.! Songs i and j inverted if i < j, but a i > a j. Songs Me You A B C D E Inversions 3-2, 4-2 Brute force: check all!(n 2 ) pairs i and j. 24

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

39 Counting Inversions: Divide-and-Conquer Divide-and-conquer

40 Counting Inversions: Divide-and-Conquer Divide-and-conquer.! Divide: separate list into two pieces Divide: O()

41 Counting Inversions: Divide-and-Conquer Divide-and-conquer.! Divide: separate list into two pieces.! Conquer: recursively count inversions in each half Divide: O() Conquer: 2T(n / 2) 5 blue-blue inversions 8 green-green inversions 5-4, 5-2, 4-2, 8-2, , 9-3, 9-7, 2-3, 2-7, 2-, -3, -7 28

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

43 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

44 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves 6 2 auxiliary array Total: 6 2

45 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves 6 2 auxiliary array Total: 6 3

46 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 4

47 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 5

48 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 6

49 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 7

50 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 8

51 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 9

52 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 + 3

53 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: 6 + 3

54 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

55 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

56 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

57 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

58 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

59 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

60 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

61 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

62 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

63 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. first half exhausted i = two sorted halves auxiliary array Total:

64 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

65 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

66 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total:

67 Merge and Count Merge and count step.! Given two sorted halves, count number of inversions where a i and a j are in different halves.! Combine two sorted halves into sorted whole. i = two sorted halves auxiliary array Total: = 3 25

68 Counting Inversions: Combine Combine: count blue-green inversions! Assume each half is sorted.! Count inversions where a i and a j are in different halves.! Merge two sorted halves into sorted whole. to maintain sorted invariant blue-green inversions: Count: O(n) Merge: O(n) T(n)! T " n/2# ( ) + T ( $ n/2% ) + O(n) & T(n) = O(nlog n) 3

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

70 Merge-and-Count(A,B) { Initialize Pointer to the front of A. Pointer2 to the front of B. Count = While A and B are nonempty, compare a i at Pointer with b at Pointer2, append the smaller one to output and advance the pointer by one. If b j is smaller, then increase Count by the number of elements still in A. Endwhile j

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

72 5.4 Closest Pair of Points

73 Closest Pair of Points Closest pair. Given n points in the plane, find a pair with smallest Euclidean distance between them. Fundamental geometric primitive.! Graphics, computer vision, geographic information systems, molecular modeling, air traffic control.! Special case of nearest neighbor, Euclidean MST, Voronoi. 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. to make presentation cleaner 33

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

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

76 Closest Pair of Points Algorithm.! Divide: draw vertical line L so that roughly!n points on each side. L 36

77 Closest Pair of Points Algorithm.! Divide: draw vertical line L so that roughly!n points on each side.! Conquer: find closest pair in each side recursively. L

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

79 Closest Pair of Points Find closest pair with one point in each side, assuming that distance <!. L 2 2! = min(2, 2) 39

80 Closest Pair of Points Find closest pair with one point in each side, assuming that distance <!.! Observation: only need to consider points within! of line L. L 2 2! = min(2, 2)! 4

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

82 Closest Pair of Points Find closest pair with one point in each side, assuming that distance <!.! Observation: only need to consider points within! of line L.! Sort points in 2!-strip by their y coordinate.! Only check distances of those within positions in sorted list! 7 L ! = min(2, 2) 2! 42

83 Closest Pair of Points Def. Let s i be the point in the 2!-strip, with the i th smallest y-coordinate. Claim. If i j " 2, then the distance between s i and s j is at least!. Pf j! No two points lie in same!!-by-!! box.! Two points at least 2 rows apart!! have distance " 2(!!).! 2 rows 29 3!! Fact. Still true if we replace 2 with 7. i 27 28!! 26 25!! 43

84 Closest Pair Algorithm Closest-Pair(p,, p n ) { 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 by y-coordinate. Scan points in y-order and compare distance between each point and next neighbors. If any of these distances is less than!, update!. O(n log n) 2T(n / 2) O(n) O(n log n) O(n) } return!. 44

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

86 5.5 Integer Multiplication

87 47 Integer Arithmetic Add. Given two n-digit integers a and b, compute a + b.! O(n) bit operations. Multiply. Given two n-digit integers a and b, compute a! b.! Brute force solution:!(n 2 ) bit operations. * + Add Multiply

88 Divide-and-Conquer Multiplication: Warmup To multiply two n-digit integers:! Multiply four!n-digit integers.! Add two!n-digit integers, and shift to obtain result. x = 2 n /2! x + x y = 2 n /2! y + y ( ) ( 2 n /2! y + y ) = 2 n! x y + 2 n /2! x y + x y xy = 2 n /2! x + x ( ) + x y ( ) T(n) = 4T n /2!# "# $ +!!(n) " $ " T(n) =!(n2 ) recursive calls add, shift assumes n is a power of 2 48

89 Karatsuba Multiplication To multiply two n-digit integers:! Add two!n digit integers.! Multiply three!n-digit integers.! Add, subtract, and shift!n-digit integers to obtain result. 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 ' T(n) = O(n log 2 3 ) = O(n.585 ) + &(n)!# "# $ add, subtract, shift 49

90 Karatsuba: Recursion Tree T(n) =! " # if n = 3T(n/2) + n otherwise log 2 n T(n) =! n 2 3 = k = ( ) k 3 2 ( ) +log 2 n " 3 2 " = 3n log2 3 " 2 T(n) n T(n/2) T(n/2) T(n/2) 3(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) 9(n/4) T(n / 2 k ) 3 k (n / 2 k ) T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) 3 lg n (2) 5

91 Matrix Multiplication

92 Matrix Multiplication Matrix multiplication. Given two n-by-n matrices A and B, compute C = AB. n! c ij = a ik b kj k =! c c 2! c n $ # & # c 2 c 22! c 2n & # " " # " & # " c n c n2! & c nn % =! a a 2! a n $ # & # a 2 a 22! a 2n & # " " # " & # " a n a n2! & a nn % '! b b 2! b n $ # & # b 2 b 22! b 2n & # " " # " & # " b n b n2! & b nn % Brute force.!(n 3 ) arithmetic operations. Fundamental question. Can we improve upon brute force? 52

93 Matrix Multiplication: Warmup Divide-and-conquer.! Divide: partition A and B into!n-by-!n blocks.! Conquer: multiply 8!n-by-!n recursively.! Combine: add appropriate products using 4 matrix additions.! C C 2 $! # & = A A 2 $! # & ' B B 2 $ # & " C 2 C 22 % " A 2 A 22 % " B 2 B 22 % ( ) + ( A 2! B 2 ) ( ) + ( A 2! B 22 ) ( ) + ( A 22! B 2 ) ( ) + ( A 22! B 22 ) C = A! B C 2 = A! B 2 C 2 = A 2! B C 22 = A 2! B 2 ( ) T(n) = 8T n/2!# "# $ +!(n2 )!##" ## $ recursive calls add, form submatrices " T(n) =!(n 3 ) 53

94 Matrix Multiplication: Key Idea Key idea. multiply 2-by-2 block matrices with only 7 multiplications.! C C 2 $! # & = A A 2 $! # & ' B B 2 $ # & " C 2 C 22 % " A 2 A 22 % " B 2 B 22 % C = P 5 + P 4! P 2 + P 6 C 2 = P + P 2 C 2 = P 3 + P 4 C 22 = P 5 + P! P 3! P 7 P = A! (B 2 " B 22 ) P 2 = ( A + A 2 )! B 22 P 3 = ( A 2 + A 22 )! B P 4 = A 22! (B 2 " B ) P 5 = ( A + A 22 )! (B + B 22 ) P 6 = ( A 2 " A 22 )! (B 2 + B 22 ) P 7 = ( A " A 2 )! (B + B 2 )! 7 multiplications.! 8 = + 8 additions (or subtractions). 54

95 Fast Matrix Multiplication Fast matrix multiplication. (Strassen, 969)! Divide: partition A and B into!n-by-!n blocks.! Compute: 4!n-by-!n matrices via matrix additions.! Conquer: multiply 7!n-by-!n matrices recursively.! Combine: 7 products into 4 terms using 8 matrix additions. Analysis.! Assume n is a power of 2.! T(n) = # arithmetic operations. ( ) T(n) = 7T n /2!# "# $ +!(n2 )!#" # $ recursive calls add, subtract " T(n) =!(n log 2 7 ) = O(n 2.8 ) 55

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

97 Fast Matrix Multiplication in Theory Q. Multiply two 2-by-2 matrices with only 7 scalar multiplications? A. Yes! [Strassen, 969]!(n log 2 7 ) = O(n 2.8 ) Q. Multiply two 2-by-2 matrices with only 6 scalar multiplications? A. Impossible. [Hopcroft and Kerr, 97] Q. Two 3-by-3 matrices with only 2 scalar multiplications? A. Also impossible.!(n log 2 6 ) = O(n 2.59 )!(n log 3 2 ) = O(n 2.77 ) Q. Two 7-by-7 matrices with only 43,64 scalar multiplications? A. Yes! [Pan, 98] Decimal wars.! December, 979: O(n ).! January, 98: O(n ).!(n log ) = O(n 2.8 ) 57

98 Fast Matrix Multiplication in Theory Best known. O(n ) [Coppersmith-Winograd, 987.] Conjecture. O(n 2+! ) for any! >. Caveat. Theoretical improvements to Strassen are progressively less practical. 58

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

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

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

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

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

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

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

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

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

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

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

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

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 421 Algorithms: Divide and Conquer

CSE 421 Algorithms: Divide and Conquer CSE 42 Algorithms: Divide and Conquer Larry Ruzzo Thanks to Paul Beame, Kevin Wayne for some slides Outline: General Idea algorithm design paradigms: divide and conquer Review of Merge Sort Why does it

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

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

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

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

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

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

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

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

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

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

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

Algorithms. Algorithms ALGORITHM DESIGN

Algorithms. Algorithms ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE ALGORITHM DESIGN Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer

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

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

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

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

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

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

Divide and Conquer Algorithms

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

More information

Lecture 15 : Review DRAFT

Lecture 15 : Review DRAFT CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.

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

Lecture 7: Divide & Conquer 2. Integer Multiplication. & Matrix Multiplication. CS 341: Algorithms. Tuesday, Jan 29 th 2019

Lecture 7: Divide & Conquer 2. Integer Multiplication. & Matrix Multiplication. CS 341: Algorithms. Tuesday, Jan 29 th 2019 Lecture 7: Divide & Conquer 2 Integer Multiplication & Matrix Multiplication CS 341: Algorithms Tuesday, Jan 29 th 2019 1 Outline For Today 1. Integer Multiplication 2. Matrix Multiplication 2 Outline

More information

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov Algorithms Divide and Conquer 4-2 Divide and Conquer, MergeSort Recursive algorithms: Call themselves on subproblem Divide and Conquer

More information

Reductions. Linear Time Reductions. Desiderata. Reduction. Desiderata. Classify problems according to their computational requirements.

Reductions. Linear Time Reductions. Desiderata. Reduction. Desiderata. Classify problems according to their computational requirements. Desiderata Reductions Desiderata. Classify problems according to their computational requirements. Frustrating news. Huge number of fundamental problems have defied classification for decades. Desiderata'.

More information

Algorithms. Algorithms 6.5 REDUCTIONS. introduction designing algorithms establishing lower bounds classifying problems intractability

Algorithms. Algorithms 6.5 REDUCTIONS. introduction designing algorithms establishing lower bounds classifying problems intractability Algorithms ROBERT SEDGEWICK KEVIN WAYNE 6.5 REDUCTIONS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction designing algorithms establishing lower bounds classifying problems

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

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

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

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

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 018 D/Q Greed SP s DP LP, Flow B&B, Backtrack Metaheuristics P, NP Design and Analysis of Algorithms Lecture 8: Greed Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Optimization

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

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 16 Dynamic Programming (plus FFT Recap) Adam Smith 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Discrete Fourier Transform

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

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

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 Number of Inversions Formal problem: Given: array,,,, of distinct elements Objective: Compute number of inversions 0 Example: 4, 1,

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

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

Lecture 9 - Matrix Multiplication Equivalences and Spectral Graph Theory 1

Lecture 9 - Matrix Multiplication Equivalences and Spectral Graph Theory 1 CME 305: Discrete Mathematics and Algorithms Instructor: Professor Aaron Sidford (sidford@stanfordedu) February 6, 2018 Lecture 9 - Matrix Multiplication Equivalences and Spectral Graph Theory 1 In the

More information

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION 1. What is performance measurement? 2. What is an algorithm? 3. How the algorithm is good? 4. What are the

More information

CMSC 451: Divide and Conquer

CMSC 451: Divide and Conquer CMSC 451: Divide and Conquer Slides By: Carl Kingsford Department of Computer Science University of Maryland, College Park Based on Sections 5.1 5.3 of Algorithm Design by Kleinberg & Tardos. Greedy Recap

More information

Parallel Models RAM. Parallel RAM aka PRAM. Variants of CRCW PRAM. Advanced Algorithms

Parallel Models RAM. Parallel RAM aka PRAM. Variants of CRCW PRAM. Advanced Algorithms Parallel Models Advanced Algorithms Piyush Kumar (Lecture 10: Parallel Algorithms) An abstract description of a real world parallel machine. Attempts to capture essential features (and suppress details?)

More information

CMPSCI 311: Introduction to Algorithms Practice Final Exam

CMPSCI 311: Introduction to Algorithms Practice Final Exam CMPSCI 311: Introduction to Algorithms Practice Final Exam Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more detail including

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

Binary Search to find item in sorted array

Binary Search to find item in sorted array Binary Search to find item in sorted array January 15, 2008 QUESTION: Suppose we are given a sorted list A[1..n] (as an array), of n real numbers: A[1] A[2] A[n]. Given a real number x, decide whether

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

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 Linear Time: O(n) CS 580: Algorithm Design and Analysis 2.4 A Survey of Common Running Times Merge. Combine two sorted lists A = a 1,a 2,,a n with B = b 1,b 2,,b n into sorted whole. Jeremiah Blocki Purdue

More information

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018 CS 580: Algorithm Design and Analysis Jeremiah Blocki Purdue University Spring 2018 Chapter 11 Approximation Algorithms Slides by Kevin Wayne. Copyright @ 2005 Pearson-Addison Wesley. All rights reserved.

More information

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher Date of Performance:... Actual Date of Completion:... Expected Date of Completion:... ----------------------------------------------------------------------------------------------------------------

More information

CSE 3101 Design and Analysis of Algorithms Practice Test for Unit 1 Loop Invariants and Iterative Algorithms

CSE 3101 Design and Analysis of Algorithms Practice Test for Unit 1 Loop Invariants and Iterative Algorithms CSE 0 Design and Analysis of Algorithms Practice Test for Unit Loop Invariants and Iterative Algorithms Jeff Edmonds First learn the steps. Then try them on your own. If you get stuck only look at a little

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

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

Lesson 2 7 Graph Partitioning

Lesson 2 7 Graph Partitioning Lesson 2 7 Graph Partitioning The Graph Partitioning Problem Look at the problem from a different angle: Let s multiply a sparse matrix A by a vector X. Recall the duality between matrices and graphs:

More information

Sorting Algorithms. + Analysis of the Sorting Algorithms

Sorting Algorithms. + Analysis of the Sorting Algorithms Sorting Algorithms + Analysis of the Sorting Algorithms Insertion Sort What if first k elements of array are already sorted? 4, 7, 12, 5, 19, 16 We can shift the tail of the sorted elements list down and

More information

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018 CS 580: Algorithm Design and Analysis Jeremiah Blocki Purdue University Spring 2018 Recap: Stable Matching Problem Definition of a Stable Matching Stable Roomate Matching Problem Stable matching does not

More information

Algorithms and Data Structures, or

Algorithms and Data Structures, or Algorithms and Data Structures, or... Classical Algorithms of the 50s, 60s and 70s Mary Cryan A&DS Lecture 1 1 Mary Cryan Our focus Emphasis is Algorithms ( Data Structures less important). Most of the

More information

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Page 1 UNIT I INTRODUCTION 2 marks 1. Why is the need of studying algorithms? From a practical standpoint, a standard set of algorithms from different

More information

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

COMP 3403 Algorithm Analysis Part 2 Chapters 4 5. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University COMP 3403 Algorithm Analysis Part 2 Chapters 4 5 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University Chapter 4 Decrease and Conquer Chapter 4 43 Chapter 4: Decrease and Conquer Idea:

More information

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211 Objectives Review implementation of Stable Matching Survey of common running times Turn in completed problem sets Jan 18, 2019 Sprenkle - CSCI211 1 Review: Asymptotic Analysis of Gale-Shapley Alg Not explicitly

More information

UNIT-2 DIVIDE & CONQUER

UNIT-2 DIVIDE & CONQUER Overview: Divide and Conquer Master theorem Master theorem based analysis for Binary Search Merge Sort Quick Sort Divide and Conquer UNIT-2 DIVIDE & CONQUER Basic Idea: 1. Decompose problems into sub instances.

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

Chapter 8. NP and Computational Intractability. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 8. NP and Computational Intractability. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 8 NP and Computational Intractability Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 Algorithm Design Patterns and Anti-Patterns Algorithm design patterns.

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM NAME: This exam is open: - books - notes and closed: - neighbors - calculators ALGORITHMS QUALIFYING EXAM The upper bound on exam time is 3 hours. Please put all your work on the exam paper. (Partial credit

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

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

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

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

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors 1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors on an EREW PRAM: See solution for the next problem. Omit the step where each processor sequentially computes the AND of

More information

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

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted. CS6402 Design and Analysis of Algorithms _ Unit II 2.1 UNIT II BRUTE FORCE AND DIVIDE-AND-CONQUER 2.1 BRUTE FORCE Brute force is a straightforward approach to solving a problem, usually directly based

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

Data Structures and Algorithms. Part 2

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

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

Systems of Inequalities and Linear Programming 5.7 Properties of Matrices 5.8 Matrix Inverses

Systems of Inequalities and Linear Programming 5.7 Properties of Matrices 5.8 Matrix Inverses 5 5 Systems and Matrices Systems and Matrices 5.6 Systems of Inequalities and Linear Programming 5.7 Properties of Matrices 5.8 Matrix Inverses Sections 5.6 5.8 2008 Pearson Addison-Wesley. All rights

More information

10. EXTENDING TRACTABILITY

10. EXTENDING TRACTABILITY 0. EXTENDING TRACTABILITY finding small vertex covers solving NP-hard problems on trees circular arc coverings vertex cover in bipartite graphs Lecture slides by Kevin Wayne Copyright 005 Pearson-Addison

More information

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

Today. Types of graphs. Complete Graphs. Trees. Hypercubes. Today. Types of graphs. Complete Graphs. Trees. Hypercubes. Complete Graph. K n complete graph on n vertices. All edges are present. Everyone is my neighbor. Each vertex is adjacent to every other vertex.

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

Reference Sheet for CO142.2 Discrete Mathematics II

Reference Sheet for CO142.2 Discrete Mathematics II Reference Sheet for CO14. Discrete Mathematics II Spring 017 1 Graphs Defintions 1. Graph: set of N nodes and A arcs such that each a A is associated with an unordered pair of nodes.. Simple graph: no

More information

10. EXTENDING TRACTABILITY

10. EXTENDING TRACTABILITY 0. EXTENDING TRACTABILITY finding small vertex covers solving NP-hard problems on trees circular arc coverings vertex cover in bipartite graphs Lecture slides by Kevin Wayne Copyright 005 Pearson-Addison

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

! Greed. O(n log n) interval scheduling. ! Divide-and-conquer. O(n log n) FFT. ! Dynamic programming. O(n 2 ) edit distance.

! Greed. O(n log n) interval scheduling. ! Divide-and-conquer. O(n log n) FFT. ! Dynamic programming. O(n 2 ) edit distance. Algorithm Design Patterns and Anti-Patterns 8. NP and Computational Intractability Algorithm design patterns. Ex.! Greed. O(n log n) interval scheduling.! Divide-and-conquer. O(n log n) FFT.! Dynamic programming.

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

Greedy algorithms is another useful way for solving optimization problems.

Greedy algorithms is another useful way for solving optimization problems. Greedy Algorithms Greedy algorithms is another useful way for solving optimization problems. Optimization Problems For the given input, we are seeking solutions that must satisfy certain conditions. These

More information

CS 380 ALGORITHM DESIGN AND ANALYSIS

CS 380 ALGORITHM DESIGN AND ANALYSIS CS 380 ALGORITHM DESIGN AND ANALYSIS Lecture 1: Course Introduction and Motivation Text Reference: Chapters 1, 2 Syllabus Book Schedule Grading: Assignments/Projects/Exams/Quizzes Policies Late Policy

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

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red. COS 521 Fall 2009 Notes on Minimum Spanning Trees 1. The Generic Greedy Algorithm The generic greedy algorithm finds a minimum spanning tree (MST) by an edge-coloring process. Initially all edges are uncolored.

More information