Divide-and-Conquer 7 2  9 4   2   4   7

Slides:



Advertisements
Similar presentations
Divide-and-Conquer 7 2  9 4   2   4   7
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Merge Sort 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
Sorting.
Divide-and-Conquer 7 2  9 4   2   4   7
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
File Organization and Processing Week 13 Divide and Conquer.
Sorting Fun1 Chapter 4: Sorting     29  9.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 7 2  9 4   2   4   7 2  2 9  9 4  4.
Merge Sort 1/12/2018 9:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
More on Merge Sort CS 244 This presentation is not given in class
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Merge Sort 12/4/ :32 AM Merge Sort 7 2   7  2   4  4 9
Divide-and-Conquer 7 2  9 4   2   4   7
Sorting, Sets and Selection
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Presentation transcript:

Divide-and-Conquer 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 11/21/2019 7:30 AM Divide-and-Conquer 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Divide-and-Conquer

Outline and Reading Divide-and-conquer paradigm (§5.2) 11/21/2019 7:30 AM Outline and Reading Divide-and-conquer paradigm (§5.2) Recurrence Equations (§5.2.1) Merge-sort (§4.1.1) Generic merging and set operations (§4.2.1) Quick-sort (§4.3) Analysis of quick-sort (§4.3.1) In-place quick-sort (§4.8) Summary of sorting algorithms Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Divide-and-Conquer Divide-and-conquer is a general algorithm design paradigm: Divide the input data S in two or more disjoint subsets S1, S2, … Conquer the subproblems by solving them recursively base case for the recursion: If the subproblems are small enough just solve them Combine the solutions for S1, S2, …, into a solution for S Analysis can be done using recurrence equations Divide-and-Conquer

Example: Sum of Queue One subproblem Divide and Conquer 11/21/2019 7:30 AM Example: Sum of Queue SumQueue(Q) if (Q.length == 0 ) return 0 else return Q.dequeue() + SumQueue(Q) One subproblem Linear reduction in size (decrease by 1) Combining: constant (cost of 1 add) Base case Subproblem size T(0)  b T(n)  c + T(n – 1) for n>0 1 # subproblems Work dividing and combining Divide-and-Conquer

Sum of Queue Solution Equation: T(0)  b Divide and Conquer 11/21/2019 7:30 AM Sum of Queue Solution Equation: T(0)  b T(n)  c + T(n – 1) for n>0 Solution: (finding the closed form solution) T(n)  c + c + T(n-2)  c + c + c + T(n-3)  kc + T(n-k) for all k  nc + T(0) for k=n  cn + b = O(n) Iterative substitution method Divide-and-Conquer

Example: Binary Search Divide and Conquer 11/21/2019 7:30 AM Example: Binary Search Search a sorted array for a given item, x If x == middle array element, return true Else, BinarySearch lower (x<mid) or upper (x>mid) sub-array 1 subproblem, half as large BinarySearch(A, x) if (A.size == 1) return (x == A[0]) mid = A.size / 2 if (x == A[mid]) return true else if (x < A[mid]) return BinarySearch( A_LowerHalf, x) else if (x > A[mid]) return BinarySearch( A_UpperHalf, x) 3 5 7 8 9 12 15 Find: 9 Divide-and-Conquer

Example: Binary Search Divide and Conquer 11/21/2019 7:30 AM Example: Binary Search Search a sorted array for a given item, x If x == middle array element, return true Else, BinarySearch lower (x<mid) or upper (x>mid) sub-array 1 subproblem, half as large BinarySearch(A, x) if (A.size == 1) return (x == A[0]) mid = A.size / 2 if (x == A[mid]) return true else if (x < A[mid]) return BinarySearch( A_LowerHalf, x) else if (x > A[mid]) return BinarySearch( A_UpperHalf, x) Equation: Base case Subproblem size T(1)  b T(n)  T(n/2) + c for n>1 1 # subproblems Work dividing and combining Divide-and-Conquer

Binary Search Solution Divide and Conquer 11/21/2019 7:30 AM Binary Search Solution Equation: T(1)  b T(n)  T(n/2) + c for n>1 Solution: (finding the closed form solution) T(n)  T(n/2) + c  T(n/4) + c + c  T(n/8) + c + c + c  T(n/2k) + kc  T(1) + c log n where k = log n  b + c log n = O(log n) Iterative substitution method Divide-and-Conquer

Recursion Tree for Binary Search Divide and Conquer 11/21/2019 7:30 AM Recursion Tree for Binary Search Problem size Cost per stage n O(1) n/2 O(1) n/4 O(1) log n … … 1 O(1) Θ(log n) Divide-and-Conquer

Example: Recursion Tree Divide and Conquer 11/21/2019 7:30 AM Example: Recursion Tree The recursion tree is of the form: where d > 0 is constant. And the base case has a running time b Divide-and-Conquer

Drawing the Recursion Tree Divide and Conquer 11/21/2019 7:30 AM Drawing the Recursion Tree With: b bn bn + dn logn Divide-and-Conquer

