Chapter 3: Divide and Conquer

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

A simple example finding the maximum of a set S of n numbers.
1 Today’s Material Medians & Order Statistics – Ch. 9.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
Algorithm Design Paradigms
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
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
Theory of Algorithms: 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 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Chapter 7: Sorting Algorithms
Introduction to Algorithms Chapter 7: Quick Sort.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
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 7 (Part 2) Sorting Algorithms Merge Sort.
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.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
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.
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.
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.
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 Strategy
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Analysis of Algorithms CS 477/677
Divide-and-Conquer The most-well known algorithm design strategy:
Advance Analysis of Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
Medians and Order Statistics
Topic: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
CSE 373 Data Structures and Algorithms
Topic: Divide and Conquer
CSC 380: Design and Analysis of Algorithms
CS200: Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Chapter 3: Divide and Conquer Quick Sort Another method which is different from Merge Sort in which, the divided sub files are not need to be merged later. This method uses a procedure known as “PARTITION” (C.A.R. Hoare).

Chapter 3: Divide and Conquer Partitioning To partition data is to divide it into two groups, so that all the items with a key value higher than a specified value are in one group, and all the item with a lower key value are in another. Data could be partitioned more than 2-ways The value used to determine in which of two groups an item is placed is called a pivot value

How is the pivot selected Chapter 3: Divide and Conquer Basically the quicksort algorithm operates by partitioning an array into two subarrays, and then calling it self to quicksort each of these subarrays. Algorithm Partition the array into left and right subarrays Call itself to sort left subarray Call itself to sort right subarray If array has only one element it is sorted How is the pivot selected

procedure PARTITION(m:integer {index}, var p:integer{index+1}); Chapter 3: Divide and Conquer Global n: integer; A:array [1..n] of items; procedure PARTITION(m:integer {index}, var p:integer{index+1}); {within A[m], A[m+1],..., A[p-1] the elements are rearranged in such a way that if initially t = A[m], then after completion A[q] = t, for some q between m and p-1, final value of p is q} var i: integer; v: item; begin v := A[m]; i:= m; {A[m] is a partition element} while (i < p) do

while(A[i] < v) do i := i+1; {i moves left to right} p := p-1; Chapter 3: Divide and Conquer begin i := i+1; while(A[i] < v) do i := i+1; {i moves left to right} p := p-1; while(A[p] > v) do p := p-1; {p moves right to left} if (i < p) then swap(A[i], A[p]); end A[m] := A[p]; A[p] := v; {the partition element belongs at p} end.

Global n: integer; A:array [1..n] of items; Chapter 3: Divide and Conquer Global n: integer; A:array [1..n] of items; procedure QUICKSORT(p, q:integer); {sort elements A[p]..A[q] which resides in the A[1..n], in ascending order. A[n+1] >= all elements in A[p..q] and set to infinity} begin if (p < q) then j := q+1; PARTITION(p,j); QUICKSORT(p, j-1); QUICKSORT(j+1, q); end end.

Quick sort Example Chapter 3: Divide and Conquer 65 70 75 80 85 60 55 50 45 inf Pivot item

Chapter 3: Divide and Conquer 65 70 75 80 85 60 55 50 45 inf Pivot item

How do we move pivot to its proper place? Chapter 3: Divide and Conquer Partition 65 70 75 80 85 60 55 50 45 inf How do we move pivot to its proper place?

Correct place for pivot Chapter 3: Divide and Conquer Correct place for pivot 65 70 75 80 85 60 55 50 45 inf Partitioned left subarray Partitioned right subarray

Chapter 3: Divide and Conquer Partition 65 70 75 80 85 60 55 50 45 inf

Chapter 3: Divide and Conquer 65 70 75 80 85 60 55 50 45 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 70 75 80 85 60 55 50 45  2 9

Chapter 3: Divide and Conquer 65 45 75 80 85 60 55 50 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 75 80 85 60 55 50 70  2 9

Chapter 3: Divide and Conquer 65 45 75 80 85 60 55 50 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 75 80 85 60 55 50 70  3 8

Chapter 3: Divide and Conquer 65 45 50 80 85 60 55 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 80 85 60 55 75 70  3 8

Chapter 3: Divide and Conquer 65 45 50 80 85 60 55 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 80 85 60 55 75 70  4 7

Chapter 3: Divide and Conquer 65 45 50 55 85 60 80 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 55 85 60 80 75 70  4 7

Chapter 3: Divide and Conquer 65 45 50 55 85 60 80 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 55 85 60 80 75 70  5 6

Chapter 3: Divide and Conquer 65 45 50 55 60 85 80 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 55 85 60 80 75 70  5 6

Chapter 3: Divide and Conquer 65 45 50 55 60 85 80 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 55 85 60 80 75 70  6 5

