1. Trang chủ
  2. » Giáo án - Bài giảng

Bài giảng Toán rời rạc: Quy hoạch động - Trần Vĩnh Đức

61 15 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 61
Dung lượng 1,38 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Bài giảng Toán rời rạc: Quy hoạch động cung cấp cho người học những nội dung kiến thức như: Đường đi ngắn nhất trên DAG, dãy con tăng dài nhất, khoảng cách soạn thảo, bài toán cái túi, nhân nhiều ma trận, đường đi ngắn nhất, tập độc lập trên cây. Mời các bạn cùng tham khảo.

Trang 1

Quy hoạch động

Trần Vĩnh Đức

HUST

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

Trang 2

Tài liệu tham khảo

▶ S Dasgupta, C H Papadimitriou, and U V Vazirani,

Algorithms, July 18, 2006.

Trang 3

Nội dung

Đường đi ngắn nhất trên DAG

Dãy con tăng dài nhất

Khoảng cách soạn thảo

Bài toán cái túi

Nhân nhiều ma trận

Đường đi ngắn nhất

Tập độc lập trên cây

Trang 4

Đồ thị phi chu trình (DAG): Nhắc lại

Trong đồ thị phi chu trình, ta có thể sắp xếp thứ tự các đỉnh sao cho nó chỉ có cung đi đi từ trái sang phải.

Chapter 6

Dynamic programming

In the preceding chapters we have seen some elegant design principles—such as conquer, graph exploration, and greedy choice—that yield definitive algorithms for a variety

divide-and-of important computational tasks The drawback divide-and-of these tools is that they can only be used

on very specific types of problems We now turn to the two sledgehammers of the algorithms craft, dynamic programming and linear programming, techniques of very broad applicability

that can be invoked when more specialized methods fail Predictably, this generality often comes with a cost in efficiency.

6.1 Shortest paths in dags, revisited

At the conclusion of our study of shortest paths (Chapter 4), we observed that the problem is especially easy in directed acyclic graphs (dags) Let’s recapitulate this case, because it lies at the heart of dynamic programming.

The special distinguishing feature of a dag is that its nodes can be linearized; that is, they

can be arranged on a line so that all edges go from left to right (Figure 6.1) To see why this helps with shortest paths, suppose we want to figure out distances from node S to the other nodes For concreteness, let’s focus on node D The only way to get to it is through its

Figure 6.1 A dag and its linearization (topological ordering).

B

D C

1 1

2

169

Hình:Đồ thị phi chu trình G và biểu diễn dạng tuyến tính của nó.

Trang 5

Đường đi ngắn nhất trên DAG

Chapter 6

Dynamic programming

In the preceding chapters we have seen some elegant design principles—such as conquer, graph exploration, and greedy choice—that yield definitive algorithms for a variety

divide-and-of important computational tasks The drawback divide-and-of these tools is that they can only be used

on very specific types of problems We now turn to the two sledgehammers of the algorithms craft, dynamic programming and linear programming, techniques of very broad applicability

that can be invoked when more specialized methods fail Predictably, this generality often comes with a cost in efficiency.

6.1 Shortest paths in dags, revisited

At the conclusion of our study of shortest paths (Chapter 4), we observed that the problem is especially easy in directed acyclic graphs (dags) Let’s recapitulate this case, because it lies at the heart of dynamic programming.

The special distinguishing feature of a dag is that its nodes can be linearized; that is, they

can be arranged on a line so that all edges go from left to right (Figure 6.1) To see why this helps with shortest paths, suppose we want to figure out distances from node S to the other nodes For concreteness, let’s focus on node D The only way to get to it is through its

Figure 6.1 A dag and its linearization (topological ordering).

B

D C

2

169

Hình:Đồ thị phi chu trình G và biểu diễn dạng tuyến tính của nó.

Xét nút D của đồ thị, cách duy nhất để đi từ S đến D là phải qua B hoặc C.

Vậy, để tìm đường đi ngắn nhất từ S tới D ta chỉ phải so sánh

hai đường:

dist(D) = min {dist(B) + 1, dist(C) + 3}

Trang 6

Thuật toán tìm đường đi ngắn nhất cho DAG

Chapter 6

Dynamic programming

In the preceding chapters we have seen some elegant design principles—such as

divide-and-conquer, graph exploration, and greedy choice—that yield definitive algorithms for a variety

of important computational tasks The drawback of these tools is that they can only be used