Iterative Substitution for The recursion tree Divide and Conquer 11/21/2019 7:30 AM Iterative Substitution for The recursion tree In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n. So, Thus, T(n) is O(n log n). Divide-and-Conquer

Guess-and-Test Method, Part 1 Divide and Conquer 11/21/2019 7:30 AM Guess-and-Test Method, Part 1 In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction: Guess: T(n) < cn log n. Wrong: we cannot make this last line be less than cn log n Divide-and-Conquer

Guess-and-Test Method, Part 2 Divide and Conquer 11/21/2019 7:30 AM Guess-and-Test Method, Part 2 Recall the recurrence equation: Guess #2: T(n) < cn log2 n. if c > b. So, T(n) is O(n log2 n). In general, to use this method, you need to have a good guess and you need to be good at induction proofs. Divide-and-Conquer

The divide-and-conquer design paradigm Modified from: Carola Wenk, University of Texas at San Antonio 11/21/2019 7:30 AM The divide-and-conquer design paradigm Divide the problem (instance) into subproblems. a subproblems, each of size n/b Conquer the subproblems by solving them recursively. The base case has a runtime c Combine subproblem solutions. Runtime is f(n) Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Master Method Many divide-and-conquer recurrence equations have the form: The Master Theorem: Divide-and-Conquer

Solution: logba=2, so case 1 says T(n) is O(n2). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 1 The form: The Master Theorem: Example: Solution: logba=2, so case 1 says T(n) is O(n2). Divide-and-Conquer

Solution: logba=1, so case 2 says T(n) is O(n log2 n). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 2 The form: The Master Theorem: Example: Solution: logba=1, so case 2 says T(n) is O(n log2 n). Divide-and-Conquer

Solution: logba=0, so case 3 says T(n) is O(n log n). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 3 The form: The Master Theorem: Example: Solution: logba=0, so case 3 says T(n) is O(n log n). Divide-and-Conquer

Solution: logba=3, so case 1 says T(n) is O(n3). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 4 The form: The Master Theorem: Example: Solution: logba=3, so case 1 says T(n) is O(n3). Divide-and-Conquer

Solution: logba=2, so case 3 says T(n) is O(n3). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 5 The form: The Master Theorem: Example: Solution: logba=2, so case 3 says T(n) is O(n3). Divide-and-Conquer

Solution: logba=0, so case 2 says T(n) is O(log n). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 6 The form: The Master Theorem: Example: (binary search) Solution: logba=0, so case 2 says T(n) is O(log n). Divide-and-Conquer

Solution: logba=1, so case 1 says T(n) is O(n). Divide and Conquer 11/21/2019 7:30 AM Master Method, Example 7 The form: The Master Theorem: Example: (heap construction) Solution: logba=1, so case 1 says T(n) is O(n). Divide-and-Conquer

Iterative “Proof” of the Master Theorem Divide and Conquer 11/21/2019 7:30 AM Iterative “Proof” of the Master Theorem Using iterative substitution, let us see if we can find a pattern: We then distinguish the three cases as The first term is dominant Each part of the summation is equally dominant The summation is a geometric series Divide-and-Conquer

Divide-and-Conquer, Algorithm Examples: Merge Sort Quick Sort Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Merge Sort 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Divide-and-Conquer

Outline Merge-sort (§4.1.1) Divide and Conquer 11/21/2019 7:30 AM Outline Merge-sort (§4.1.1) Algorithm Merging two sorted sequences Merge-sort tree Execution example Analysis Generic merging and set operations (§4.2.1) Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Merge-Sort Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm Like heap-sort It uses a comparator It has O(n log n) running time Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Merge-Sort Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S1, S2)  partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) S  merge(S1, S2) Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1 and S2 of about n/2 elements each Recur: recursively sort S1 and S2 Conquer: merge S1 and S2 into a unique sorted sequence f(n)=bn 2T(n/2) Base case: merge(elem1,elem2) Running time = b Divide-and-Conquer

Merging Two Sorted Sequences Divide and Conquer 11/21/2019 7:30 AM Merging Two Sorted Sequences The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while A.isEmpty()  B.isEmpty() if A.first().element() < B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) while A.isEmpty() S.insertLast(A.remove(A.first())) while B.isEmpty() S.insertLast(B.remove(B.first())) return S Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Merge-Sort Tree An execution of merge-sort is depicted by a binary tree each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution the root is the initial call the leaves are calls on subsequences of size 0 or 1 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Divide-and-Conquer

Execution Example Partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 Divide and Conquer 11/21/2019 7:30 AM Execution Example Partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, base case 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, base case 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, …, base case, merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, …, merge, merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Merge 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Divide-and-Conquer

Analysis of Merge-Sort Divide and Conquer 11/21/2019 7:30 AM Analysis of Merge-Sort The height h of the merge-sort tree is O(log n) at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O(n) we partition and merge 2i sequences of size n/2i we make 2i+1 recursive calls Thus, the total running time of merge-sort is O(n log n) depth #seqs size 1 n 2 n/2 i 2i n/2i … Divide-and-Conquer

