Tải bản đầy đủ (.pdf) (64 trang)

Bài giảng Toán rời rạc: Thuật toán tham lam - Trần Vĩnh Đức

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.32 MB, 64 trang )

CuuDuongThanCong.com

Thuật toán tham lam
Trần Vĩnh Đức
HUST

Ngày 1 tháng 9 năm 2019

/>
1 / 64


Tài liệu tham khảo

S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani,
Algorithms, July 18, 2006.

CuuDuongThanCong.com

/>
2 / 64


Nội dung

Cây bao trùm nhỏ nhất

Mã hóa Huffman

Cơng thức Horn


Phủ các tập

CuuDuongThanCong.com

/>

ted edges are potential links, and the goal is to pick enough of the
Bài toán
nodes
are connected. But this is not all; each link also has a main
flected in that edge’s weight. What is the cheapest possible networ
1

A

4

3

4

B

4

E

C
4
2


D

6

5

F

mediate observation is that the optimal set of edges cannot contain
Bạn cần xây dựng mạng máy tính bằng cách kết nối từng cặp
e removingmáy.
an edge from this cycle would reduce the cost without
connectivity:
Cần chọn một số kết nối để mạng liên thông;

y1

nhưng không
phảiedge
tất cảcannot
các cặp:disconnect
Mỗi kết nốiatốn
một chi phí
Removing
a cycle
graph.
(tiền bảo trì).

Mạngbe

vớiconnected
chi phí nhỏ nhất
gì?
solution must
and làacyclic:
undirected graphs of this
CuuDuongThanCong.com
/>rees.
The particular tree
we want is the one with minimum tota
4 / 64


nodes are connected. But this is not all; each link also has a main
flected in that edge’s weight. What is the cheapest possible networ
1

A

4

3

4

B

4

E


C
4
2

D

6

5

F

mediate observation is that the optimal set of edges cannot contain
Tính chất
e removing
ancạnh
edge
from
this cycle
would
cost
Xóa một
trên
chu trình
khơng làm
mất reduce
tính liên the
thơng
củawithout

đồ
connectivity:
thị.

y 1 Vậy,
Removing
cycle
cannot
disconnect
mạng vớiachi
phí edge
nhỏ nhất
phải là
một cây. a graph.

solution must be connected and acyclic: undirected graphs of this
rees. The particular tree we want is the one with minimum tota
CuuDuongThanCong.com
/>as
the minimum spanning
tree. Here is its formal definition. 5 / 64


Bài toán Cây bao trùm nhỏ nhất (Minimal Spaning Tree)
Input: Đồ thị vô hướng G = (V, E); mỗi cạnh có trọng số we .
Output: Một cây T = (V, E′ ) với E′ ⊆ E, với tổng trọng số

weight(T) =
we
e∈E′


là nhỏ nhất.

CuuDuongThanCong.com

/>
6 / 64


um spanning trees

Input: An undirected graph G = (V, E ); edge weights we .

Tìm tocây
bao atrùm
e asked
network
collection of computers by linking
selected
Output: A tree T = (V, E ′ ), with E ′ ⊆ E , that minimizes
his translates into a graph problem
in which nodes are computers,
=
we .
s are potential links, and the goal is to pick enough of weight(T)
these edges
are connected. But this is not all; each link also has a maintenance e∈E ′
minimum spanning tree has a cost of
n that edge’s weight. WhatInisthe
thepreceding

cheapestexample,
possiblethe
network?
1

A

4

3

4

B

4

E

C
4
2

D

6

A

1


E

C
4

5

F

B

4

2

5

D

F

However,
is notcannot
the onlycontain
optimalasolution.
observation is that the optimal
set this
of edges
cycle, Can you spot anothe

ng an edge from this cycle would reduce the cost without compro5.1.1 A greedy approach
vity: Đây có phải lời giải
tối ưu khơng?

