1.39k likes | 1.55k Views
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.
E N D
Randomized Algorithms Eduardo Laber Loana T. Nogueira
Quicksort • Objective
Quicksort • Objective • Sort a list of n elements
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 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
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
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
Suppose we know how to find y Time to find y: cn steps, for some constant c
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
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
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
What´s the problem with the scheme above? Quicksort How to find y?
Deterministic Quicksort • Let y be the first element of S
Deterministic Quicksort • Let y be the first element of S • Split S into two sets: S< and S>
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
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> )
Performance • Worst Case: O( n2 ) • Avarage Case: O( nlogn )
Performance • Worst Case: O( n2 ) (The set is already sorted) • Avarage Case: O( nlogn )
Performance • Worst Case: O( n2 )(The set is already sorted) • Avarage Case: O( nlogn )
An Randomzied Algorithm • An algorithm that makes choice (random) during the algorithm execution
Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S
Randomized Quicksort (RandQS) • Choose an element y uniformly at random of S • Every element of S has equal probability fo being chosen
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>
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>
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>
Intuition • For some instance Quicksort works very bad O( n2 )
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
Analysis • For sorting algorithms: we measure the running time of RandQS in terms of the number of comparisons it performs
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
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
Analysis Si: the ith smallest element of S
Analysis Si: the ith smallest element of S S1 is the smallest element of S
Analysis Si: the ith smallest element of S S1 is the smallest element of S Sn is the largest element of S
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
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
Analysis Xij is a count of comparisons between Si and Sj: n The total number of comparisons: Xij i = 1 j > i
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
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
Analysis Pij: the probability that Si and Sj are compared in an execution
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
Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y
Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y S<
Analysis – Binary Tree T of RandQS • Each node is labeled with a distinct element of S T y S> S<
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