Divide-and-conquer. Curs 2015

Size: px
Start display at page:

Download "Divide-and-conquer. Curs 2015"

Transcription

1 Divide-and-conquer Curs 2015

2 The divide-and-conquer strategy. 1. Break the problem into smaller subproblems, 2. recursively solve each problem, 3. appropriately combine their answers. Known Examples: Binary search Mergesort Quicksort Strassen matrix multiplication Julius Caesar (I-BC) Divide et impera J. von Neumann ( ) Merge sort

3 Collaborative Filtering In many commercial webs collaborative filtering is a technique to match your preferences with other customers in the Web, to guess which product you should be recommended. One manner to do it, make rankings of given product (music, movies, novels) and match your interest with similar rankings by other people. One way to quantify the notion of how similar are two ordered lists is by counting the inversions between the two lists.

4 Counting inversions Given n items (1, 2,..., n) consider A has a list of preferences L A = {1, 2,,..., n}, and B has a list L B = {b 1, b 2,..., b n }. We want to see how similar (close) L B is to L A Two items i, j form an inversion if i < j in L A but i > j in L B. For example: Consider L A = {1, 2, 3, 4, 5, 6, 7, 8} and L B = {1, 5, 3, 2, 7, 4, 8, 6} Number of inversions 8 (5,3),(5,2), (5,4), (5,7), (3,2) (7,4), (7,6), (8,6) Number of inversions 28 All pairs are inversions

5 D&C for counting inversions Brute force algorithm: Look at every pair (i, j) to see if it is an inversion: ( n 2) = n 2 /2 = O(n 2 ) Algorisme D&C 1. Divide: Separate the lists into two half 2. Conquer: Recursively count inversions in each half 3. Combine: Count inversions where i i j are in different halves 4. Return: The sum of the 3 quantities The strategy for 3 will be similar to Mergesort

6 Combine two halfs E and D key idea: sort using merge sort: Given subproblems E and D at same level of recursion, with E and D already sorted: scan from left to right E and D compare i E with j D if i < j then i is not inverted with left element in D if i > j then i is inverted with every left element in D append the smaller to sorted list. Complexity: T (n) = 2T (n/2) + O(n) = O(n lg n) (3,2)

7 Example (3,2), (7,4), (8,6) (5,2),(5,3), (7,6) (5,4) Total inversions= 7

8 2D-Closest pair of points Give n points in the plane, find a pair of points with the smallest Euclidean distance. Assumption: No two points have the same x-coordinate. Brute force algorithm: Compute the distance between every pair (i, j) and compare with the others: O(n 2 ) Very easy: sort by coordinate. O(n lg n). But sorting method does not generalize to higher dimensions (2) why?

9 D&C for 2D-Closest pair of points D&C 1. Divide: Separate the plane by a line L, into two half E and D with same number of points (±1) 2. Conquer: Recursively find the minimal distance between pairs of points in each half. 3. Combine: Taking into consideration pairs of points (p, q) with p E and q D 4. Return: The pair of points at minimal distance. E Recursive calls D L3 L2 L3 L1 L3 L2 L3

10 D& C Algorithm At each step: Divide: Sort the n points by its x coordinate. Take n/2 into left of L (E) and n/2 into right of L (D) (O(n lg n)) Conquer: Return d = min{d E, d D } (2T (n/2)) dd p 1 de q1 q2 p2 E L D Combine: There might be two points, one in E and other in D, that are closer than d

11 D& C Algorithm: Combine phase Take a vertical band of width 2d around L Any p E and q D s.t. d(p, q) d must reside in this band. There could be many other points inside the band. Focus only of points in the band To find the closest p, q in this band: Sort by increasing y-coordinate the points in the band, Y = {y 1, y 2,... y m }. Cost: O(n lg n) E 2 d dd p 1 de q1 q2 p2 L D

12 D& C Algorithm: Combine phase Consider a grid with d/2 inside the band There is at most 1 point inside each d 2 d 2 cell. (the diagonal of the cell = d 2 < d) Two points > 2 cells rows apart have distance > d (The distance between two points in two consecutive cells is 5 4 = 1.118d.) d Two points > 2 cells columns apart have distance > d (The same argument, that above) 2 d L d/2

