• 沒有找到結果。

僞機率課…

N/A
N/A
Protected

Academic year: 2022

Share "僞機率課…"

Copied!
32
0
0

加載中.... (立即查看全文)

全文

(1)

Analysis and  Randomized 

Algorithms

Michael Tsai 2011/3/18

(2)

僞機率課…

 本部分會用到機率的一些概念…

 可能高中有教過…

 可能本學期的機率課 會教你們

 中間如果有聽不太懂的部分, 歡迎隨時發問

http://www.csie.ntu.edu.tw/~hil/head20060519a.jpg

隨機客

(3)

誠徵助理一名…

 使用31415926人力銀行

 每天和一個人面試

 每次面試必須繳交小筆費用 給31415926人力 銀行

 每次決定錄取一個人必須花費大筆費用 (炒前 一個助理魷魚, 繳交錄取費給人力銀行)

 隨時都要有最好的助理. 因此只要面試到比目前 更好的, 就炒現在助理魷魚並錄取面試者

(4)

誠徵助理‐我要最好的之pseudo code

Hire-Assistant(n) best=0

for i=1 to n

interview candidate i

if candidate i is better than candidate best

best=i

hire candidate i

(5)

誠徵助理‐請問要花多少大洋?

 假設錄取了m次, 則為

 是固定的, 因此我們下面只看 的部分

 問題是, m=?

 跟來應徵的人的好壞程度有關…

 本問題和很多計算問題的型態很相像.

 把每一個可能的選項都檢視一遍

 必須一直記得目前為止最好的

(6)

誠徵助理‐請問要花多少大洋?

 最糟的狀況?

 每天都看到一個更好的

 每天都炒前一個助理魷魚, 錄取來應徵的

 則錄取之花費為

 那如果是”一般”情形呢?

 “一般來講”要花多少錢?

(7)

Probabilistic Analysis

Probabilistic Analysis: 使用機率來分析問題; 在此通 常是拿來分析演算法執行所需時間

要使用此法, 必須知道或假設inputs的分配

(distribution).

則當我們計算(平均)執行時間時, 我們使用所有可能

的inputs發生的機率, 將它們的執行時間做平均

Q: 何時無法使用?

A: 當不知道input的分配時 (不知道各種input各自有 多可能發生)

預告: 不知道的時候, 可以用某些方法變成知道

(8)

僞機率課: 

Indicator Random Variable

We define the indicator random variable associated  with event  as

 1

0

, if  occurs.

, if  does not occur.

(9)

僞機率課: 

Indicator Random Variable

 When flipping a fair coin,

what is the Expected number of heads?

 Sample space={H,T}

 ,

 Expected number of heads of one flip:

 1 ∙ 0 ∙

栗子助教

舉個栗子

(10)

僞機率課: 

Indicator Random Variable

Lemma:

Given a sample space  and an event  in the  sample space  , let  . Then 

Pr . Proof:

1 ∙ Pr 0 ∙ Pr ̅ Pr

(11)

僞機率課: 

Indicator Random Variable

 Indicator Random Variable讓我們在分析重複的動作的時 候很方便.

 Expected number of heads in  coin flips?

 I H ,  : the i‐th flip comes up head.

 number of heads in  coin flips?

 ∑

 ∑ ∑

1

2 2

 上面第二個等號使用linearity of expectation性質

(12)

誠徵助理‐請問要花多少大洋?

 必須假設面試的人的好壞情形

 假設: 是很隨機(random)的

 詳細版:

我們把所有來面試的n個人做排名(1至n)

rank(i)為第i個人的排名

1 , 2 , … , 為 1,2, … , 的 一個permutation

各種permutation發生的機率各為 !

(13)

誠徵助理‐請問要花多少大洋?

 使用Indicator Random Variable

1 0

 Number of total hires:

 ∑

 又 Pr

, if candidate i is hired , if candidate i is not hired

(14)

誠徵助理‐請問要花多少大洋?

 Pr =?

 當candidate i被錄取時: 它比第1至i‐1個 candidate都好.

 此事件發生的機率為

 因為是random, 所以前i個candidate每個人為最 好之機率各為

(15)

誠徵助理‐請問要花多少大洋?

 ∑ ∑ ∑

ln 1

 所以平均 (或者”一般”)來說,

 需要花 ln 的錄取費用

 比worst case的 少了不少.

(16)

誠徵助理‐12345678人力銀行

 12345678人力銀行: 每次安排面試都從最差的人 排到最好的人. 固定這樣.

那我豈不是一定要花大錢?

醬子不行!

我自己來把他們的順序打亂!

(17)

誠徵助理‐12345678人力銀行

假設我們可以找出一種”打亂的方 法”

使得不管input是什麼, 打亂過後每 種permutation都是一樣機率

則不管原本的distribution是怎樣

最後都可以變成每種permutation 都有相同發生機率

Randomized Algorithm!

…… !permutation

某種permutation 某種permutation 某種permutation

!

!

!

!

!

!

!

!

!

!

!

!

(18)

Probabilistic Analysis和 Randomized Algorithm的差別

Probabilistic Analysis:

假設input之distribution(各種input發生的機率)

Algorithm是deterministic的(一種input只產生一種結果,  12345678人力銀行會造成每次都花最多錢)

Randomized Algorithm:

不管input之distribution, 但透過randomize來使其變成某一 distribution

Algorithm不是deterministic(一種input可能會產生多種結果)

好處: 沒有一種特別的input可以使algorithm產生worst‐

case behavior

“來搗亂的12345678人力銀行將無法使我每次都花最多錢”

