Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.

Slides:



Advertisements
Similar presentations
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Advertisements

Introduction to Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
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.
Divide-and-Conquer Dr. Yingwu Zhu P65-74, p83-88, p93-96, p
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
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
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.
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Spring 2015 Lecture 5: QuickSort & Selection
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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]
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
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.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
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.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
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.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 4 Divide-and-Conquer
Chapter 5 Divide & conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Divide and Conquer.
CSCE350 Algorithms and Data Structure
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Divide-And-Conquer-And-Combine
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer Merge sort and quick sort Binary search
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Introduction to Algorithms
CSC 380: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Chapter 4: Divide and Conquer The Design and Analysis of Algorithms

2 Chapter 4. Chapter 4. Divide and Conquer Algorithms Basic Idea Merge Sort Quick Sort Binary Search Binary Tree Algorithms Closest Pair and Quickhull Conclusion

3 Basic Idea Divide instance of problem into two or more smaller instances Solve smaller instances recursively Obtain solution to original (larger) instance by combining these solutions

4 Basic Idea subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n

5 General Divide-and-Conquer Recurrence T(n) = aT(n/b) + f (n) where f(n)   (n d ), d  0 Master Theorem: If a < b d, T(n)   (n d ) If a = b d, T(n)   (n d log n) If a > b d, T(n)   (n log b a ) Examples: T(n) = T(n/2) + n  T(n)   (n ) Here a = 1, b = 2, d = 1, a < b d T(n) = 2T(n/2) + 1  T(n)   (n log 2 2 ) =  (n ) Here a = 2, b = 2, d = 0, a > b d

6 Examples T(n) = T(n/2) + 1  T(n)   (log(n) ) Here a = 1, b = 2, d = 0, a = b d T(n) = 4T(n/2) + n  T(n)   (n log 2 4 ) =  (n 2 ) Here a = 4, b = 2, d = 1, a > b d T(n) = 4T(n/2) + n 2  T(n)   (n 2 log n) Here a = 4, b = 2, d = 2, a = b d T(n) = 4T(n/2) + n 3  T(n)   (n 3 ) Here a = 4, b = 2, d = 3, a < b d

7 Mergesort Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B and C Sort arrays B and C recursively Merge sorted arrays B and C into array A

8 Mergesort

9

10 Mergesort

11 Analysis of Mergesort All cases have same efficiency: Θ(n log n) Number of comparisons in the worst case is close to theoretical minimum for comparison-based sorting:  log 2 n!  ≈ n log 2 n n Space requirement: Θ(n) (not in-place) Can be implemented without recursion (bottom-up)

12 Quicksort Select a pivot (partitioning element) – here, the first element Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot (see next slide for an algorithm) Exchange the pivot with the last element in the first (i.e.,  ) sub- array — the pivot is now in its final position Sort the two sub-arrays recursively p A[i]  pA[i]  p

13 Analysis of Quicksort Best case: split in the middle — Θ(n log n) Worst case: sorted array — Θ(n 2 ) Average case: random arrays — Θ(n log n) Improvements:  better pivot selection: median of three partitioning  switch to insertion sort on small subfiles  elimination of recursion Quicksort is considered the method of choice for internal sorting of large files (n ≥ 10000)

14 Binary Search Very efficient algorithm for searching in sorted array: K vs A[0]... A[m]... A[n-1] If K = A[m], stop (successful search); otherwise, continue searching by the same method in A[0..m-1] if K < A[m], and in A[m+1..n-1] if K > A[m]

15 Analysis of Binary Search Time efficiency Worst-case recurrence: Cw (n) = 1 + Cw(  n/2  ), Cw (1) = 1 solution: Cw(n) =  log2(n+1)  Optimal for searching a sorted array Limitations: must be a sorted array (not linked list)

16 Binary Tree Algorithms Binary tree is a divide-and-conquer ready structure Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) if T   Inorder(Tleft) print(root of T) Inorder(Tright) Efficiency: Θ(n)

17 Binary Tree Algorithms Ex. 2: Computing the height of a binary tree h(T) = max{ h(TL), h(TR) } + 1 if T   and h(  ) = -1 Efficiency: Θ(n)

18 Closest Pair Problem Step 1: Divide the points given into two subsets S1 and S2 by a vertical line x = c so that half the points lie to the left or on the line and half the points lie to the right or on the line.

19 Closest Pair Problem Step 2 Find recursively the closest pairs for the left and right subsets. Step 3 Set d = min {d1, d2} Let C1 and C2 be the subsets of points in the left subset S1 and of the right subset S2, respectively, that lie in this vertical strip. The points in C1 and C2 are stored in increasing order of their y coordinates, which is maintained by merging during the execution of the next step. Step 4 For every point P(x,y) in C1, we inspect points in C2 that may be closer to P than d. There can be no more than 6 such points (because d ≤ d2)

20 Closest Pair Problem The worst case scenario is depicted below

21 Efficiency of the Closest Pair Problem Running time of the algorithm T(n) = 2T(n/2) + M(n), where M(n)  O(n) By the Master Theorem (with a = 2, b = 2, d = 1) T(n)  O(n log n)

22 Quickhull Algorithm Convex hull: smallest convex set that includes given points Assume points are sorted by x- coordinate values Identify extreme points P1 and P2 (leftmost and rightmost) Compute upper hull recursively:  find point P max that is farthest away from line P1P2  compute the upper hull of the points to the left of line P1Pmax  compute the upper hull of the points to the left of line PmaxP2 Compute lower hull in a similar manner

23 Quickhull Algorithm P1P1 P2P2 P max

24 Efficiency of Quickhull Algorithm Finding point farthest away from line P1P2 can be done in linear time Time efficiency:  worst case: Θ(n 2 ) (as quicksort)  average case: Θ(n ) (under reasonable assumptions about distribution of points given) If points are not initially sorted by x-coordinate value, this can be accomplished in O(n log n) time

25 Conclusion Very efficient solutions. It is the choice when the following is true:  the problem can be described recursively - in terms of smaller instances of the same problem.  the problem-solving process can be described recursively, i.e. we can combine the solutions of the smaller instances to obtain the solution of the original problem Note that we assume that the problem is split in at least two parts of equal size, not simply decreased by a constant factor.