randomized algorithms
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/1.jpg)
Randomized Algorithms
Eduardo Laber
Loana T. Nogueira
![Page 2: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/2.jpg)
Quicksort
Objective
![Page 3: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/3.jpg)
Quicksort
Objective
– Sort a list of n elements
![Page 4: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/4.jpg)
An Idea
![Page 5: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/5.jpg)
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
![Page 6: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/6.jpg)
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
![Page 7: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/7.jpg)
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
![Page 8: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/8.jpg)
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
![Page 9: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/9.jpg)
Suppose we know how to find y
![Page 10: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/10.jpg)
Suppose we know how to find y
Time to find y: cn steps, for some constant c
![Page 11: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/11.jpg)
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
![Page 12: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/12.jpg)
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
T(n) 2T(n/2) + (c+1)n
The total number os steps in out sorting procedure would be given by the recurrence
![Page 13: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/13.jpg)
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
T(n) 2T(n/2) + (c+1)n
The total number os steps in out sorting procedure would be given by the recurrence
c’nlogn
![Page 14: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/14.jpg)
What´s the problem with the scheme above?
Quicksort
![Page 15: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/15.jpg)
What´s the problem with the scheme above?
Quicksort
How to find y?
![Page 16: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/16.jpg)
Deterministic Quicksort
Let y be the first element of S
![Page 17: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/17.jpg)
Deterministic Quicksort
Let y be the first element of S
Split S into two sets: S< and S>
![Page 18: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/18.jpg)
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
![Page 19: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/19.jpg)
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> )
![Page 20: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/20.jpg)
Performance
– Worst Case: O( n2 )
– Avarage Case: O( nlogn )
![Page 21: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/21.jpg)
Performance
– Worst Case: O( n2 ) (The set is already sorted)
– Avarage Case: O( nlogn )
![Page 22: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/22.jpg)
Performance
– Worst Case: O( n2 ) (The set is already sorted)
– Avarage Case: O( nlogn )
![Page 23: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/23.jpg)
An Randomzied Algorithm
![Page 24: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/24.jpg)
An Randomzied Algorithm
An algorithm that makes choice (random) during the algorithm execution
![Page 25: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/25.jpg)
Randomized Quicksort (RandQS)
![Page 26: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/26.jpg)
Randomized Quicksort (RandQS)
Choose an element y uniformly at random of S
![Page 27: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/27.jpg)
Randomized Quicksort (RandQS)
Choose an element y uniformly at random of S
– Every element of S has equal probability fo being
chosen
![Page 28: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/28.jpg)
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>
![Page 29: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/29.jpg)
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>
![Page 30: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/30.jpg)
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>
![Page 31: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/31.jpg)
Intuition
For some instance Quicksort works very bad
O( n2 )
![Page 32: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/32.jpg)
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
![Page 33: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/33.jpg)
Analysis
![Page 34: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/34.jpg)
Analysis
For sorting algorithms: we measure the running time of RandQS in terms of the number of comparisons it performs
![Page 35: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/35.jpg)
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
![Page 36: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/36.jpg)
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
![Page 37: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/37.jpg)
Analysis
Si: the ith smallest element of S
![Page 38: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/38.jpg)
Analysis
Si: the ith smallest element of S
S1 is the smallest element of S
![Page 39: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/39.jpg)
Analysis
Si: the ith smallest element of S
S1 is the smallest element of SSn is the largest element of S
![Page 40: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/40.jpg)
Analysis
Si: the ith smallest element of S
Define the random variable
S1 is the smallest element of SSn is the largest element of S
xik = 1, if Si and Sj are compared
0, otherwise
![Page 41: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/41.jpg)
Analysis
Si: the ith smallest element of S
Define the random variable
S1 is the smallest element of SSn is the largest element of S
xik = 1, if Si and Sj are compared
0, otherwise
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
![Page 42: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/42.jpg)
Analysis
Xij is a count of comparisons between Si and Sj:
The total numberof comparisons:
i = 1
n
j > iXij
![Page 43: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/43.jpg)
Analysis
Xij is a count of comparisons between Si and Sj:
The total numberof comparisons:
i = 1
n
j > iXij
We are interested in the expected number of comparisons
E[ ] i = 1
n
j > iXij
![Page 44: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/44.jpg)
Analysis
Xij is a count of comparisons between Si and Sj:
The total numberof comparisons:
i = 1
n
j > iXij
We are interested in the expected number of comparisons
E[ ] = i = 1
n
j > iXij
i = 1
n
j > iE[Xij]
By the linearity of E[]
![Page 45: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/45.jpg)
Analysis
Pij: the probability that Si and Sj are compared in an execution
![Page 46: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/46.jpg)
Analysis
Pij: the probability that Si and Sj are compared in an execution
Since Xij only assumes the values 0 and 1,
ikikikik PPPXE )1(01ij ij ij ij
![Page 47: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/47.jpg)
Analysis – Binary Tree T of RandQS
Each node is labeled with a distinct element of S
y
T
![Page 48: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/48.jpg)
Analysis – Binary Tree T of RandQS
Each node is labeled with a distinct element of S
y
T
S<
![Page 49: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/49.jpg)
Analysis – Binary Tree T of RandQS
Each node is labeled with a distinct element of S
y
T
S< S>
![Page 50: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/50.jpg)
Analysis – Binary Tree T of RandQS
Each node is labeled with a distinct element of S
y
T
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
![Page 51: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/51.jpg)
Analysis – Binary Tree T of RandQS
Each node is labeled with a distinct element of S
y
T
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
There is an comparison betweenSi and Sj if and only if one of these elements is an ancestor of the other
![Page 52: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/52.jpg)
Analysis – Binary Tree T of RandQS
Consider the permutation obtained by visiting the nodes of T in increasing order of the level numbers, and in a lef-to-rigth order within each level
![Page 53: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/53.jpg)
Example: S=(3, 6, 2, 5, 4,1)
![Page 54: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/54.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
![Page 55: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/55.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
1
{3, 6, 5, 4}{1}
![Page 56: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/56.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
1 5
{3, 6, 5, 4}
{6}{3, 4}
{1}
![Page 57: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/57.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
1 5
{3, 6, 5, 4}
4 6
3
{6}{3, 4}
{3}
{1}
![Page 58: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/58.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
1 5
{3, 6, 5, 4}
4 6
3
{6}{3, 4}
{3}
{1}
![Page 59: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/59.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
1 5
{3, 6, 5, 4}
4 6
3
{6}{3, 4}
{3}
{1}
![Page 60: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/60.jpg)
Example: S=(3, 6, 2, 5, 4,1)
2
1 5
{3, 6, 5, 4}
4 6
3
{6}{3, 4}
{3}
{1}
= (2, 1, 5, 4, 6, 3)
![Page 61: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/61.jpg)
Back to the Analysis
To compute pij we make two observations:– There is a comparison between Si and Sj if and only
if Si or Sj occurs earlier in the permutation than any element Sl such that i < l < j
Any of the elements Si, Si+1, ..., Sj is likely to be the first of theses elements to be chosen as a partitioning element, and hence to appear first in
The probability that this first element is either Si or Sj is exactly 2/(j-i+1)
![Page 62: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/62.jpg)
Back to the Analysis
To compute pij we make two observations:– There is a comparison between Si and Sj if and only
if Si or Sj occurs earlier in the permutation than any element Sl such that i < l < j
Any of the elements Si, Si+1, ..., Sj is likely to be the first of theses elements to be chosen as a partitioning element, and hence to appear first in
The probability that this first element is either Si or Sj is exactly 2/(j-i+1)
![Page 63: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/63.jpg)
Back to the Analysis
To compute pij we make two observations:– There is a comparison between Si and Sj if and only
if Si or Sj occurs earlier in the permutation than any element Sl such that i < l < j
Any of the elements Si, Si+1, ..., Sj is likely to be the first of theses elements to be chosen as a partitioning element, and hence to appear first in
The probability that this first element is either Si or Sj is exactly 2/(j-i+1)
![Page 64: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/64.jpg)
Back to the Analysis
To compute pij we make two observations:– There is a comparison between Si and Sj if and only
if Si or Sj occurs earlier in the permutation than any element Sl such that i < l < j
Any of the elements Si, Si+1, ..., Sj is likely to be the first of theses elements to be chosen as a partitioning element, and hence to appear first in
The probability that this first element is either Si or Sj is exactly 2/(j-i+1)
![Page 65: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/65.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
![Page 66: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/66.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
i = 1
n
j > iE[Xij] =
i = 1
n
j > iPij =
![Page 67: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/67.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
i = 1
n
j > iE[Xij] =
i = 1
n
j > iPij = 2/(j-i+1)
i = 1
n
j > i
![Page 68: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/68.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
i = 1
n
j > iE[Xij] =
i = 1
n
j > iPij = 2/(j-i+1)
i = 1
n
j > i
2/ki = 1 k=1
n n-1+1
![Page 69: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/69.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
i = 1
n
j > iE[Xij] =
i = 1
n
j > iPij = 2/(j-i+1)
i = 1
n
j > i
2/ki = 1 k=1
n n-1+1
2i = 1 k=1
n n
1/k
![Page 70: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/70.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
i = 1
n
j > iE[Xij] =
i = 1
n
j > iPij = 2/(j-i+1)
i = 1
n
j > i
2/ki = 1 k=1
n n-1+1
2i = 1 k=1
n n
1/kSérie Harmônica
![Page 71: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/71.jpg)
Análise
Therefore,
Pij = 2/(j-i+1)
i = 1
n
j > iE[Xij] =
i = 1
n
j > iPij = 2/(j-i+1)
i = 1
n
j > i
2/ki = 1 k=1
n n-1+1
2i = 1 k=1
n n
1/kSérie Harmônica 2 nln n
![Page 72: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/72.jpg)
RandQs x DetQs
Expected time of RandQs: O( nlogn ) A certain expected value may not garantee a reasonable
probability of success. We could have, for example, the following probabilities
n
nlog of executing O( n2 ) operations
of executing O( nlogn ) operations1- n
nlog
![Page 73: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/73.jpg)
RandQs x DetQs
For n=100 => in 7 % cases, the algorithm would execute in O( n2 ).
Some times we want to garantee that the algorithm performance will not be far from its avarage one
![Page 74: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/74.jpg)
RandQs x DetQs
For n=100 => in 7 % cases, the algorithm would execute in O( n2 ).
Some times we want to garantee that the algorithm performance will not be far from its avarage one
Objective: Prove that with high probability the RandQS algorithm works well
![Page 75: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/75.jpg)
High Probability Bound
![Page 76: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/76.jpg)
High Probability Bound
The previous analysis only says that the expected running time is O(nlog n)
![Page 77: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/77.jpg)
High Probability Bound
The previous analysis only says that the expected running time is O(nlog n)
This leaves the possibility of large “deviations” from this expected value
![Page 78: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/78.jpg)
High Probability Bound
RECALL:– Quicksort choses a pivot at random from the input
array
![Page 79: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/79.jpg)
High Probability Bound
RECALL:– Quicksort choses a pivot at random from the input
array– Splits it into smaller and larger elements
![Page 80: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/80.jpg)
High Probability Bound
RECALL:– Quicksort choses a pivot at random from the input
array– Splits it into smaller and larger elements– Recurses on both subarrays
![Page 81: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/81.jpg)
High Probability Bound
RECALL:– Quicksort choses a pivot at random from the input
array– Splits it into smaller and larger elements– Recurses on both subarrays
Fix an element x in the input
![Page 82: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/82.jpg)
High Probability Bound
RECALL:– Quicksort choses a pivot at random from the input
array– Splits it into smaller and larger elements– Recurses on both subarrays
Fix an element x in the input
x belogs to a sequence of subarrays
![Page 83: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/83.jpg)
High Probability Bound
RECALL:– Quicksort choses a pivot at random from the input
array– Splits it into smaller and larger elements– Recurses on both subarrays
Fix an element x in the input
x belogs to a sequence of subarraysx´s contribution to the running time is proportional to the number of different subarrays it belongs
![Page 84: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/84.jpg)
High Probability Bound
Every time x is compared to a pivot, its current subarray is split and x goes to one of the subarrays
![Page 85: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/85.jpg)
High Probability Bound
Every time x is compared to a pivot, its current subarray is split and x goes to one of the subarrays
With high probability, x is compared to O(log n) pivots
![Page 86: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/86.jpg)
High Probability Bound
Every time x is compared to a pivot, its current subarray is split and x goes to one of the subarrays
With high probability, x is compared to O(log n) pivots
With probability 1- n-c, for some pos. constant c
![Page 87: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/87.jpg)
GOOD and BAD Splits
![Page 88: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/88.jpg)
GOOD and BAD Splits
We say that a pivot is good if each of the subarrays has size at most ¾ (equivalently,at least ¼) of the size of the split subarray
![Page 89: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/89.jpg)
GOOD and BAD Splits
We say that a pivot is good if each of the subarrays has size at most ¾ (equivalently,at least ¼) of the size of the split subarray
Otherwise, it is bad
![Page 90: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/90.jpg)
GOOD and BAD Splits
We say that a pivot is good if each of the subarrays has size at most ¾ (equivalently,at least ¼) of the size of the split subarray
Otherwise, it is bad The probability of a good and of a bad split is ½
![Page 91: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/91.jpg)
GOOD and BAD Splits
We say that a pivot is good if each of the subarrays has size at most ¾ (equivalently,at least ¼) of the size of the split subarray
Otherwise, it is bad The probability of a good and of a bad split is ½
X can participate in at most log4/3 n good splits
![Page 92: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/92.jpg)
High Probability Bound
Upper bound the probability of less than M/4 good splits in M splits
![Page 93: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/93.jpg)
High Probability Bound
Upper bound the probability of less than M/4 good splits in M splits
Set M so that log4/3 n M/4
![Page 94: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/94.jpg)
High Probability Bound
Upper bound the probability of less than M/4 good splits in M splits
Set M so that log4/3 n M/4 Let M = 32 ln n:
![Page 95: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/95.jpg)
High Probability Bound
Upper bound the probability of less than M/4 good splits in M splits
Set M so that log4/3 n M/4
Let M = 32 ln n log4/3 n 8 ln nGood choice
![Page 96: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/96.jpg)
High Probability Bound
Upper bound the probability of less than M/4 good splits in M splits
Set M so that log4/3 n M/4
Let M = 32 ln n log4/3 n 8 ln n
exp(-M/8) 1/n4
Good choice
![Page 97: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/97.jpg)
High Probability Bound
Upper bound the probability of less than M/4 good splits in M splits
Set M so that log4/3 n M/4
Let M = 32 ln n log4/3 n 8 ln n
exp(-M/8) 1/n4
The probablility of participating in more than
M = 32 ln n splits is less than 1/n4
Good choice
![Page 98: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/98.jpg)
High Probability Bound
The probability that any element participates in more than M=32 ln n splits is less than 1/n3
![Page 99: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/99.jpg)
High Probability Bound
The probability that any element participates in more than M=32 ln n splits is less than 1/n3
With probability at least 1-1/n3, the running time of quicksort is O(n log n)
![Page 100: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/100.jpg)
Advantages of Randomized Algorithms
For many problems, randomized algorithms run faster than the best know deterministic algorithms
Many randomized algorithms are simpler to describe and implement than deterministic algorithms for comparable performance
![Page 101: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/101.jpg)
Advantages of Randomized Algorithms
For many problems, randomized algorithms run faster than the best know deterministic algorithms
Many randomized algorithms are simpler to describe and implement than deterministic algorithms for comparable performance
![Page 102: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/102.jpg)
Advantages of Randomized Algorithms
For many problems, randomized algorithms run faster than the best know deterministic algorithms
Many randomized algorithms are simpler to describe and implement than deterministic algorithms for comparable performance
![Page 103: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/103.jpg)
Minimum Cut Problem
Entrada:
– Grafo G=(V,E)
Saída:
– Conjunto S V que minimiza , ou seja, o
número de arestas que tem uma extremidade em S
e outra em
SSe ,
S
![Page 104: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/104.jpg)
Minimum Cut Problem
S S 3, SSe
Notação– d(v) : grau do vértice v no grafo– N(v) : vizinhança de v
![Page 105: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/105.jpg)
Minimum Cut Problem - Applications
Network Reliability: If a graph has a small min-cut, then it is poorly connected
Clustering– Web pages = nodes– Hyperlinks = edges
Divide the graph into cluster with little connection between different clusters.
![Page 106: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/106.jpg)
Contração de arestas
Dado um grafo G=(V,E) e uma aresta e=(u,v), a contração da aresta e produz o grafo G/e=(V’, E’), onde
,,' uvvuVV
evuvuEE de esextremidad sãoe|,'
EvxEuxvuxuvx ,ou,e,|,
![Page 107: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/107.jpg)
Contração de arestas - Exemplo
G G/ea
b
c
d
uv
f
g
a
b
c
d
u v
f
g
e
![Page 108: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/108.jpg)
Lema 1
Lema 1: o tamanho do corte mínimo em G/e é maior ou igual ao tamanho do corte mínimo em G
![Page 109: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/109.jpg)
Lema 1
Lema 1: o tamanho do corte mínimo em G/e é maior ou igual ao tamanho do corte mínimo em G
Prova: podemos associar cada corte em G/e a um corte em G com mesmo tamanho. Basta substituir em S os nós obtidos por contração pelos nós originais.
![Page 110: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/110.jpg)
Lema 2
Lema 2: Se o corte mínimo em um grafo tem
tamanho k, então d(v) k, para todo v V.
![Page 111: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/111.jpg)
Lema 2
Lema 2: Se o corte mínimo em um grafo tem
tamanho k, então d(v) k, para todo v V.
Prova: Caso contrário S={ v } seria um corte de
tamanho menor que k.
![Page 112: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/112.jpg)
Corolário
Corolário 1: Se o corte mínimo em um grafo
tem tamanho k, então |E| k.n/2
![Page 113: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/113.jpg)
Corolário
Corolário 1: Se o corte mínimo em um grafo tem tamanho k, então |E| k.n/2
Prova: Segue do lema 2 e de que
2
Vv
vd
E
![Page 114: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/114.jpg)
Randomized MinCut
G0 G
Para i=1 até |V|-2
– selecione aleatoriamente ei em Gi-1
– faça Gi = Gi-1 / ei
Retorne os vértices de um dos supervértices obtidos
![Page 115: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/115.jpg)
Probabilidade
Lema 3: Seja t1, t2, ..., tk uma coleção de eventos. Temos que
k
i
i
jji
k
ii ttt
1
1
11
PrPr Prova:
Base:
1
122 Pr
PrPr
t
tttt i okPrPrPr 12121 ttttt
Assuma que vale para k, provar para k+1.
![Page 116: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/116.jpg)
Teorema
Seja C = { e1, e2, ..., ek } um corte mínimo no
grafo G=(V,E). Se nenhuma aresta de C é
escolhida pelo RndMinCut, então as arestas
que sobram no grafo final são as arestas de C.
![Page 117: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/117.jpg)
Teorema - prova
Sejam A e B os dois supervértices obtidos e seja AC e BC as componentes conexas obtidas retirando C.
Como nenhuma aresta de C foi escolhida:
CC
CC
BBAB
BAAA
ou
e,ou
![Page 118: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/118.jpg)
Teorema - prova
Logo, A=AC e B=BC
De fato, assuma que a AC e b BC tal que a,b A . Neste caso, existe um caminho em A entre a e b que utiliza somente arestas escolhidas pelo algoritmo. Como qualquer caminho entre a e b tem que utilizar arestas de C, logo uma aresta de C é escolhida. Contradição!
![Page 119: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/119.jpg)
Análise
Seja C o conjunto de arestas de um corte
mínimo em G.
Calculamos a probabilidade do algoritmo não
escolher nenhuma aresta de C para contração.
Se isto acontece, o algoritmo retorna um corte
mínimo.
![Page 120: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/120.jpg)
Análise
Sortei : evento indicando que o algoritmo não sorteou uma aresta de C na i-ésima iteração.
2
1
PrCencontrar Pr
mínimo corteencontrar Prn
iisorte
![Page 121: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/121.jpg)
Análise
Temos:
2
1
1
1
2
1
PrPrn
i
i
jji
n
ii sortesortesorte
E
Csorte 1Pr 1
Segue da relação entre o tamanho do corte e o número de arestas (corolário 1) que:
n
nsorte
2Pr 1
![Page 122: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/122.jpg)
Análise
Na segunda iteração o grafo G1 tem n-1 vértices e seu corte mínimo tem tamanho |C|.
Logo,
1
3
1
21Pr 12
n
n
Cn
Csortesorte
![Page 123: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/123.jpg)
Análise
Em geral,
Segue que,
1
21Pr
1
1
insortesorte
i
jji
1
2
1
21Pr
2
1
2
1
nninsorte
n
i
n
ii
![Page 124: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/124.jpg)
Análise
Logo, conseguimos um min-cut com probabilidade maior ou igual a
1
2
nn
n=100 => 0,02 % (RUIM)
O que fazer ?
![Page 125: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/125.jpg)
Randomized MinCut 2
Repetir o processo RndMinCut várias vezes e devolver o melhor corte encontrado
Repetindo K vezes a probabilidade de encontrar o corte mínimo é
K
nn
1
211
![Page 126: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/126.jpg)
Análise
Repetindo n2/2 vezes a probabilidade de sucesso é (e-1)/e 64 %
K repetições => O (K.m) tempo
![Page 127: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/127.jpg)
Complexidade do Algoritmo mais rápido de Karger
Running time: O(n2 log n)
Space: O(n2)
Este algoritmo encontra um min-cut com probabilidade
(1/log n) [D. Karger e C. Stein, Stoc 1993]
Quem se habilita ?
![Page 128: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/128.jpg)
Minimum Cut Problem – Deterministic Algorithm Complexity
O(nm log n2/m)
J. Hao and J. B. Orlin[1994]
(baseado em fluxo em redes)
![Page 129: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/129.jpg)
Dois tipos de algoritmos randomizados
Algoritmos Las Vegas– Sempre produzem a resposta correta
– Tempo de execução é uma variável aleatória
Exemplo: RandQs– Sempre produz seqüência ordenada
– Tempo de término varia de execução para
execução em uma dada instância
![Page 130: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/130.jpg)
Dois tipos de algoritmos randomizados
Algoritmos Monte-Carlo– Podem produzir respostas incorretas
– A probabilidade de erro pode ser cotada
– Executando o algoritmo diversas vezes podemos
tornar a probabilidade de erro tão pequena quanto
se queira
Exemplo: Min-Cut
![Page 131: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/131.jpg)
MAX SAT
Entrada
n variáveis booleanas : x1,... Xn
m cláusulas : C1,... Cm
Pesos wi >= 0 para cada clausula Ci
Objetivo : Encontrar uma atribuição de verdadeiro/falso
para xi que maximize a soma dos pesos das clausulas
satisfeitas
![Page 132: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/132.jpg)
MAX SAT
Algoritmo Aleatorizado
Para i=1,...,n
Se random(1/2) = 1
xi true
Senão
xi false
Com probabilidade ½ dizemos que uma variável é verdadeira ou falsa
![Page 133: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/133.jpg)
MAX SAT
Teorema : O algoritmo tem aproximação ½
Prova: Considere a variável aleatória xj
Logo,
)()()( jj
jjj
j xEwxwEwE
jj
j xww
contrário caso 0,
satisfeita é j clausula a se ,1jx
![Page 134: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/134.jpg)
MAX SAT
E(xj) = Pr(clausula j ser satisfeita)
Lj : número de literais na clausula j
Obs: A clausula j não é satisfeita somente se todos literais forem 0
Cj = (x1 v x3 v x5 v x6)
Devemos ter x1=0, x3 = 1, x5 =0 e x6 =0
Probabilidade = (1/2)4
Caso Geral=(1/2)Lj
![Page 135: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/135.jpg)
MAX SAT
Probabilidade da clausula j ser satisfeita é 1-(1/2)Lj
Logo,
0,5-aproximação
Obs: é um limite superior
222
11)(
OPTw
wwEj
j
j
L
j
j
j
jw
![Page 136: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/136.jpg)
MAX SAT
O que aconteceria se toda clausula tivesse exatamente 3 literais?
7/8-aproximação
Hastad 97) Se MAXE3SAT tem aproximação (7/8 + para algum > 0, P = NP
jj
jj wwwE
8
7
2
11)(
3
![Page 137: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/137.jpg)
MAX SAT: Desaleatorização
C1 = (x1 v x2 v x3) w1
C2 = ( x2 v x3 ) w2
Cada folha da árvore corresponde a um atribuição:Cada folha esta associada a um peso (soma dos pesos
das clausulas satisfeitas pela atribuição correspondente a folha)
![Page 138: Randomized Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062422/568137c3550346895d9f61a6/html5/thumbnails/138.jpg)
MAX SAT: Desaleatorização
E(c(I)) = 7/8 w1 + w2 /2