1. Trang chủ
  2. » Tất cả

Bài giảng toán rời rạc thuật toán tham lam trần vĩnh đức

20 2 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

Thuật toán tham lam Trần Vĩnh Đức HUST Ngày 1 tháng 9 năm 2019 1 / 64CuuDuongThanCong com https //fb com/tailieudientucntt http //cuuduongthancong com?src=pdf https //fb com/tailieudientucntt Tài liệu[.]

CuuDuongThanCong.com Thuật toán tham lam Trần Vĩnh Đức HUST Ngày tháng năm 2019 https://fb.com/tailieudientucntt / 64 Tài liệu tham khảo I S Dasgupta, C H Papadimitriou, and U V Vazirani, Algorithms, July 18, 2006 CuuDuongThanCong.com https://fb.com/tailieudientucntt / 64 Nội dung Cây bao trùm nhỏ Mã hóa Huffman Cơng thức Horn Phủ tập CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 A 4 B E C D F mediateIobservation is that the optimal set of edges cannot contain Bạn cần xây dựng mạng máy tính cách kết nối cặp e removingmáy an edge from this cycle would reduce the cost without connectivity: I Cần chọn số kết nối để mạng liên thông; y1 I tất cặp: Mỗi kết nối tốn chi phí Removing a cycle edge cannot disconnect a graph (tiền bảo trì) Mạngbe vớiconnected chi phí nhỏ gì? solutionImust and làacyclic: undirected graphs of this CuuDuongThanCong.com https://fb.com/tailieudientucntt rees The particular tree we want is the one with minimum tota / 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 A 4 B E C D 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 chu trình khơng làm reduce tính liên the thơng củawithout đồ connectivity: thị y Vậy, Removing cycle cannot disconnect mạng vớiachi phí edge nhỏ phải 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 https://fb.com/tailieudientucntt as the minimum spanning tree Here is its formal definition / 64 Bài toán Cây bao trùm nhỏ (Minimal Spaning Tree) I Input: Đồ thị vơ hướng G = (V, E); cạnh có trọng số we I Output: Một T = (V, E′ ) với E′ ⊆ E, với tổng trọng số weight(T) = ∑ we e∈E′ nhỏ CuuDuongThanCong.com https://fb.com/tailieudientucntt / 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? A 4 B E C D A E C F B 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 https://fb.com/tailieudientucntt / 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 A 4 B E C D F ate observation is that the optimal set of edges cannot co với đồ thị rỗng 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ỏ mà không tạo thành ectivity: chu trình Removing a cycle edge cannot disconnect a graph https://fb.com/tailieudientucntt CuuDuongThanCong.com / 64 Ví dụ: Thuật toán Kruskal CuuDuongThanCong.com 15 11 Hình: Nguồn: tikz examples https://fb.com/tailieudientucntt / 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 cách chia here tập đỉnh the cutthành (S, Vhai − S) nhóm: S V − S S V −S e e Hình: Nhát cắt cạnh nối 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 https://fb.com/tailieudientucntt 10 / 64 algorithms B D Tính chất Cắt Algorithms 131 E (b) phần mộtA MST Giả sử cạnh X C G = (V, E) Chọn tập đỉnh S cho cạnh at X nối(a) VEdges − S, vàX: xét e là(b) cạnh property work AnS undirected graph SetcóX trọng has số nhỏ nối hai phân hoạch Khi đó, X ∪ {e} 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 A 2 B C D (c) 3 A E e The cut: B F E C F D V −S S C CuuDuongThanCong.com A E C E MST T : https://fb.com/tailieudientucntt ′ 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 − S bao trùm nhỏ 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 ofhttps://fb.com/tailieudientucntt 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 phần MST T; cạnh e phần The correctness Kruskal’s method follows from a certain cut property, which T Tính of chất Cắt is general enough to also justify a whole slew of other minimum spanning tree algorithms 5.1.2 The cut property CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 I 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 I Việc thêm cạnh e vào T tạo chu trình is general enough to also justify a whole slew of other minimum spanning tree I Chu trình chứa cạnh e′ khác qua nhát cắt algorithms 5.1.2 The cut property https://fb.com/tailieudientucntt 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 I Xét đồ thị T′ = (T ∪ {e}) − {e′ } The correctness of Kruskal’s method follows from a certain cut property, which I T′ Tại sao? is general enough to also justify a whole slew of other minimum spanning tree algorithms G = (V, E) G liên thông |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 https://fb.com/tailieudientucntt 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 I Xét đồ thị T′ = T ∪ {e} − {e′ } The correctness of Kruskal’s method follows from a certain cut property, which I T′ is general enough to also justify a whole slew of other minimum spanning tree I Cây T′ bao trùm nhỏ vì: algorithms ′ ) = weight(T) + w(e) − w(e′ ) 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 https://fb.com/tailieudientucntt 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 đắn Thuật tố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? A B E C 4 D 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 chọn cạnh từ E theo quy tắc sau mising connectivity: Lặp lại việc thêm cạnh nhỏ mà khơng tạo thành trình a cycle edge cannot disconnect a graph Property 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 https://fb.com/tailieudientucntt 17 / 64 Cài đặt thuật toán Kruskal Sử dụng cấu trúc liệu disjoint sets: tập thành phần liên thông Disjoint sets có ba phép tốn: I makeset(x): tạo tập chứa phần tử x I find(x): x thuộc tập nào? I union(x, y): hợp hai tập chứa x y CuuDuongThanCong.com https://fb.com/tailieudientucntt 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 tập cạnh X for all u ∈ V: makeset(u) X=∅ Sắp xếp 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 https://fb.com/tailieudientucntt 19 / 64 Cấu trúc liệu Disjoint sets I Lưu trữ tập dùng có hướng I Các nút phần tử tập I Mỗi nút x có trỏ tới nút cha π(x) I Ngồi nút có rank để lưu trữ độ cao từ nút I Phần tử gốc đại diện, tên, tập I Cha gốc CuuDuongThanCong.com https://fb.com/tailieudientucntt 20 / 64 ... https://fb.com/tailieudientucntt 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... https://fb.com/tailieudientucntt as the minimum spanning tree Here is its formal definition / 64 Bài toán Cây bao trùm nhỏ (Minimal Spaning Tree) I Input: Đồ thị vơ hướng G = (V, E); cạnh có trọng... https://fb.com/tailieudientucntt / 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

Ngày đăng: 27/02/2023, 07:58

Xem thêm: