SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Greedy Algorithm-
   Huffman Code &
Gale-Shapley Algorithm
   100971016 陳心睿
   100971004 李魁林


                   1
請問如何把36元換成最少的硬幣?
• 可用的硬幣種類有 {1,5,10,20}




                         2
Introduction
• Greedy Algorithm Introduction
• Huffman Code
  –   Huffman Tree
  –   Huffman Algorithm
  –   Prefix Code
  –   Huffman Algorithm Time Complexity
• Gale-Shapley Algorithm

                                          3
Greedy Algorithm Introduction
• 一種短視近利的想法
• 每一步都不管大局,只求局部解決方法
• 它透過一步步的選擇局部最佳解來得到問題的解答
• 它所做的每一個選擇是根據某種準則來決定的
• 前後決定並無關聯性。前面的決定不會影響後面的決
  定
• 用來解決部分的最佳化問題
• 並不能解決所有最佳化問題



                                    4
Greedy Algorithm Introduction
• 在解決最佳化問題的過程中,通常是經過一個
  序列的步驟,而且在每個步驟中會有眾多選擇
 – 最佳化問題(optimization problem)
   • 很多問題常常存在許多的解答,而每個解答相對地也可以
     有一個數值,不過,我們希望能找出一個有最佳數值(最
     大值或最小值)的解答,稱為最佳解


• 貪婪策略步驟
 – 找出可能的所有選擇
 – 檢查可能選擇是否可行
 – 將可行解加入解答中

                                 5
Greedy Algorithm Introduction
• 在所有解決最佳化問題的演算法中,貪婪演算
  法是最直覺的一種方法

• 有效率的方法,但是並不保證永遠可得到全域
  最佳解

• 貪婪演算法相關應用議題:Huffman Codes、
  Shortest Paths Problem、 0/1 Knapsack Problem、
  Job Scheduling Problem等。


                                             6
Huffman Code
• 檔案的壓縮演算法分類
 – 可恢復性
   • 沒有失真的問題,適合用於資料性質的一般檔案
 – 不可恢復性
   • 有失真的問題,但是通常其壓縮率會比較好
   • 適合用於多媒體檔案,例如JPEG、MPEG等
• 霍夫曼碼
 – 可恢復性
 – 以檔案中每個字元出現的頻率多寡,作為壓縮的依
   據

                              7
• 壓縮文字檔方式
 – 二元字碼(binary character code)法
   • 每個字元以一個唯一的二元字串來表示
 – 分類
   • 固定長度碼 (Fixed-length codeword)
        – 代表每個字元的字碼(codeword)之長度相同
        – 例如文字檔包含a ~ f六個字元,22<6<23,則字碼長度為3
   • 不固定長度碼(Variable-length codeword)
        – 給予較常出現的字元較短的字碼,較不常出現的字元給予較長字碼,
          以此來獲得較佳的壓縮結果
        – 例如文字檔中字元a出現的頻率最高,令字碼0代表a


                                             8
A       B       C         D      E      F
  Frequency (in thousands)      45      13      12        16     9      5
   Fixed-length codeword       000     001      010       011   100    101
  Variable-length codeword      0      101      100       111   1101   1100


• 固定長度碼需要10000*3 =30000bits 儲存空間
• 可變長度字碼只需
  (45*1+13*3+12*3+16*3+9*4+5*4)*10000=224,000 bits 儲存
  空間
• 二者相差約25%大小
                             Content Source :Cormen,
                             Introduction to Algorithms                       9
• 建構二元樹(binary tree)模型來代表二
  元字碼的編碼與解碼
 – 葉子代表某個字元
 – 一個字元的二元字碼,可以由二元樹樹根到
   該字元葉子的路徑來表示
 – 其中0代表二元樹節點的「左子節點」,1則
   代表「右子節點」


                             10
• 字元編碼的二元樹範例:
 – Fixed-length codeword




 Graph Source: Cormen,
 Introduction to Algorithms
                              11
– Variable-length
  codeword




Graph Source: Cormen,
Introduction to Algorithms
                             12
Huffman Tree
• 霍夫曼碼是透過建立霍夫曼二元樹的方法得到
• 貪婪準則
 – 字元發生頻率越高者,以越短的二元字串來表示之
