Divide and Conquer (Part II) Multiplication of two numbers Let U = (u 2n-1 u 2n-2 … u 1 u 0 ) 2 and V = (v 2n-1 v 2n-2 …v 1 v 0 ) 2, and our goal is to.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

Introduction to Algorithms Quicksort
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
A simple example finding the maximum of a set S of n numbers.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Quicksort Lecture 3 Prof. Dr. Aydın Öztürk. Quicksort.
1 Today’s Material Medians & Order Statistics – Ch. 9.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
1 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.
Divide-and-Conquer Matrix multiplication and Strassen’s algorithm Median Problem –In general finding the kth largest element of an unsorted list of numbers.
CS4413 Divide-and-Conquer
Using Divide and Conquer for Sorting
Chapter 3: Divide and Conquer
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Foundations of Algorithms, Fourth Edition
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Divide and Conquer Reading Material: Chapter 6 (except Section 6.9).
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Project 2 due … Project 2 due … Project 2 Project 2.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Order Statistics(Selection Problem)
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Divide and Conquer Strategy
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Analysis of Algorithms CS 477/677
Order Statistics Comp 122, Spring 2004.
Richard Anderson Lecture 13 Recurrences and Divide and Conquer
Topic: Divide and Conquer
Richard Anderson Lecture 12 Recurrences and Divide and Conquer
CS 583 Analysis of Algorithms
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Topic: Divide and Conquer
Order Statistics Comp 122, Spring 2004.
Richard Anderson Lecture 14 Divide and Conquer
Chapter 9: Selection of Order Statistics
The Selection Problem.
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Presentation transcript:

Divide and Conquer (Part II) Multiplication of two numbers Let U = (u 2n-1 u 2n-2 … u 1 u 0 ) 2 and V = (v 2n-1 v 2n-2 …v 1 v 0 ) 2, and our goal is to find U times V. Ordinary multiplication requires execution time α (n 2 ). Alternatively, let U = 2 n U 1 + U 0 and V = 2 n V 1 + V 0 where U 1 = (u 2n-1 u 2n-2 … u n ) 2 and U 0 = (u n-1 u n-2 … u 0 ) 2 Likewise, V 1 = (v 2n-1 v 2n-2 … v n ) 2 and V 0 = (v n-1 v n-2 … v 0 ) 2 U X V = 2 2n U 1 V n (U 1 V 0 + U 0 V 1 ) + U 0 V 0. Apparently there is no saving, this multiplication also requires execution time proportional to n 2. 2/16/2016

Divide and Conquer (Part II) Multiplication: Divide and Conquer U X V = (2 2n + 2 n )U 1 V n (U 1 – U 0 )( V 0 – V 1 ) + (2 n + 1) U 0 V 0. which requires only three multiplications and some extra addition. If T(n) denotes time to multiply two binary integers of size n each, then T(2n) = 3 T(n) + cn, and T(1) =1, where cn denotes the cost associated with additions and shifting of binary integers etc.. 2/16/2016

Divide and Conquer (Part II) Multiplication: Divide and Conquer Solution of this recurrence equation is (by Master Theorem) or repeated substitution, for n = 2 m, is T(2 m ) = 3 m T(1) + c(3 m -2 m ) In general, T(n)  3  lg n  T(1) + c(3  lg n  - 2  lg n  )  3c X 3 lg n = 3c X n lg 3  3c X /16/2016

Divide and Conquer (Part II) Matrix Multiplication Let C = A B = (c ij ) be the product of two n x n matrices A and B, where a 11 a 12 … a 1n b 11 b 12 … b 1n A =a 21 a 22 … a 2n and B = b 21 b 22 … b 2n… … … … a n1 a n2 … a nn b n1 b n2 … b nn We calculate n 2 terms, c ij for i =1,2,…,n,, j =1,2,…,n. Each c ij requires n scalar multiplications. Therefore, total complexity of this matrix multiplication is of the order n 3. 2/16/2016

Divide and Conquer (Part II) Suppose n = 2 k for some integer value of k. Let r … sa …be … g C = … … … = A X B = … … … X … … … t … uc …df … h Where r,s,t,u are all n/2 X n/2 matrices In this way of obtaining C, by first partitioning A and B in submatrices, we calculate 4 matrices r,s,t, and u. 2/16/2016

Divide and Conquer (Part II) Suppose n = 2 k for some integer value of k. Let Each submatrix has two matrix multiplications, of size n/2 X n/2 each. Hence, the recurrence equation associated with this divide and conquer approach would satisfy: T(n) = 8T(n/2) + (n 2 ) T(n) =  (n 3 ) Thus, there is no saving. 2/16/2016

Divide and Conquer (Part II) Matrix Multiplication: Strassen’s Algorithm P 1 = a (g-h) = ag - ah P 2 = (a+b)h = ah + bh P 3 = (c+d) e = ce + de P 4 = d (f-e) = df - de P 5 = (a+d)(e+h)= ae + ah + de + dh P 6 = (b-d)(f+h) = bf + bh - df – dh and P 7 = (a-c) (e+g) = ae + ag – ce - cg 2/16/2016

Divide and Conquer (Part II) Then, we can obtain r,s,t, and u as follows: r = P 5 +P 4 - P 2 +P 6 s = P 1 + P 2 t = P 3 +P 4 u = P 5 +P 1 - P 3 -P 7 For example, P 5 +P 4 - P 2 +P 6 = (ae+ah+de+dh)+(df - de) - (ah + bh) + (bf + bh - df -dh) = ae+bf = r 2/16/2016