Kruskal’s minimum spanning tree algorithm starts with the empty g

selects
edges from
E according to the following rule.
moving a cycle edge cannot
disconnect
a graph.

Repeatedly add the next lightest edge that doesn’t produce a cycl

must be connected and acyclic: undirected graphs of this kind are
words,
it constructs
treeweight,
edge by edge and, apart from
e particular tree we wantInisother
the one
with
minimumthe
total
avoid
simply
picks whichever edge is cheapest at the moment.
inimum spanning tree. Here
is cycles,

its formal
definition.

CuuDuongThanCong.com

algorithm:
every decision it makes is the one with the most obvi
/>7 / 64


dges are potential links, and the goal is to pick enough o
toán Kruskal
esThuật
are connected.
But this is not all; each link also has a
d in that edge’s weight. What is the cheapest possible ne
1

A

4

3

4

B

4


E

C
4
2

D

6

5

F

ate observation is that the optimal set of edges cannot co
với đồ thị rỗng và chọn cạnh từ E theo quy tắc sau.
ovingBắtanđầuedge
from this cycle would reduce the cost wit
Lặp lại việc thêm cạnh nhỏ nhất mà không tạo thành
ectivity: chu trình.
Removing a cycle edge
cannot disconnect a graph.
/>
CuuDuongThanCong.com

8 / 64


Ví dụ: Thuật tốn Kruskal


CuuDuongThanCong.com

1
7
8

2

5

3

7
9

5
15

4

5
9

6
8

6

11


7

Hình: Nguồn: tikz examples

/>
9 / 64


Nhát
130

cắt

5.1

FigureĐịnh
5.2 nghĩa
T ∪ {e}. The addition of e (dotted) to T (solid lines) produces a
cycle. Xét
Thisđồ
cycle
contain
at least
one
edge,
shown
as e′ , across
thị must
G = (V,
E). Một

nhát
cắtother
là một
cách
chia here
tập đỉnh
the cutthành
(S, Vhai
− S).
nhóm: S và V − S.

S

V −S

e

e

Hình: Nhát cắt và các cạnh nối giữa hai phân hoạch.
The correctness of Kruskal’s method follows from a certain cut property, which
is general enough to also justify a whole slew of other minimum spanning tree
CuuDuongThanCong.com
/>10 / 64
algorithms.


B

1


D

Tính chất Cắt

Algorithms
131 E
(b) phần của mộtA MST
Giả sử các cạnh X là một
nào C
đó của
G = (V, E). Chọn một tập đỉnh bất kỳ S sao cho khơng có cạnh
nào at
của
X nối(a)
giữa
và VEdges
− S, vàX:
xét e là(b)
cạnh
property
work.
AnS undirected
graph.
SetcóX trọng
has số nhỏ
nhất
nối
hai
phân

hoạch
này.
Khi
đó,
X

{e}

một
phần củaFmột
art of the MST T on the right. (c) If S = {A,
B B, C , D},Dthen
nàoacross
đó. the cut (S, V − S) is e = {D, E }. X ∪ {e}
weightMST
edges

own on the right.

1

A
2

2

B

1


C
2

D

(c)
3
3
4

A

E

e

The
1 cut:
B

F

E

C

F

D


V −S

S
C

CuuDuongThanCong.com

A

E

C

E

MST T :
/>′

11 / 64


A

E

C

Ví dụ
Edges X:
D


A

F
B

The cut: e
D

E

C

e
B F

D

S

V −S

F

A

T:

F


D

A E

C

E
MST

C

B

E

C
MST T :

A

D

E

C

F

B


D

F

AC

CE

E

B

DF

F

MST T :

MST T :
B

D

V −S

Nhát cắt S và V − S và một cây bao trùm nhỏ nhất.

e and e′ cross between S and V − S, and e is specifically the lightest edge