• 相對於二元樹的觀點
 – 頻率越高的字元其相對應之葉子,離樹根越近,頻率
   越低的則越遠。
 – 二元樹的建立步驟,就是先合併頻率越低的字元



                        13
Huffman Algorithm
  HUFFMAN(C)
1. n  |C|
2. Q  C
3. for i  1 to n-1
4. do allocate a new node z
5. left[z]  x  EXTRACT-MIN(Q)
6. right[z]  y  EXTRACT-MIN(Q)
7. f[z]  f[x] + f[y]
8. INSERT(Q , z)
9. return EXTRACT-MIN(Q)
                                   14
Huffman Algorithm
    10       15       12       13       3            4            1
a        e        i        o        s            t            z
                                                              4
                                                         T1
    10       15       12       13       4            3            1
a        e        i        o        t            s            z

                                                     T2 8
                                                 4                4
                                            T1                t
    10       15       12       13           3            1
a        e        i        o            s            z                15
Huffman Algorithm
    15       12               13
e        i            o                    18                15                       18
                                   T3
                                                10       e                    T3
                               8                                                           10
                      T2                    a                             8
                                                                  T2                   a
                  4                    4                                          4                      25
                                                              4
             T1                    t                     T1                   t
                                                                                                    T4
             3            1                                                                    12            13
                                                         3            1
         s            z                              s            z                        i             o



                                                                                                    16
Huffman Algorithm
              25                                                                                            58
                                                  33
         T4                                  T3                                               0        T5       1
                                                                                                                         25
    12             13                   18                     15                             33                 T4
                                                       e                                 T3                               1
i             o                 T2                                              0             1             0
                                              10                                    18                 15        12           13
                            8                                                                      e
                   T2                    a                          0       T2           1                  i            o
                                                                        8                10
               4                    4
         T1                     t                      0        T2
                                                                            1
                                                                                     a
                                                           4                    4
         3              1                          T1                       t
    s              z                          0                1
                                                   3                1
                                              s                z                                                    17
Prefix Code
符號   s     z     t   a              e       i      o
編碼 00000 00001 0001 001            01      10     11

• 透過霍夫曼演算法建構的二元樹,符合Prefix
  code特性。
  – 任一編碼不為其它編碼之字首
• 不符合Prefix code特性會照成解碼混淆。
 假設a = 01, e = 00, i = 1, s = 0001, t = 00011,欲解壓縮二元編碼為00011。
 可能結果:
                Case 1:00 01 1  eai
                Case 2:0001 1  si
                Case 3:00011  t                      18
• 壓縮檔最佳碼
 – 一個檔案的一個最佳碼永遠使用一棵接近完整二元
   樹(almost complete binary tree)來表示,即每個內
   部節點都剛好有兩個子節點
 – 若一棵接近完整二元樹有N個葉子,則表示它有N-1
   個內部節點。
 – 對於字元表C中的每個字元c而言,假設f(c)代表在
   檔案內c的頻率,d(c)代表在二元樹內葉子c的深度,
   則對該檔案編碼後所需的位元數目為:
               B(T )         f (c)d (c)
                       c C

                                          19
58
                                                     0        T5        1
                                                                              25
                                                     33                  T4
                                       0
                                            T3                     0           1
                                                      1
                                       18                     15        12         13
                          0       T2       1
                                                          e         i              o
                                                10
                          8
         0           T2       1             a
             4                    4
        T1                    t
0                1
    3                1
s                z                    B(T)=3*5+1*5+4*4+10*3+15*2+12
                                      *2+13*2=146 bits
                                                                                       20
Time Complexity
  HUFFMAN(C)
1. n  |C|
2. Q  C                           Ο( n )
3. for i  1 to n-1
4. do allocate a new node z
5. left[z]  x  EXTRACT-MIN(Q)    Ο( log n ) ×
6. right[z]  y  EXTRACT-MIN(Q)   (n-1)
7. f[z]  f[x] + f[y]              O( n log n )
8. INSERT(Q , z)
9. return EXTRACT-MIN(Q)
                                            21
穩定婚姻問題
STABLE MARRIAGE PROBLEM



                          22
