




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第第頁(yè)數(shù)據(jù)結(jié)構(gòu)與算法分析深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
四川高校軟件學(xué)院同學(xué)試驗(yàn)報(bào)告
試驗(yàn)名稱:數(shù)據(jù)結(jié)構(gòu)與算法分析
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
試驗(yàn)報(bào)告
班級(jí)__姓名學(xué)號(hào)
一、試驗(yàn)號(hào)題目:深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)二、試驗(yàn)的目的和要求:1.采納C++實(shí)現(xiàn);2.嫻熟掌控圖的應(yīng)用;
3.嫻熟掌控圖的鄰接表存儲(chǔ)結(jié)構(gòu)以及拓?fù)渑判虻幕舅枷搿?.上機(jī)調(diào)試程序,掌控查錯(cuò)、排錯(cuò)使程序能正確運(yùn)行。三、試驗(yàn)的環(huán)境:1.硬件環(huán)境:2.軟件環(huán)境:
〔1〕操作系統(tǒng)windows*PSP2?!?〕編譯系統(tǒng)Mingw322.95
C-Free開(kāi)發(fā)工具:BorlandC++Builder6.0C-Free中運(yùn)用的編譯系統(tǒng):Mingw322.95C-Free中運(yùn)用的調(diào)試系統(tǒng):GDB5.2.1C-Free中運(yùn)用的VCL組件:SynEdit1.1
〔3〕編輯軟件特點(diǎn)
運(yùn)用c-Free自帶的編輯軟件,C-Free的智能輸入功能能夠大大提高你的代碼編寫(xiě)速度,它能夠
記住你已經(jīng)輸入的全部標(biāo)識(shí)符、關(guān)鍵字,下一次輸入標(biāo)識(shí)符時(shí),你不需要輸入全部的標(biāo)識(shí)符名稱,輸入一到二個(gè)字母,編輯窗口中會(huì)涌現(xiàn)你需要的標(biāo)識(shí)符。
四、算法描述:
深度優(yōu)先搜尋
深度優(yōu)先搜尋類似于樹(shù)的先根遍歷。假設(shè)給定圖G的初態(tài)是全部頂點(diǎn)均未訪問(wèn)過(guò),在G中任選一頂點(diǎn)v為初始出發(fā)點(diǎn),那么深度優(yōu)先搜尋可定義如下:首先,訪問(wèn)出發(fā)點(diǎn)v,并將其標(biāo)記為已訪問(wèn)過(guò),然后依次從v的未被訪問(wèn)過(guò)的鄰接點(diǎn)出發(fā)深度優(yōu)先遍歷圖,直到圖中全部和v有路徑相通的頂點(diǎn)都被訪問(wèn)到;假設(shè)此時(shí)圖中還有頂點(diǎn)未被訪問(wèn),那么另選圖中一個(gè)未曾被訪問(wèn)的頂點(diǎn)作為起始點(diǎn),重復(fù)上述過(guò)程,直到圖中全部頂點(diǎn)都被訪問(wèn)到為止。
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
廣度優(yōu)先搜尋
廣度優(yōu)先搜尋類似于樹(shù)的按層次遍歷的過(guò)程。從圖中的某個(gè)頂點(diǎn)v0出發(fā),在訪問(wèn)v0之后依次訪問(wèn)v0的全部未被訪問(wèn)過(guò)的鄰接點(diǎn)w1,w2,w3,…wk,然后按這些鄰接點(diǎn)被訪問(wèn)的先后次序依次從w1,w2,w3,…wk出發(fā)訪問(wèn)它們的鄰接點(diǎn),如此下去,直至圖中全部已被訪問(wèn)的頂點(diǎn)的鄰接點(diǎn)都被訪問(wèn)到。假設(shè)此時(shí)圖中尚有頂點(diǎn)未被訪問(wèn),那么另選圖中一個(gè)未曾被訪問(wèn)的頂點(diǎn)作起始點(diǎn),重復(fù)上述過(guò)程,直至圖中全部頂點(diǎn)都被訪問(wèn)到為止。
五、源程序清單:
#includeiostream.h#includestdlib.h#includestdio.h#includeassert.h#includectype.h
//Usedbythemarkarray#defineUNVISITED0#defineVISITED1
//Graphabstractclass
classGraph{public:
//Returnthenumberofvertices
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
virtualintn()=0;
//Returnthecurrentnumberofedgesvirtualinte()=0;
//Returntheinde*ofthefirstneigborofgivenverte*virtualintfirst(int)=0;
//Returntheinde*ofthene*tneigborofgivenverte*virtualintne*t(int,int)=0;
//StoreanedgedefinedbytwoverticesandweightvirtualvoidsetEdge(int,int,int)=0;//Deleteedgedefinedbytwovertices
virtualvoiddelEdge(int,int)=0;
//Returnweightofedgeconnectingtwovertices//Return0ifnosuchedgee*istsvirtualintweight(int,int)=0;//Getmarkvalueforaverte*virtualintgetMark(int)=0;//Setmarkvalueforaverte*virtualvoidsetMark(int,int)=0;};
classEdge{
public:
intvecte*,weight;
Edge(){vecte*=-1;weight=-1;}
Edge(intv,intw){vecte*=v;weight=w;}};
classGraphm:publicGraph{//Implementadjacencymatri*private:
intnumVerte*,numEdge;//Storenumberofvertices,edgesint**matri*;//Pointertoadjacencymatri*int*mark;//Pointertomarkarray
public:
Graphm(intnumVert){//Makegraphw/numVertverticesinti,j;
numVerte*=numVert;numEdge=0;
mark=newint[numVert];//Initializemarkarrayfor(i=0;inumVerte*;i++)mark[i]=UNVISITED;
matri*=(int**)newint*[numVerte*];//Makematri*for(i=0;inumVerte*;i++)
matri*[i]=newint[numVerte*];
for(i=0;inumVerte*;i++)//Edgesstartw/0weight
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
for(intj=0;jnumVerte*;j++)matri*[i][j]=0;}
~Graphm(){//Destructor
delete[]mark;//Returndynamicallyallocatedmemoryfor(inti=0;inumVerte*;i++)delete[]matri*[i];delete[]matri*;}
intn(){returnnumVerte*;}//Numberofverticesinte(){returnnumEdge;}//Numberofedges
intfirst(intv){//Returnv'sfirstneighborinti;
for(i=0;inumVerte*;i++)if(matri*[v][i]!=0)returni;returni;//Returnnifnone}
intne*t(intv1,intv2){//Getv1'sneighborafterv2inti;
for(i=v2+1;inumVerte*;i++)if(matri*[v1][i]!=0)returni;returni;}
//Setedge(v1,v2)towgt
voidsetEdge(intv1,intv2,intwgt){Assert(wgt0,Illegalweightvalue);if(matri*[v1][v2]==0)numEdge++;matri*[v1][v2]=wgt;}
voiddelEdge(intv1,intv2){//Deleteedge(v1,v2)if(matri*[v1][v2]!=0)numEdge--;matri*[v1][v2]=0;}
intweight(intv1,intv2){returnmatri*[v1][v2];}intgetMark(intv){returnmark[v];}voidsetMark(intv,intval){mark[v]=val;}};
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
//Abstractqueueclass
templateclassElemclassQueue{public:
//Reinitializethequeue.Theuserisresponsiblefor//reclaimingthestorageusedbythestackelements.virtualvoidclear()=0;
//Placeanelementattherearofthequeue.Return//trueifsuccessful,falseifnot(ifqueueisfull).virtualboolenqueue(constElem)=0;
//Removetheelementatthefrontofthequeue.Return//trueifsuccesful,falseifqueueisempty.
//Theelementremovedisreturnedinthefirstparameter.virtualbooldequeue(Elem)=0;//RemoveElemfromfront//Returninfirstparameteracopyofthefrontelement.//Returntrueifsuccesful,falseifqueueisempty.virtualboolfrontValue(Elem)const=0;//Returnthenumberofelementsinthequeue.virtualintlength()const=0;};
//Array-basedqueueimplementation
templateclassElemclassAQueue:publicQueueElem{private:
intsize;//Ma*imumsizeofqueueintfront;//Inde*offrontelementintrear;//Inde*ofrearelement
Elem*listArray;//Arrayholdingqueueelementspublic:
AQueue(intsz=DefaultListSize){//Constructor
//Makelistarrayonepositionlargerforemptyslotsize=sz+1;
rear=0;front=1;listArray=newElem[size];}
~AQueue(){delete[]listArray;}//Destructorvoidclear(){front=rear;}boolenqueue(constElemit){
if(((rear+2)%size)==front)returnfalse;//Fullrear=(rear+1)%size;//CircularincrementlistArray[rear]=it;returntrue;
}
booldequeue(Elemit){
if(length()==0)returnfalse;//Empty
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
it=listArray[front];
front=(front+1)%size;//Circularincrementreturntrue;}
boolfrontValue(Elemit)const{if(length()==0)returnfalse;//Emptyit=listArray[front];returntrue;}
virtualintlength()const
{return((rear+size)-front+1)%size;}};
voidPreVisit(Graph*G,intv){
coutPreVisitverte*v\n;}
voidPostVisit(Graph*G,intv){
coutPostVisitverte*v\n;}
voidDFS(Graph*G,intv){//DepthfirstsearchPreVisit(G,v);//TakeappropriateactionG-setMark(v,VISITED);
for(intw=G-first(v);wG-n();w=G-ne*t(v,w))if(G-getMark(w)==UNVISITED)
DFS(G,w);PostVisit(G,v);//Takeappropriateaction}
voidBFS(Graph*G,intstart,Queueint*Q){intv,w;
Q-enqueue(start);//InitializeQG-setMark(start,VISITED);
while(Q-length()!=0){//ProcessallverticesonQQ-dequeue(v);
PreVisit(G,v);//Takeappropriateactionfor(w=G-first(v);wG-n();w=G-ne*t(v,w))if(G-getMark(w)==UNVISITED){G-setMark(w,VISITED);
Q-enqueue(w);}
PostVisit(G,v);//Takeappropriateaction
深度優(yōu)先搜尋和廣度優(yōu)先搜尋算法實(shí)現(xiàn)
}
}
//TestDepthFirstSearchandBreadthFirstSearchintmain(intargc,char*argv[]){
Graph*g=newGraphm(6);//Initializeagraphmgintchance;g-setEdge(0,2,1);g-setEdge(2,0,1);g-setEdge(2,1,1);g-setEdge(1,2,1);g-setEdge(1,5,1);g-setEdge(5,1,1);g-setEdge(2,5,1);g-setEdge(5,2,1);g-setEdge(3,5,1);g-setEdge(5,3,1);g-setEdg
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 海南大學(xué)《中國(guó)古文名篇導(dǎo)讀》2023-2024學(xué)年第二學(xué)期期末試卷
- 公章申請(qǐng)流程
- 抽排水施工方案
- 2025年鄉(xiāng)村醫(yī)生崗位理論知識(shí)考試模擬試題及答案(共100題)
- 植生袋綠化施工方案
- 橋梁基座施工方案范本
- 基坑錨桿支護(hù)施工方案
- 抗腐蝕混凝土施工方案
- 球罐畢業(yè)設(shè)計(jì)總說(shuō)明書(shū)(非常詳盡)
- 動(dòng)物醫(yī)學(xué)院紀(jì)檢衛(wèi)生獎(jiǎng)懲措施
- 社會(huì)心理學(xué)-社會(huì)心理學(xué)的研究方法
- (完整)TRD工法樁施工方案
- 南陽(yáng)糧庫(kù)專用門(mén)窗施工方案
- 某物業(yè)管理月度品質(zhì)管理考核標(biāo)準(zhǔn)
- 新民主主義革命理論(教學(xué)案例)
- 貴州民族大學(xué)輔導(dǎo)員考試試題2023
- 2023年陜西公務(wù)員申論考試真題及答案-B卷
- 建筑施工安全風(fēng)險(xiǎn)辨識(shí)分級(jí)管控指南
- 船塢的施工方法與流程
- 九年級(jí)化學(xué)下冊(cè)第9單元溶液課題3溶液的濃度第二課時(shí)化學(xué)反應(yīng)中的溶質(zhì)質(zhì)量分?jǐn)?shù)的計(jì)算作業(yè)講義新人教版
- 四川省2023年普通高等學(xué)校高職教育單獨(dú)招生文化考試(普高類)英語(yǔ)試題(含答案解析)
評(píng)論
0/150
提交評(píng)論