版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、7.3.2.連通圖的廣度優(yōu)先遍歷1.廣度優(yōu)先遍歷以x開始的連通圖訪問(wèn)X,且x入隊(duì)列若隊(duì)列不空,重復(fù)以下步驟取隊(duì)頭元素并放入v中考察v的各個(gè)鄰接點(diǎn),若未訪問(wèn),則先訪問(wèn),然后放在隊(duì)列尾部返回步驟算法描述算法描述:2.算法演示01 v12 v23 V34 V45 v56 v67 v78 v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1v2v3v4v5v6v7v8例圖及其鄰接表表示演示開始,以v1為遍歷的起點(diǎn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5隊(duì)列隊(duì)列v1訪問(wèn)v101v12v23V34
2、V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v1V1入隊(duì)列01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v1取隊(duì)頭元素01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v1v2V1的鄰接點(diǎn)v2沒(méi)有被訪問(wèn)過(guò),訪問(wèn)之,且入隊(duì)列01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v1
3、v2v201v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v1v2v2v3V1的鄰接點(diǎn)v3沒(méi)有被訪問(wèn)過(guò),訪問(wèn)之,且入隊(duì)列01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v1v2v2v3v301v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v301v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v
4、2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v301v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v301v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v3V2的鄰接點(diǎn)v1已經(jīng)被訪問(wèn)過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v3v4V2的鄰接點(diǎn)v4沒(méi)有被訪問(wèn)過(guò),訪問(wèn)之,且入隊(duì)列01v
5、12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v3v4v401v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v3v4v4v5V2的鄰接點(diǎn)v5沒(méi)有被訪問(wèn)過(guò),訪問(wèn)之,且入隊(duì)列01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v2v3v3v4v4v5v501v12v23V34V45v56v67v78v8v2v3v1v4v5v1
6、v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v501v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v501v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v501v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v5V3的鄰接點(diǎn)v1已經(jīng)被訪問(wèn)
7、過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v5v6V3的鄰接點(diǎn)v6沒(méi)有被訪問(wèn)過(guò),訪問(wèn)之,且入隊(duì)列01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v5v6v601v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v5v6v6v7V3的鄰接點(diǎn)v7沒(méi)有被訪問(wèn)過(guò),訪問(wèn)
8、之,且入隊(duì)列01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v3v4v4v5v5v6v6v7v701v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v4v5v5v6v6v7v701v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v4v5v5v6v6v7v701v12v23V34V45v56v67v78v8v2v3v1
9、v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v4v5v5v6v6v7v701v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v4v5v5v6v6v7v7V4的鄰接點(diǎn)v2已經(jīng)被訪問(wèn)過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v4v5v5v6v6v7v7v8V4的鄰接點(diǎn)v8沒(méi)有被訪問(wèn)過(guò),訪問(wèn)之,且入隊(duì)列01v12v23V34V45v56v67v78v8v2
10、v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v4v5v5v6v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v5v6v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v5v6v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v
11、4v5v1隊(duì)列隊(duì)列v2v3v4v5v5v6v6v7v7v8v8V5的鄰接點(diǎn)v2、v8已經(jīng)被訪問(wèn)過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v
12、5v6v6v7v7v8v8V6的鄰接點(diǎn)v3、v7已經(jīng)被訪問(wèn)過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v7v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v7v7v8v8V7的鄰接點(diǎn)v3、v6
13、已經(jīng)被訪問(wèn)過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v7v8v801v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v7v8v8V8的鄰接點(diǎn)v4、v5已經(jīng)被訪問(wèn)過(guò)不再訪問(wèn)01v12v23V34V45v56v67v78v8v2v3v1v4v5v1v6v7v2v8v2v8v3v7v3v6v4v5v1隊(duì)列隊(duì)列v2v3v4v5v6v7v8隊(duì)列為空,算法結(jié)束3.算法實(shí)現(xiàn) 從演示過(guò)程可
14、以看出,我們必須知道頂點(diǎn)是否已經(jīng)被訪問(wèn)過(guò)。在具體實(shí)現(xiàn)時(shí),我們用一個(gè)數(shù)組visited來(lái)記錄頂點(diǎn)是否被訪問(wèn)過(guò)。如果visitedi的值為True,則頂點(diǎn)vi已經(jīng)被訪問(wèn),否則沒(méi)有被訪問(wèn)。3.算法實(shí)現(xiàn)Void BFSGraph G,int x)Visited100=False;/假設(shè)圖中頂點(diǎn)數(shù)沒(méi)有超過(guò)100個(gè)Visitedx=True;coutx;Queue.push(x);While(!Q.empty()V= Queue.front();Queue.pop();For(v的每個(gè)鄰接點(diǎn)w)Ifvisitedw=false)Visitedw=True;coutw;Queue.pushw);當(dāng)圖的存儲(chǔ)結(jié)構(gòu)為鄰接表時(shí),廣度優(yōu)先算法可以表示如下:void BFS (ALGraph mg,int x)bool visited100=false;queue q;coutmg.vexsx.data 0;w=:NextAdjVex(mg,v,w)if(visitedw=false)coutmg.vexsw.data ;visitedw=true;q.push(w);練習(xí)題:對(duì)于下面
溫馨提示
- 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. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣東環(huán)境保護(hù)工程職業(yè)學(xué)院《工程熱力學(xué)D》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東第二師范學(xué)院《糧食質(zhì)量安全與控制實(shí)驗(yàn)》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東財(cái)貿(mào)職業(yè)學(xué)院《社會(huì)工作專業(yè)英語(yǔ)》2023-2024學(xué)年第一學(xué)期期末試卷
- 贛南科技學(xué)院《大氣污染控制》2023-2024學(xué)年第一學(xué)期期末試卷
- 贛東學(xué)院《創(chuàng)新創(chuàng)業(yè)教育》2023-2024學(xué)年第一學(xué)期期末試卷
- 三年級(jí)品德與社會(huì)下冊(cè)第三單元第一課我們的生活需要誰(shuí)教案新人教版
- 三年級(jí)數(shù)學(xué)上冊(cè)8分?jǐn)?shù)的初步認(rèn)識(shí)1分?jǐn)?shù)的初步認(rèn)識(shí)第1課時(shí)幾分之一導(dǎo)學(xué)案新人教版
- 三年級(jí)數(shù)學(xué)上冊(cè)二千克和克第2課時(shí)克的認(rèn)識(shí)教案蘇教版
- 三年級(jí)數(shù)學(xué)下冊(cè)五面積第1課時(shí)什么是面積教案北師大版
- 防爆安全課件百度
- 專題05 閱讀-2023-2024學(xué)年六年級(jí)英語(yǔ)寒假專項(xiàng)提升(人教PEP版)
- 做賬實(shí)操-期貨公司的賬務(wù)處理示例
- Java Web 開發(fā)從入門到實(shí)戰(zhàn) 課件 第8章 過(guò)濾器與監(jiān)聽(tīng)器
- 高考重慶語(yǔ)文試卷及答案
- 雙方共用消防通道協(xié)議書
- 綠化租擺服務(wù)投標(biāo)方案(技術(shù)標(biāo))
- 整本書閱讀《鄉(xiāng)土中國(guó)》議題思辨:無(wú)訟之“訟”教學(xué)設(shè)計(jì) 中職語(yǔ)文高教版基礎(chǔ)模塊下冊(cè)
- 醫(yī)學(xué)教材 鼻出血的正確處理方法
- 水利水電移民安置驗(yàn)收資料目錄、工作報(bào)告、驗(yàn)收?qǐng)?bào)告、有關(guān)表格
- 2024年人教版生物八年級(jí)上冊(cè)中考復(fù)習(xí)知識(shí)點(diǎn)綱要
- 機(jī)電樣板實(shí)施施工方法及工藝要求
評(píng)論
0/150
提交評(píng)論