Gale-Shapley Algorithm
1. function stableMatching {
2.    Initialize all m ∈ M and w ∈ W to free
3.    while ∃ free man m who still has a woman w to propose to {
4.      w = m's highest ranked such woman to whom he has not yet proposed
5.      if w is free
6.        (m, w) become engaged
7.      else some pair (m', w) already exists
8.        if w prefers m to m'
9.          (m, w) become engaged
10.         m' becomes free
11.       else
12.         (m', w) remain engaged
13. }
14. }
Gale-Shapley Algorithm
1. 從未訂婚的男士中挑一位向自己還未求
   過婚的女士中最喜愛的那位求婚。
2. 女士若未訂婚,則兩人定婚(但之後可取
   消婚約)。
   女士若已訂婚,但愛求婚者比愛未婚夫
   多,則取消原本婚約,改與求婚者訂婚,
   原本未婚夫則恢復成單身。
3. 重複1,2直到 n 對都完成訂婚為止。
Gale-Shapley Algorithm
此演算法保證以下兩點:
• 所有人都會配對結婚
• 這些婚姻都是穩定的
所有人都會配對結婚
• 女士只要有被求過一次婚的最後就不可
  能單身。
• 男士會依序向自己喜愛的女性求婚,最
  差的狀況就是n個女性都求過婚,這也表
  示不可能有沒被求過婚的女性,因男女
  人數相等,故若『無沒有訂婚的女性,
  就一定沒有沒訂婚的男性』。
這些婚姻都是穩定的
• 假設存在春嬌跟志明兩人彼此相愛,但
  最後並沒有結婚,分別嫁娶了別人。
• 若志明愛春嬌勝過他現任妻子,則表示
  志明一定有先跟春嬌求婚,但春嬌最後
  嫁給別人,必定是春嬌愛現任的老公勝
  過志明,因此假設不成立
• 所以『所有的配對都是穩定的狀態』。
最佳解
A: YXZ B: ZYX C: XZY (男士)
X: BAC Y: CBA Z: ACB (女士)
若從男士優先的角度會配出 (AY,BZ,CX)
若從女士優先的角度會配出 (AZ,BX,CY)
以上兩種都是『穩定狀態』!

Stable Marriage Problem 主要是要找『穩定』
狀態的解,但此解不見得是最佳解
Gale-Shapley Algorithm -Time Complexity

考慮worst-case,總共 n 個男士,每個男士
有一份長度為 n 的女士清單,依序向 n 位
女士求過婚,所以最差的狀況就是 O(n^2)
貪婪策略(Greedy Method)
• Incremental Greedy Method:
  從沒有答案開始,選擇眼前最好的方式
  (Greedy),逐步填滿答案,每次根據現況
  算出一部分答案,直到答案都填滿為止。
• Iterative Greedy Method:
  先隨便弄個答案,之後選擇眼前最好的
  方式(Greedy),逐步修飾答案,每次根據
  現況修飾一部分答案,直到答案夠漂亮
  為止。
100971016 陳心睿
100971004 李魁林

Q&A

                32

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Greedy Algorithm

  • 1. Greedy Algorithm- Huffman Code & Gale-Shapley Algorithm 100971016 陳心睿 100971004 李魁林 1
  • 3. Introduction • Greedy Algorithm Introduction • Huffman Code – Huffman Tree – Huffman Algorithm – Prefix Code – Huffman Algorithm Time Complexity • Gale-Shapley Algorithm 3
  • 4. Greedy Algorithm Introduction • 一種短視近利的想法 • 每一步都不管大局,只求局部解決方法 • 它透過一步步的選擇局部最佳解來得到問題的解答 • 它所做的每一個選擇是根據某種準則來決定的 • 前後決定並無關聯性。前面的決定不會影響後面的決 定 • 用來解決部分的最佳化問題 • 並不能解決所有最佳化問題 4
  • 5. Greedy Algorithm Introduction • 在解決最佳化問題的過程中,通常是經過一個 序列的步驟,而且在每個步驟中會有眾多選擇 – 最佳化問題(optimization problem) • 很多問題常常存在許多的解答,而每個解答相對地也可以 有一個數值,不過,我們希望能找出一個有最佳數值(最 大值或最小值)的解答,稱為最佳解 • 貪婪策略步驟 – 找出可能的所有選擇 – 檢查可能選擇是否可行 – 將可行解加入解答中 5
  • 6. Greedy Algorithm Introduction • 在所有解決最佳化問題的演算法中,貪婪演算 法是最直覺的一種方法 • 有效率的方法,但是並不保證永遠可得到全域 最佳解 • 貪婪演算法相關應用議題:Huffman Codes、 Shortest Paths Problem、 0/1 Knapsack Problem、 Job Scheduling Problem等。 6
  • 7. Huffman Code • 檔案的壓縮演算法分類 – 可恢復性 • 沒有失真的問題,適合用於資料性質的一般檔案 – 不可恢復性 • 有失真的問題,但是通常其壓縮率會比較好 • 適合用於多媒體檔案,例如JPEG、MPEG等 • 霍夫曼碼 – 可恢復性 – 以檔案中每個字元出現的頻率多寡,作為壓縮的依 據 7
  • 8. • 壓縮文字檔方式 – 二元字碼(binary character code)法 • 每個字元以一個唯一的二元字串來表示 – 分類 • 固定長度碼 (Fixed-length codeword) – 代表每個字元的字碼(codeword)之長度相同 – 例如文字檔包含a ~ f六個字元,22<6<23,則字碼長度為3 • 不固定長度碼(Variable-length codeword) – 給予較常出現的字元較短的字碼,較不常出現的字元給予較長字碼, 以此來獲得較佳的壓縮結果 – 例如文字檔中字元a出現的頻率最高,令字碼0代表a 8
  • 9. A B C D E F Frequency (in thousands) 45 13 12 16 9 5 Fixed-length codeword 000 001 010 011 100 101 Variable-length codeword 0 101 100 111 1101 1100 • 固定長度碼需要10000*3 =30000bits 儲存空間 • 可變長度字碼只需 (45*1+13*3+12*3+16*3+9*4+5*4)*10000=224,000 bits 儲存 空間 • 二者相差約25%大小 Content Source :Cormen, Introduction to Algorithms 9
  • 10. • 建構二元樹(binary tree)模型來代表二 元字碼的編碼與解碼 – 葉子代表某個字元 – 一個字元的二元字碼,可以由二元樹樹根到 該字元葉子的路徑來表示 – 其中0代表二元樹節點的「左子節點」,1則 代表「右子節點」 10
  • 11. • 字元編碼的二元樹範例: – Fixed-length codeword Graph Source: Cormen, Introduction to Algorithms 11
  • 12. – Variable-length codeword Graph Source: Cormen, Introduction to Algorithms 12
  • 13. Huffman Tree • 霍夫曼碼是透過建立霍夫曼二元樹的方法得到 • 貪婪準則 – 字元發生頻率越高者,以越短的二元字串來表示之 • 相對於二元樹的觀點 – 頻率越高的字元其相對應之葉子,離樹根越近,頻率 越低的則越遠。 – 二元樹的建立步驟,就是先合併頻率越低的字元 13
  • 14. Huffman Algorithm HUFFMAN(C) 1. n  |C| 2. Q  C 3. for i  1 to n-1 4. do allocate a new node z 5. left[z]  x  EXTRACT-MIN(Q) 6. right[z]  y  EXTRACT-MIN(Q) 7. f[z]  f[x] + f[y] 8. INSERT(Q , z) 9. return EXTRACT-MIN(Q) 14
  • 15. Huffman Algorithm 10 15 12 13 3 4 1 a e i o s t z 4 T1 10 15 12 13 4 3 1 a e i o t s z T2 8 4 4 T1 t 10 15 12 13 3 1 a e i o s z 15
  • 16. Huffman Algorithm 15 12 13 e i o 18 15 18 T3 10 e T3 8 10 T2 a 8 T2 a 4 4 4 25 4 T1 t T1 t T4 3 1 12 13 3 1 s z s z i o 16
  • 17. Huffman Algorithm 25 58 33 T4 T3 0 T5 1 25 12 13 18 15 33 T4 e T3 1 i o T2 0 1 0 10 18 15 12 13 8 e T2 a 0 T2 1 i o 8 10 4 4 T1 t 0 T2 1 a 4 4 3 1 T1 t s z 0 1 3 1 s z 17
  • 18. Prefix Code 符號 s z t a e i o 編碼 00000 00001 0001 001 01 10 11 • 透過霍夫曼演算法建構的二元樹,符合Prefix code特性。 – 任一編碼不為其它編碼之字首 • 不符合Prefix code特性會照成解碼混淆。 假設a = 01, e = 00, i = 1, s = 0001, t = 00011,欲解壓縮二元編碼為00011。 可能結果: Case 1:00 01 1  eai Case 2:0001 1  si Case 3:00011  t 18
  • 19. • 壓縮檔最佳碼 – 一個檔案的一個最佳碼永遠使用一棵接近完整二元 樹(almost complete binary tree)來表示,即每個內 部節點都剛好有兩個子節點 – 若一棵接近完整二元樹有N個葉子,則表示它有N-1 個內部節點。 – 對於字元表C中的每個字元c而言,假設f(c)代表在 檔案內c的頻率,d(c)代表在二元樹內葉子c的深度, 則對該檔案編碼後所需的位元數目為: B(T ) f (c)d (c) c C 19
  • 20. 58 0 T5 1 25 33 T4 0 T3 0 1 1 18 15 12 13 0 T2 1 e i o 10 8 0 T2 1 a 4 4 T1 t 0 1 3 1 s z B(T)=3*5+1*5+4*4+10*3+15*2+12 *2+13*2=146 bits 20
  • 21. Time Complexity HUFFMAN(C) 1. n  |C| 2. Q  C Ο( n ) 3. for i  1 to n-1 4. do allocate a new node z 5. left[z]  x  EXTRACT-MIN(Q) Ο( log n ) × 6. right[z]  y  EXTRACT-MIN(Q) (n-1) 7. f[z]  f[x] + f[y] O( n log n ) 8. INSERT(Q , z) 9. return EXTRACT-MIN(Q) 21
  • 23.
  • 24. Gale-Shapley Algorithm 1. function stableMatching { 2. Initialize all m ∈ M and w ∈ W to free 3. while ∃ free man m who still has a woman w to propose to { 4. w = m's highest ranked such woman to whom he has not yet proposed 5. if w is free 6. (m, w) become engaged 7. else some pair (m', w) already exists 8. if w prefers m to m' 9. (m, w) become engaged 10. m' becomes free 11. else 12. (m', w) remain engaged 13. } 14. }
  • 25. Gale-Shapley Algorithm 1. 從未訂婚的男士中挑一位向自己還未求 過婚的女士中最喜愛的那位求婚。 2. 女士若未訂婚,則兩人定婚(但之後可取 消婚約)。 女士若已訂婚,但愛求婚者比愛未婚夫 多,則取消原本婚約,改與求婚者訂婚, 原本未婚夫則恢復成單身。 3. 重複1,2直到 n 對都完成訂婚為止。
  • 27. 所有人都會配對結婚 • 女士只要有被求過一次婚的最後就不可 能單身。 • 男士會依序向自己喜愛的女性求婚,最 差的狀況就是n個女性都求過婚,這也表 示不可能有沒被求過婚的女性,因男女 人數相等,故若『無沒有訂婚的女性, 就一定沒有沒訂婚的男性』。
  • 28. 這些婚姻都是穩定的 • 假設存在春嬌跟志明兩人彼此相愛,但 最後並沒有結婚,分別嫁娶了別人。 • 若志明愛春嬌勝過他現任妻子,則表示 志明一定有先跟春嬌求婚,但春嬌最後 嫁給別人,必定是春嬌愛現任的老公勝 過志明,因此假設不成立 • 所以『所有的配對都是穩定的狀態』。
  • 29. 最佳解 A: YXZ B: ZYX C: XZY (男士) X: BAC Y: CBA Z: ACB (女士) 若從男士優先的角度會配出 (AY,BZ,CX) 若從女士優先的角度會配出 (AZ,BX,CY) 以上兩種都是『穩定狀態』! Stable Marriage Problem 主要是要找『穩定』 狀態的解,但此解不見得是最佳解
  • 30. Gale-Shapley Algorithm -Time Complexity 考慮worst-case,總共 n 個男士,每個男士 有一份長度為 n 的女士清單,依序向 n 位 女士求過婚,所以最差的狀況就是 O(n^2)
  • 31. 貪婪策略(Greedy Method) • Incremental Greedy Method: 從沒有答案開始,選擇眼前最好的方式 (Greedy),逐步填滿答案,每次根據現況 算出一部分答案,直到答案都填滿為止。 • Iterative Greedy Method: 先隨便弄個答案,之後選擇眼前最好的 方式(Greedy),逐步修飾答案,每次根據 現況修飾一部分答案,直到答案夠漂亮 為止。