ype.
Therefore
w(e)
) ≤ weight(T).
Since
T isofan MS
ween
S and V
−≤
S,w(e
and),′eand
is weight(T
specifically
the lightest
edge

ust be the case
that weight(T )′ = weight(T) and that T is also an MST.


e) ≤ w(e ), and weight(T ) ≤ weight(T). Since T is an MST,
re
5.3 shows ′an
of />theand
cut property.
Which
is e′ ?
tCuuDuongThanCong.com
weight(T
) =example

weight(T)
that T ′ is
also edge
an MST.
12 / 64


Figure
5.2minh
T ∪ {e}.
The chất
addition
of e (dotted) to T (solid lines) produces a
Chứng
Tính
Cắt
cycle. This cycle must contain at least one other edge, shown here as e′ , across
the cut (S, V − S).

S

V −S

e

e

Xét X là một phần của MST T; nếu cạnh e cũng là một phần của

The correctness

Kruskal’s
method follows from a certain cut property, which
T thì Tính of
chất
Cắt đúng.
is general enough to also justify a whole slew of other minimum spanning tree
algorithms.

5.1.2 The cut property

CuuDuongThanCong.com

/>
13 / 64


Figure 5.2 T ∪ {e}. The addition of e (dotted) to T (solid lines) produces a
Chứng
Tính
chấtatCắt
cycle.
This minh
cycle must
contain
least (2)
one other edge, shown here as e′ , across
the cut (S, V − S).

S


V −S

e

e

Giả sử e không thuộc MST T. Xét T ∪ {e}.

The correctness of Kruskal’s method follows from a certain cut property, which
Việc thêm
cạnhjustify
e vào aT whole
sẽ tạo slew
ra một
is general enough
to also
of chu
othertrình.
minimum spanning tree
algorithms.Chu trình này chứa ít nhất một cạnh e′ khác đi qua nhát cắt.

5.1.2 The cut property

/>14 / 64
Say that in the process of building
a minimum spanning tree (MST), we have already

CuuDuongThanCong.com



cycle. This cycle must contain at least one other edge, shown here as e′ , across
the
cut (S, minh
V − S).Tính chất Cắt (3)
Chứng

S

V −S

e

e

Xét đồ thị T′ = (T ∪ {e}) − {e′ }.

The correctness
of Kruskal’s method follows from a certain cut property, which
T′ là một
sao?a whole slew of other minimum spanning tree
is general enough
to cây.
also Tại
justify
algorithms.

G = (V, E) là một cây nếu và chỉ nếu G liên thông và
|E| = |V| − 1;

5.1.2 The cut property


Say that in the process of building a minimum spanning tree (MST), we have already
15 /add
64
chosen some edges and are />so far on the right track. Which edge should we

CuuDuongThanCong.com


cycle. This cycle must contain at least one other edge, shown here as e′ , across
the
cut (S, minh
V − S).Tính chất Cắt (3)
Chứng

S

V −S

e

e

Xét đồ thị T′ = T ∪ {e} − {e′ }.

The correctness
of Kruskal’s method follows from a certain cut property, which
T′ là một
is general enough
to cây.

also justify a whole slew of other minimum spanning tree

