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

Bài giảng toán rời rạc đường đi trên đồ thị (version 0 2) trần vĩnh đức

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

CuuDuongThanCong.com Đường đồ thị (Version 0.2) Trần Vĩnh Đức HUST Ngày 24 tháng năm 2018 https://fb.com/tailieudientucntt / 52 Tài liệu tham khảo ▶ S Dasgupta, C H Papadimitriou, and U V Vazirani, Algorithms, July 18, 2016 ▶ Chú ý: Nhiều hình vẽ tài liệu lấy tùy tiện mà chưa xin phép CuuDuongThanCong.com https://fb.com/tailieudientucntt / 52 Nội dung Khoảng cách tìm kiếm theo chiều rộng Thuật toán Dijkstra Cài đặt hàng đợi ưu tiên Đường ngắn có cạnh độ dài âm Đường ngắn DAG CuuDuongThanCong.com https://fb.com/tailieudientucntt In Figure 4.2, for example, vertex B is at distance from S, and there are two shorte DFStovàit đường điheld up, the strings along each of these paths become ta paths When S is aph and (b) its depth-first search tree Figure 4.1 (a) A simple graph and (b) its depth-first search tree (b) S (a) A B (b) E D S C A B S A D A D B E B E C C 104 Đường DFS thường đường ngắn CuuDuongThanCong.com https://fb.com/tailieudientucntt / 52 vertex s high enough, the other balls that get pulled up along with it are precisely theKhoảng vertices reachable cách from s And to find their distances from s, you need only measure how far below s they hang In Figure 4.2, for example, vertex B is at distance from S, and there are two shortest paths to it When S is held up, the strings along each of these paths become taut Khoảng cách hai đỉnh độ dài đường ngắn chúng Figure 4.1 (a) A simple graph and (b) its depth-first search tree (a) E D S C A B v A B C D E (b) Khoảng cách (S − v) S A D B E C 104 CuuDuongThanCong.com https://fb.com/tailieudientucntt / 52 Mơ hình vật lý đồ thị Algorithms 105 Algorithm Giả sử cạnh có độ dài Ta nhấc đỉnh S lên: A physical model of a graph Figure 4.2 A physical model of a graph S S S E A S A A C D B C C D EA C D E B B B On the other hand, edge (D, E ) plays no role in any shortest path and the hand, edgeslack (D, E ) plays no role in any shortest path and therefore remains k CuuDuongThanCong.com https://fb.com/tailieudientucntt / 52 E D Algorithms Tìm kiếm theo chiều rộng (Breadth-First Search) 105 A physical model of a graph Chia đồ thị thành mức: ▶ S mức có khoảng cách A S ▶ Các đỉnh có khoảng cách tới S ▶ Các đỉnh có khoảng cách tới CS B ▶ S A C D E B Ý tưởng thuật toán: Khi mức d xác định, mức d + có er hand, plays no qua rolecác in any thể edge thăm (D, bằngE )cách duyệt hàngshortest xóm củapath mức and d therefore ck adth-first search CuuDuongThanCong.com https://fb.com/tailieudientucntt 2, the lifting of s partitions the graph into layers: s itself, the nodes at7 / 52 Ý tưởng loang theo chiều rộng Khởi tạo: Hàng đợi Q chứa đỉnh s, đỉnh mức Với khoảng cách d = 1, 2, 3, , ▶ có thời điểm Q chứa đỉnh có khoảng cách d khơng có khác ▶ Khi đỉnh có khoảng cách d loại bỏ dần từ đầu hàng đợi, ▶ hàng xóm chưa thăm thêm vào cuối hàng đợi CuuDuongThanCong.com https://fb.com/tailieudientucntt / 52 n Figure 4.2, for example, vertex B is at distance from S, and there are two aths to it When S is held up, the strings along each of these paths beco Bài tập igure 4.1 (a) A simple graph and (b) its depth-first search tree (a) Chạy thuật toán BFS cho đồ thị đỉnh S Ghi hàng đợi Q sau lần thăm đỉnh (b) E D S C A B Đỉnh thăm S Hàng đợi [S] S A B C 04 CuuDuongThanCong.com https://fb.com/tailieudientucntt / 52 procedure bfs(G, s) Input: đồ thị G = (V, E), có hướng vơ hướng; đỉnh s ∈ V Output: Với đỉnh u đến từ s, dist(u) = khoảng cách từ s tới u for all u ∈ V: dist(u) = ∞ dist(s) = Q = [s] (hàng đợi chứa s) while Q khác rỗng: u = eject(Q) (loại bỏ u khỏi hàng đợi) for all edges (u, v) ∈ E: if dist(v) = ∞: inject(Q, v) (thêm v vào hàng đợi) dist(v) = dist(u) + CuuDuongThanCong.com https://fb.com/tailieudientucntt 10 / 52 vertex s high enough, the other balls that get pulled up along with it are precisely the vertices reachable from s And to find their distances from s, you need only measure how far below s they hang In Figure 4.2, for example, vertex B is at distance from S, and there are two shortest paths to it When S is held up, the strings along each of these paths become taut Bài tập Hãy chạy thuật toán BFS cho đồ thị ghi nội dung Figure 4.1 (a) graph and (b) its depth-first search tree hàng đợiAQsimple sau bước: (a) (b) E S A D C B Thứ tự thăm đỉnh S S Hàng đợi A [S] D B E C 104 CuuDuongThanCong.com https://fb.com/tailieudientucntt 11 / 52 Nội dung Khoảng cách tìm kiếm theo chiều rộng Thuật tốn Dijkstra Cài đặt hàng đợi ưu tiên Đường ngắn có cạnh độ dài âm Đường ngắn DAG CuuDuongThanCong.com https://fb.com/tailieudientucntt (each stretch of highway) is important For the remainder of this chapter, we will deal with this more general scenario, annotating every edge e ∈ E with a length le If e = (u, v), we will sometimes also write l(u, v) or luv Độ dài cạnh Figure 4.5 Edge lengths often matter San Francisco 95 Sacramento 133 Reno 271 290 409 445 Bakersfield 291 112 Los Angeles 275 Las Vegas These le ’s not have to correspond to physical lengths They could denote time (driving time between cities) or money (cost of taking a bus), or any other quantity Trong toán thực tế, cạnh e thường gắn với độ dài le that we would like to conserve In fact, there are cases in which we need to use negative lengths, but we will briefly overlook this particular complication CuuDuongThanCong.com https://fb.com/tailieudientucntt 13 / 52 Câu hỏi Liệu ta sửa thuật tốn BFS để chạy đồ thị tổng quát G = (V, E) cạnh có độ dài nguyên dương le ? CuuDuongThanCong.com https://fb.com/tailieudientucntt 14 / 52 between and v Fordummy any edgenodes e = (u, v) of E , ureplace it by le edges of length 1, by adding le − dummy nodes between u and v ′ contains all the vertices interest betwee Graph G Tách cạnh thành cạnh với Vđộthatdài đơnus,vịand the distances ′ ′ thesame vertices V G that interest us, and thethe distances Graph themGarecontains exactlyallthe as in Most importantly, edges between of G all have un them are the same as incompute G Most importantly, of G ′ allBFS haveon unit length exactly Therefore, we can distances inthe G edges by running G ′ length Therefore, we can compute distances in G by running BFS on G ′ Figure Breaking edges into unit-length Figure 4.64.6Breaking edges into unit-length pieces pieces 2 AA BB 1 C C 2 D D 32 E E A B B D D C C E E A Alarm clocks Thay cạnhclocks ewere = (u, lwe độ dài thêm leedges, −1 IfAlarm efficiency not v) an issue, could stop here.1, Butbằng when cách G has very long e cạnh the G ′ it engenders thickly populated with stop dummy nodes, theGBFS If tạm efficiency an issue, we could here But and when hasspends very long edge đỉnh were u vàisnot v most diligently distances with to these nodes nodes, that we and don’tthe careBFS spen the of G ′itsit time engenders is computing thickly populated dummy about at all most of its time diligently computing distances to these nodes that we don’t ca Toabout see this atmore all concretely, consider the graphs G and G ′ of Figure 4.7, and imagine that the BFS, started at node s of G ′ , advances by one unit of distance per minute For ′ Tofirst see99 this more itconcretely, consideralong the graphs G and of endless Figure 4.7, and imagin the minutes tediously progresses S − A and S −G B, an desert ′ the BFS, started at node of Gwe, advances one unit of distance per minute F ofthat dummy nodes Is there some sway can snoozeby through these boring phases and alarm wake us up whenever something thehave first an 99 minutes ithttps://fb.com/tailieudientucntt tediously progresses along interesting S − A andisS happening— − B, an endless dese CuuDuongThanCong.com 15 / 52 Vấn đề Algorith Algorithms 109 Figure 4.7 BFS on G ′ is mostly uneventful The dotted lines show some “wavefronts.” Figure 4.7 BFS on G ′ is mostly uneventful The dotted lines show some early “wavefronts.” G: G: S 100100 S A A 50 50 200 200 B B G: G: S A A S B More generally, at any given moment the breadth-first search is advancing along certain edges of G , and there is an alarm for every endpoint node toward which it More generally, at given moment the Cả is99moving, bước set di tochuyển tiên đềutime xửof lýarrival Sbreadth-first − at Athat node S − search B cácadvancin go any off atđầu the estimated Some of is these might be overestimates because BFS may later find shortcuts, as a result of future certain edges of G , and there is an alarm for every endpoint node toward đỉnh tạm arrivals elsewhere In off the preceding example, a quicker route to B was upon Some is moving, set to go at the estimated time of arrival atrevealed that node arrival at A However, nothing interesting can possibly happen before an alarm goes might because BFS may signal later the find shortcuts, as a result o off be Theoverestimates sounding of the next alarm must therefore arrival of the wavefront arrivals elsewhere the preceding example, a quicker to Balong was reveal to a real node u ∈ VInby BFS At that point, BFS might also startroute advancing someatnew out of u, and alarms need to be can set for their endpoints arrival A edges However, nothing interesting possibly happen before an ala The following “alarm clock algorithm” simulates the execution of BFS on off The sounding of the next alarmfaithfully must therefore signal the arrival of the w ′ https://fb.com/tailieudientucntt 16 / 52 to a real node u ∈ V by BFS At that point, BFS might also start advancin G CuuDuongThanCong.com Giải pháp: Đặt Alarm clock! Chapter Figure 4.7 BFS on G ′ is m “wavefronts.” ▶ Với đỉnh A, đặt hẹn T = 100 ▶ Với đỉnh B, đặt hẹn T = 200 ▶ Bị đánh thức A thăm lúc T = 100 ▶ Ước lượng lại thời gian đến B T = 150; đặt lại Alarm cho B CuuDuongThanCong.com https://fb.com/tailieudientucntt G: 100 S A G 50 200 B More generally, at any given certain edges of G , and there is moving, set to go off at th might be overestimates beca arrivals elsewhere In the prec arrival at A However, nothin 17 / 52 Thuật toán Alarm Clock Đặt alarm clock cho đỉnh s thời điểm T = Lặp lại khơng cịn alarm: Giả sử alarm kêu thời điểm T cho đỉnh u Vậy thì: - Khoảng cách từ s tới u T - Với hàng xóm v u G: * Nếu chưa có alarm cho v, đặt alarm cho v thời điểm T + l(u, v) * Nếu alarm v đặt, lại muộn so với T + l(u, v), đặt lại alarm cho v T + l(u, v) CuuDuongThanCong.com https://fb.com/tailieudientucntt 18 / 52 long edges into unit-length pieces by introducing “dummy” nodes Figure 4.6 shows an example of this transformation To construct the new graph G ′ , For any edge e = (u, v) of E , replace it by le edges of length 1, by adding le − dummy nodes between u and v Graph Ví dụ G ′ contains all the vertices V that interest us, and the distances between them are exactly the same as in G Most importantly, the edges of G ′ all have unit length Therefore, we can compute distances in G by running BFS on G ′ Thời gian Figure 4.6 Breaking edges into unit-length pieces A B 1 C D E A A B − 0B 2 C C − 1 D − − D − E4 Alarm clocks If efficiency were not an issue, we could stop here But when G has very long edges, the G ′ it engenders is thickly populated with dummy nodes, and the BFS spends most of its time diligently computing distances to these nodes that we don’t care about at all To see this more concretely, consider the graphs G and G ′ of Figure 4.7, and imagine that the BFS, started at node s of G ′ , advances by one unit of distance per minute For the first 99 minutes it tediously progresses along S − A and S − B, an endless desert of dummy nodes Is there some way we can snooze through these boring phases CuuDuongThanCong.com https://fb.com/tailieudientucntt E − − 5 5 19 / 52 Hàng đợi ưu tiên Tại cần hàng đợi ưu tiên? Để cài đặt hệ thống Alarm Hàng đợi ưu tiên gì? Là tập với phần tử gắn với giá trị số (còn gọi khóa) có phép tốn sau: Insert Thêm phần tử vào tập Decrease-key Giảm giá trị khóa phần tử cụ thể Delete-min Trả lại phần tử có khóa nhỏ xóa khỏi tập Make-queue xây dựng hàng đợi ưu tiên cho tập phần tử giá trị khóa cho trước Khóa phần tử (đỉnh) alarm đỉnh Insert Descrease-key để đặt alarm; Delete-min để xác định thời điểm alarm kêu CuuDuongThanCong.com https://fb.com/tailieudientucntt 20 / 52 ... = 100 ▶ Với đỉnh B, đặt hẹn T = 200 ▶ Bị đánh thức A thăm lúc T = 100 ▶ Ước lượng lại thời gian đến B T = 1 50; đặt lại Alarm cho B CuuDuongThanCong.com https://fb.com/tailieudientucntt G: 100 ... is mostly uneventful The dotted lines show some early “wavefronts.” G: G: S 100 100 S A A 50 50 200 200 B B G: G: S A A S B More generally, at any given moment the breadth-first search is advancing... 109 Figure 4.7 BFS on G ′ is mostly uneventful The dotted lines show some “wavefronts.” Figure 4.7 BFS on G ′ is mostly uneventful The dotted lines show some early “wavefronts.” G: G: S 100 100

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

Xem thêm:

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

TÀI LIỆU LIÊN QUAN