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.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Divide and Conquer Sanghyun Park Fall 2002 CSE, POSTECH.
Introduction to Algorithms
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
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.
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
Quicksort Quicksort     29  9.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
CS 171: Introduction to Computer Science II Quicksort.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
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.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
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.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
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 Sorting Small instance.  n
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
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.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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 A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
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.
Sorting. Typically, most organizations spend large percentage of computing time (25% - 50%) on sorting Internal sorting the list is small enough to sort.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Quicksort Dr. Yingwu Zhu. 2 Quicksort A more efficient exchange sorting scheme than bubble sort – A typical exchange involves elements that are far apart.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Divide and Conquer Sorting
Divide-And-Conquer-And-Combine
Divide and Conquer.
Divide And Conquer Distinguish between small and large instances.
Chapter 4: Divide and Conquer
Sorting Algorithms Ellysa N. Kosinaya.
Divide-And-Conquer-And-Combine
Divide-and-Conquer The most-well known algorithm design strategy:
Divide-and-Conquer The most-well known algorithm design strategy:
Divide And Conquer Distinguish between small and large instances.
Chapter 7 : Sorting 교수 : 이상환 강의실 : 113,118호, 324호 연구실 : 과학관 204호
Presentation transcript:

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 be effectively run on a parallel computer Min-max problem, matrix multiplication and so on This chapter Develops the mathematics needed to analyze the complexity of divide and conquer algorithms Proves that the divide and conquer algorithms for the min-max and sorting problems are optimal

3 Table of contents The Divide and Conquer method Solving a small instance Solving a large instance Applications Divide and Conquer Sorting Insertion Sort Selection Sort Bubble Sort Merge Sort Quick Sort

4 Divide and Conquer (1)  Distinguish between small and large instances  Small instances solved differently from large ones  All instances are non-overlapping Large instance (Problem) Small instance Smaller smaller Divide Conquer

5 Divide and Conquer (2) A small instance is solved using some direct/simple strategy  Sort a list that has n (≤ 10) elements Use count, insertion, bubble, or selection sort  Find the minimum of n (≤ 2) elements When n = 0, there is no minimum element When n = 1, the single element is the minimum When n = 2, compare the two elements and determine which is smaller A large instance is solved as follows:  Divide the large instance into k (≥ 2) smaller instances  Solve the smaller instances somehow  Combine the results of the smaller instances to obtain the result for the original large instance

6 Table of contents The Method Divide and Conquer Solving a small instance Solving a large instance Applications Divide and Conquer Sorting Insertion Sort Selection Sort Bubble Sort Merge Sort Quick Sort

7 Divide and Conquer Sorting (1) Sort n elements into nondecreasing order Divide-and-conquer sorting algorithm If n is 1, Terminate Otherwise, Partition the large instance of n elements into two or more small instances Sort each small instances Combine the sorted small instances into a single sorted instance Divide-and-conquer algorithms have best complexity when a large instance is divided into small instances of approximately the same size When k = 2 and n = 24, divide into two small instances of size 12 each When k = 2 and n = 25, divide into two small instances of size 13 and 12, respectively

8 Divide and Conquer Sorting (2) Partitioning Schemes Partitioning the n elements into two unbalanced collections (i.e., n-1elements & 1 element) All three sort methods in this manner take O(n 2 ) time Insertion sort Selection sort Bubble sort Partitioning the n elements into two balanced collections (i.e., n/k element & the rest elements into 2 groups) The following methods in this manner take O(n log n) time Merge sort Quick sort

9 Insertion Sort by Divide & Conquer k = 2 divide-and-conquer sorting method Complexity is O(n 2 ) Divide Phase First n - 1 elements (a[0:n-2]) define the first small instance Last element (a[n-1]) defines the second small instance a[0: n-2] is sorted recursively Conquer Phase Combining is done by inserting a[n-1] into the sorted a[0:n-2] Here we show the recursive solution, but normally implemented non- recursively a[0].. a[n-1] a[n-2] ……

10 Example for Insertion Sort Original Array  Divide it into two arrays  Sort the 1 st array recursively  Conquer the two arrays : Insert the 2 nd array Into the 1 st array 