壞處: 要多花時間打亂

(19)

誠徵助理‐12345678人力銀行之 pseudo code

Randomized_Hire_Assistant(n)

Randomly permute the list of candidates best=0

for i=1 to n

interview candidate i

if candidate i is better than candidate best

best=i

hire candidate i

(20)

誠徵助理‐比較兩個演算法

 Hire-Assistant(n)

假設input是random的話, 平均花費為

ln

但如果不是random的話, 就不一定了…

 Randomized-Hire-Assistant(n)

不管input是怎麼樣, 我都把它變成random

因此平均花費固定都是 ln

就算input不是random也一樣

(21)

打亂的方法1‐排序打亂法

 方法一: 排序打亂法

Permute_By_Sorting(A) n=A.length

let P[1..n] be a new array for i=1 to n

P[i]=Random(1,

sort A, using P as sort keys

“希望”所有的P[i]都是unique Θ log

(22)

Uniform Random Permutation

 某方法可以使得產出每一種排列的可能性(機率) 都一樣, 則稱此方法可以產生uniform random  permutation

 Permute_By_Sorting可以產生uniform random  permutation嗎?

(23)

打亂的方法1‐排序打亂法

Lemma: 假設拿到的priority都是unique的(沒有一樣的), 則 Permute_By_Sorting可以產生uniform random permutation.

證明: 首先我們看A[i]正好拿到第i大的priority的case的機率. (也 就是完全沒有改變原本的順序的case)

1,2, … , ,  為A[i]拿到第i小的priority事件.

則我們是要算

Pr ∩ ⋯ ∩

Pr Pr Pr Pr … Pr |

∩… ∩

(24)

打亂的方法1‐排序打亂法

 Pr ?

 A[1]拿到最小的priority的機率

 應為

 因為任一A[i]拿到最小priority的機率應該都一 樣

(25)

打亂的方法1‐排序打亂法

 Pr | ?

 A[1]拿到最小的priority後, A[2]拿到第二小的 priority的機率

 應為

 因為不考慮A[1]之後(A[1]已經拿了最小的了), 任 一A[i]拿到第二小priority的機率應該都一樣

(26)

打亂的方法1‐排序打亂法

Pr ∩ ⋯ ∩

Pr Pr Pr Pr … Pr |

∩… ∩

∙ ⋯ ∙ ∙ 1

!

(似乎有點端倪, 因為總共有 !種排列)

事實上剛剛的這個case可以推廣到任何一種排列的case

每一個case的機率都是 !

因此Permute_By_Sorting可以產生uniform random permutation

(27)

打亂的方法2‐交換打亂法

Randomize_In_Place(A) n=A.length

for i=1 to n

swap A[i] with A[Random(i,n)]

0 1 2 3 n 中間亂數選一個

和箭頭指的的 交換

(28)

打亂的方法2‐交換打亂法

 Lemma: Randomize_In_Place可以產生uniform  random permutation

 證明: 我們想要證明一個假設:

 在第i次執行迴圈前

 1 … 1 這個排列出現的機率為 ! !

(29)

打亂的方法2‐交換打亂法

 用歸納法證明.

 起始條件: 

 i=1的時候,  1 … 0 也就是空集合出現的機率應為

!

! 1. 這是正確的. 因為沒有任何元素的時候, 排列 出來的一定是空集合.

 假設當迴圈執行第i次時前,  1 … 1 這個排列出現的 機率為 ! !

 則我們需要證明當迴圈執行第i+1次前,  1 … 這個排列 出現的機率為 ! !

(30)

打亂的方法2‐交換打亂法

假設有兩個事件

事件 : A 1 … i 1 裡面是目前的排列

事件 : A i 放入了目前的這個元素

則在A 1 … i 放入了目前這樣的排列的機率應為 Pr Pr Pr |

根據前面的假設: Pr ! !

又Pr | 表示, 在A 1 … i 1 裡面是目前的排列的條件下,  是目前放這個元素的機率. 

在迴圈裏面,  可能和自己開始一直到最後一個元素其中一 個交換(n‐i+1個元素其中一個), 因此機率應為 .

所以Pr = ! ! !

!

成立!

(31)

打亂的方法2‐交換打亂法

 最後當最後一個迴圈執行完畢以後, i=n+1

 此時 1 … 排列出現之機率應為 ! ! !

 表示任何一種排列出現之機率均為 !, 因此

Randomize_In_Place可以產生uniform random  permutation.

 喔耶.

(32)

Question to you

 過去我們學過的演算法/資料結構中, 有哪些適 合使用”混亂大法”?

參考文獻

相關文件

In addition, three seminars were held and in-depth interviews with 20 public-sector organizations and 20 individuals in the target sample population were

Thus, for example, the sample mean may be regarded as the mean of the order statistics, and the sample pth quantile may be expressed as.. ξ ˆ

A Complete Example with equal sample size The analysis of variance indicates whether pop- ulation means are different by comparing the variability among sample means with

Population: the form of the distribution is assumed known, but the parameter(s) which determines the distribution is unknown.. Sample: Draw a set of random sample from the

Students in this Learning Unit should recognise the concepts of sample statistics and population parameters and their relationships:. Population Parameter

Spacelike distributions assumed identical in Euclidean and Minkowski space First calculation to work strictly in Euclidean space found no IR divergence.

The entire moduli space M can exist in the perturbative regime and its dimension (∼ M 4 ) can be very large if the flavor number M is large, in contrast with the moduli space found

The point should then be made that such a survey is inadequate to make general statements about the school (or even young people in Hong Kong) as the sample is not large enough