




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、2022-3-2212022-3-222Decrease and Conquer(V)Chapter 5l The application of DFSl Breadth-First Search Algorithm (BFS)2022-3-223Goals of this lecturebAt the end of this lecture, you should : Master topological sort, know how to find the topological order of a graph (if exist) Master the idea of breadth-
2、first search algorithm, analyze its time complexity, solve other real problems by BFS Master the comparision between DFS and BFS2022-3-224DFS and Topological Sort bA topological sort of a directed acyclic graph G = (V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v
3、), then u appears before v in the ordering.132421342022-3-225Applications of Topological SortbA general life-related applicationdressing orderGraph AlgorithmsData StructureJava LanguageAdvanced MathematicsGraph TheorybAssembly lines in industriesbCourses arrangement in schools2022-3-226The Algorithm
4、 Time Complexity Analysis: Line 1: (|V| +|E|) Line 2: (|V|) Line 3: (1)TopologicalSort(G)/Input: A directed acyclic graph G = (V, E)/Output: A topological order of all the vertices of G1 call DFS(G) to compute finishing times postnu for each vertex u2 as each vertex is finished, insert it into the f
5、ront of a linked list3 return the linked list of vertices2022-3-227Getting DressedUnderwearSocksShoesPantsBeltShirtWatchTieJacket2022-3-228Getting DressedUnderwearSocksShoesPantsBeltShirtWatchTieJacket123456789Socks9/9Underwear6/8Pants7/7Shoes8/6Watch5/5Shirt1/4Belt4/3Tie2/2Jacket3/1/4/2/1/3/5/8/7/6
6、/92022-3-229bNote: We only introduce BFS for undirected graphs. But it also works for directed graphs. Archetype for Prims minimum-spanning-tree algorithm Archetype for Dijkstras single-source shortest-paths algorithmBreadth-first Search (outline)wThe idea of BFSwThe single source shortest-paths pro
7、blem for unweighted graphwThe Breadth-first search algorithmwThe running time of BFS algorithm2022-3-2210The Breadth-First SearchbThe idea of the BFS:Visit the vertices as follows:w Visit all vertices directly connected to starting vertexw Visit all vertices that are two edges away from the starting
8、 vertexw Visit all vertices that are three edges away from the starting vertex1. 2022-3-2211The Single Source Shortest-paths ProblembThe problem: Input: A graph G = (V, E) and a source vertex sV Output: A shortest path from s to each vertex v V and the distance dv Where distance dv: The length of th
9、e shortest path from s to v. For example dc=2. Define ds=0ebdsac2022-3-2212What does the BFS do?bGiven a graph G = (V, E), the BFS returns: The shortest distance dv from s to v The predecessor or parent v, which is used to derive a shortest path from s to vertex v.bIn addition to the two arrays dv a
10、nd v, BFS also uses another array colorv, which has three possible values: WHITE: represented “undiscovered” vertices; GRAY: represented “discovered” but not “processed” vertices; BLACK: represented “processed” vertices. 2022-3-2213The Breadth-First Search AlgorithmbG is given by its adjacency-lists
11、.bInitialization: First Part: lines 1 4 Second Part: lines 5 - 9bMain Part: lines 10 18bEnqueue(Q, v): add a vertex v to the end of the queue QbDequeue(Q): Extract the first vertex in the queue Q 2022-3-2214Example of Breadth-First Searchbgiven the undirected graph below and the source vertex s, fin
12、d the distance dv from s to each vertex v V, and the predecessor v by the algorithm described earlier.bsdcafe2022-3-2215Example (Continued)bInitializationvertex u s a b c d e fcoloruduu GWWWWWW 0 NILNIL NILNILNILNILNILQ = (put s into Q (discovered),mark s gray (unprocessed)bsdcafe02022-3-2216Example
13、 (continued)bWhile loop, first iteration Dequeue s from Q. Find adjs= Mark b, e as “G” Update db, de, b, e Put b, e into Q Mark s as “B”bQ= bsdcafe011vertex u s a b c d e fcoloruduu GWWWWWW 0 1 1 NILNIL sNILNIL sNIL2022-3-2217Example (continued)bWhile loop, second iteration Dequeque b from Q, find a
14、djb= Mark a, c, f as “G”, Update da, dc, df, a, c, f Put a, c, f into Q Mark b as “B”bQ=2bsdcafe01122vertex u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 1 2NIL b s bNIL s b2022-3-2218Example (continued)bWhile loop, third iteration Dequeque e from Q, find adje= Mark d as “G”, mark e as “B” Update dd, d,
15、Put d into Q bQ=bsdcafe0112222vertex u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2219Example (continued)bWhile loop, fourth iteration Dequeque a from Q, find adja= mark a as “B”bQ=bsdcafe0112222vertex u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2220Exa
16、mple (continued)bWhile loop, fifth iteration Dequeque c from Q, find adjc= mark c as “B”bQ=bsdcafe0112222vertex u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2221Example (continued)bWhile loop, sixth iteration Dequeque f from Q, find adjf= mark f as “B”bQ=22bsdcafe0112222vertex
17、 u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2222Example (continued)bWhile loop, seventh iteration Dequeque d from Q, find adjd= mark d as “B”bQ is empty22bsdcafe0112222vertex u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2223Example (continued)bWhile lo
18、op, eighth iterationbSince Q is empty, stop22bsdcafe0112222vertex u s a b c d e fcoloruduu GWWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2224Example (continued)Question:How do you construct a shortestpath from s to any vertexby using the following table?22bsdcafe0112222vertex u s a b c d e fcoloruduu G
19、WWWWWW 0 2 1 2 2 1 2NIL b s b e s b2022-3-2225bGiven a vertex v, the procedure Print-Path(G, s, v) prints a path from s to v.Print-Path(G,s,v)/ print the shortest path from s to vif v=s print selse if v = Nil print “no path from s “to” v existselse Print-Path(G, s, v) print vThe Answer2022-3-2226Ana
20、lysis of the BFS Algorithmu = every vertex, but only once (Why?)3(|V|-1)53|V|+266deg(u)+2(6deg( )2)u Vu2022-3-2227bHence, T(V, E) = O(|E|).( ,)(3| 2)(6deg( )2)u VT V EVu( ,)(3| 2)(12| 2|)5| 12| 2(|)T V EVEVVEO VE Analysis of the BFS Algorithm (cont.)bHence the total amount of time needed for process
21、ing all the vertices is at most bSo, we get: bRemark: since G is connected, we have |E|=|V|-1.2022-3-2228bAn old world puzzle: A peasant finds himself on a riverbank with a wolf, a goat and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room for only the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage. S
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 論文課題申報書
- 音樂陶笛課題立項申報書
- 建黨精神課題申報書
- 古琴課題申報書
- 快樂讀書吧課題申報書
- 賣房貸款合同范本
- 咨詢費購銷合同范本
- 共享充電寶股合同范本
- 70歲以上用工合同范例
- 品牌童裝贈與合同范本
- 南充市高2025屆高三高考適應(yīng)性考試(二診)英語試卷
- 2025年黑龍江省伊春市單招職業(yè)適應(yīng)性測試題庫含答案
- 8.3 摩擦力(課件)2024-2025學(xué)年人教版八年級物理下冊
- 2025年黑龍江職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫帶答案
- 2025年湖南有色金屬職業(yè)技術(shù)學(xué)院單招職業(yè)傾向性測試題庫附答案
- 第五章產(chǎn)前檢查及高危妊娠監(jiān)測課件
- 2025年02月曲靖市師宗縣事業(yè)單位委托公開遴選工作人員(含遴選)26人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 2025年寶雞職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫及完整答案1套
- 2025年車位買賣合同模板電子版
- AI創(chuàng)作指令合集系列之-教案寫作指令
- 2024年全國中學(xué)生生物學(xué)聯(lián)賽試題含答案
評論
0/150
提交評論