Graphs and its traversal algorithms - Tutorialspoint

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?