13 How many squares a point can influence?: For every point in the sorted Y = {y 1, y 2,... y m }, starting from y 1 we only have to explore the distance between y i and the nest 10 ordered points in Y. y i, y j, d(y i, y j ) d if i j 10 So for every point in he band we only have to compare with the 10 nearest points in Y, with a total cost 10n. d

14 Closest-Pair Algorithm: Closest-Pair (p 1,..., p n ) Sort by the x-coordinate to compute L d 1 =Closet-Pair(E) d 2 =Closet-Pair(D) d = min{d 1, d 2 } Delete points > d from L Sort the remaining points by y-coordinate to form listed Y Scan in order Y list computing the distance with next 11 elements If any of those distances is < d update d T (n) = 2T (n/2) + O(n lg n) = O(n lg 2 n) Do you know how to improve to O(n lg n)

15 Random-Quicksort Consider the function Ran-Partition: Ran-Partition (A[p,..., q]) r = rand(p, q) u.a.r. interchange A[p] and A[r] Using Ran-Partition, consider the following randomized Divide and Conquer algorithm, on input A[1,..., n]: Ran-Quicksort (A[p,..., q]) r = Ran-Partition (A[p,..., q]) if p < q then Ran-Quicksort (A[1,..., r 1]) Ran-Quicksort (A[+1,..., q]) else return A[p] end if

16 Example A={1,3,5,6,8,10,12,14,15,16,17,18,20,22,23} Ran Partition of input

