Tomado del libro Cormen

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Advertisements

CSE 3101: Introduction to the Design and Analysis of Algorithms
Lecture 3: Randomized Algorithm and Divide and Conquer II: Shang-Hua Teng.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.
Introduction to Algorithms
CSE 2331/5331 Topic 5: Prob. Analysis Randomized Alg.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
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]
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
7.Quicksort Hsu, Lih-Hsing. Computer Theory Lab. Chapter 7P Description of quicksort Divide Conquer Combine.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort!. A practical sorting algorithm Quicksort  Very efficient Tight code Good cache performance Sort in place  Easy to implement  Used in older.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
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.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
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.
Deterministic and Randomized Quicksort Andreas Klappenecker.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
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.
CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Randomized Algorithms
Lecture 8 Randomized Algorithms
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
Sorting atomic items Chapter 5 Distribution based sorting paradigms.
Sorting We have actually seen already two efficient ways to sort:
Insertion Sort
Sorting We have actually seen already two efficient ways to sort:
Quick Sort (11.2) CSE 2011 Winter November 2018.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Ch 7: Quicksort Ming-Te Chi
Randomized Algorithms
Lecture 3 / 4 Algorithm Analysis
Topic: Divide and Conquer
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithm Analysis
Order Statistics Comp 550, Spring 2015.
Data Structures Sorting Haim Kaplan & Uri Zwick December 2014.
CS 583 Analysis of Algorithms
EE 312 Software Design and Implementation I
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Chapter 7 Quicksort.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Topic: Divide and Conquer
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
CS 583 Analysis of Algorithms
Chapter 7: Quick Sort Recursive sorting using Partition
The Selection Problem.
Quicksort and Randomized Algs
Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II
CS200: Algorithm Analysis
Sorting We have actually seen already two efficient ways to sort:
Medians and Order Statistics
Presentation transcript:

Tomado del libro Cormen nalysis of A lgorithms Tomado del libro Cormen

OUTLINE Quicksort Description Analysis Randomized Quicksort

Quicksort

Quicksort Quicksort(A,p,r) if p < r q  Partition(A,p,r) Quicksort(A,p,q-1) Quicksort(A,q+1,r)

Partition - two finger algorithm Partition(A,p,r) choose an element to be a pivot and pull it out of the array, say at left end maintain two fingers starting at each end of the array slide them towards each other until you get a pair of elements where right finger has a smaller element and left finger has a bigger one (when compared to pivot) swap them and repeat until fingers meet put the pivot element where they meet.

Partition - two finger algorithm Partition(A,p,r) pivot  A[p] i  p j r while i < j do while (A[i]  pivot & i  r) do i  i+1 while (A[j] > pivot & j  p) do j  j-1 if i < j then A[i]  A[j] end-while A[p]  A[j] return j

Partition ( A,1,8) pivot 2 p r 1 2 3 4 5 6 7 8 A 2 8 7 1 3 5 6 4 i j

pivot 2 1 2 3 4 5 6 7 8 2 8 7 1 3 5 6 4 i j 1 2 3 4 5 6 7 8 2 1 7 8 3 5 6 4 j i

pivot 2 1 2 3 4 5 6 7 8 2 1 7 8 3 5 6 4 j i 1 2 3 4 5 6 7 8 1 2 7 8 3 5 6 4 j i

Loop Invariant for Partition At the start of each iteration of the while loop and for any array index k, 1. If p  k  i-1, then A[k]  pivot. 2. If j+1  k  r, then A[k] > pivot.

Partition - four finger algorithm Partition(A,p,r) pivot A[r] i  p - 1 for j  p to r-1 do if A[j]  pivot then i  i + 1 A[i]  A[j] A[i + 1]  A[r] return i + 1

Partition ( A,1,8) pivot 4 p r 1 2 3 4 5 6 7 8 A 2 8 7 1 3 5 6 4 i j

pivot 4 1 2 3 4 5 6 7 8 2 8 7 1 3 5 6 4 j i 1 2 3 4 5 6 7 8 2 8 7 1 3 5 6 4 i j

pivot 4 1 2 3 4 5 6 7 8 2 8 7 1 3 5 6 4 i j 1 2 3 4 5 6 7 8 2 1 7 8 3 5 6 4 i j

pivot 4 1 2 3 4 5 6 7 8 3 2 1 8 7 5 6 4 i j 1 2 3 4 5 6 7 8 3 2 1 8 7 5 6 4 i j

pivot 4 1 2 3 4 5 6 7 8 3 2 1 8 7 5 6 4 i j 1 2 3 4 5 6 7 8 3 2 1 4 7 5 6 8 i j

Loop Invariant for Partition At the start of each iteration of the for loop and for any array index k, 1. If p  k  i, then A[k]  pivot. 2. If i +1  k  j-1, then A[k] > pivot. 2. If k = r, then A[k] = pivot.