Chapter 3: Divide and Conquer 65 45 50 55 60 85 80 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 45 50 55 85 60 80 75 70 

Chapter 3: Divide and Conquer 60 45 50 55 65 85 80 75 70 inf (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 60 45 50 55 85 65 80 75 70 

Chapter 3: Divide and Conquer 60 45 50 55 65 85 80 75 70 inf Pivot item

Chapter 3: Divide and Conquer (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p 65 70 75 80 85 60 55 50 45  2 9 65 45 75 80 85 60 55 50 70  3 8 65 45 50 80 85 60 55 75 70  4 7 65 45 50 55 85 60 80 75 70  5 6 65 45 50 55 60 85 80 75 70  6 5 60 45 50 55 65 85 80 75 70

Chapter 3: Divide and Conquer Selection of the pivot Ideally the pivot should be the median of the items being sorted. That is, half the items should be greater than the pivot, and half smaller. The worst situation results when a subarray with n elements is divided into one subarray with 1 element and the other with n-1 elements. There are two problems with it Performance of algorithm (n2) Recursive function calls take space on the machine stack

Worst case analysis of QUICKSORT Chapter 3: Divide and Conquer Worst case analysis of QUICKSORT Assumptions: 1. The n elements to be sorted are distinct 2. The partitioning element in PARTITION is chosen with some random selection procedure. The number of element comparisons in each call of “PARTITION” is p-m+1 (distinct elements) p-m+2 (repeated elements)

At level one, one call PARTITON(1, n+1); r = n; Chapter 3: Divide and Conquer Let r be the total number of elements comparisons required at any level of recursion- At level one, one call PARTITON(1, n+1); r = n; At level two, at most two calls are made and r = n-1; At each level of recursion O(r) elements comparisons are made so- or (n2)

Average case analysis of QUICKSORT Chapter 3: Divide and Conquer Average case analysis of QUICKSORT Average case value CA(n) is much less than its worst case, under the assumptions made earlier. The partitioning element v in the call of PARTITION(m, p) has equal probability of being the ith , , smallest element, in A[m,p-1], hence two files remaining to be sorted will be A[m:j] and A[j+1:p-1] with probability 1/(p-m), for all

From this we obtain the recurrence relation ...(1) Chapter 3: Divide and Conquer From this we obtain the recurrence relation ...(1) n+1 is the number of comparisons required by PARTITION on its first call ...(2) replacing n by n-1we get ...(3)

subtracting (3) from (2) we get Chapter 3: Divide and Conquer subtracting (3) from (2) we get or repeatedly using this equation to substitute for CA(n-1), CA(n-2),... we get

Chapter 3: Divide and Conquer = O(n log n)

is known as Euler’s constant Chapter 3: Divide and Conquer , where is known as Euler’s constant = O(n log n)

Matrix Multiplication Chapter 3: Divide and Conquer Matrix Multiplication Let A and B are two matrices, the product C = AB is also a matrix defined as- As a divide and conquer solution of this problem (n = 2k), for n = 2 the solution needs 8 multiplications and 4 additions and complexity of it is O(n3).

Strassen Matrix Multiplication Chapter 3: Divide and Conquer Strassen Matrix Multiplication Strassen discovered a way to compute Cij using 7 multiplic-ations and 18 additions. 7 multiplication of size are computed as- P = (A11+A22) (B11+B22) T = (A11+A12) B22 Q = (A21+A22) B11 U = (A21-A11) (B11+B12) R = A11 (B12-B22) V = (A12-A22) (B21+B22) S = A22 (B21-B11)

required time T(n) is given by Chapter 3: Divide and Conquer and then C11 = P+S-T+V C12 = R+T C21 = Q+S C22 = P+R-Q+U required time T(n) is given by

= 7kT(1) + an2((7/4)0+(7/4)1+...+(7/4)k-1) Chapter 3: Divide and Conquer T(n) = 7T(n/2) + an2 = 7[7T(n/4) + an2/4] + an2 : = 7kT(1) + an2((7/4)0+(7/4)1+...+(7/4)k-1) = 7log n+ 7log n+cn2[(7/4)k]

= cnlog 4[n log(7/4)] + nlog 7 Chapter 3: Divide and Conquer = cnlog 4 (7/4)log n+7log n = cnlog 4[n log(7/4)] + nlog 7 = cnlog 4 nlog 7-log 4+nlog 7 = cnlog 4+log 7-log4+nlog 7 = O(n log 7) = O(n2.81) Victor Pan improved time to O(n2.681)

Chapter 3: Divide and Conquer Ex. 1, 2, 4, 6, 8, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 28, 29, 38, 39, 40, 41

Chapter 3: Divide and Conquer End of Chapter 3