1 / 138

Randomized Algorithms

Randomized Algorithms. Eduardo Laber Loana T. Nogueira. Quicksort. Objective. Quicksort. Objective Sort a list of n elements. An Idea. An Idea. Imagine if we could find an element y  S such that half the members of S are smaller than y, then we could use the following scheme. An Idea.

Télécharger la présentation

Randomized Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Randomized Algorithms Eduardo Laber Loana T. Nogueira

  2. Quicksort • Objective

  3. Quicksort • Objective • Sort a list of n elements

  4. An Idea

  5. An Idea Imagine if we could find an element y  S such that half the members of S are smaller than y, then we could use the following scheme

  6. An Idea Imagine if we could find an element y  S such that half the members of S are smaller than y, then we could use the following scheme • Partition S\{y} into two sets S1 and S2

  7. An Idea Imagine if we could find an element y  S such that half the members of S are smaller than y, then we could use the following scheme • Partition S\{y} into two sets S1 and S2 • S1: elements of S that are smaller than y • S2: elements of S that are greater than y

  8. An Idea Imagine if we could find an element y  S such that half the members of S are smaller than y, then we could use the following scheme • Partition S\{y} into two sets S1 and S2 • S1: elements of S that are smaller than y • S2: elements of S that are greater than y • Recursively sort S1 and S2

  9. Suppose we know how to find y

  10. Suppose we know how to find y Time to find y: cn steps, for some constant c

  11. Suppose we know how to find y Time to find y: cn steps, for some constant c we could partition S\{y} into S1 and S2 in n-1 additional steps

  12. Suppose we know how to find y Time to find y: cn steps, for some constant c we could partition S\{y} into S1 and S2 in n-1 additional steps The total number os steps in out sorting procedure would be given by the recurrence T(n)  2T(n/2) + (c+1)n

  13. Suppose we know how to find y Time to find y: cn steps, for some constant c we could partition S\{y} into S1 and S2 in n-1 additional steps The total number os steps in out sorting procedure would be given by the recurrence  c’nlogn T(n)  2T(n/2) + (c+1)n

  14. What´s the problem with the scheme above? Quicksort

  15. What´s the problem with the scheme above? Quicksort How to find y?

  16. Deterministic Quicksort • Let y be the first element of S

  17. Deterministic Quicksort • Let y be the first element of S • Split S into two sets: S< and S>

  18. Deterministic Quicksort • Let y be the first element of S • Split S into two sets: S< and S> • S< : elements smaller than y • S> : elements greater than y

  19. Deterministic Quicksort • Let y be the first element of S • Split S into two sets: S< and S> • S< : elements smaller than y • S> : elements greater than y • Qsort ( S< ), Qsort ( S> )

  20. Performance • Worst Case: O( n2 ) • Avarage Case: O( nlogn )

  21. Performance • Worst Case: O( n2 ) (The set is already sorted) • Avarage Case: O( nlogn )

  22. Performance • Worst Case: O( n2 )(The set is already sorted) • Avarage Case: O( nlogn )

  23. An Randomzied Algorithm

  24. An Randomzied Algorithm • An algorithm that makes choice (random) during the algorithm execution

  25. Randomized Quicksort (RandQS)

  26. Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S

  27. Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S • Every element of S has equal probability fo being chosen

  28. Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S • Every element of S has equal probability fo being chosen • By comparing each element fo S with y, determine S< and S>

  29. Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S • Every element of S has equal probability fo being chosen • By comparing each element fo S with y, determine S< and S> • Recursively sort S< and S>

  30. Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S • Every element of S has equal probability fo being chosen • By comparing each element fo S with y, determine S< and S> • Recursively sort S< and S> • OUTPUT: S<, followed by y and then S>

  31. Intuition • For some instance Quicksort works very bad  O( n2 )

  32. Intuition • For some instance Quicksort works very bad  O( n2 ) • Randomization produces different executions for the same input. There is no instance for which RandQS works bad in avarage

  33. Analysis

  34. Analysis • For sorting algorithms: we measure the running time of RandQS in terms of the number of comparisons it performs

  35. Analysis • For sorting algorithms: we measure the running time of RandQS in terms of the number of comparisons it performs This is the dominat cost in any reasonable implementation

  36. Analysis • For sorting algorithms: we measure the running time of RandQS in terms of the number of comparisons it performs This is the dominat cost in any reasonable implementation Our Goal: Analyse the expected number of comparisons in an execution of RandQS

  37. Analysis Si: the ith smallest element of S

  38. Analysis Si: the ith smallest element of S S1 is the smallest element of S

  39. Analysis Si: the ith smallest element of S S1 is the smallest element of S Sn is the largest element of S

  40. Analysis Si: the ith smallest element of S Define the random variable S1 is the smallest element of S Sn is the largest element of S 1, if Si and Sj are compared xik= 0, otherwise

  41. Analysis Si: the ith smallest element of S Define the random variable Dado um experimento aleatório com espaço amostral S, uma variável aleatória é uma função que associa a cada elemento amostral um número real S1 is the smallest element of S Sn is the largest element of S 1, if Si and Sj are compared xik= 0, otherwise

  42. Analysis Xij is a count of comparisons between Si and Sj: n   The total number of comparisons: Xij i = 1 j > i

  43. Analysis Xij is a count of comparisons between Si and Sj: n   The total number of comparisons: Xij i = 1 j > i We are interested in the expected number of comparisons n   E[ ] Xij i = 1 j > i

  44. Analysis Xij is a count of comparisons between Si and Sj: n   The total number of comparisons: Xij i = 1 j > i By the linearity of E[] n We are interested in the expected number of comparisons n     E[Xij] E[ ] = Xij i = 1 j > i i = 1 j > i

  45. Analysis Pij: the probability that Si and Sj are compared in an execution

  46. Analysis Pij: the probability that Si and Sj are compared in an execution Since Xij only assumes the values 0 and 1, ij ij ij ij

  47. Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y

  48. Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y S<

  49. Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y S> S<

  50. Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y S> S< The root of T is compared to the elements in the two sub-trees, but no comparisons is perfomed between an element of the left and righ sub-trees

More Related