• 沒有找到結果。

Probabilistic Analysis and Randomized Al gorithms

N/A
N/A
Protected

Academic year: 2022

Share "Probabilistic Analysis and Randomized Al gorithms"

Copied!
32
0
0

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

全文

(1)

Analysis and Randomized Al gorithms

Michael Tsai 2011/3/18

(2)

僞機率課…

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

 可能高中有教過…

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

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

http://www.csie.ntu.edu.tw/~hil/head20060 519a.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 can didate best

best=i

hire candidate i

(5)

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

 假設錄取了 m 次 , 則為

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

 問題是 , m=?

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

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

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

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

 

(6)

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

?

 最糟的狀況 ?

 每天都看到一個更好的

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

 則錄取之花費為

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

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

 

(7)

Probabilistic Analysis

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

要使用此法 , 必須知道或假設 inputs 的分配 (distr ibution).

則當我們計算 ( 平均 ) 執行時間時 , 我們使用所有可 能的 inputs 發生的機率 , 將它們的執行時間做平均

Q: 何時無法使用 ?

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

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

(8)

僞機率課 :

Indicator Random Variable

 We define the indicator random variable associat ed with event as

 

, 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:

 

栗子助教

舉個栗子

(10)

僞機率課 :

Indicator Random Variable

Lemma:

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

Proof:

 

(11)

僞機率課 :

Indicator Random Variable

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

 Expected number of heads in coin flips?

 , : the i-th flip comes up head.

 number of heads in coin flips?

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

 

(12)

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

?

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

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

 詳細版 :

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

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

則為的一個 permutation

各種 permutation 發生的機率各為

 

(13)

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

?

 使用 Indicator Random Variable

 Number of total hires:

 又

 

, if candidate i is hired

, if candidate i is not hired

(14)

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

?

 =?

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

 此事件發生的機率為

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

 

(15)

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

?

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

 需要花的錄取費用

 比 worst case 的少了不少 .

 

(16)

誠徵助理 -12345678 人力銀行

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

那我豈不是一定要花大錢 ? 醬子不行 !

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

(17)

誠徵助理 -12345678 人力銀行

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

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

則不管原本的 distribution 是怎樣

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

Randomized Algorithm!

permutation 

permutation某種 permutation某種 permutation某種

  1

  2

  3

3

� ! 

3

� ! 

3

� ! 

3

� ! 

2

� ! 

2

� ! 

2

� ! 

2

� ! 

1

� ! 

1

� ! 

1

� ! 

1

� ! 

(18)

Probabilistic Analysis 和 Random ized Algorithm 的差別

Probabilistic Analysis:

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

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

Randomized Algorithm:

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

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

好處 : 沒有一種特別的 input 可以使 algorithm 產生 worst-c ase behavior

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

壞處 : 要多花時間打亂

(19)

誠徵助理 -12345678 人力銀行之 pse udo 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 bes t

best=i

hire candidate i

(20)

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

 Hire-Assistant(n)

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

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

 Randomized-Hire-Assistant(n)

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

因此平均花費固定都是

就算 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 r andom permutation

 Permute_By_Sorting 可以產生 uniform random permutation 嗎 ?

(23)

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

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

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

 設 , 為 A[i] 拿到第 i 小的 priority 事件 .

 則我們是要算

 …

 

(24)

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

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

 應為

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

 

(25)

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

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

 應為

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

 

(26)

打亂的方法 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 次執行迴圈前

 這個排列出現的機率為

 

(29)

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

 用歸納法證明 .

 起始條件 :

 i=1 的時候 ,  也就是空集合出現的機率應為 . 這是正 確的 . 因為沒有任何元素的時候 , 排列出來的一定是 空集合 .

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

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

 

(30)

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

假設有兩個事件

事件 : 裡面是目前的排列

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

則在放入了目前這樣的排列的機率應為

根據前面的假設

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

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

所以 =

成立 !

 

(31)

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

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

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

 表示任何一種排列出現之機率均為 , 因此 Ran domize_In_Place 可以產生 uniform random pe rmutation.

 喔耶 .

 

(32)

Question to you

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

參考文獻

相關文件

Search the portion of List preceding TestEntry for TargetValue, and report the result of that search if (TargetValue > TestEntry):. Search the portion of List following

Now given the volume fraction for the interface cell C i , we seek a reconstruction that mimics the sub-grid structure of the jump between 0 and 1 in the volume fraction

I would like to thank the Education Bureau and the Academy for Gifted Education for their professional support and for commissioning the Department of English Language and

(b) reviewing the positioning of VPET in the higher education system in Hong Kong, exploring the merits of developing professional vocational qualifications at the degree

220V 50 Hz single phase A.C., variable stroke control, electrical components and cabling conformed to the latest B.S.S., earthing through 3 core supply cable.. and 2,300 r.p.m.,

Two examples of the randomly generated EoSs (dashed lines) and the machine learning outputs (solid lines) reconstructed from 15 data points.. size 100) and 1 (with the batch size 10)

* 1. List any 5 types of market segmentation. Briefly describe the characteristics and contents of a good research report.. Resources for the TEKLA curriculum at

Two cross pieces at bottom of the stand to make a firm base with stays fixed diagonally to posts. Sliding metal buckles for adjustment of height. Measures accumulated split times.