on very specific types of problems We now turn to the two sledgehammers of the algorithms

craft, dynamic programming and linear programming, techniques of very broad applicability

that can be invoked when more specialized methods fail Predictably, this generality often

comes with a cost in efficiency

6.1 Shortest paths in dags, revisited

At the conclusion of our study of shortest paths (Chapter 4), we observed that the problem is

especially easy in directed acyclic graphs (dags) Let’s recapitulate this case, because it lies at

the heart of dynamic programming

The special distinguishing feature of a dag is that its nodes can be linearized; that is, they

can be arranged on a line so that all edges go from left to right (Figure 6.1) To see why

this helps with shortest paths, suppose we want to figure out distances from node S to the

other nodes For concreteness, let’s focus on node D The only way to get to it is through its

Figure 6.1 A dag and its linearization (topological ordering).

B

D C

1 1

dist(v) = min(u,v) ∈E{dist(u) + ℓ(u, v)}

Trang 7

Chapter 6

Dynamic programming

In the preceding chapters we have seen some elegant design principles—such as

divide-and-conquer, graph exploration, and greedy choice—that yield definitive algorithms for a variety

of important computational tasks The drawback of these tools is that they can only be used

on very specific types of problems We now turn to the two sledgehammers of the algorithms

craft, dynamic programming and linear programming, techniques of very broad applicability

that can be invoked when more specialized methods fail Predictably, this generality often

comes with a cost in efficiency

6.1 Shortest paths in dags, revisited

At the conclusion of our study of shortest paths (Chapter 4), we observed that the problem is

especially easy in directed acyclic graphs (dags) Let’s recapitulate this case, because it lies at

the heart of dynamic programming

The special distinguishing feature of a dag is that its nodes can be linearized; that is, they

can be arranged on a line so that all edges go from left to right (Figure 6.1) To see why

this helps with shortest paths, suppose we want to figure out distances from node S to the

other nodes For concreteness, let’s focus on node D The only way to get to it is through its

Figure 6.1 A dag and its linearization (topological ordering).

B

D C

1 1

Trang 8

Ý tưởng quy hoạch động

Chapter 6

Dynamic programming

In the preceding chapters we have seen some elegant design principles—such as

divide-and-conquer, graph exploration, and greedy choice—that yield definitive algorithms for a variety

of important computational tasks The drawback of these tools is that they can only be used

on very specific types of problems We now turn to the two sledgehammers of the algorithms

craft, dynamic programming and linear programming, techniques of very broad applicability

that can be invoked when more specialized methods fail Predictably, this generality often

comes with a cost in efficiency

6.1 Shortest paths in dags, revisited

At the conclusion of our study of shortest paths (Chapter 4), we observed that the problem is

especially easy in directed acyclic graphs (dags) Let’s recapitulate this case, because it lies at

the heart of dynamic programming

The special distinguishing feature of a dag is that its nodes can be linearized; that is, they

can be arranged on a line so that all edges go from left to right (Figure 6.1) To see why

this helps with shortest paths, suppose we want to figure out distances from node S to the

other nodes For concreteness, let’s focus on node D The only way to get to it is through its

Figure 6.1 A dag and its linearization (topological ordering).

B

D C

1 1

2

169Hình:Để giải bài toán D ta cần giải bài toán con C và B.

Quy hoạch động là kỹ thuật giải bài toán bằng cách xác định một tập các bài toán con và giải từng bài toán con một, nhỏ nhất trước,

▶ dùng câu trả lời của bài toán nhỏ để hình dung ra đáp án của bài toán lớn hơn ,

Trang 9

Nội dung

Đường đi ngắn nhất trên DAG

Dãy con tăng dài nhất

Khoảng cách soạn thảo

Bài toán cái túi

Nhân nhiều ma trận

Đường đi ngắn nhất

Tập độc lập trên cây

Trang 10

Bài toán dãy con tăng dài nhất

Cho dãy số a1, a2, , an Một dãy con là một tập con các số lấy theo thứ tự, nó có dạng

ai1, ai2, , aik

ở đó 1 ≤ i1 < i2 < · · · < ik ≤ n, và dãy tăng là dãy mà các phần

tử tăng dần Nhiệm vụ của bạn là tìm dãy tăng có số phần tử

nhiều nhất.

Ví dụ

Dãy con dài nhất của dãy 5, 2, 8, 6, 3, 6, 9, 7 là:

Trang 11

Bài tập

Hãy tìm dãy con tăng dài nhất của dãy

