Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tomado del libro Cormen

Similar presentations


Presentation on theme: "Tomado del libro Cormen"— Presentation transcript:

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

2 OUTLINE Quicksort Description Analysis Randomized Quicksort

3 Quicksort

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

5 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.

6 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 while (A[j] > pivot & j  p) do j  j if i < j then A[i]  A[j] end-while A[p]  A[j] return j

7 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

8 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

9 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

10 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.

11 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

12 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

13 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

14 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

15 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

16 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

17 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.

18 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

19 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

20 BEST CASE WORST CASE

21 AVERAGE CASE

22

23

24 Randomized Quicksort RandQS

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

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

27 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 .

28 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)

29 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

30 Total number of comparisons

31 Expected value

32 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.

33 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.

34 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

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

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

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

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

39 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.

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

41 Pr[ zi is compared to zi]

42

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

44

45

46 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:

47 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:

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

49 Pivot 2 1/3 2 1 3 1/3

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

51 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.

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

53 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

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

55 Proof of Markov bound

56 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

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

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

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

60 and

61 And for any  > 0

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

63 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

64 Proof of Chernoff bound: for any t > 0
and

65 and

66 then

67 using the inequality 1+x <= ex

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


Download ppt "Tomado del libro Cormen"

Similar presentations


Ads by Google