圖的深度優(yōu)先遍歷
《圖的深度優(yōu)先遍歷》由會(huì)員分享,可在線閱讀,更多相關(guān)《圖的深度優(yōu)先遍歷(54頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、單擊此處編輯母版標(biāo)題樣式,,單擊此處編輯母版文本樣式,,第二級(jí),,第三級(jí),,第四級(jí),,第五級(jí),,,*,7.3圖的遍歷,,回顧其他數(shù)據(jù)結(jié)構(gòu)的遍歷:,,順序表的遍歷,,單鏈表的遍歷,,二叉樹(shù)的遍歷,,展望:,,那么對(duì)于圖,我們?cè)鯓舆M(jìn)行遍歷呢?,,圖的深度優(yōu)先遍歷,,圖的廣度優(yōu)先遍歷,,,這兩個(gè)算法是后面拓?fù)渑判?、求關(guān)鍵路徑算法的基礎(chǔ),,,7.3.1.連通圖的深度優(yōu)先遍歷,,,,1.深度優(yōu)先遍歷以v開(kāi)始的連通圖,訪問(wèn),v,,分別深度優(yōu)先遍歷,v,的各個(gè)未被訪問(wèn)的鄰接點(diǎn),算法描述:,,2.算法演示,,,0,,,1,v1,,2,v2,,3,V3,,4,V4,,5,v5,,6,v6,,7,v7,,8,v8
2、,,v2,,v3,,v1,,v4,,v5,,v1,,v6,,v7,,v2,,v8,,v2,,v8,,v3,,v7,,v3,,v6,,v4,,v5,,v1,v2,v3,v4,v5,v6,v7,v8,例圖及其鄰接表表示,,演示開(kāi)始,以v1為遍歷的起點(diǎn),,,0,,,1,v1,,2,v2,,3,V3,,4,V4,,5,v5,,6,v6,,7,v7,,8,v8,,v2,,v3,,v1,,v4,,v5,,v1,,v6,,v7,,v2,,v8,,v2,,v8,,v3,,v7,,v3,,v6,,v4,,v5,,v1,,0,,,1,v1,,2,v2,,3,V3,,4,V4,,5,v5,,6,v6,,7,v7,,
3、8,v8,,v2,,v3,,v1,,v4,,v5,,v1,,v6,,v7,,v2,,v8,,v2,,v8,,v3,,v7,,v3,,v6,,v4,,v5,,,,v1,,0,,,1,v1,,2,v2,,3,V3,,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v7,,8,v8,,v2,,v3,,v1,,v4,,v5,,v1,,v6,,v7,,v2,,v8,,v2,,v8,,v3,,v7,,v3,,v6,,v4,,v5,,,,v1,,,v2,v1,v5,,,v4,v2,,,v8
24、,v4,,,v5,v2,v8,,,v3,v1,v7,,,v6,v3,,,v7,v3,v6,,0,,,1,v1,,2,v2,,3,V3,,4,V4,,5,v5,,6,v6,,7,v7,,8,v8,,v2,,v3,,v1,,v4,,v5,,v1,,v6,,v7,,v2,,v8,,v2,,v8,,v3,,v7,,v3,,v6,,v4,,v5,,,,v1,,,v2,v1,v5,,,v4,v2,,,v8,v4,,,v5,v2,v8,,,v3,v1,v7,,,v6,v3,,,v7,v3,v6,,演示結(jié)束,,3.算法實(shí)現(xiàn),,從演示過(guò)程可以看出,我們必須知道頂點(diǎn)是否已經(jīng)被訪問(wèn)過(guò)。在具體實(shí)現(xiàn)時(shí),我們用一個(gè)全局?jǐn)?shù)
25、組visited[]來(lái)記錄頂點(diǎn)是否被訪問(wèn)過(guò)。如果visited[i]的值為T(mén)rue,則頂點(diǎn)vi已經(jīng)被訪問(wèn),否則沒(méi)有被訪問(wèn)。,,3.算法實(shí)現(xiàn),,Void DFS(Graph G,int v),,{,,Visited[v]=True;cout< 26、rue;//以前未被訪問(wèn),此處被訪問(wèn),,//改變對(duì)應(yīng)的標(biāo)志為已經(jīng)訪問(wèn),,cout< 27、,8,v8,,v2,,v3,,v1,,v4,,v5,,v1,,v6,,v7,,v2,,v8,,v2,,v8,,v3,,v7,,v3,,v6,,v4,,v5,,v1,v2,v3,v4,v5,v6,v7,v8,例圖及其鄰接表表示,,,答案為:,,以v2為起始點(diǎn):v2-v1-v3-v6-v7-v4-v8-v5,,,以v8為起始點(diǎn):v8-v4-v2-v1-v3-v6-v7-v5,,思考題:,,若圖不是連通圖,如何進(jìn)行深度優(yōu)先遍歷?,,請(qǐng)建立下圖的鄰接表結(jié)構(gòu),并進(jìn)行深度優(yōu)先遍歷.,v1,v2,v3,v4,v5,v6,v7,,bool visited[100]={false};,,,void DFSTraverse(ALGraph mg),,{,,for(int i=1 ;i<=mg.vexnum;i++),,{,,if(visited[i]==false),,DFS(mg,i);,,},,},,
- 溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 消防泵概述ppt課件
- 孕期情緒管理課件
- 高等有機(jī)化學(xué)——反應(yīng)機(jī)理課件
- 鼻鼻竇影像學(xué)表現(xiàn)課件
- 專題仿寫(xiě)句子課件
- 基礎(chǔ)統(tǒng)計(jì)學(xué)ActiveLearningLectureSlidesChapter11NonparametricTests
- 《nsaid相關(guān)胃損傷患者教育》培訓(xùn)課件
- 微帶天線課件
- “學(xué)黨紀(jì)、明規(guī)矩、強(qiáng)黨性”研討發(fā)言材料范文(五篇)
- 大學(xué)生消費(fèi)調(diào)查報(bào)告課件
- 辦公室主任年度工作總結(jié)范文(三篇)
- 《中國(guó)共產(chǎn)黨紀(jì)律處分條例》模擬測(cè)試題
- 一年級(jí)數(shù)學(xué)上冊(cè)冀教版15-16-17-18減幾課件
- 醫(yī)院經(jīng)營(yíng)管理者的修煉課件
- 2024年黨建工作總結(jié)和2025年工作計(jì)劃范文(三篇)