algorithms.Cây T cũng là cây bao trùm nhỏ nhất vì:

) = weight(T) + w(e) − w(e′ ) và w(e) ≤ w(e′ ).
5.1.2 The weight(T
cut property

Say that in the process of building a minimum spanning tree (MST), we have already
16 /add
64
chosen some edges and are />so far on the right track. Which edge should we

CuuDuongThanCong.com


uppose you are asked to network a collection of computers by linking select
airs
of them.
This
translates
into a graph
in which nodes are computer
Tính
đúng
đắn
của Thuật
toánproblem
Kruskal?
ndirected edges are potential links, and the goal is to pick enough of these edg

hat the nodes are connected. But this is not all; each link also has a maintenan
ost, reflected in that edge’s weight. What is the cheapest possible network?
1

A
3

4

B

4

E

C
4

4
2

D

6

5

F

One immediate observation is that the optimal set of edges cannot contain a cyc

ecause removing an edge from this cycle would reduce the cost without compr
Bắt đầu với đồ thị rỗng và chọn cạnh từ E theo quy tắc sau.
mising connectivity:
Lặp lại việc thêm cạnh nhỏ nhất mà không tạo thành
trình. a cycle edge cannot disconnect a graph.
Property 1 chu
Removing

o the solution must be connected and acyclic: undirected graphs of this kind a
alled trees. The particular tree we want is the one with minimum total weigh
nown
as the minimum spanning
tree. Here is its formal definition.
CuuDuongThanCong.com
/>17 / 64


Cài đặt thuật toán Kruskal

Sử dụng cấu trúc dữ liệu disjoint sets: mỗi tập là một thành phần
liên thông.
Disjoint sets có ba phép tốn:
makeset(x): tạo ra một tập chỉ chứa phần tử x.
find(x): x thuộc tập nào?
union(x, y): hợp hai tập chứa x và y.

CuuDuongThanCong.com

/>
18 / 64



procedure kruskal(G, w)
Input: đồ thị liên thông vô hướng G = (V, E);
với trọng số cạnh we
Output: MST định nghĩa bởi tập cạnh X.
for all u ∈ V:
makeset(u)
X=∅
Sắp xếp các cạnh e theo trọng số
for all {u, v} ∈ E, lấy không giảm theo trọng số:
if find(u) ̸= find(v):
thêm cạnh {u, v} vào X
union(u, v)

CuuDuongThanCong.com

/>
19 / 64


Cấu trúc dữ liệu Disjoint sets

Lưu trữ tập dùng cây có hướng.
Các nút là các phần tử của tập.
Mỗi nút x có một con trỏ tới nút cha π(x) của nó.
Ngồi ra mỗi nút có một rank để lưu trữ độ cao của cây con
từ nút này.
Phần tử ở gốc là đại diện, hoặc là tên, của tập.
Cha của gốc là chính nó.


CuuDuongThanCong.com

/>
20 / 64


anged in no particular order, and each has parent pointers that eventually le
of the tree. This root element is a convenient representative, or name, for th
guished from the other elements by the fact that its parent pointer is a self-l

Ví dụ

5.5 A directed-tree
of {B,
twoE}
sets
Cây có hướngrepresentation
biểu diễn hai tập
và{B,
{A,E}
C,and
D, F,{A,
G,C,
H}D, F, G, H}.

CuuDuongThanCong.com

H


E

B

C

/>
D

F

G

A

21 / 64


Cài đặt Disjoint sets

procedure markset(x)
π(x) = x
rank(x) = 0
function find(x)
while x ̸= π(x): x = π(x)
return x
Ở đây, π(x) chỉ đến cha của x.

CuuDuongThanCong.com


/>
22 / 64


procedure union(x, y)
rx = find(x)
ry = find(y)
if rx = ry : return
if rank(rx ) > rank(ry ): π(ry ) = rx
else:
π(rx ) = ry
if rank(rx ) = rank(ry ): rank(ry ) = rank(rx ) + 1

CuuDuongThanCong.com

/>
23 / 64


Bài tập
Hãy vẽ cây biểu diễn disjoint sets sau các phép toán sau:
makeset(A), makeset(B),..., makeset(G)
union(A, D), union(B, E), union(C, F)
union(C, G), union(E, A)
union(B, G)

CuuDuongThanCong.com

/>
24 / 64



Tính chất của union-find

Tính chất
Với mọi x, ta ln có rank(x) < rank(π(x)).

Tính chất
Mọi nút gốc r với rank k có ít nhất 2k nút trong cây của nó.

Tính chất
Nếu có n phần tử, có nhiều nhất n/2k nút có rank k.

CuuDuongThanCong.com

/>
25 / 64


×