Greedy Algorithm
description
Transcript of Greedy Algorithm
Greedy Algorithm
p2.
Activity-selection problem: Problem :
目的:安排最多的相容活動。
},...,,{ nS 21 , n 個活動 .
對任一活動 i ,Start time : Si
finish time : fiii fS
1S 1f
2S 2f
3S 3f
4S 4f
任二活動 i , j 可相容表示此兩種活動不重疊。即 ii fS or jj fS
p3.
Greedy-Activity-Selector(S,f) : Algorithm :
Complexity : Thm :上述演算法可產生最多的相容活動。
假設 nfff 21 , 此可由 quicksort 達到 ~ ).log( nnO
Greedy-Activity-Selector(S,f)
{ n = length(S) ;
A = {1} ; j=1;
for i=2 to n do
if ji fS then } ; }{ { ijiAA
return A }
ji fS
p4.
Greedy 方法之要素: 1. Globally optimal solution 可由選擇 local optimal
solution 獲得。 2. 一最佳解包含子問題之最佳解。 ( 此與 Dynamic Programming 類似 )
p5.
0-1 knapsack problem: ( NP complete )
Problem :
Goal : to carry as much value as possible.
Fractional knapsack problem : Problem :
n itemsn items
i-th item :worth dollarsweight units
iv
iwinteger. : ,, wwv ii
A person can carry W units.
同上,每一件東西可只取部分 ( 如 1/4, …. )
依序由大而小排列i
i
w
v
Example : W=50item1 :
item2 :
item3 :
65010 1111 wvvw , ,
510020 2222 wvvw , ,
412030 3333 wvvw , ,
取 item1 , item2 及2/3 的 item3.
)log( nnO
p6.
Huffman codes:a
45
000
0
Frequency (x1000)
Fixed-length codeword
variable-length codeword
b
13
001
101
c
12
010
100
d
16
011
111
e
9
100
1101
f
5
101
1100
總共有 bits ,, 0003000001003 若用 fixed-length codeword :
總共有若用 variable-length codeword :
bits ,000224
500041 ,300013 ,200013 ,600013 ,90004 50004
p7.
Prefix codes:No codeword is also a prefix of some other codes.字首T :
a : 45
100
55
30
d : 16
1
1
1
25
0
0
b : 13
1
14c : 12
00
e : 9
1
f : 5
0
Cc
T cdcfTB )()()(
cost of the tree T.
Constructing a Huffman code:(a) f : 5
(b)
(c)
e : 9 c : 12 b : 13 d : 16 a : 45
c : 12 b : 13 1410
f : 5 e : 9
d : 16 a : 45
1410
f : 5 e : 9
d : 16 2510
c : 12 b : 13
a : 45
(d) 2510
c : 12 b : 13 1410
f : 5 e : 9
3010
d : 16
a : 45
(e) a : 45
1410
f : 5 e : 9
3010
d : 16
2510
c : 12 b : 13
55 10
(f)
1410
f : 5 e : 9
3010
d : 16
2510
c : 12 b : 13
55 10
55 10
a : 45
( 同前頁: T)
source :Example :” A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS.”
11 3 3 1 2 5 1 2 6 2 4 5 3 1 2 4 3 2
A B C D E F G I L M N D P R S T U
0 1 1 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1 0 0 1 0 0 1 0 00 0 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 10 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 0 0 0 1 1 1 0 1 1 1 0 1 0 1 0
0 1 1 1 0 1 1 1 0 0 1
1110
2310
5 6N I 10
310
1 2F G
610
1210
3 3O B
6
3
A
1610
3710
10
410
2 2U L
8
4
S
10
410
2 2D R
8
4
M 10
210
1 1C P
5
3
T
1010
5
E
1021
11
6010
p10.
A Huffman tree is a binary tree of integers with these two properties:Each internal node is the sum of of its childrenIts weighted external path length is minimal
p11.
Algorithm: Generating a Huffman code
Input: a sequence of charactersoutput: a bit code for the input charactersPostconditions: the bit code has the
unique prefix property and is optimal1. Tally the frequencies of the input
characters2. Load the letter-frequency pairs into a
min priority queue3. Coalesce the pairs into a Huffman tree4. Encode the character at each leaf with
the bit sequence along root-to-leaf path
p12.
Huffman(C) : Algorithm :
Complexity :
Huffman(C)
{
// Q : priority queue
)log( nnO
; Cn; CQ
for i=1 to n-1 do
{ 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) ; }
Lemma 17.2::字母 , :字元 , c c :出現 次 . ][cf
令 且 為最小的兩個 .yx, ][ , ][ yfxf
存在一最佳的 prefix-free code, 使 x,y 之碼等長且只差最後一個 bit.
Proof :
x
y
b c
T ( optimal )
b
y
x c
T’
b
c
x y
T’’
'
)()()()()'()(c
Tc
T cdcfcdcfTBTBC C
)()()()()()()()( '' bdbfxdxfbdbfxdxf TTTT
)(bdT =
)(xdT =
0 ))()())(()(( xdbdxfbf TT
同理 )''()'( TBTB
C C
C
Lemma 17.3:C令 T : full binary tree representing an optimal prefix code over .
若 T’ 不是 C’ 的最佳 prefix code. 則可找到 T’’ 使 B(T’’) < B(T’)
x y
z
leaves
z
)()()( yfxfzf
},{' yxTT
}{},{' zyxCC
T’ represents an optimal prefix code for C’.
Proof :
對任一 Cc ).()( , },{ ' cdcdyx TT
).()()()( ' cdcfcdcf TT 1 )()()( ' zdydxd TTT ))()(()()()()()()( yfxfzdzfydyfxdxf TTT
)()()'()( yfxfTBTB
)()()(')'( TByfxfTB
T 是 C 之最佳之 prefix code.
p15.
Thm:Huffman 之演算法可產生最佳之 prefix code.
Proof :由 lemma 17.2 , 17.3.
Example :若用 { 0 , 1 , 2 } 來編碼,試將上述方法一般化以求得最佳之編碼方法。
p16.
Matroids:
1. S :有限非空集合 2. X :為 S 的部分集合所形成之集合 ( 即 X 的元素為集合 )
並滿足:若 且 則 3. 若 且 則存在 使得
Graphic matroid : 且 A : acyclic.
),( XSM
independent subset
XBA , BA ABx .}{ XxA
XB .XABA( hereditary property )
( exchange property )
Example : Matric matroid
if rows in I are linearly independent. , , } rows { XISIS
),( , ),( EVGXSM GGG 為一 graph.
ESG
EA GXA ( i.e. A forms a forest )
p17.
Thm 17.5:若 G 為一 undirected graph 則 為 一 matroid.),( GGG XSM
Proof :1. E : finite
2. is hereditary, acyclic graph 之部分仍為 acyclic.GX
3. 假設 A,B 為 G 中之 forests. 且 AB
A :包含 個 trees.AV
B :包含 個 trees.BV ( B 的樹較少 )( 但有較大顆的 )
故在 B 中有一樹 T 包含 A 中的 2 顆樹的 vertices.
亦即 T 中存在一 edge (u,v) 使得 u 和 v 分佈在 A 中的兩顆樹 . 故將(u,v) 加到 A 中 不會產生 cycle. 故 .)},{( GXvuA
由 1. 2. 3. 定理得證 .
p18.
Thm 17.6:All maximal independent subsets in a matroid have the same size.
Proof : , A is maximal if it has no extension.XA
即不存在 使得Xx .}{ XxA
假設 A : maximal independent subset.
B : maximal independent subset 且 .AB
存在 使得 A 為 maximal.ABx }{ XxA
p19.
Weighted Matroid: Definition :
Greedy algorithms on a weighted matroid Problem :
, weight function : w(x) for each),( XSM . ,)()( SAxwAwSxAx
Given , find has maximal possible
weight.
),( XSMw
)( s.t. AwXA
Example : Minimum Spanning Tree
: weight function defined on E; .wEVG , ),(
Define , and ),( XEMG . 0, )()(' 0 Eeewwew
Let A be a maximal independent set in X.
Then A corresponds to a spanning tree in G.
).()()(' AwwVAw 01
p20.
Algorithm : ),( XSM
Greedy(M,w)
{
Sort S[M] into nonincreasing order by weight w ;
; A
For each , taken in nonincreasing order by weight w(x) ;][MSx
do if ][}{ MXxA
then }{xAA Return A }
nn log
)(nf
若 則上述需 步驟。nS ))(log( nnfnnO
Lemma 17.7:令 為一加權 ( weight ) matroid.),( XSM
Proof :
S 依加權函數 w, 排列由大到小 .令 x 為 S 中第一個使 之 independent 元素 ( 但此 x 並不一定存在 ).Xx }{
若此 x 存在,則存在一最佳 ( 即 w(A) 最大 ) 之 A .; AxS
若沒有上述 x 存在,則 為唯一的 independent set. Why ?
現假設 B 為一非空之 optimal subset.
若 , 則取 A 等於 B 故得證 .Bx
若 , 則 B 中之元素的 weight 不會比 x 之 weight 大 .Bx
假設 但已知 XyBy }{ ).()( ywxw
令 t.indepneden ~}{xA
由 exchange property, 可將 A 擴充至 而且 A 仍保持為 independent. 取
BA
}{}{ xyBA
).()()()()( BwxwywBwAw B 為 optimal A 為 optimal 且含 x.
p22.
Thm 17.8:
Proof :假設 x 為 A 之 extension 但不為 之 extension.
: }{ x
令 為任一 matroid.),( XSM
若 且 x 不為 之 extension, 則 x 不為任一 independent set 之extension.
Sx
: }{xA independent. x 為 A 之 extension.independent. 由假設 x 不是 之 extension.
p23.
Thm 17.9(Matroids optimal-substructure):
Thm 17.10 :
Proof :若 且 且 A : maximum weighted
令 x 為 Greedy 演算法中第一個被選入的元素 .尋找 M 中包含 x 之 maximum-weight independent subset 可以被轉化為尋找 matroid 之 maximum weight ind. Set.
),( XSM
)','(' XSM
}},{:{' XyxSyS
}}{:}{{' XxBxSBX
XA Ax '.}{' XxAA
反之若 則 '' XA .}{' XxAA
又 故由 M 中含 x 之 maximum-weight solution
可找到 M’ 之 maximum-weight solution. 反之亦然 .).()'()( xeAwAw
給定 , 則 Greedy(M,w) 可以找到 optimal solution.wXSM , ),('
Proof :
p24.
A task-scheduling problem: S={1,2,…..,n} n unit-time tasks.
Deadlines : task i 需在 di 前完成 .
Penalties : 若 task i 不能在 di 前完成,則罰 wi
若 task i 能在 di 前完成著無 penalty.
; ,,, nddd 21
ndi 1; ,,, nwww 21
目標:安排一執行順序使 penalty 最小 .
Example :1
4
60
2
2id
iw 70
3
4
40
4
3
50 30
5
1
6
4
10
7
6
20
Schedule : < 2 4 1 3 7 5 6 >
penalty20+30=50.
Def:
在一 schedule 中: late task : if it finishes after its deadline. early task : if it finishes before its deadline.
Early-first form : early tasks precede the late tasks. Canonical form : same as early-first form and the early
tasks are scheduled in order of nondecreasing deadlines.
a set A of task is independent :若存在一 schedule 使得 A 中無
late schedule. 故任一 schedule 中之 early tasks 形成一 independent
set. 令 X 表所有 independent set 之集合 . 如何判定一 tasks 集合是否為 independent ?
若 i jk k+1
i,j : early taskijij ddkdd 1
j
k k+1
i
t=1,2,…,n ,令 Nt(A) 表 A 中之 tasks 其 deadline t.
p26.
Lemma 17.11:
Proof :若 , 則 A 中存在 late task.
令 A : tasks 所形成之集合 .則下列序列等價
1. A : independent.
2. For t=1,2,…n , .)( tANt 3. 若 A 中之 tasks 依 deadlines ( nondecreasing ) 排序,則無 late task.
tANt )(
故 ).()( 21 ),()( 32 ),()( 13 顯然 .
p27.
Thm 17.12:
Proof :(1) clearly.
(2) 由上述 independent set 之定義知其滿足 matroid 之第 2 個條件。 (3) 且
S={ unit tasks with deadline }
XBA ,
X={ independent sets of tasks } (S,X) is a matroid.
.AB
p28.
Solution by Greedy Algorithm
Sort w1,….., wn in decreasing order
Check A {i} X ?
Time complexity: O(n*n).
0NYT
51
1
0 249 50
NYT a51
(a)
2
0 249 50
NYT a51
(aa)
100 1
47 48
3
2
50a
r49
51
(aar)
(aard)
2
148
4
2
50a
r49
51
1
0 145 46
NYT d47
2
148
4
2
50a
r49
51
(aardv)
1
146
d47
1
0 143 44
NYT45
v
1
0 143 44
2
3
46
492
147
514
2 a
48
45
50
NYT v
d
r
(aardv)
3
1
5
2a
r 2
1 d1
0 1NYT v
51
(aardv)
Adaptive Huffman Coding: ( dynamic )
start
Is this the first appearance of the symbol
Call update procedure
Nodenumber max in block?
stop
Yes
Code is the path from The root node to the corresponding node
Encoding :
Read in symbol
Send code for NYT node followed by Index in the NYT list
No
Yes
No
Adaptive Huffman Coding: ( dynamic )
start
Is the node an external node?
Call update procedurestop
Yes
Decoding :
Go to rootOf the tree
No
Is the node the NYT node?
Read bit and go to corresponding node
Yes Read e bits
Is the e-bit number p less than r?
No Read e bits
Yes
Read one more bit
Decode the (P+1) element in NYT list
Is this the last bit?
Yes
Updating: start
Firstappearancefor symbol?
Go to symbolexternal node
Nodenumber max in block?
Go to symbolexternal node
Is thisthe root node?
stopYes
YesNYT gives birthto new NYT and External node
Increment weightOf external node and old NYT node
Go to old NYT node
Switch node withhighest numbered node in block
Yes
No
Go toparent node
No
No