Divide and Conquer (Part II) Hence, by Master Theorem, T(n) = 7T(n/2) +  (n 2 ) =  (n lg 7 ) =  (n 2.81 ) In other words, it is possible to multiply two matrices at a rate faster than  (n 3 ). 2/16/2016

Divide and Conquer (Part II) 2/16/2016

Divide and Conquer (Part II) Method 2: 1.Choose x = A[1]. 2.Set i  2, and set j  n. 3.If A[j] > x, then j  j -1, else go to step 4. 4.If A[i] < x, then i  i +1, else go to step 5. 5.If i < j, then interchange A[i] and A[j], otherwise set q  j and interchange A[1] and A[q]. 2/16/2016

Divide and Conquer (Part II) 2/16/2016

Divide and Conquer (Part II) Method 3: 1.To partition A[1],…,A[n] this method proceeds as follows: 2.Set i =1, j = n, X = A[1] 3.Compare A[i] and A[j], and exchange if A[i]  A[j]. 4.If no exchange is required then decrease j by 1 and continue. If exchange is required then increase i by 1 after the exchange and continue. If i = j, then stop, else 5.Go to step 2. To see how this method works suppose we wish to partition 503, 087, 512, 061, 908, 170, and /16/2016

Divide and Conquer (Part II) 2/16/2016

Divide and Conquer (Part II) Quick-Sort A[1], …, A[n] algorithm is: 1.Find a partition of A[1], …, A[n] such that A[1], …, A[q] are all smaller than (new) A[q+1], …, A[n] for 1  q  (n -1) 2.Quick-Sort A[1], …,A[q]. 3.Quick-Sort A[q + 1], …, A[n]. 2/16/2016

Divide and Conquer (Part II) Comments: 1.All comparisons are made with the same element. Amount of data movement is reasonable. This makes these partition procedures reasonable for large values of n. 2.Other choices for X are A[n], A[  n/2  ] and randomly chosen value i.e., A[i]. 3.It makes sense to sort an array by special procedures such as Bubble-Sort, for small values of n, say n  M. Some analyses and other supporting arguments suggest that M  9 is a reasonable bound for shifting from Quick-Sort to Bubble-Sort. 2/16/2016

Divide and Conquer (Part II) Analysis of Quick-sort: Exact analysis of Quick-sort is not possible --- we cannot control the size of the sub-arrays. We will consider the average analysis of Quick-sort. Assumptions: A[1], …, A[n] are all distinct. A[i]'s are randomly distributed. A[i]  {1,2, …, n} for i = 1, …, n. The partition Method #3 is used. 2/16/2016

Divide and Conquer (Part II) Observations: 1.All n! permutations of A[1], …, A[n] are equally likely. 2.Suppose A[i] = k. Then after one application of the partition method, size of the left sub-array is (k - 1), 3.size of the right sub-array is (n - k) and k is in its correct position. 4.Pr (A[i] = k) = 1/n. 5.Exactly (n + 1) comparisons are made in arranging the given array A[1], …, A[n] in partition method 2. [Reason - Each time i is increased by 1 or j is decreased by 1 a comparison is made until i becomes equal to j.] 2/16/2016

Divide and Conquer (Part II) 2/16/2016 Suppose AC(n) = Average number of comparisons required to sort A[1], …, A[n] by Quick-sort. Then, AC(n) = (n + 1) + (1/n){ [AC(k - 1) + AC(n - k)]} = (n + 1) + (1/n){ [AC(k) + AC(n - k - 1)]} = (n + 1) + AC(k) Consequently, n AC(n) = n (n + 1) + 2 [AC(0) + … + AC(n - 1)]. Substituting (n + 1) for n in the above equation gives, (n + 1) AC(n + 1) = (n + 1) (n + 2) + 2 [AC(0) + … + AC(n - 1) + AC(n)],

Divide and Conquer (Part II) 2/16/2016

Divide and Conquer (Part II) 2/16/2016

Divide and Conquer (Part II) 2/16/2016

Divide and Conquer (Part II) Selection Problem 2/16/2016 Find the i-th smallest element of a given array A[1], A[2], …,A[n]} The algorithm described below borrows an idea from Quick-Sort--- how to partition an array into two subarrays. Algorithm 1.Call A[1] the pivotal element. 2.Use a partition procedure, as in Quick-Sort, to find the location of the pivotal element, k, in the partitioned array (in which all elements to the left of the pivotal element are smaller than it and to the right are larger than it). 3.If i = k, then we have found the i-th smallest element; the answer is (old) A[1].

Divide and Conquer (Part II) 2/16/ If 1  i  k-1, then find the i-th smallest element among (new) A[1], …, A[k-1], else 5.if k<i  n, then find the i-th smallest element among new A[k+1], …,A[n]. (This case can be seen as equivalent to finding the (i-k)-th smallest element out of (n-k) of the right subarray after renaming A[k+1], …, A[n] as A[1], …,A[n-k]). Analysis of the above algorithm Please see the book

Divide and Conquer 2/16/2016 Delete sample document icons and replace with working document icons as follows: From Insert Menu, select Object... Click “Create from File” Locate File name in “File” box Make sure “Display as Icon” is checked Click OK Select icon From Slide Show Menu, Select “Action Settings” Click “Object Action” and select “Edit” Click OK Questions