Greedy Algorithms. 2 Greedy Methods ( 描述 1) * 解最佳化問題的演算法, 其解題過程可看成是由一 連串的決策步驟所組成, 而每一步驟都有一組選擇 要選定. * 一個 greedy method 在每一決策步驟總是選定那目 前看來最好 的選擇. *Greedy.

Slides:



Advertisements
Similar presentations
Algorithms Chapter 15 Dynamic Programming - Rod
Advertisements

Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
Advanced Chemical Engineering Thermodynamics
期末專題 - 吊人頭遊戲 第 35 組 組員 : 電機系 49841XXXX XXX 電機系 49841OOOO OOO.
自由進入及離開. 定義  長期 ─ 是指生產者能夠改變所有生產因素的情況。  自由進入及離開 ─ 是指公司能夠自由進入及離開市場而不受限 制。
CH-23 失效原因樹分析 FTA Failure Tree Analysis. 前言 為了提昇系統可靠度,產品在開發階段,利 用類似品管方法之魚骨圖分析手法,找出潛在 缺點,並加以改進,此種分析方法稱之為失效 原因樹分析法 (Failure Tree Analysis)– FTA 。 FTA 是一種系統化的方法,可以有效的找出.
: Boxes ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11003: Boxes 解題者:蔡欣燁 解題日期: 2007 年 3 月 19 日.
1 Secure Context-sensitive Authorization 2005 Author : Kazuhiro Minami, David Kotz Presented by Shih Yu Chen.
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
消費者物價指數反映生活成本。當消費者物價指數上升時,一般家庭需要花費更多的金錢才能維持相同的生活水準。經濟學家用物價膨脹(inflation)來描述一般物價持續上升的現象,而物價膨脹率(inflation rate)為物價水準的變動百分比。
Chapter 3 Growth of Functions Asymptotic notation Θ-notation: f(n) = Θ(g(n)) , g(n) is an asymptotically tight bound for f(n) 。 Θ(g(n)) = {f(n)|
第 4 章 迴歸的同步推論與其他主題.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
投資分析與決策 1. 投資分析技術簡介 2. 損益平衡分析 3. 要素評分法 4. 狀況評估法 5. 工程經濟分析法.
第一章 演算法:效率、分析與量級 1.1演算法 1.2發展有效率演算法的重要性 1.3演算法的分析 1.4量級(Order)
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 貳 研究設計.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
第 5 章 線性規劃的假設分析 學習目標 5.2 繼續偉伯公司的個案研究(5.2節) 5.3
: War on Weather ★★☆☆☆ 題組: Contest Volumes Archive with Online Judge 題號: 10915: War on Weather 解題者:陳明凱 題意:題目總共會給你 k 個點座標代表殺手衛星的位置, 距離地球表面最少 50 公里以上,並且會給你.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
選舉制度、政府結構與政 黨體系 Cox (1997) Electoral institutions, cleavage strucuters, and the number of parties.
Chapter 2 Getting Started Insertion Sort: 能有效率地排序小數字的演算法 範例 :
: Playing War ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11061: Playing War 解題者:陳盈村 解題日期: 2008 年 3 月 14 日 題意:在此遊戲中,有一類玩家一旦開始攻擊, 就會不停攻擊同一對手,直到全滅對方或無法再.
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
Johnson’s algorithm Johnson’s演算法可用於計算All pairs shortest path問題。
短缺,盈餘與均衡. 遊戲規則  老師想出售一些學生喜歡的小食。  老師首先講出價錢,有興趣買的請舉手。
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
公司加入市場的決定. 定義  平均成本 = 總成本 ÷ 生產數量 = 每一單位產量所耗的成本  平均固定成本 = 總固定成本 ÷ 生產數量  平均變動成本 = 總變動成本 ÷ 生產數量.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
Marble on tree ★★★☆☆ 題組: ACM Programming Contest World Finals, 1998 題號: Marble on tree 解題者:呂為萱 解題日期: 2011 年 3 月 16 日 題意: 有 n 個箱子,被擺放在 rooted.
卡內基訓練  Copyright, 1997 © Dale Carnegie & Associates, Inc. 1 簡報標題 TIP 如需進一步建議, 請看卡內基訓練  的簡報技巧.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
1 100: The 3n+1 Problem ★★★☆☆ 題組: VOLUME CII 題號: 10721: Problem C-Chopsticks 陳冠男 解題者:陳冠男、侯沛彣 解題日期: 2006 年 4 月 23 日 給定一個正整數 n (n>1) ,當 n 為奇數時令 n  3n+1.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
: Expect the Expected ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11427: Expect the Expected 解題者:李重儀 解題日期: 2008 年 9 月 21 日 題意:玩一種遊戲 (a game.
逆向選擇和市場失調. 定義  資料不對稱 在交易其中,其中一方較對方有多些資料。  逆向選擇 出現在這個情況下,就是當買賣雙方隨意在 市場上交易,與比較主動交易者作交易為佳 。
: Flip Sort ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10327: Flip Sort 解題者:歐子揚 解題日期: 2010 年 2 月 26 日 題意:在這個問題中使用一種排序方式 (Flip) ,意思就是 只能交換相鄰的.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Structural Equation Modeling Chapter 6 CFA 根據每個因素有多重指標,以減少 測量誤差並可建立問卷的構念效度 驗證性因素分析.
Learning Method in Multilingual Speech Recognition Author : Hui Lin, Li Deng, Jasha Droppo Professor: 陳嘉平 Reporter: 許峰閤.
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 7 貪婪法則 Greedy Approach.
Extreme Discrete Summation ★★★★☆ 題組: Contest Archive with Online Judge 題號: Extreme Discrete Summation 解題者:蔡宗翰 解題日期: 2008 年 10 月 13 日.
Probability Distribution 機率分配 汪群超 12/12. 目的:產生具均等分配的數值 (Data) ,並以 『直方圖』的功能計算出數值在不同範圍內出現 的頻率,及繪製數值的分配圖,以反應出該 機率分配的特性。
1/17 A Study on Separation between Acoustic Models and Its Application Author : Yu Tsao, Jinyu Li, Chin-Hui Lee Professor : 陳嘉平 Reporter : 許峰閤.
Dynamic Programming.
Biological Science Database 個人化服務設定步驟. Biological Science Database 僅提供專題選 粹服務 專題選粹 (Alerts) :查詢後,提供儲存檢 索策略的功能,日後每週將符合條件的 更新資料,採 方式通知。每筆設定 最多每週可收到.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 壹 企業研究導論.
: Wine trading in Gergovia ★★☆☆☆ 題組: Contest Volumes with Online Judge 題號: 11054: Wine trading in Gergovia 解題者:劉洙愷 解題日期: 2008 年 2 月 29 日 題意:在 Gergovia.
兩黨與多黨政黨體系 Lijphart (1984) Party Systems: Two-Party and Multiparty Patterns.
:Commandos ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11463: Commandos 解題者:李重儀 解題日期: 2008 年 8 月 11 日 題意: 題目會給你一個敵營區內總共的建築物數,以及建築物 之間可以互通的路有哪些,並給你起點的建築物和終點.
:Count the Trees ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10007:Count the Trees 解題者:楊家豪 解題日期: 2006 年 3 月 題意: 給 n 個點, 每一個點有自己的 Label,
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
1 Knapsack Cryptosystems 2 ◎ Merkle-Hellman Knapsack Cryptosystem 觀察: (1) 0/1 knapsack problem (i.e. sum of subset) 例:已知 C = 14, A = (1, 10, 5, 22, 3)
幼兒行為觀察與記錄 第八章 事件取樣法.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Presentation transcript:

Greedy Algorithms

2 Greedy Methods ( 描述 1) * 解最佳化問題的演算法, 其解題過程可看成是由一 連串的決策步驟所組成, 而每一步驟都有一組選擇 要選定. * 一個 greedy method 在每一決策步驟總是選定那目 前看來最好 的選擇. *Greedy methods 並不保證總是得到最佳解, 但在有 些問題卻可以得到最佳解.

Greedy Algorithms3 Greedy Methods ( 描述 2) *Greedy 演算法經常是非常有效率且簡單的演算 ; 但 但較難證明其正確性 ( 與 DP 演算法比較 ). * 很多 heuristic algorithms 都採用 greedy methods 的 策略.

Greedy Algorithms4 一個活動選擇問題 ( 定義 ) 假設有 n 個 活動 提出申請要使用一個場地, 而這場 地在同一時間點時最多只能讓一個活動使用. 問題是: 從這 n 個活動選一組數量最多, 且可以在這場地舉辦 的活動集 時間軸 假設活動 i, 其提出 申請使用場地的時 段為一半關半開的 區間 [s i, f i ), 並以符 號 I i 代表.

Greedy Algorithms5 一個活動選擇問題 ( 設計 1) *Let P(A) denote the problem with A as the given set of proposed activities and S denote an optimal solution of P(A). For any activity i in A, we have 1. i  S  S is an optimal solution of P(A\{ i }). 2. i  S  S \{ i } is an optimal solution of P(A\N[ i ]) but not necessary an optima solution of P(A\{ i }). i N[ i ] :N[ i ] : N[ i ]={j  A: I j  I i   }

Greedy Algorithms6 一個活動選擇問題 ( 設計 2) *What kind of activity i in A will be contained in an optimal solution of P(A) : an activity with 1.minimum f i  s i or 2.minimum | N[ i ] | or 3.minimum f i or 4.minimum s i. Answer :. Proof : Let f 1 = min { f i } and S be an optimal solution of P(A). If 1  S then there is one and only one activity in S, say j, such that I j  I 1  . Then S \{ j}  { 1} is also an optimal solution. Proof : Let f 1 = min { f i } and S be an optimal solution of P(A). If 1  S then there is one and only one activity in S, say j, such that I j  I 1  . Then S \{ j}  { 1} is also an optimal solution.

Greedy Algorithms7 一個活動選擇問題 ( 程式 + 例子 ) Greedy-ASP(s, f, n) { /* f[1]  f[2]  …  f[n]*/ Ans = {1}; for(i=2, j=1; i  n; i++) if(s[ i ]  f[ j ]) {Ans = Ans  { i }; j = i; } } time Input: i s i f i

Greedy Algorithms8 Greedy 演算法的要素 *Optimal substructure (a problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems) *Greedy-choice property *Priority queue or sorting

Greedy Algorithms9 Knapsack Problem (Greedy vs. DP) Given n items:weights: w 1 w 2 … w n values: v 1 v 2 … v n a knapsack of capacity W Find the most valuable load of the items that fit into the knapsack Example: item weight value Knapsack capacity W= $ $ $ $10

Greedy Algorithms10 Knapsack Problem Given n items:weights: w 1 w 2 … w n values: v 1 v 2 … v n a knapsack of capacity W T[i, j]: the optimal solution using item 1,...,i with weight at most j. If w i > j T[i, j] = T[i-1, j]; otherwise T[i, j] = max{ T[i-1, j], w i +T[i-1, j - w i ]}. How good is this method?

Greedy Algorithms and Fractional Knapsack Problem *Constraints of 2 variants of the knapsack problem:  0-1 knapsack problem: each item must either be taken or left behind.  Fractional knapsack problem: the thief can take fractions of items. *The greedy strategy of taking as mush as possible of the item with greatest v i / w i only works for the fractional knapsack problem.

Greedy Algorithms12 Huffman Codes Alphabet : a b c d e f Frequency in a file Fixed-length codeword Variable-length codeword file length 1 = 300; file length 2 = 224 Compression ratio = (300  224)/300  100%  25% *A very effective technique for compressing data *Consider the problem of designing a binary character code *Fixed length code vs. variable-length code, e.g.:

Greedy Algorithms13 Prefix Codes & Coding Trees *We consider only codes in which no codeword is also a prefix of some other codeword. *The assumption is crucial for decoding variable-length code (using a binary tree). E.g.: a :45 c :12 b :13 d :16 f : 5 e :

Greedy Algorithms14 Optimal Coding Trees *For a alphabet C, and its corresponding coding tree T, let f(c) denote the frequency of c  C in a file, and let d T (c) denote the depth of c’s leaf in T. (d T (c) is also the length of the codeword for c.) *The size required to encode the file is thus: B(T) =  c  C f(c) d T (c) *We want to find a coding tree with minimum B(T).

Greedy Algorithms15 Observation 1 *Any optimal coding tree for C, |C| > 1, must be a full binary tree, in which every nonleaf node has two children. E.g.: for the fixed-length code: a :45 c :12 b :13 d :16 f : 5 e : 9

Greedy Algorithms16 Observation 2 *Assume C = { c 1, c 2, …, c n }, and f(c 1 )  f(c 2 )  …  f(c n ). Then there exists an optimal coding tree T such that : c 1 c 2 T:T: and d T (c 1 ) = d T (c 2 ) = max c  C d T (c)

Greedy Algorithms17 Observation 3 *If is T an optimal coding tree for C, then T' is an optimal coding tree for C \{c 1, c 2 }  {c'} with f(c') = f(c 1 ) + f(c 2 ). c 1 c 2 T:T: c 1 c 2 T': c'

Greedy Algorithms18 Huffman’s Algorithm ( 例 ) a :45 c :12 b :13 d :16 f : 5 e : 9 a :45 c :12 b :13 d :16 f : 5 e : 9 14 a :45 c :12 b :13 d :16 f : 5 e :

Greedy Algorithms19 Huffman’s Algorithm ( 例 - 續 1) a :45 c :12 b :13 d :16 f : 5 e : d :16 f : 5 e : c :12 b :13 25 a :45

Greedy Algorithms20 Huffman’s Algorithm ( 例 - 續 2) d :16 f : 5 e : c :12 b :13 25 a :45 55 d :16 f : 5 e : c :12 b :13 25

Greedy Algorithms21 Huffman’s Algorithm ( 例 - 續 3) a :45 55 d :16 f : 5 e : c :12 b :13 25 a :45 c :12 b :13 d :16 25 f : 5 e :

Greedy Algorithms22 Huffman’s Algorithm (pseudo-code) Huffman(C) Q  C // Q :priority queue while(|Q| > 1) z  Allocate-Node( ) x  left[z]  Extract-Min(Q) y  right[z]  Extract-Min(Q) f [z]  f [x] + f [y] insert(Q, z) return Extract-Min(Q) Time efficiency:.