Quick Guide to Graph Traversal Analysis | by Riccardo Di Sipio
文章推薦指數: 80 %
Traversing a graph means exploring its structure by visiting the nodes according to some systematic rule. The most effective rule depends on the type of graph ... GetstartedOpeninappSigninGetstartedFollow612KFollowers·Editors'PicksFeaturesDeepDivesGrowContributeAboutGetstartedOpeninappQuickGuidetoGraphTraversalAnalysisAnalyzinggraphstofindoptimalpathsdoesn’thavetobehardRiccardoDiSipioMay11,2021·3minreadAgraphisadatastructurecomposedofasetofobjects(nodes)equippedwithconnections(edges)amongthem.Graphscanbedirectediftheconnectionsareorientedfromonenodetoanother(e.g.AliceowesmoneytoBob),orundirectediftheorientationisirrelevantandtheconnectionsjustrepresentrelationships(e.g.AliceandBobarefriends).Agraphissaidtobecompleteifallnodesareconnectedtoeachother.Adirectedgraphwithnoloopsissaidtobeacyclic.Atreeisanundirectedgraphinwhichanytwonodesareconnectedbyexactlyoneedge.Theinitialnodeinatreeiscalledroot,andterminalnodes(i.e.nodeswithnodownstream-connectednodes)arecalledleaves.Afewpracticalexamplesofgraphsarefriendshipnetworks(e.g.onsocialmedia),genealogical(family)trees,molecules,particlesproducedattheLargeHadronCollider,acompany’sorganizationalchart.Canyoutellwhichcategoryofgraphsdotheybelongto?MainGraphTraversalAlgorithmsTraversingagraphmeansexploringitsstructurebyvisitingthenodesaccordingtosomesystematicrule.Themosteffectiveruledependsonthetypeofgraphandtheproblemathandsoit’snotpossibletomaketrulygeneralstatements.Sincemanydatastructuresincomputersciencehaveanunderlyinggraphthatisatree,it’sworthspendingsomeefforttounderstandbetterthestrategiestoexploresuchagraphinordertofindpathsornodesthatsatisfytheconditionsoftheproblemathand.Inmanycases,theproblemcanbereducedtoastatementsuchas“findthepathbetweennodeAandBsuchthatacertainquantityisoptimized”.Thequantitymaybeforexamplethenumberofedges(i.e.thelengthofthepath),orthesumofacertainfeature(possiblybinary)associatedtoeachnode.TwomainapproachesarecalledDepth-FirstSearch(DFS)andBreadth-FirstSearch(BFS).IntheBFSapproach,nodesatthesamedepthlevelareexploredsequentially,untilthestoppingconditionisfulfilled.Itmakesuseofaqueuetofindtheshortestpathwithaniterativeapproach.BFSusuallytakesuplotsofmemorybecauseoneneedstomaintainanadditionaldatastructurecontainingthenodesyettoexplore,oftenintheformofa“alreadyvisited”booleanvariable.BFSisoftenthebestapproachwhenthedepthofthetreeisnotbalancedorifthesearchnodeisnottoofarfromtheroot(“shallowtree”).Atypicalexampleisasocialnetworkwherewewanttosearchforpeoplewhohavesimilarinterestsofaspecificperson.There’sagoodchancethatthetargetnodesaredirectfriendsorfriendsoffriends,i.e.therootnodecanhavemanybranchings,butthetargetsarejustafewedgesaway.TheDFSalgorithmexploresthegraphbygoingfromtheroottoaleaf,andrepeattheoperationuntiltheconditionissatisfied.Itmakesuseofastacktofindtheshortestpath.Ifagivenbranchdoesn’tfulfilltherequest,thealgorithm“backtracks”tothehigherlevelandthengoesdownanotherpath.Ifarecursiveapproachisused,lessmemoryisneededasonedoesn’thavetokeeptrackofthenodesthathavebeenalready“visited”.DFSisusuallyaneffectiveapproachincasethetreerepresentsaseriesofdecisionsand/orthetargetleafisfarfromtheroot.Atypicaluse-caseexampleisthesimulationofgamessuchaschess,inwhichateachiterationstherearefewpossibilitiesbuteachdecisiongoesfarintermsofnextmoves.CodeExamplesAfewlinesofcodeareoftenmoreexplicativethanlotsoftext.Inthefollowing,we’llexploreasimplegraphmadeof6nodesand5edges.ThestructureofthegraphusedintheexamplesbelowDepth-FirstSearchExpectedoutput:AA->BB->DB->EA->CC->FBreadth-FirstSearchExpectedoutput:ABCDEFRuleofThumbShortestpathonsimplegraph->BFSLowwidth,largedepth->BFSLargewidth,lowdepth->DFS(recursionstackwillnotoverflow)Scanallpossibleresults->DFSRiccardoDiSipioNLPMachineLearningengineeratCeridian.PreviouslysmashingprotonsattheCERNLHC.Viewsaremyown.Follow1111 11GraphAlgorithmsDataStructuresTreesComputerScienceMorefromTowardsDataScienceFollowYourhomefordatascience.AMediumpublicationsharingconcepts,ideasandcodes.ReadmorefromTowardsDataScienceMoreFromMediumMyFirstForayintoaDataScienceCompetitionPederNorrInterpretingtheOLSsummariesinPythonusingPandasandstatsmodelCelBroomheadWhatisDecisiontreeandhowitworks?AkashNHForecastingsoftwareproject’scompletiondatethroughMonteCarloSimulationLucasColucciinTowardsDataScienceDataScienceonWhyReadingtheNewsMightStressYouOutASamuelPottingerinTowardsDataScienceBestmetricforRegression ?RMSE,MSE,MAE,R2NicolasMaurer“Haberman’sCancerSurvival”ExploratoryDataAnalysis_PythonManjunathTOnBuildingaDataScienceTeam,PartIShayStrong
延伸文章資訊
- 1Quick Guide to Graph Traversal Analysis | by Riccardo Di Sipio
Traversing a graph means exploring its structure by visiting the nodes according to some systemat...
- 2Graph - 演算法筆記
Traversal 中文稱作「遍歷」。圖的遍歷,也就是指通盤地讀取圖的資訊:決定好從哪裡開始讀,依照什麼順序讀,要讀到哪裡為止。詳細地設計好流程,始能通盤地讀取圖的資訊; ...
- 3[Data Structure][Graph] - Traversal - BFS - iT 邦幫忙
圖形的走訪Traversal 指從某個頂點作為起點,依照某種順序,一個一個拜訪(visit)所有能到達的頂點。 走訪的順序分為: 廣度優先(Breadth First Search) ...
- 4Depth 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 ...
- 5Graph traversal - Wikipedia
In computer science, graph traversal refers to the process of visiting (checking and/or updating)...