11 Insertion Sort Example [4, 3, 5, 1, 2] [4, 3, 5, 1][2] [4, 3, 5] [4, 3][3, 4][5] [1] [4][3] [3, 4, 5] [1, 3, 4, 5] [1, 2, 3, 4, 5] Conquer Phase: Insert the last item into the array Divide Phase: Just split the array

12 Selection Sort by Divide & Conquer k = 2 divide-and-conquer sorting method Complexity is O(n 2 ) Divide Phase: To divide a large collection into two smaller instances First find the largest element & The largest element defines one small instance The remaining n-1 elements define the second small instance The second small instance is sorted recursively Conquer Phase: Append the first smaller instance (largest element) to the right end of the sorted second small instance Here we show the recursive solution, but normally implemented non- recursivelya[0]a[n-1] a[n-2] ……

13 Example for Selection Sort Original Array  Find the largest element  Divide it into two arrays  Conquer the two arrays : Just append the 2 nd array to the right of the 1 st array  Sort the 1 st array recursively 

14 Selection Sort Example [4, 3, 5, 1, 2] [4, 3, 1, 2][5] [3, 1, 2] [1, 2] [3] [4] [1][2] [1, 2, 3] [1, 2, 3, 4] [1, 2, 3, 4, 5] Divide Phase: Move the largest item by max process Conquer Phase: Merge two arrays

15 Bubble Sort by Divide & Conquer k = 2 divide-and-conquer sorting method Complexity is O(n 2 ) Divide Phase: To divide a large collection into two smaller instances First find the largest element by bubbling (a series of swapping) The largest element defines one small instance The remaining n-1 elements define the second small instance Conquer Phase: Merge two arrays Here we show the recursive solution, but normally implemented non- recursively a[0].. a[n-1] a[n-2] ……

16 Example for Bubble Sort Original Array  Move the largest element to the right end by bubbling process  Divide it into two arrays  Sort the 1 st array recursively  Conquer the two arrays : Just append the 2 nd array to the right of the 1 st array  12345

17 Bubble Sort Example [4, 3, 5, 1, 2] [3, 4, 1, 2][5] [3, 1, 2] [1, 2] [3] [4] [1][2] [1, 2, 3] [1, 2, 3, 4] [1, 2, 3, 4, 5] Divide phase: Move the largest item by Bubbling process Conquer Phase: Merge two arrays

18 Table of contents The Method Divide and Conquer Solving a small instance Solving a large instance Applications Divide and Conquer Sorting Insertion Sort Selection Sort Bubble Sort Merge Sort Quick Sort

19 Merge Sort by Divide and Conquer (1) k = 2 divide-and-conquer sorting method Divide Step First ceil(n/2) elements define one of the smaller instances Remaining floor(n/2) elements define the second smaller instance Conquer Step Each of the two smaller instances is sorted recursively The sorted smaller instances are combined using a merge process The complexity of merge sort is O(n log n) Here we show the recursive solution, but normally implemented non- recursively

20 Merge Sort by Divide and Conquer (2) An Example for Merge Process A large instance divided into two instances (A and B) and sort them A = (2, 5, 6) B = (1, 3, 8, 9, 10) C = ( ) Compare smallest elements of A and B and merge smaller into C A = (2, 5, 6) B = (3, 8, 9, 10) C = (1) A = (5, 6) B = (3, 8, 9, 10) C = (1, 2) A = (5, 6) B = (8, 9, 10) C = (1, 2, 3) A = (6) B = (8, 9, 10) C = (1, 2, 3, 5) A = ( ) B = (8, 9, 10) C = (1, 2, 3, 5, 6) When one of A and B becomes empty, append the other list to C A = ( ) B = ( ) C = (1, 2, 3, 5, 6, 8, 9, 10) O(1) time needed to move an element into C Total time is O(n + m), where n and m are the number of elements initially in A and B

21 Merge Sort: Example (1) [8, 3, 13, 6, 2, 14, 5, 9, 10, 1, 7, 12, 4] [8, 3, 13, 6, 2, 14, 5][9, 10, 1, 7, 12, 4] [8, 3, 13, 6][2, 14, 5] [8, 3][13, 6] [8][3][13][6] [2, 14][5] [2][14] [9, 10, 1][7, 12, 4] [9, 10][1] [9][10] [7, 12][4] [7][12] Divide Phase