IN PRACTICE Often choose pivot in fixed way as middle element for small arrays median of 1st, middle, and last for larger arrays median of 3 medians of 3 (9 elements in all) for largest arrays

Analysis Partition does n-1 comparisons on a list of length n pivot is compared to each other element. If pivot is ith largest then two subproblems are of size i-1 and n-i. Pivot is equally likely to be any one of 1st through nth largest

BEST CASE WORST CASE

AVERAGE CASE

Randomized Quicksort RandQS

RandPartition(A,p,r) Randomized Partition A[p]  A[random(p,r)]

RandQS RandQS(A,p,r) if p < r q  RandPartition(A,l,r) RandQS(A,p,q-1) RandQS(A,q+1,r)

Input: a set of numbers A Output: the elements of A sorted in increasing order. Steps: 1. Choose and element q uniformly at random form A. 2. Determine the set A1 of elements of A smaller that q, and the set A2 of elements of A larger that q 3. Recursively sort A1 and A2 .

Lemma 7.1 Let be X the number fo comparisons A[j]  pivot in partition. Then the running time of Quicksort and RandQS is T(n) = O(n+X)

Expected number of Comparisons Let be {z1 , z2 , …, zn } the sorted permutation of the elements of A, and Zij = {zi , zi+i, …, zj }. 1 if zi and zj are compared Xij = 0 in other case

Total number of comparisons

Expected value

Calculating Pr[ zi is compared to zi] In general, once a pivot q is chosen with zi < q <, zj, we know that zi and zj cannot be compared in subsequent time. If zi is chosen as pivot before any other item of Zij , then zi will be compared to each item of Zij except itself.

Simiarly, if zj is chosen as pivot before any other item of Zij , then zj will be compared to each item of Zij except itself.

2 | 3 | 5| 7 | 1| 4 | 8 | 6 q 2 | 3 | 5| 1| 4 | 6 7 8 q 2 | 3 | 5| 1| 4 | 6 7 8

2 | 3 | 1| 4 5 6 7 8 q 7 5 8 6 2 | 3 | 1| 4

2 | 1 3 4 5 6 7 8 q 7 5 8 3 6 4 2 | 1

7 5 8 3 6 2 4 1 1 | 2 | 3| 4 | 5| 6 | 7 | 8  = { 7 , 5, 8, 3, 6, 2, 4, 1}

 = { 7 , 5, 8, 3, 6, 2, 4, 1} root q Level 1

There is a comparison between zi and zj if and only if zi or zj occurs earlier in the permutation  than any element zk with i < k < j. Prior to the point at which an element from Zij has been chosen as pivot, the whole set Zij is in the same partition. Therefore, any element of Zij is equally likely to be chosen as pivot.

Zij has j-i+1 elements. The probability that any given element is the first one chosen as pivot is

Pr[ zi is compared to zi]

The Expected Time E[T(n)] = O(n+E[X]) = O(n+n lgn) = O(n lgn)

Observations There is more than one possible execution path for a given input By example, for n = 3 if A=[2,3,1] we have the following execution paths:

The probability of a given execution path can be different of the probability of another execution path. By example, for A=[2,3,1] n = 3 we have the following execution paths with its probabilities:

Pivot 1 1 1/3 1/3 1 3 1/2 1/3 2 2 3 1/6 1/6

Pivot 2 1/3 2 1 3 1/3

Pivot 3 3 1/3 1/3 3 1 1/2 2 1/3 2 1 1 1/6 1/6

Deviations of the expected time for RandQs (tail probabilities) PROBABILTY OF DEVIATION OF THE EXPECTED TIME We calculate the probability that a randomized algorithm follows and execution path that takes a time larger that the expected time.

Probability distribution for T(n) Pr[T(n)=k] k E[T(n)] r E[T(n)] Pr[T(n)  r E [T(n)] ]

In the case of RandQs we have that T(n) is a random variable that satisfies: n lgn  T(n)  n2 Best case time Worst case time

PROBABILTY INEQUALITIES USED Markov Inequality (bound) Chebischev Inequality (bound)

Proof of Markov bound

CHERNOFF BOUND Bernoulli trials: Let Xi , 1 i  n random variables with 1(success) with probability p Xi = 0(failure) with probability q=1-p

Binomial random variable: Let Xi be n independent Bernoulli trials then is a Binomial random variable with

Poisson trials: Let Xi , 1 i  n random variables with 1(success) with probability pi Xi = 0(failure) with probability qi=1-pi

THEOREM:Chernoff bound for sum of independent Poisson trials Let Xi be n independent Bernoulli trials with Then for

and

And for any  > 0

CHERNOFF BOUND in RandQS E[X]= O(n lgn)

THEOREM: RandQs runs in n lgn with high probability for n  16 Proof: Pr that will run in more tha 1.5 times the expected time

Proof of Chernoff bound: for any t > 0 and

and

then

using the inequality 1+x <= ex

using the inequality 1+x <= ex Minimizing the right expression