Chapter 6 Divide and Conquer  Introduction  Binary Search  Mergesort  The Divide and Conquer Paradigm  Quicksort  Multiplication of Large Integers.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

Introduction to Algorithms
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
Algorithm Design Paradigms
The Divide-and-Conquer Strategy
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
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 The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.
Spring 2015 Lecture 5: QuickSort & Selection
1 Dynamic Programming Jose Rolim University of Geneva.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Divide and Conquer Reading Material: Chapter 6 (except Section 6.9).
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Divide-and-Conquer 7 2  9 4   2   4   7
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.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
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.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
7.3 Divide-and-Conquer Algorithms and Recurrence Relations If f(n) represents the number of operations required to solve the problem of size n, it follow.
Divide and Conquer Strategy
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
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 (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.
Data Structures and Algorithm Analysis Algorithm Analysis and Sorting
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Chapter 2. Divide-and-conquer Algorithms
UNIT- I Problem solving and Algorithmic Analysis
Chapter 4 Divide-and-Conquer
Techniques Based on Recursion
Unit-2 Divide and Conquer
Topic: Divide and Conquer
ICS 353: Design and Analysis of Algorithms
Topic: Divide and Conquer
Lecture 15, Winter 2019 Closest Pair, Multiplication
Topic: Divide and Conquer
The Selection Problem.
ICS 353: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Chapter 6 Divide and Conquer  Introduction  Binary Search  Mergesort  The Divide and Conquer Paradigm  Quicksort  Multiplication of Large Integers  Matrix Multiplication  The Closest Pair Problem

6.1 Introduction Main idea: A divide-and-conquer algorithm divides the problem instance into a number of subinstances, recursively solves each subinstance separately, and then combines the solutions to the subinstances to obtain the solution to the original problem instance.

6.1 Introduction 1.X A[1]; y A[1] 2.For I 2 to n 3.If A[i]>y then y A[i] 4.If A[i]<x then x A[i] 5.End for 6.Return (x,y) E.g.: The problem of finding both the minimum and maximum in an array of integers A[1..n] and assume for simplicity that n is a power of 2. A straightforward algorithm might look line the one below.

6.1 Introduction Algorithm 6.1 MINMAX Input: An array A[1..n] of n integers, where n is a power of 2. Output: (x,y): the minimum and maximum integers in A. 1.minmax(1,n) Procedure minmax(low,high) 1. if high-low=1 then 2. if A[low]<A[high] then return(A[low],A[high]) 3. else return(A[high],A[low]) 4. end if 5. else 6. mid 7. (x1,y1) minmax(low,mid) 8. (x2,y2) minmax(mid+1,high) 9. x min{x1,x2} 10. y max{y1,y2} 11. return (x, y) 12. end if

6.1 Introduction Theorem 6.1  Given an array A[1..n] of n elements, where n is a power of 2, it is possible to find both the minimum and maximum of the elements in A using only (3n/2)-2 element comparisons.

6.2 Binary Search  Binary search algorithm is one of the divide-and-conquer algorithm.

Algorithm 6.2 BINARYSEARCHREC Input: An array A[1..n] of n elements sorted in nondecreasing order and an element x. Output: j if x=A[j],1<=j<=n, and 0 otherwise. 1.binarysearch(1,n) Procedure binarysearch(low,high) 1. if low>high then return 0 2. else 3. mid 4. if x=A[mid] then return mid 5. else if x<A[mid] then return binarysearch(low,mid-1) 6. else return binarysearch(mid+1,high) 7. end if 6.2

Theorem 6.2 The number of element comparisons performed by Algorithm BINARYSEARCHREC to search for an element in an array of n elements is at most +1. Thus, the time complexity of Algorithm BINARYSEARCHREC is O(log n). 6.2

6.3 Mergesort Algorithm 6.3 MERGESORT Input: An array A[1..n] of n elements. Output: A[1..n] sorted in nondecreasing order. 1.mergesort(A,1,n) Procedure mergesort(low,high) 1. if low<high then 2. mid 3. mergesort(A,low,mid) 4. mergesort(A,mid+1,high) 5. MERGE(A,low,mid,high) 6. end if

Theorem 6.3 Algorithm MERGESORT sorts an array of n elements in time (n log n) and space (n). 6.3

6.4 The Divide-and-conquer Paradigm  In general, a divide-and-conquer algorithm has the following format. (1)If the size of the instance I is “ small ”, then solve the problem using a straightforward method and return the answer. Otherwise, continue to the next step. (2) Divide the instance I into p subinstances I 1,I 2, …,I p of approximately the same size. (3) Recursively call the algorithm on each subinstance I j, 1<=j<=p, to obtain p partial solutions. (4) Combine the results of the p partial solutions to obtain the solution to the original instance I. Return the solution of instance I.

6.5 Selection: Finding the Median and the kth Smallest Element  Problem: the median of a sequence of n sorted numbers A[1..n] is the “middle” element. If n is odd, then the middle element is the (n+1)/2 th element in the sequence. If n is even, then there are two middle elements occurring at positions n/2 and n/2+1. in this cases, we will choose the n/2th smallest element. Thus, in both cases, the median is the  n/2  th smallest element.

Algorithm 6.4 SELECT Input:An array A[1..n] of n elements and an integer k, 1 k n. Output: The kth smallest element in A. 1. select(A,1,n,k) Procedure select(A,low,high,k) 1. p high-low+1 2. if p<44 then sort A and return (A[k]) 3. Let q=. Divide A into q groups of 5 elements each. If 5 does not divide p, then discard the remaining elements 4. Sort each of the q groups individually and exact its median. Let the set of medians be M. 5. mm select(M,1,q, ). {mm is the median of medians} 6. Partition A[low..high] into three arrays : A1={a|a mm} 7.case |A1| k: return select(A1,1,|A1|,k) |A1|+|A2| k: return mm |A1|+|A2|<k: return select(A3,1,|A3|,k-|A1|-|A2|) 8. end case 6.5

Theorem 6.4 The kth smallest element in a set of n elements drawn from a linearly ordered set can be found in (n) time. In particular, the median of n elements can be found in (n) time. 6.5

6.6 Quicksort Algorithm 6.5 SPLIT Input: An array of elements A[low..high]. Output: (1) A with its elements rearranged, if necessary, as described above. (2) w,the new position of the splitting element A[low]. 1. i=low 2. x=A[low] 3. for j=low+1 to high 4. if A[j]<=x then 5. i=i+1 6.if i/=j then interchange A[i] and A[j] 7.end if 8. end for 9. interchange A[low] and A[i] 10. w=i 11. Return A and w

6.6 Quicksort Algorithm 6.6 QUICKSORT Input: An array A[1..n] of n elements. Output: The elements in A sorted in nondecreasing order. 1.quicksort(A,1,n) Procedure quicksort(A,low,high) 1.if low<high then 2. SPLIT(A[low..high],w) {w is the new position of A[low]} 3. quicksort(A,low,w-1) 4. quicksort(A,w+1,high) 5. end if

Theorem 6.5  The running time of Algorithm QUICKSORT is in the worst case. If, however, the median is always chosen as the pivot, then its time Complexity is (n log n). 6.6

Theorem 6.6  The average number of comparisons performed by Algorithm QUICKSORT to sort an array of n elements is (n log n). 6.6

Multiplication of Large Integers  Let u and v be two n-bit integers. The traditional multiplication algorithm requires (n 2 ) digit multiplications to compute the product of u and v. Using the divide and conquer technique, the bound can be reduced significantly. 6.7

WX YZ

Matrix Multiplication  Let A and B to be two n*n matrices. We with to compute their product C=AB The traditional algorithm: 6.8

Matrix Multiplication Recursive version Assume n=2 k, k>=0. 6.8

Matrix Multiplication Strassen ’ s algorithm Assume n=2 k, k>=0. 6.8

Matrix Multiplication Strassen ’ s algorithm 6.8

Matrix Multiplication Strassen ’ s algorithm 6.8

6.9 The Closest Pair Problem  Let S be a set of n points in the plane. In this section, we consider the problem of finding a pair of points p and q in S whose mutual distance is minimum. In other words, we want to find two points p1=(x1,y1) and p2=(x2,y2) in S with the property that the distance between them defined by Is minimum among all pairs of points in S.

Algorithm 6.7 CLOSESTPAIR Input: A set S of n points in the plane. Output: The minimum separation realized by two points in S. 1.Sort The points in S in nondecreasing order of their x-coordinates. 2.Y The points in S sorted in nondecreasing order of their y-coordinates. 3. cp(1,n) 6.9

Procedure cp(low,high) 1.If high-low+1 3 then compute by a straightforward method. 2. else 3.mid 4. x(S[mid]) 5. cp(low,mid) 6. cp(mid+1,high) 7. min{, } 8.k 0 9.for i 1 to n {Extract T from Y} 10. if |x(Y[i])- | then 11. k k T[k] Y[i] 13. end if 14.end for {k is the size of T} {Initialize to any number greater than } 16. for i 1 to k-1 {compute } 17. for j i+1 to min{i+7,k} 18. if d(T[i],T[j])< then d(T[i],T[j]) 19. end for 20. end for 21. min{, } 22.End if 23.Return 6.9

 Observation 6.5 Each point in T needs to be compared with at most seven points in T. 6.9

Theorem 6.7  Given a set S of n points in the plane, Algorithm CLOSESTPAIR finds a pair of points in S with minimum separation in ⊙ (n log n) time.