22 Merge Sort: Example (2) [3, 8][6, 13] [3, 6, 8, 13] [8][3][13][6] [2, 14] [2, 5, 14] [2, 3, 5, 6, 8, 13, 14] [5] [2][14] [9, 10] [1, 9, 10] [1] [9][10] [7, 12] [4, 7, 12] [1, 4, 7, 9, 10,12] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13,14] [4] [7][12] Conquer Phase: all sub components are sorted

23 Merge Sort Analysis Number of leaf nodes is n Number of nonleaf nodes is n-1 Downward pass over the recursion tree  Divide large instances into small ones  O(1) time at each node  O(n) total time at all nodes Upward pass over the recursion tree  Merge pairs of sorted lists  O(n) time merging at each level that has a nonleaf node  Number of levels is O(log n)  Total time is O(n log n)

24 Quick Sort by Divide and Conquer Small instance has n <= 1. So, Every small instance is a sorted instance To sort a large instance, select a pivot element from out of the n elements Partition the n elements into 3 groups left, middle and right The middle group contains only the pivot element All elements in the left group are <= pivot All elements in the right group are >= pivot Sort left and right groups recursively combine left group, middle group and right group Use 6 as the pivot Sort left and right groups recursively

25 Quick Sort by Divide and Conquer: Choice of Pivot Leftmost element in list that is to be sorted  When sorting a[6:20], use a[6] as the pivot  Text implementation does this Randomly select one of the elements to be sorted as the pivot  When sorting a[6:20], generate a random number r in the range [6, 20].  Use a[r] as the pivot Median-of-Three rule. From the leftmost, middle, and rightmost elements of the list to be sorted, select the one with median key as the pivot  When sorting a[6:20], examine a[6], a[13] ((6+20)/2), and a[20].  Select the element with median (i.e., middle) key  If a[6].key = 30, a[13].key = 2, and a[20].key = 10, a[20] becomes the pivot  If a[6].key = 3, a[13].key = 2, and a[20].key = 10, a[6] becomes the pivot  If a[6].key = 30, a[13].key = 25, and a[20].key = 10, a[13] becomes the pivot

26 Quick Sort by Divide and Conquer: Partitioning Sort a = [6, 2, 8, 5, 11, 10, 4, 1, 9, 7, 3] Suppose the leftmost element “6” is the pivot When another array b is available:  Scan a from left to right (omit the pivot in this scan), placing elements <= pivot at the left end of b and the remaining elements at the right end of b  The pivot is placed at the remaining position of the b Sort left and right groups recursively a b

27 Quick Sort Example [6, 2, 8, 5, 11, 10, 4, 1, 9, 7, 3] [2, 5, 4, 1, 3, 6, 7, 9, 10, 11, 8] [2, 5, 4, 1, 3][7, 9, 10, 11, 8] [1, 2, 3, 4, 5][7, 8, 11, 10, 9] [1][1] [3, 4, 5] [3, 5, 4] [5, 4] [4, 5] [4][4] [8, 11, 10, 9] [8, 9, 10, 11] [9, 10, 11] [9, 11, 10] [11, 10] [10, 11] [10]

28 Time Complexity of Quick Sort O(n) time to partition an array of n elements Let t(n) be the time needed to sort n elements t(0) = t(1) = c, where c is a constant When t > 1, t(n) = t(|left|) + t(|right|) + dn, where d is a constant t(n) is maximum when either |left| = 0 or |right| = 0 following each partitioning Overall Time Complexity The worst-case computing time for quick sort is Θ (n^2) When left is always empty The best-case computing time for quick sort is Θ (n*logn) When left and right are always of about the same size The average complexity of quick sort is also Θ (n*logn) Theorem 19.2 in your textbook

29 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May be effectively run on a parallel computer Min-max problem, matrix multiplication and so on This chapter Develops the mathematics needed to analyze the complexity of divide and conquer algorithms Proves that the divide and conquer algorithms for the min-max and sorting problems are optimal

30 Table of contents The Divide and Conquer method Solving a small instance Solving a large instance Applications Divide and Conquer Sorting Insertion Sort Selection Sort Bubble Sort Merge Sort Quick Sort