5, 3, 8, 6, 2, 6, 9, 7.

Trang 12

DAG của dãy tăngS Dasgupta, C.H Papadimitriou, and U.V Vazirani 171

Figure 6.2 The dag of increasing subsequences.

Notice that (1) this graph G = (V, E) is a dag, since all edges (i, j) have i < j, and (2) there is a one-to-one correspondence between increasing subsequences and paths in this dag Therefore, our goal is simply to find the longest path in the dag!

Here is the algorithm:

for j = 1, 2, , n:

L(j) = 1 + max{L(i) : (i, j) ∈ E}

return maxjL(j)

L(j) is the length of the longest path—the longest increasing subsequence—ending at j (plus

1, since strictly speaking we need to count nodes on the path, not edges) By reasoning in the same way as we did for shortest paths, we see that any path to node j must pass through one

of its predecessors, and therefore L(j) is 1 plus the maximum L(·) value of these predecessors.

If there are no edges into j, we take the maximum over the empty set, zero And the final

answer is the largest L(j), since any ending position is allowed.

This is dynamic programming In order to solve our original problem, we have defined a collection of subproblems {L(j) : 1 ≤ j ≤ n} with the following key property that allows them

to be solved in a single pass:

(*) There is an ordering on the subproblems, and a relation that shows how to solve

a subproblem given the answers to “smaller” subproblems, that is, subproblems

that appear earlier in the ordering.

In our case, each subproblem is solved using the relation

Hình:DAG G = (V, E) của dãy 5, 2, 8, 6, 3, 6, 9, 7.

Ta xây dựng DAG G = (V, E) cho dãy a1, a2, , an như sau:

Trang 13

Tìm đường đi dài nhất trên DAG

Trang 14

Bài tập

Hãy tìm đường đi dài nhất trên DAG sau:

Figure 6.2 The dag of increasing subsequences.

that is, whenever i < j and ai< aj(Figure 6.2)

Notice that (1) this graph G = (V, E) is a dag, since all edges (i, j) have i < j, and (2)there is a one-to-one correspondence between increasing subsequences and paths in this dag

Therefore, our goal is simply to find the longest path in the dag!

Here is the algorithm:

for j = 1, 2, , n:

L(j) = 1 + max{L(i) : (i, j) ∈ E}

return maxjL(j)

L(j)is the length of the longest path—the longest increasing subsequence—ending at j (plus

1, since strictly speaking we need to count nodes on the path, not edges) By reasoning in thesame way as we did for shortest paths, we see that any path to node j must pass through one

of its predecessors, and therefore L(j) is 1 plus the maximum L(·) value of these predecessors

If there are no edges into j, we take the maximum over the empty set, zero And the final

answer is the largest L(j), since any ending position is allowed.

This is dynamic programming In order to solve our original problem, we have defined acollection of subproblems {L(j) : 1 ≤ j ≤ n} with the following key property that allows them

to be solved in a single pass:

(*) There is an ordering on the subproblems, and a relation that shows how to solve

a subproblem given the answers to “smaller” subproblems, that is, subproblemsthat appear earlier in the ordering

In our case, each subproblem is solved using the relation

14 / 61 CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 15

Tìm đường đi dài nhất

Figure 6.2 The dag of increasing subsequences.

Notice that (1) this graph G = (V, E) is a dag, since all edges (i, j) have i < j, and (2) there is a one-to-one correspondence between increasing subsequences and paths in this dag Therefore, our goal is simply to find the longest path in the dag!

Here is the algorithm:

for j = 1, 2, , n:

L(j) = 1 + max {L(i) : (i, j) ∈ E}

return maxjL(j)

L(j) is the length of the longest path—the longest increasing subsequence—ending at j (plus

1, since strictly speaking we need to count nodes on the path, not edges) By reasoning in the same way as we did for shortest paths, we see that any path to node j must pass through one

of its predecessors, and therefore L(j) is 1 plus the maximum L(·) value of these predecessors.

If there are no edges into j, we take the maximum over the empty set, zero And the final

answer is the largest L(j), since any ending position is allowed.

This is dynamic programming In order to solve our original problem, we have defined a collection of subproblems {L(j) : 1 ≤ j ≤ n} with the following key property that allows them

to be solved in a single pass:

(*) There is an ordering on the subproblems, and a relation that shows how to solve

a subproblem given the answers to “smaller” subproblems, that is, subproblems

that appear earlier in the ordering.

