啟發(fā)式算法筆記01_隨機算法(Randomised Algorithms)

本篇為在University of Birmingham 學習Advanced Nature-Inspired Search and Optimisation課程中的筆記之一
This is one of the notes from the Advanced Nature-Inspired Search and Optimisation course at the University of Birmingham

[toc]

1 問題引出——螺栓螺母的匹配問題

1.1 一個螺栓與n個螺母(Matching one Bolt to n Nuts)

  • The everyday problem: given one bolt and a collection of n nuts of different sizes, find a nut match the bolt
  • In mathematical form: given an array of n elements, find the first element of which the value equals to x
  • Q: How to solve it using an algorithm? How to solve it efficiently?

1.2 n個螺栓與n個螺母(the real Nuts and Bolts Problem)

  • A disorganized carpenter has a collection of n nuts of distinct sizes and n bolts and would like to find the corresponding pairs of bolts and nuts. Each nut matches exactly one bolt (and vice versa), and he can only compare nuts to bolts, i.e., he can neither compare nuts to nuts nor bolts to bolts.
  • Can you help the carpenter match the nuts and bolts quickly?
  • This is called Nuts and Bolts Problem or Lock and Key problem.
  • Q: How to formulate the problem? How to solve it using an algorithm? How to solve it efficiently?

2 方案介紹——隨機算法 (Randomised Algorithms)

“For many problems, a randomised algorithm is the simplest, the fastest or both.” —— Prabhakar Raghavan, Vice President of Engineering at Google.

2.1 算法一覽_Categories of algorithms by design paradigm

  1. 分治算法(Divide and conquer algorithms, e.g., quicksort algorithm, Merge-Sort)
  2. 數學規(guī)劃算法(Mathematical programming algorithms, e.g., linear programming, Multi-objective programming, Dynamic programming algorithms)
  3. 搜索和枚舉算法(Search and enumeration algorithms)
    1. 蠻力算法_Brute force algorithms, enumerating all possible candidate solutions and check
    2. 改進的蠻力算法_Improved brute force algorithms, e.g., branch and bound algorithms
    3. 啟發(fā)式算法_Heuristic algorithms
      1. 局部搜索_Local search, e.g., greedy search
      2. 隨機算法_Randomised algorithms, which include Evolutionary Computation, etc.

2.2 啟發(fā)式算法與隨機算法_Heuristic Algorithms & Randomised Algorithms

1). 啟發(fā)式在計算機中的解釋:

一種(通常是簡單的)算法油狂,可以在合理的時間內為問題提供足夠好的解決方案 (A (usually simple) algorithm that produces a good enough solution for a problem in a reasonable time frame)

  1. 解決方案(通常)不是最佳方案肉迫,但令人滿意:
    1. 更快:替代蠻力(窮舉)搜索
    2. 權衡最優(yōu)性纺蛆,完整性,準確性或精度以提高速度抬旺。
  2. 通常用于解決其他算法難以解決的問題,例如蠻力算法
  3. 包括確定性(例如本地搜索_local search algorithm)和隨機算法(Randomised Algorithm)
2). 隨機算法
  1. Randomised algorithm: An heuristic algorithm that makes random choices during execution to produce a result_隨機選擇產生結果
    1. Takes a source of random numbers to make random choices
    2. Behaviour, e.g., output or running time can vary even on a fixed input
  2. The goal: design an algorithm and analyse it to show that its behaviour is likely to be good, on every input
3). 隨機算法_Randomised Algorithms & 確定性算法_Deterministic Algorithms
graph LR
input => Algorithm
Algorithm=>output

<center>圖1. Deterministic Algorithms</center>

graph LR
input --> Algorithm
Random_number --> Algorithm
Algorithm-->output

<center>圖2. Randomised Algorithms</center>


3 解決方案——拉斯維加斯算法與蒙特卡洛算法

3.1 一個螺栓與n個螺母(Matching one Bolt to n Nuts)

  • 對于第一個螺栓與n個螺母(Matching one Bolt to n Nuts)的問題腕侄,解決思想為使用隨機數找到問題的解決方案(Using random numbers to find a solution to a problem)

  • 將其抽象為數學問題即為: 給定n個元素的數組殊校,找到其值等于x的第一個元素(The problem: given an array of n elements, find the first element of which the value equals to x)

  • 具有代表性的兩個解決算法分別是拉斯維加斯算法和蒙特卡洛算法