Recurrence Equation Analysis Divide and Conquer 11/21/2019 7:30 AM Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. Likewise, the basis case (n < 2) will take at most b steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation. That is, a solution that has T(n) only on the left-hand side. Divide-and-Conquer

Iterative Substitution Divide and Conquer 11/21/2019 7:30 AM Iterative Substitution In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n. So, Thus, T(n) is O(n log n). Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM 2. Quick-Sort 7 4 9 6 2  2 4 6 7 9 4 2  2 4 7 9  7 9 2  2 9  9 Divide-and-Conquer

Outline Quick-sort (§4.3) Analysis of quick-sort (4.3.1) Divide and Conquer 11/21/2019 7:30 AM Outline Quick-sort (§4.3) Algorithm Partition step Quick-sort tree Execution example Analysis of quick-sort (4.3.1) In-place quick-sort (§4.8) Summary of sorting algorithms Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Quick-Sort Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Recur: sort L and G Conquer: join L, E and G x x L G E x Divide-and-Conquer

Partition We partition an input sequence as follows: Divide and Conquer 11/21/2019 7:30 AM Partition We partition an input sequence as follows: We remove, in turn, each element y from S and We insert y into L, E or G, depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time Thus, the partition step of quick-sort takes O(n) time Algorithm partition(S, p) Input sequence S, position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G  empty sequences x  S.remove(p) while S.isEmpty() y  S.remove(S.first()) if y < x L.insertLast(y) else if y = x E.insertLast(y) else { y > x } G.insertLast(y) return L, E, G Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Quick-Sort Tree An execution of quick-sort is depicted by a binary tree Each node represents a recursive call of quick-sort and stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or 1 7 4 9 6 2  2 4 6 7 9 4 2  2 4 7 9  7 9 2  2 9  9 Divide-and-Conquer

Execution Example Pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 Divide and Conquer 11/21/2019 7:30 AM Execution Example Pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 2  2 9 4  4 9 3  3 8  8 9  9 4  4 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Partition, recursive call, pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  2 4 7 9 3 8 6 1  1 3 8 6 2  2 9 4  4 9 3  3 8  8 9  9 4  4 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Partition, recursive call, base case 7 2 9 4 3 7 6 1   1 2 3 4 6 7 8 9 2 4 3 1  2 4 7 3 8 6 1  1 3 8 6 1  1 9 4  4 9 3  3 8  8 9  9 4  4 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, …, base case, join 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 3 8 6 1  1 3 8 6 1  1 4 3  3 4 3  3 8  8 9  9 4  4 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Recursive call, pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 7 9 7 1  1 3 8 6 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Partition, …, recursive call, base case 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 7 9 7 1  1 3 8 6 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Divide-and-Conquer

Execution Example (cont.) Divide and Conquer 11/21/2019 7:30 AM Execution Example (cont.) Join, join 7 2 9 4 3 7 6 1  1 2 3 4 6 7 7 9 2 4 3 1  1 2 3 4 7 9 7  17 7 9 1  1 4 3  3 4 77 78 9  9 9  9 4  4 Divide-and-Conquer

Worst-case Running Time Divide and Conquer 11/21/2019 7:30 AM Worst-case Running Time The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of L and G has size n - 1 and the other has size 0 The running time is proportional to the sum n + (n - 1) + … + 2 + 1 Thus, the worst-case running time of quick-sort is O(n2) depth time n 1 n - 1 … … Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Expected Running Time Consider a recursive call of quick-sort on a sequence of size s Good call: the sizes of L and G are each less than 3s/4 Bad call: one of L and G has size greater than 3s/4 A call is good with probability 1/2 1/2 of the possible pivots cause good calls: 7 2 9 4 3 7 6 1 9 7 2 9 4 3 7 6 1 2 4 3 1 7 9 7 1  1 1 7 2 9 4 3 7 6 Good call Bad call 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bad pivots Good pivots Bad pivots Divide-and-Conquer

Expected Running Time, Part 2 Divide and Conquer 11/21/2019 7:30 AM Expected Running Time, Part 2 Probabilistic Fact: The expected number of coin tosses required in order to get k heads is 2k For a node of depth i, we expect i/2 ancestors are good calls The size of the input sequence for the current call is at most (3/4)i/2n Therefore, we have For a node of depth 2log4/3n, the expected input size is one The expected height of the quick-sort tree is O(log n) The amount or work done at the nodes of the same depth is O(n) Thus, the expected running time of quick-sort is O(n log n) Divide-and-Conquer

Summary of Sorting Algorithms Divide and Conquer 11/21/2019 7:30 AM Summary of Sorting Algorithms Algorithm Time Notes selection-sort O(n2) in-place slow (good for small inputs) insertion-sort quick-sort O(n log n) expected in-place, randomized fastest (good for large inputs) heap-sort O(n log n) fast (good for large inputs) merge-sort sequential data access fast (good for huge inputs) Divide-and-Conquer

Divide and Conquer 11/21/2019 7:30 AM Conclusions Divide and conquer is just one of several powerful techniques for algorithm design. Divide-and-conquer algorithms can be analyzed using recurrences and the master method (so practice this math). Can lead to more efficient algorithms Divide-and-Conquer