Chapter 5: Probabilistic Analysis
Chapter 5: Probabilistic Analysis
Chapter 5: Probabilistic Analysis
In practice, many algorithms perform better than their worst-case. The average case is analyzed by: 1. construct a probabilistic model of the input 2. determine the probabilities and running times (or costs) of alternate executions 3. calculate expected running time (or cost) Through randomization, one can often ensure that the probabilisitic model is true.
CS 5633 Analysis of Algorithms Chapter 5: Slide 2
Example 1: Analysis
On iteration i, max is assigned a value i A[i] is the maximum of the rst i numbers. Probability that A[i] is the maximum of the rst i numbers = 1/i Probability of assignment = 1/i, cost = 1 Prob. of no assignment = (i 1)/i, cost = 0 On iteration i, the expected cost is: (1/i)(1) + ((i 1)/i)(0) = 1/i Over the initial assignment and n 1 iterations, the expected cost is: n 1 which is between ln n and 1 + ln n i=1 i
CS 5633 Analysis of Algorithms Chapter 5: Slide 5
Example 2: Analysis
Loop invariant: Before iteration i, A[1] through A[i 1] is a random permutation of the rst i 1 values. For any permutation of the rst i values, there is exactly one way to permute the rst i 1 values, and then swap A[i] into the correct position. So after iteration i, the rst i values have been randomly permuted, making the loop invariant true the next iteration.
CS 5633 Analysis of Algorithms Chapter 5: Slide 7