1). 介紹 拉斯維加斯算法_Las Vegas algorithm 與蒙特卡洛算法_Monte Carlo algorithm
# 拉斯維加斯算法_Las Vegas algorithm
begin
    repeat
        Randomly select one element a out of n elements. 
    until a == x
end

如上所述,拉斯維加斯算法始終返回正確的結果晶默。上面的代碼說明了此屬性谨娜。變量a是隨機生成的;生成a后磺陡,使用a索引n個元素(數組)中比較x趴梢。如果該索引包含值x,則返回a币他;該算法將重復此過程坞靶,直到找到x。盡管可以保證使用此Las Vegas算法來找到正確的答案蝴悉,但它沒有固定的運行時間彰阴。

# 蒙特卡洛算法_Monte Carlo algorithm
begin
    i := 0 
    repeat
        Randomly select one element a out of n elements.
        i := i + 1
    until (a == x)||(i == k)
end

由于拉斯維加斯直到在數組中找到x才結束,它是以運行次數來賭博拍冠。另一方面尿这,Monte Carlo運行k次,這意味著不一定在執(zhí)行代碼的k次循環(huán)中庆杜,在數組中找到“x”妻味;意味著它可能會找到解決方案,也可能不會欣福。因此,與拉斯維加斯不同的是焦履,蒙特卡洛(Monte Carlo)在正確性上賭博拓劝。

2). 比較 拉斯維加斯算法_Las Vegas algorithm 與蒙特卡洛算法_Monte Carlo algorithm
  1. 拉斯維加斯算法_Las Vegas algorithm: 始終提供正確結果的隨機算法,一次運行到另一次運行的唯一變化是運行次數(A randomised algorithm that always gives correct results, the only variation from one run to another is the running time)

  2. 蒙特卡洛算法_Monte Carlo algorithm: 一種隨機算法嘉裤,其運行次數是確定性的郑临,但其結果在一定(通常較小)概率下可能不正確屑宠。(A randomised algorithm whose running time is deterministic, but whose results may be incorrect with a certain (typically small) probability.)

  3. 不同點:

    1. 蒙特卡洛算法運行固定數量的次數(Monte Carlo algorithm runs for a fixed number of steps)
    2. 拉斯維加斯算法無限循環(huán)運行厢洞,直到找到正確的結果(Las Vegas algorithm runs in an infinite loop until the correct results are found)
    3. 可以使用提前終止將拉斯維加斯算法轉換為蒙特卡洛算法(Las Vegas algorithm can be converted into Monte Carlo algorithm using early termination)
3). 總結 拉斯維加斯算法_Las Vegas algorithm 與蒙特卡洛算法_Monte Carlo algorithm
  • 元素搜索問題非常簡單,但是
    1. 對于確定性順序(線性)搜索算法,例如躺翻,從頭開始逐一搜索數組:
      1. 平均時間復雜度: O( \frac{n+1}{2}) \cong O (n)
      2. 最壞時間復雜度: O(n)
    2. 對于拉斯維加斯算法_Las Vegas algorithm
      1. 平均時間復雜度: 取決于輸入; 如果一半數組包含0,另一半包含1丧叽,則查找第一個元素的值等于1的平均時間復雜度為O(1)
      2. 最壞時間復雜度: 無限(Unbound)
    3. 對于蒙特卡洛算法_Monte Carlo algorithm
      1. 平均時間復雜度: O(1)
      2. 最壞時間復雜度: O(1)
      3. 會有找不到元素的可能性。

3.2 n個螺栓與n個螺母(the real Nuts and Bolts Problem)

  • 一個沒有條理的木匠有n個不同大小的螺母和n個螺栓的集合公你,并希望找到相應的螺栓和螺母配對踊淳。每個螺母正好匹配一個螺栓(反之亦然),并且他只能將螺母與螺栓進行比較陕靠,即迂尝,他既不能將螺母與螺母進行比較,也不能將螺栓與螺栓進行比較剪芥。(A disorganized carpenter has a collection of n nuts of distinct sizes and n bolts and would like to find the corresponding pairs of bolts and nuts. Each nut matches exactly one bolt (and vice versa), and he can only compare nuts to bolts, i.e., he can neither compare nuts to nuts nor bolts to bolts.)
  • 如果采用暴力算法(將每個螺母與所有螺栓進行比較以找到匹配的螺栓), 時間復雜度為O(n)
  • 本節(jié)推薦使用隨機化快速排序_Randomised quicksort algorithm