In our case, each subproblem is solved using the relation

L(j) = 1 + max{L(i) : (i, j) ∈ E},

Làm thế nào tìm được đường đi dài nhất từ các L-giá trị?

Ta quản lý các cạnh trên đường đi bởi con trỏ ngược prev(j)

giống như trong tìm đường đi ngắn nhất.

▶ Dãy tối ưu có thể tìm được theo con trỏ ngược này.

15 / 61 CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 16

Ta có nên dùng đệ quy?

Có nên dùng đệ quy để tính công thức sau?

L(j) = 1 + max {L(i) : (i, j) ∈ E}

Trang 17

Nội dung

Đường đi ngắn nhất trên DAG Dãy con tăng dài nhất

Khoảng cách soạn thảo

Bài toán cái túi

Nhân nhiều ma trận

Đường đi ngắn nhất

Tập độc lập trên cây

Trang 18

Khoảng cách soạn thảo

▶ Khi chương trình kiểm tra chính tả bắt gặp lỗi chính tả, nó sẽ tìm trong từ điển một từ gần với từ này nhất.

▶ Ký hiệu thích hợp cho khái niệm gần trong trường hợp này là gì?

Trang 19

Khoảng cách soạn thảo

Trang 20

Lời giải quy hoạch động

Câu hỏi

Bài toán con là gì?

Để tìm khoảng cách soạn thảo giữa hai xâu x[1 m] và

Trang 21

Ví dụ

Hình:Bài toán con E(7, 5)

Để giải bài toán E(8, 6), ta xem xét các khả năng gióng hàng của hai ký tự ở vị trí x[8] và y[6]:

Trang 22

Bài toán con

Làm thế nào để tính E(i, j) từ các bài toán con?

Làm thế nào để gióng hàng x[1 i] và y[1 j]?

▶ Việc gióng hàng ở cột phải nhất có thể chia làm ba trường

ở đó diff(i, j) = 0 nếu x[i] = y[j] và 1 nếu ngược lại.

Trang 23

Đưa về bài toán nhỏ hơn

ở đó diff(i, j) = 0 nếu x[i] = y[j] và 1 nếu ngược lại.

Trang 24

Thuật toán quy hoạch động

Trang 26

Ví dụ

E(i, j) = min {1+E(i−1, j), 1+E(i, j−1), diff(i, j)+E(i−1, j−1)}

ở đó diff(i, j) = 0 nếu x[i] = y[j] và 1 nếu ngược lại.

Trang 27

DAG của bài toán & đường đi độ dài 6

Trang 28

DAG của bài toán

Xây dựng đồ thị G = (V, E) với

tập đỉnh ứng với các vị trí (i, j) trong bảng,

▶ các cung

(i−1, j) → (i, j), (i, j−1) → (i, j), (i−1, j−1) → (i, j)

đều có trọng số 1, ngoại trừ cung

{(i − 1, j − 1) → (i, j) : x[i] = y[j]} có trọng số 0.

Ta cần tìm đường đi ngắn nhất từ đỉnh s = (0, 0) tới t = (m, n).

Trên đường đi này: sang phải (thêm), đi xuống (xóa), đi chéo (thay thế).

Trang 29

Số lượng các bài toán con

Đầu vào x1, x2, , xn và một bài toán con là x1, x2, , xi.

Số bài toán con là tuyến tính.

Đầu vào x1, x2, , xn và y1, y2, , ym bài toán con là

x1, x2, , xi và y1, y2, , yj Số bài toán con là O(mn).

Đầu vào x1, x2, , xn và bài toán con là xi, xi+1, , xj Số

bài toán con là O(n2).

Trang 30

Số lượng các bài toán con (2)

The number of subproblems is therefore linear

ii The input is x1, , xn, and y1, , ym A subproblem is x1, , xiand y1, , yj

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10

y1 y2 y3 y4 y5 y6 y7 y8

The number of subproblems is O(mn)

iii The input is x1, , xnand a subproblem is xi, xi+1, , xj

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10

The number of subproblems is O(n2)

iv The input is a rooted tree A subproblem is a rooted subtree

If the tree has n nodes, how many subproblems are there?

We’ve already encountered the first two cases, and the others are coming up shortly.▶ Đầu vào là một cây có gốc Một bài toán con là một cây con có gốc.

Nếu cây có n nút thì có thể có bao nhiêu bài toán con?

Ngày đăng: 16/07/2021, 09:44

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w