17 Expected Complexity of Ran-Partition The expected running time T (n) of Rand-Quicksort is dominated by the number of comparisons. Every call to Rand-Partition has cost Θ(1) + Θ(number of comparisons) }{{} p q If we can count the number of comparisons, we can bound the the total time of Quicksort. Let X be the number of comparisons made in all calls of Ran-Quicksort X is a rv as it depends of the random choices of Ran-Partition

18 Expected Complexity of Ran-Partition Note: In the first application of Ran-Partition A[r] compares with all n 1 elements. Key observation: Any two keys are compared iff one of them is a pivot, and they are compared at most one time never compare For simplicity assume all keys are different, for any input A[i,..., j] to Ran-Quicksort, 1 i < j n, let Z i,j be the ordered set of key {z i, z i+1,..., z j } (with z i the smallest). Note Z i,j = j Therefore choosing u.a.r. a pivot is done with probability. 1 Z i,j = 1 j 1 + 1

19 Define the indicator r.v.: { 1 if z i is compared to z j, X ij = 0 otherwise. Then, X = n 1 i=1 n j=i+1 X i,j (this is true because we never compare a pair more than once) n 1 E [X ] = E n i=1 j=i+1 X i,j = n 1 n i=1 j=i+1 E [X i,j ] As E [X i,j ] = 0Pr [X i,j = 0] + 1Pr [X i,j = 1] E [X i,j ] = Pr [X i,j = 1] = Pr [z i is compared to z j ]

20 End of the proof and main theorem E [X ] = n 1 i=1 n j=i+1 E [X i,j] Pr [z i is compared to z j ] As z i and z j compare iff one of them is chosen as pivot, then Pr [X i,j ] = 1 = Pr [z i is pivot] + Pr [z j is pivot] Because pivots as chosen u.a.r. in Z i,j : Pr [z i is pivot] = Pr [z j is pivot] = 1 j 1+1 Therefore: E [X ] = n 1 n i=1 j=i+1 2 j i + 1.

21 n 1 E [X ] = n i=1 j=i+1 = 2 < 2 = 2 n i=1 2 j i + 1 ( n i + 1 ) n ( n ) i=1 n H n = 2 n H n = O(n lg n). i=1 Therefore, E [X ] = 2n ln n + Θ(n). Theorem The expected complexity of Ran-Quicksort is E [T n ] = O(n lg n).

22 Selection and order statistics Problem: Given a list A of n of unordered distinct keys, and a i Z, 1 i n, select the element x A that is larger than exactly i 1 other elements in A. Notice if: 1. i = 1 MINIMUM element 2. i = n MAXIMUM element 3. i = n+1 2 the MEDIAN 4. i = 0.9 n order statistics Sort A (O(n lg n) and search for A[i] (Θ(n)). Can we do it in linear time? Yes, Selection is easier than Sorting

23 Quick-Select Given unordered A[1,..., n] return the i-th. element Quick-Select (A[p,..., q], i) r = Ran-Partition (p, q) to find position of pivot if i = r return A[r] if i < r Quick-Select (A[p,..., r 1], i) else Quick-Select (A[r + 1,..., q], i) 1 3 A Search for i=2 in A m u h e c b k v 1 8 3=Ran Partition(1,8) e c b h u v k m

24 Quick-Select Algorithm Quickselect (A[p,..., q], i) if p = q then return A[p] else r =Ran-Partition (A[p,..., q]) k = r p + 1 if i = k then return A[q] if i < k then return Quickselect (A[p,..., q 1], i) else return Quickselect (A[q + 1,..., r], i k) end if end if end if

25 Analysis. Lucky: at each recusrsive call the search space is reduced in 9/10 of the size. Then T (n) T (9n/10) + Θ(n) = Θ(n). Unlucky: T (n) = T (n 1) + Θ(n) = Θ(n 2 ). In this case it is worst than sorting!. Theorem Given A[1,..., n] and i, the expected number of steps for Quick-Select to find the i-th. element in A is O(n)

26 Proof Given A[1,..., n] let T (n) be a rv counting the expected number of steps for Quick-Select to find the ith element. Quick-Select (A, i) returns the k-th. element with probability 1 A. Define the indicator rv: X ij = { 1 if subarray A = k, 0 otherwise. Therefore, E [X k ] = 1 n To get an UB on E [T (n)] assume the desired i-th element always fells in the k 1 largest side of the partition. When X k = 1 we have subarrays of size k 1 and n k. We get the recurrence: n T (n) X k T (max{k 1, n k}) + O(n) k=1 k m k k=ran Partition(A)

27 Proof (cont.) [ n ] E [T (n)] E X k T (max{k 1, n k}) + O(n) E [T (n)] = 1 n = = k=1 n E [X k T (max{k 1, n k})] + O(n) k=1 n E [X k ] E [T (max{k 1, n k})] + O(n)? k=1 = 1 n n E [X k ] E [T (max{k 1, n k})] k=1 Notice max{k 1, n k} = { k 1 n k n 1 k=1 E [T (k)] + O(n) = O(n) if k > n/2, otherwise.

28 Deterministic linear selection. Generate deterministically a good split element x. Divide the n elements in n/2 groups, each with 5 elements (+ possible one group with < 5 elements).

29 Deterministic linear selection. Sort each set to find its median, say x i. (Each sorting needs 6 comparisons, i.e. Θ(1)) Total: 6n/2

30 Deterministic linear selection. Use recursively Select to find the median x of the medians {x i }, 1 i 6n/2. Use deterministic Partition (quick sort) to re-arrange the groups corresponding to medians {x i } around x, in linear time on the number of medians. x

31 Deterministic linear selection. Al least 3 2 n/5 = 3n/10 of the elements are x. x

32 Deterministic linear selection. Al least 3 2 n/5 = 3n/10 of the elements are x. x

33 The deterministic algorithm Select (A, i) 1.- Divide the n elements into n/5 groups of Find the median by insertion sort, and take the middle element 3.- Use Select recursively to find the median x of the n/5 medians 4.- Use Partition to place x and its group. Let k=rank of x 5.- if i = k then return x else if i < k then use Select to find the i-th smallest in the left else use Select recursively to find the i k-th smallest in the right end if Notice steps 4 and 5 are the same as Quickselect.

34 Example Get the mean ( n/2 ) on the following input: PARTITION around 10: To get the 7th element (mean) call SELECT on this instance

35 Worst case Analysis. As at least 3n 10 of the elements are x. At least 3n 10 elements are < x. In the worst case, step 5 calls Select recursively 7n/10 Steps 1, 2 and 4 take O(n) time. Step 3 takes time T (n/5) and step 5 takes time T (7n/10). so we have T (n) = { Θ(1) if n 50 T (n/5) + T (7n/10) + Θ(n) if n > 50 Therefore, T (n) = Θ(n)

36 Notice: If we make groups of 7, the number of elements x is 2n 7, which yield T (n) T (n/7) + T (5n/7) + O(n) with solution T (n) = O(n). However, if we make groups of 3, then T (n) T (n/3) + T (2n/3) + O(n), which has a solution T (n) = O(n ln n).

37 Conclusions From a randomized algorithm we remove the randomization to get a fast deterministic algorithm for selection. From the practical point of view, the deterministic algorithm is slow. Use Quickselect.

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

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

More information

5. DIVIDE AND CONQUER I

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

More information

5. DIVIDE AND CONQUER I

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

More information

Algorithms And Programming I. Lecture 5 Quicksort

Algorithms And Programming I. Lecture 5 Quicksort Algorithms And Programming I Lecture 5 Quicksort Quick Sort Partition set into two using randomly chosen pivot 88 31 25 52 14 98 62 30 23 79 14 31 2530 23 52 88 62 98 79 Quick Sort 14 31 2530 23 52 88

More information

Lecture 4. Quicksort

Lecture 4. Quicksort Lecture 4. Quicksort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018 Networking

More information

Copyright 2000, Kevin Wayne 1

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

More information

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici.

Divide-and-Conquer. Consequence. Brute force: n 2. Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common usage. Break up problem of

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 8 Analyzing Quick Sort Sofya Raskhodnikova and Adam Smith Reminder: QuickSort Quicksort an n-element array: 1. Divide: Partition the array around a pivot

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 2005 Pearson-Addison Wesley. All rights reserved. 1 Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve each

More information

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

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

More information

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

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

More information

Divide-Conquer-Glue Algorithms

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

More information

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Algorithms. Quicksort. Slide credit: David Luebke (Virginia) 1 Algorithms Quicksort Slide credit: David Luebke (Virginia) Sorting revisited We have seen algorithms for sorting: INSERTION-SORT, MERGESORT More generally: given a sequence of items Each item has a characteristic

More information

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort Partition n elements array A into two subarrays of n/2 elements each Sort the

More information

Algorithm Design and Analysis

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

More information

CPS 616 DIVIDE-AND-CONQUER 6-1

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

More information

Partition and Select

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

More information

Analysis of Algorithms. Randomizing Quicksort

Analysis of Algorithms. Randomizing Quicksort Analysis of Algorithms Randomizing Quicksort Randomizing Quicksort Randomly permute the elements of the input array before sorting OR... modify the PARTITION procedure At each step of the algorithm we

More information

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

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

More information

Analysis of Algorithms CMPSC 565

Analysis of Algorithms CMPSC 565 Analysis of Algorithms CMPSC 565 LECTURES 38-39 Randomized Algorithms II Quickselect Quicksort Running time Adam Smith L1.1 Types of randomized analysis Average-case analysis : Assume data is distributed

More information

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

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

More information

Divide and conquer. Philip II of Macedon

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

More information

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

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

More information

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

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

More information

5. DIVIDE AND CONQUER I

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

More information

Week 5: Quicksort, Lower bound, Greedy

Week 5: Quicksort, Lower bound, Greedy Week 5: Quicksort, Lower bound, Greedy Agenda: Quicksort: Average case Lower bound for sorting Greedy method 1 Week 5: Quicksort Recall Quicksort: The ideas: Pick one key Compare to others: partition into

More information

ITEC2620 Introduction to Data Structures

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

More information

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue Divide-Conquer-Glue Tyler Moore CSE 3353, SMU, Dallas, TX February 19, 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 5 - Jan. 12, 2018 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. 1 /

More information

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM Sorting Chapter 11-1 - Sorting Ø We have seen the advantage of sorted data representations for a number of applications q Sparse vectors q Maps q Dictionaries Ø Here we consider the problem of how to efficiently

More information

Data selection. Lower complexity bound for sorting

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

More information

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

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

More information

Fundamental Algorithms

Fundamental Algorithms Chapter 2: Sorting, Winter 2018/19 1 Fundamental Algorithms Chapter 2: Sorting Jan Křetínský Winter 2018/19 Chapter 2: Sorting, Winter 2018/19 2 Part I Simple Sorts Chapter 2: Sorting, Winter 2018/19 3

More information

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1. CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, 2018 Problem 3-1. (Quicksort Median-of-3 Partition) One way to improve the randomized quicksort procedure

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 2: Sorting Harald Räcke Winter 2015/16 Chapter 2: Sorting, Winter 2015/16 1 Part I Simple Sorts Chapter 2: Sorting, Winter 2015/16 2 The Sorting Problem Definition Sorting

More information

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz Quicksort Where Average and Worst Case Differ S.V. N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu February 1, 2016 S.V. N. Vishwanathan (UCSC) CMPS101 1 / 28 Basic Idea Outline

More information

Review Of Topics. Review: Induction

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

More information

Kartsuba s Algorithm and Linear Time Selection

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

More information

Divide and Conquer Algorithms

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

More information

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

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

More information

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

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

More information

1 Divide and Conquer (September 3)

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

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2017-2018 Outline 1 Sorting Algorithms (contd.) Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.) Analysis of Quicksort Time to sort array of length

More information

CS483 Design and Analysis of Algorithms

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

More information

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos 6331 - Algorithms, CSE, OSU Quicksort Instructor: Anastasios Sidiropoulos Sorting Given an array of integers A[1... n], rearrange its elements so that A[1] A[2]... A[n]. Quicksort Quicksort(A, p, r) if

More information

Divide and Conquer Algorithms

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

More information

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain Univ. Politècnica de Catalunya, Spain 10th Seminar on the Analysis of Algorithms MSRI, Berkeley, U.S.A. June 2004 1 Introduction 2 3 Introduction Partial sorting: Given an array A of n elements and a value

More information

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d CS161, Lecture 4 Median, Selection, and the Substitution Method Scribe: Albert Chen and Juliana Cook (2015), Sam Kim (2016), Gregory Valiant (2017) Date: January 23, 2017 1 Introduction Last lecture, we

More information

Divide and Conquer Strategy

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

More information

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3) Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3) Quicksort p r Quicksort(A, p, r) 1. If p

More information

Extended Algorithms Courses COMP3821/9801

Extended Algorithms Courses COMP3821/9801 NEW SOUTH WALES Extended Algorithms Courses Aleks Ignjatović School of Computer Science and Engineering University of New South Wales rithms What are we going to do in this class We will do: randomised

More information

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

More information

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text. Chapter 1 Quicksort 1.1 Prerequisites You need to be familiar with the divide-and-conquer paradigm, standard notations for expressing the time-complexity of an algorithm, like the big-oh, big-omega notations.

More information

Mergesort and Recurrences (CLRS 2.3, 4.4)

Mergesort and Recurrences (CLRS 2.3, 4.4) Mergesort and Recurrences (CLRS 2.3, 4.4) We saw a couple of O(n 2 ) algorithms for sorting. Today we ll see a different approach that runs in O(n lg n) and uses one of the most powerful techniques for

More information

Quick Sort Notes , Spring 2010

Quick Sort Notes , Spring 2010 Quick Sort Notes 18.310, Spring 2010 0.1 Randomized Median Finding In a previous lecture, we discussed the problem of finding the median of a list of m elements, or more generally the element of rank m.

More information

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

Introduction to Randomized Algorithms: Quick Sort and Quick Selection Chapter 14 Introduction to Randomized Algorithms: Quick Sort and Quick Selection CS 473: Fundamental Algorithms, Spring 2011 March 10, 2011 14.1 Introduction to Randomized Algorithms 14.2 Introduction

More information

Linear Time Selection

Linear Time Selection Linear Time Selection Given (x,y coordinates of N houses, where should you build road These lecture slides are adapted from CLRS.. Princeton University COS Theory of Algorithms Spring 0 Kevin Wayne Given

More information

Selection and Adversary Arguments. COMP 215 Lecture 19

Selection and Adversary Arguments. COMP 215 Lecture 19 Selection and Adversary Arguments COMP 215 Lecture 19 Selection Problems We want to find the k'th largest entry in an unsorted array. Could be the largest, smallest, median, etc. Ideas for an n lg n algorithm?

More information

CS 231: Algorithmic Problem Solving

CS 231: Algorithmic Problem Solving CS 231: Algorithmic Problem Solving Naomi Nishimura Module 4 Date of this version: June 11, 2018 WARNING: Drafts of slides are made available prior to lecture for your convenience. After lecture, slides

More information

Divide and Conquer Algorithms

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

More information

Divide and Conquer. Arash Rafiey. 27 October, 2016

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

More information

Objec&ves. Review. Divide and conquer algorithms

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

More information

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

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

More information

Searching. Sorting. Lambdas

Searching. Sorting. Lambdas .. s Babes-Bolyai University arthur@cs.ubbcluj.ro Overview 1 2 3 Feedback for the course You can write feedback at academicinfo.ubbcluj.ro It is both important as well as anonymous Write both what you

More information

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

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

More information

MAKING A BINARY HEAP

MAKING A BINARY HEAP CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.uc sd.edu Office 4208 CSE Building Lecture 19: Divide and Conquer Design examples/dynamic Programming MAKING A BINARY HEAP Base case. Break

More information

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Inf 2B: Sorting, MergeSort and Divide-and-Conquer Inf 2B: Sorting, MergeSort and Divide-and-Conquer Lecture 7 of ADS thread Kyriakos Kalorkoti School of Informatics University of Edinburgh The Sorting Problem Input: Task: Array A of items with comparable

More information

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows: Outline Computer Science 331 Quicksort Mike Jacobson Department of Computer Science University of Calgary Lecture #28 1 Introduction 2 Randomized 3 Quicksort Deterministic Quicksort Randomized Quicksort

More information

1 Terminology and setup

1 Terminology and setup 15-451/651: Design & Analysis of Algorithms August 31, 2017 Lecture #2 last changed: August 29, 2017 In this lecture, we will examine some simple, concrete models of computation, each with a precise definition

More information

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS OHSU/OGI (Winter 2009) CS532 ANALYSIS AND DESIGN OF ALGORITHMS LECTURE 7 1 Quick Sort QuickSort 1 is a classic example of divide and conquer. The hard work is to rearrange the elements of the array A[1..n]

More information

MAKING A BINARY HEAP

MAKING A BINARY HEAP CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.uc sd.edu Office 4208 CSE Building Lecture 19: Divide and Conquer Design examples/dynamic Programming MAKING A BINARY HEAP Base case. Break

More information

Notes for Recitation 14

Notes for Recitation 14 6.04/18.06J Mathematics for Computer Science October 4, 006 Tom Leighton and Marten van Dijk Notes for Recitation 14 1 The Akra-Bazzi Theorem Theorem 1 (Akra-Bazzi, strong form). Suppose that: is defined

More information

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

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

More information

CSE 421 Algorithms: Divide and Conquer

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

More information

Divide and Conquer. Andreas Klappenecker

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

More information

data structures and algorithms lecture 2

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

More information

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21 COMP 250: Quicksort Carlos G. Oliver February 13, 2018 Carlos G. Oliver COMP 250: Quicksort February 13, 2018 1 / 21 1 1 https://xkcd.com/1667/ Carlos G. Oliver COMP 250: Quicksort February 13, 2018 2

More information

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1 Divide & Conquer CS 320, Fall 2017 Dr. Geri Georg, Instructor georg@colostate.edu CS320 Div&Conq 1 Strategy 1. Divide the problem up into equal sized sub problems 2. Solve the sub problems recursively

More information

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort Sorting Algorithms We have already seen: Selection-sort Insertion-sort Heap-sort We will see: Bubble-sort Merge-sort Quick-sort We will show that: O(n log n) is optimal for comparison based sorting. Bubble-Sort

More information

Introduction to Divide and Conquer

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

More information

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

More information

Quicksort algorithm Average case analysis

Quicksort algorithm Average case analysis Quicksort algorithm Average case analysis After today, you should be able to implement quicksort derive the average case runtime of quick sort and similar algorithms Q1-3 For any recurrence relation in

More information

Find Min and Max. Find them independantly: 2n 2. Can easily modify to get 2n 3. Should be able to do better(?) Try divide and conquer.

Find Min and Max. Find them independantly: 2n 2. Can easily modify to get 2n 3. Should be able to do better(?) Try divide and conquer. Find Min and Max Find them independantly: 2n 2. Can easily modify to get 2n 3. Should be able to do better(?) Try divide and conquer. Find_Max_Min(ELEM *L, int lower, int upper) { if (upper == lower) return

More information

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

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

More information

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

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

More information

Divide-and-Conquer. a technique for designing algorithms

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

More information

Concrete models and tight upper/lower bounds

Concrete models and tight upper/lower bounds Lecture 3 Concrete models and tight upper/lower bounds 3.1 Overview In this lecture, we will examine some simple, concrete models of computation, each with a precise definition of what counts as a step,

More information

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai Divide-and-Conquer Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, 33.4. CSE 6331 Algorithms Steve Lai Divide and Conquer Given an instance x of a prolem, the method works as follows: divide-and-conquer

More information

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1 Quicksort Recurrence analysis Quicksort introduction November 17, 2017 Hassan Khosravi / Geoffrey Tien 1 Announcement Slight adjustment to lab 5 schedule (Monday sections A, B, E) Next week (Nov.20) regular

More information

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms CSE 312, Winter 2011, W.L.Ruzzo 8. Average-Case Analysis of Algorithms + Randomized Algorithms 1 insertion sort Array A[1]..A[n] for i = 1..n-1 { T = A[i] Sorted j swap j = i-1 compare while j >= 0 &&

More information

Reductions, Recursion and Divide and Conquer

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

More information

CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness

CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness CSC236 Intro. to the Theory of Computation Lecture 7: Master Theorem; more D&C; correctness Amir H. Chinaei, Fall 2016 Office Hours: W 2-4 BA4222 ahchinaei@cs.toronto.edu http://www.cs.toronto.edu/~ahchinaei/

More information

Divide and Conquer. Recurrence Relations

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

More information

CSE 591 Homework 3 Sample Solutions. Problem 1

CSE 591 Homework 3 Sample Solutions. Problem 1 CSE 591 Homework 3 Sample Solutions Problem 1 (a) Let p = max{1, k d}, q = min{n, k + d}, it suffices to find the pth and qth largest element of L and output all elements in the range between these two

More information

Chunksort: A Generalized Partial Sort Algorithm

Chunksort: A Generalized Partial Sort Algorithm Chunksort: A Generalized Partial Sort Algorithm Conrado Martínez Univ. Politècnica de Catalunya, Spain Ottawa-Carleton Discrete Math Day May 2006 1 Introduction 2 The algorithm 3 The analysis 4 Conclusions

More information

Analysis of Approximate Quickselect and Related Problems

Analysis of Approximate Quickselect and Related Problems Analysis of Approximate Quickselect and Related Problems Conrado Martínez Univ. Politècnica Catalunya Joint work with A. Panholzer and H. Prodinger LIP6, Paris, April 2009 Introduction Quickselect finds

More information

CS483 Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms CS483 Design and Analysis of Algorithms Lecture 6-8 Divide and Conquer Algorithms Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: STII, Room 443, Friday 4:00pm - 6:00pm or by appointments

More information

CS361 Homework #3 Solutions

CS361 Homework #3 Solutions CS6 Homework # Solutions. Suppose I have a hash table with 5 locations. I would like to know how many items I can store in it before it becomes fairly likely that I have a collision, i.e., that two items

More information

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input Bucket-Sort Have seen lower bound of Ω(nlog n) for comparisonbased sorting algs Some cheating algorithms achieve O(n), given certain assumptions re input One example: bucket sort Assumption: input numbers

More information