1). 快速排序_Quicksort algorithm

快速排序: 給定n個數字組成的數組A垄开,按遞增順序對數字進行排序 (Given a array A of n numbers, sort the numbers in increasing order)


快速排序圖解
# 快速排序_Quicksort algorithm
less, equal, greater := three empty arrays 
if length(array) > 1
    pivot := select an element of array 
    for each x in array
    if x < pivot then add x to less
    if x = pivot then add x to equal 
    if x > pivot then add x to greater
quicksort(less)
quicksort(greater)
array := concatenate(less, equal, greater)
  • 平均時間復雜度:O(nlogn)(n是數組的大小)
    {Deterministic quicksort algorithm time complexity: O(nlogn) on average for a random permutation array (n is the size of the array)}
  • 最壞時間復雜度:O(n^2)
2). 隨機化快速排序_Randomised quicksort algorithm

4 擴展

4.1 隨機算法的應用

  1. 數學:

    1. 數論税肪,例如素數檢驗_Number theory, e.g., primality test
    2. 計算幾何:圖形算法溉躲,例如最小分割_Computational Geometry: graph algorithms, e.g., minimum cut
    3. 線性代數:矩陣計算_Linear algebra: matrix computations
  2. 計算機科學:

    1. 數據分析:網頁排名_Data analysis: PageRank
    2. 并行計算:避免死鎖_Parallel computing: Deadlock avoidance
    3. 優(yōu)化:自然啟發(fā)的優(yōu)化和搜索算法_Optimisation: Nature inspired Optimisation and Search algorithms
  3. 計算生物: DNA read alignment

4.2 隨機算法的優(yōu)缺點

  1. 優(yōu)點
    1. 簡單性:通常非常容易實現(Usually very easy to implement)
    2. 性能:通常以高概率產生(接近)最佳解決方案(Usually produce (near-) optimum solutions with high probability)
  2. 缺點
    1. 以有限的概率得到錯誤的答案(Getting a wrong answer with a finite probability.)
      1. 解決方案:重復算法
    2. 難以分析運行時間和獲得錯誤解決方案的概率(Difficult to analyse the running time and probability of getting an incorrect solution)
    3. 不可能獲得真正的隨機數(Impossible to get truly random numbers)

4.3 閱讀材料

  1. Motwani, Rajeev; Raghavan, Prabhakar (1995). Randomized Algorithms. New York: Cambridge University Press. ISBN 0-521-47465-5.
  2. Richard M. Karp (1991), An introduction to randomized algorithms, 34, Discrete Mathematics.
  3. Michael W. Mahoney(2011),Randomized algorithms for matrices and data.
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市寸认,隨后出現的幾起案子签财,更是在濱河造成了極大的恐慌,老刑警劉巖偏塞,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唱蒸,死亡現場離奇詭異,居然都是意外死亡灸叼,警方通過查閱死者的電腦和手機神汹,發(fā)現死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來古今,“玉大人屁魏,你說我怎么就攤上這事∽叫龋” “怎么了氓拼?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長抵碟。 經常有香客問我桃漾,道長,這世上最難降的妖魔是什么拟逮? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任撬统,我火速辦了婚禮,結果婚禮上敦迄,老公的妹妹穿的比我還像新娘恋追。我一直安慰自己凭迹,他們只是感情好,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布苦囱。 她就那樣靜靜地躺著嗅绸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沿彭。 梳的紋絲不亂的頭發(fā)上朽砰,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音喉刘,去河邊找鬼瞧柔。 笑死,一個胖子當著我的面吹牛睦裳,可吹牛的內容都是我干的造锅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼廉邑,長吁一口氣:“原來是場噩夢啊……” “哼哥蔚!你這毒婦竟也來了?” 一聲冷哼從身側響起蛛蒙,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤糙箍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后牵祟,有當地人在樹林里發(fā)現了一具尸體深夯,經...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年诺苹,在試婚紗的時候發(fā)現自己被綠了咕晋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出锋谐,到底是詐尸還是另有隱情,我是刑警寧澤质蕉,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站翩肌,受9級特大地震影響模暗,放射性物質發(fā)生泄漏。R本人自食惡果不足惜摧阅,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绷蹲。 院中可真熱鬧棒卷,春花似錦顾孽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蜒什,卻和暖如春测秸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背灾常。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工霎冯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钞瀑。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓沈撞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親雕什。 傳聞我的和親對象是個殘疾皇子缠俺,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355