Graphs and its traversal algorithms - Tutorialspoint
文章推薦指數: 80 %
The Depth First Search (DFS) is a graph traversal algorithm. In this algorithm one starting vertex is given, and when an adjacent vertex is ... TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Graphsanditstraversalalgorithms DataStructureAlgorithmsAnalysisofAlgorithmsAlgorithms Inthissectionwewillseewhatisagraphdatastructure,andthetraversalalgorithmsofit.Thegraphisonenon-lineardatastructure.Thatisconsistsofsomenodesandtheirconnectededges.Theedgesmaybedirectororundirected.ThisgraphcanberepresentedasG(V,E).ThefollowinggraphcanberepresentedasG({A,B,C,D,E},{(A,B),(B,D),(D,E),(B,C),(C,A)})Thegraphhastwotypesoftraversalalgorithms.ThesearecalledtheBreadthFirstSearchandDepthFirstSearch.BreadthFirstSearch(BFS)TheBreadthFirstSearch(BFS)traversalisanalgorithm,whichisusedtovisitallofthenodesofagivengraph.Inthistraversalalgorithmonenodeisselectedandthenalloftheadjacentnodesarevisitedonebyone.Aftercompletingalloftheadjacentvertices,itmovesfurthertocheckanotherverticesandchecksitsadjacentverticesagain.Algorithmbfs(vertices,start) Input:Thelistofvertices,andthestartvertex. Output:Traverseallofthenodes,ifthegraphisconnected. Begin defineanemptyqueueque atfirstmarkallnodesstatusasunvisited addthestartvertexintotheque whilequeisnotempty,do deleteitemfromqueandsettou displaythevertexu forallvertices1adjacentwithu,do ifvertices[i]isunvisited,then markvertices[i]astemporarilyvisited addvintothequeue mark done markuascompletelyvisited done EndDepthFirstSearch(DFS)TheDepthFirstSearch(DFS)isagraphtraversalalgorithm.Inthisalgorithmonestartingvertexisgiven,andwhenanadjacentvertexisfound,itmovestothatadjacentvertexfirstandtrytotraverseinthesamemanner.Algorithmdfs(vertices,start) Input:Thelistofallvertices,andthestartnode. Output:Traverseallnodesinthegraph. Begin initiallymakethestatetounvisitedforallnodes pushstartintothestack whilestackisnotempty,do popelementfromstackandsettou displaythenodeu ifuisnotvisited,then markuasvisited forallnodesiconnectedtou,do ifithvertexisunvisited,then pushithvertexintothestack markithvertexasvisited done done End ArnabChakraborty Publishedon27-Aug-201912:23:57 RelatedQuestions&AnswersAlgorithmsandComplexities FixedandFloodingRoutingalgorithms PlanarGraphsandtheirProperties IsomorphismandHomeomorphismofgraphs ConstructBSTfromitsgivenlevelordertraversalinC++ RebalancingAlgorithms BipartiteGraphs EulerianGraphs PlanarGraphs HamiltonianGraphs Constructthefullk-arytreefromitspreordertraversalinC++ Whatarethetypesofprocessschedulingalgorithmsandwhichalgorithmsleadtostarvation? DifferencebetweenDeterministicandNon-deterministicAlgorithms EulerianandHamiltonianGraphsinDataStructure SudokuSolvingalgorithms PreviousPage PrintPage NextPage Advertisements Print AddNotes Bookmarkthispage ReportError Suggestions Save Close Dashboard Logout
延伸文章資訊
- 1Graph - 演算法筆記
Traversal 中文稱作「遍歷」。圖的遍歷,也就是指通盤地讀取圖的資訊:決定好從哪裡開始讀,依照什麼順序讀,要讀到哪裡為止。詳細地設計好流程,始能通盤地讀取圖的資訊; ...
- 2Depth First Search or DFS for a Graph - GeeksforGeeks
Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The ...
- 3Quick Guide to Graph Traversal Analysis | by Riccardo Di Sipio
Traversing a graph means exploring its structure by visiting the nodes according to some systemat...
- 4[Data Structure][Graph] - Traversal - DFS - iT 邦幫忙
圖形的走訪Traversal 指從某個頂點作為起點,依照某種順序,一個一個拜訪(visit)所有能到達的頂點。 走訪的順序分為: 廣度優先(Breadth First Search) ...
- 5Graph: Depth-First Search(DFS,深度優先搜尋)
在Binary Tree: Traversal(尋訪)中介紹過Pre-Order Traversal,其Visiting順序:「Current(V)-left(L)-right(R)」可以解讀成...