算法导论第五章学习笔记

来源:互联网 发布:sql count 求和 编辑:程序博客网 时间:2024/06/13 03:39

Probabilistic analysis

We must use knowledge of, or make assumptionsabout, the distribution of inputs.

  • The expectation is over this distribution.
  • This technique requires that we can make a reasonable characterization of the input distribution.
  • The expectation is taken over the distribution of the possible inputs. Thus we are, in effect, averaging the running time over all possible inputs.

Randomized algorithms

What makes an algorithm randomized: An algorithm is randomized if its behavior is determined in part by values produced by a random-number generator.

Indicator random variables

-------A simple yet powerful technique for computing the expected value of a random

variable.

Definition

Suppose we are given a sample space S and an event A. Then the indicator random variableI{A} associated with event A is defined as

Lemma

For an event A, let XA = I {A}. Then E [XA] = Pr {A}.

Proof Letting be the complement of A, we have

E [XA] = E [I {A}]

= 1 · Pr {A} + 0 · Pr {} (definition of expected value)

= Pr {A} .

Example:Determine the expected number of heads in n coin ßips.

Let X be a random variable for the number of heads in n ßips.Could compute .

  

For i = 1, 2, . . . , n, define Xi = I {the i th flip results in event H}.

Then .

Lemma says that E[Xi ] = Pr {H} = 1/2 for i = 1, 2, . . . , n.

Expected number of heads is .

Problem: We want .We have only the individual expectations E [X1] , E [X2] , . . . , E [Xn].

Solution: Linearity of expectation says that the expectation of the sum equals

the sum of the expectations. Thus,

Note: Indicator random variables greatly simplify the calculation. So it is used throughout Introduction to Algorithms .

Analysis of the hiring problem

Assume that the candidates arrive in a random order.Let X be a random variable that equals the number of times we hire a new office assistant.

Define indicator random variables X1, X2, . . . , Xn, where Xi = I {candidate i is hired} .

Useful properties:

X = X1 + X2 +···+ Xn.

LemmaE [Xi ] = Pr {candidate i is hired}.

We need to compute Pr {candidate i is hired}.Candidate i is hired if and only if candidate i is better than each of candidates1, 2, . . . , i − 1.

  

Assumption that the candidates arrive in random order

candidates 1, 2, . . . , iarrive in random order

any one of these first i candidates is equally likely tobe the best one so far.

Thus, Pr {candidate i is the best so far} = 1/i .Which implies E[Xi ] = 1/i .

Thus, the expected hiring cost is , which is much better than the worst case cost of .

原创粉丝点击