Depth First Search (DFS) C++ Program To Traverse A Graph ...
文章推薦指數: 80 %
BFS vs DFS ; Stands for “Breadth-first search”, Stands for “Depth-first search” ; The nodes are explored breadth wise level by level. The nodes ...
Skiptocontent
Menu
MENUMENUHomeResourcesFREEeBooksQATesting
FreeQATrainingTestCasesSDLCTestLink
SoftwareTestingBugZillaMobileTestingJIRA
AgileMethodologyDatabaseTestingETLTesting
QualityAssuranceTestManagementSAPERPTesting
Courses
SoftwareTesting(LIVECourse)Selenium(LIVECourse)
SoftwareTestingSeleniumQTP/UFTJIRAVideosRubyCucumber
Automation
AutomationTestingSoapUIJIRAAppiumKarateFramework
SeleniumQTP/UFTALMQCPostman
JMeterLoadRunnerAPITestingRobotFramework
TestNGJUnitEclipseMaven
TypesOfTesting
AllTestingTypesRegressionTestingUnitTestingSmokeTesting
FunctionalTestingIntegrationTestingSystemTestingUsabilityTesting
UATTestingBetaTestingBlackBoxTestingWhiteBoxtesting
LoadTestingStressTestingSecurityTestingPerformanceTesting
Tutorials
C++C#DevOpsVBScriptTeamManagementComputerNetworkingJest
PythonJAVAUNIXSVNAngularJSSpockLaravel
SpecFlowJSONFlaskSOAtestMockitoKarma
MachineLearningBlockchainGitHubGatlingWiremock
Data
OraclePL/SQL
DataWarehouseExcelVBA
BigDataJDBC
MongoDB
Menu
MENUMENUHomeResourcesFREEeBooksQATesting
FreeQATrainingTestCasesSDLCTestLink
SoftwareTestingBugZillaMobileTestingJIRA
AgileMethodologyDatabaseTestingETLTesting
QualityAssuranceTestManagementSAPERPTesting
Courses
SoftwareTesting(LIVECourse)Selenium(LIVECourse)
SoftwareTestingSeleniumQTP/UFTJIRAVideosRubyCucumber
Automation
AutomationTestingSoapUIJIRAAppiumKarateFramework
SeleniumQTP/UFTALMQCPostman
JMeterLoadRunnerAPITestingRobotFramework
TestNGJUnitEclipseMaven
TypesOfTesting
AllTestingTypesRegressionTestingUnitTestingSmokeTesting
FunctionalTestingIntegrationTestingSystemTestingUsabilityTesting
UATTestingBetaTestingBlackBoxTestingWhiteBoxtesting
LoadTestingStressTestingSecurityTestingPerformanceTesting
Tutorials
C++C#DevOpsVBScriptTeamManagementComputerNetworkingJest
PythonJAVAUNIXSVNAngularJSSpockLaravel
SpecFlowJSONFlaskSOAtestMockitoKarma
MachineLearningBlockchainGitHubGatlingWiremock
Data
OraclePL/SQL
DataWarehouseExcelVBA
BigDataJDBC
MongoDB
LastUpdated:November29,2021
ThisTutorialCoversDepthFirstSearch(DFS)inC++inWhichAGraphorTreeisTraversedDepthwise.YouwillAlsoLearnDFSAlgorithm&Implementation:
Depth-firstsearch(DFS)isyetanothertechniqueusedtotraverseatreeoragraph.
DFSstartswitharootnodeorastartnodeandthenexplorestheadjacentnodesofthecurrentnodebygoingdeeperintothegraphoratree.ThismeansthatinDFSthenodesareexploreddepth-wiseuntilanodewithnochildrenisencountered.
Oncetheleafnodeisreached,DFSbacktracksandstartsexploringsomemorenodesinasimilarfashion.
=> WatchOutTheBeginnersC++TrainingGuideHere.
WhatYouWillLearn:DepthFirstSearch(DFS)InC++DFSAlgorithmTraversalsWithIllustrationsDepth-FirstSearchImplementationRuntimeAnalysisIterativeDFSBFSvsDFSApplicationsOfDFSConclusionRecommendedReading
DepthFirstSearch(DFS)InC++
UnlikeBFSinwhichweexplorethenodesbreadthwise,inDFSweexplorethenodesdepth-wise.InDFSweuseastackdatastructureforstoringthenodesbeingexplored.Theedgesthatleadustounexplorednodesarecalled‘discoveryedges’whiletheedgesleadingtoalreadyvisitednodesarecalled‘blockedges’.
Next,wewillseethealgorithmandpseudo-codefortheDFStechnique.
DFSAlgorithm
Step1:Inserttherootnodeorstartingnodeofatreeoragraphinthestack.
Step2:Popthetopitemfromthestackandaddittothevisitedlist.
Step3:Findalltheadjacentnodesofthenodemarkedvisitedandaddtheonesthatarenotyetvisited,tothestack.
Step4:Repeatsteps2and3untilthestackisempty.
Pseudocode
Thepseudo-codeforDFSisgivenbelow.
Fromtheabovepseudo-code,wenoticethattheDFSalgorithmiscalledrecursivelyoneachvertextoensurethatalltheverticesarevisited.
TraversalsWithIllustrations
LetusnowillustratetheDFStraversalofagraph.Forclaritypurposes,wewillusethesamegraphthatweusedintheBFSillustration.
Let0bethestartingnodeorsourcenode.First,wemarkitasvisitedandaddittothevisitedlist.Thenwepushallitsadjacentnodesinthestack.
Next,wetakeoneoftheadjacentnodestoprocessi.e.thetopofthestackwhichis1.Wemarkitasvisitedbyaddingittothevisitedlist.Nowlookfortheadjacentnodesof1.As0isalreadyinthevisitedlist,weignoreitandwevisit2whichisthetopofthestack.
Next,wemarknode2asvisited.Itsadjacentnode4isaddedtothestack.
Next,wemark4whichisthetopofthestackasvisited.Node4hasonlynode2asitsadjacentwhichisalreadyvisited,henceweignoreit.
Atthisstage,onlynode3ispresentinthestack.Itsadjacentnode0isalreadyvisited,henceweignoreit.Nowwemark3asvisited.
Nowthestackisemptyandthevisitedlistshowsthesequenceofthedepth-firsttraversalofthegivengraph.
Ifweobservethegivengraphandthetraversalsequence,wenoticethatfortheDFSalgorithm,weindeedtraversethegraphdepth-wiseandthenbacktrackitagaintoexplorenewnodes.
Depth-FirstSearchImplementation
Let’simplementtheDFStraversaltechniqueusingC++.
#include
usingnamespacestd;
//graphclassforDFStravesal
classDFSGraph
{
intV;//No.ofvertices
list
visited(V,false);
for(inti=0;i
延伸文章資訊
- 1[演算法] [C++ / Python] 深度優先搜尋Depth-First-Search - Part I
[演算法] [C++ / Python] 深度優先搜尋Depth-First-Search - Part I ... 更新:熱騰騰的Part II 出爐囉! 深度優先搜尋,Depth-First...
- 2【筆記】DFS (Depth First Search,深度優先搜尋) - Yui Huang ...
HOME · APCS · CPE · C++ 基礎語法 · 學習歷程 · 題庫&題解 · 自我介紹.
- 3BFS DFS(C++ ) - IT閱讀
BFS DFS(C++ ). /* A / \ B C / \ / \ D E F G 深度優先遍歷(DFS) : A B D E C F G 廣度優先遍歷(BFS) ...
- 4Graph: Depth-First Search(DFS,深度優先搜尋)
DFSVisit(int vertex, int &time) :利用遞迴函式呼叫,進行 color 、 discover 、 finish 與 predecessor 等資料更新的主要函式。 ...
- 5實作Graph與DFS、BFS圖形走訪演算法 - 寫點科普
這邊我們利用C++ STL 的deque 功能來建立可以前後增加刪除的陣列。 #include <stdio.h> #include <deque> using namespace std; c...