GraphTraversals
CSC1300–DiscreteStructuresVillanovaUniversity
VillanovaCSC1300-DrPapalaskari 1
MajorThemesGraphtraversals:• Eulercircuit/trail
– Everyedgeexactlyonce
• Hamiltoncycle– Everyvertexexactlyonce– Shortestcycle:
• TravelingSalesmanProblem
– ShortestPath:• Dijkstra’salgorithm
8
2
574
3 2
8
59
VillanovaCSC1300-DrPapalaskari 2
TheBridgesofKönigsbergPuzzle18thcenturymapofthecityofKönigsbergwith7bridgesoverthePregelriver
Find a walk through the city that would cross each bridge once and only once before returning to the starting point
VillanovaCSC1300-DrPapalaskari 3
Euler’sSoluWontoTheBridgesofKönigsberg
A
C
B
D
LeonhardEuler(1707-1783)rephrasedthequesWonintermsofamul9graph(i.e.,mulWpleedgesallowed)
CADB
IsthereacircuitinthismulWgraphthattraversesallitsedgesexactlyonce?
VillanovaCSC1300-DrPapalaskari 4
EulercircuitsandtrailsEulercircuit:traversesalltheedgesofthegraphexactlyonceEulertrail:atrailtraversingalltheedgesofthegraphexactlyonce
Examples:
HasanEulercircuit
HasanEulertrail
HasneitherEulercircuitnorEulertrail
VillanovaCSC1300-DrPapalaskari 5
NecessaryandsufficientcondiWonsforEulercycles
Theorem:AconnectedgraphGhasanEulercycleifandonlyifeveryvertexofGhasevendegree.
(è)Anecessarycondi9onGhasanEulercycleonlyifeveryvertexhasevendegreeAssumeGhasanEulercycle.ObservethateveryWmethecyclepassesthroughavertex,itcontributes2tothevertex’sdegree,sincethecycleentersviaanedgeincidentwiththisvertexandleavesviaanothersuchedge.
VillanovaCSC1300-DrPapalaskari 6
(ç)Asufficientcondi-onIfeveryvertexinGhasevendegree,GhasanEulercycle
LemmaAssumeeveryvertexinamulWgraphhasevendegree.Startatanarbitrarynon-isolatedvertexv0,chooseanarbitraryedge(v0,v1),thenchooseanarbitraryunusededgefromv1andsoon.ThenaeerafinitenumberofstepstheprocesswillarriveatthestarWngvertexv0,yieldingacyclewithdisWnctedges.ProofIntheaboveprocedure,onceyouenteredavertexv,therewillalwaysbeanotherunusededgetoexitvbecausevhasanevendegreeandonlyanevennumberoftheedgesincidentwithithadbeenusedbeforeyouenteredit.Theonlyedgefromwhichyoumaynotbeabletoexitaeerenteringitisv0(becauseanoddnumberofedgesincidentwithv0havebeenusedasyoudidn’tenteritatthebeginning),butifyouhavereachedv0,thenyouhavealreadyconstructedarequiredcycle.
VillanovaCSC1300-DrPapalaskari 7
AprocedureforconstrucWnganEulercycle
AlgorithmEuler(G)//Input:ConnectedgraphGwithallverWceshavingevendegrees//Output:EulercycleConstructacycleinGusingtheprocedurefromLemmaRemovealltheedgesofcyclefromGtogetsubgraphHwhileHhasedgesfindanon-isolatedvertexvthatisbothincycleandinH//theexistenceofsuchavertexisguaranteedbyG’sconnecWvityconstructsubcycleinHusingLemma’sproceduresplicesubcycleintocycleatvremovealltheedgesofsubcyclefromHreturncycle
VillanovaCSC1300-DrPapalaskari 8
ExampleG
cycle1231
H135724681357
2468
13572468
subcycle34653
13572468
splicing:346531231
1357
2468
13572468
VillanovaCSC1300-DrPapalaskari 9
Example(cont.)
H
1357
2468
H
13572468
splicing678612346531
13572468
Eulercycleobtained12346786531
13572468
VillanovaCSC1300-DrPapalaskari 10
cycle12346531
NecessaryandsufficientcondiWonsforEulertrails
TheoremAconnectedgraphGhasanEulertrailifandonlyififithasexactlytwoverWcesofodddegree.
VillanovaCSC1300-DrPapalaskari 11
IcosianGameIcosianGameThispuzzlehasbeeninventedbytherenownedIrishmathemaWcianSirWilliamHamilton(1805-1865)andpresentedtotheworldunderthenameoftheIcosianGame.Thegame'sboardwasawoodenboardwithholesrepresenWngmajorworldciWeswithgroovesrepresenWngconnecWonsbetweenthem.TheobjectofthegamewastofindacircularroutethatwouldpassthroughalltheciWesexactlyoncebeforereturningtothestarWngpoint.
VillanovaCSC1300-DrPapalaskari 12
TravelingSalesmanProblemfindthelowestcostHamiltoncycle
VillanovaCSC1300-DrPapalaskari 13
OpWmaltourofthe13,509ciWesintheUSAwithpopulaWonsgreaterthan500.
MoreaboutTSP:hjp://www.math.uwaterloo.ca/tsp/index.htmlImage:hjp://www.wired.com/images_blogs/wiredscience/2013/01/tsp_map.jpg
HamiltonCycle?
K6 C3 K4
VillanovaCSC1300-DrPapalaskari 14
Hypercubes,revisited
VillanovaCSC1300-DrPapalaskari 15
ImagesbyNerdBoy1392-Ownwork,CCBY-SA3.0,hjps://commons.wikimedia.org/w/index.php?curid=5514315Watchduck(a.k.a.TilmanPiesk)-Ownwork,PublicDomain,hjps://commons.wikimedia.org/w/index.php?curid=2674082hjps://people.eecs.berkeley.edu/~jfc/cs174lecs/lec11/lec11.htmlSeealso:hjps://en.wikipedia.org/wiki/Hypercube
FindaHamiltoncycle
Listthebitstrings,inorder:1. __________2. __________3. __________4. __________5. __________6. __________7. __________8. __________9. __________
VillanovaCSC1300-DrPapalaskari 16
000
000
FindaHamiltoncycle
Listthebitstrings,inorder:1. __________2. __________3. __________4. __________5. __________6. __________7. __________8. __________9. __________10. __________11. __________12. __________13. __________14. __________15. __________16. __________
VillanovaCSC1300-DrPapalaskari 17ImagebyWatchduck(a.k.a.TilmanPiesk)-Ownwork.ThisW3C-unspecifiedvectorimagewascreatedwithInkscape.,PublicDomain,hjps://commons.wikimedia.org/w/index.php?curid=11402515
GraycodesDevicesindicateposiWonbyclosingandopeningswitchesorsensinganddigiWzingrotaWonalangle.Decimal Binary... ...2 0103 0114 100... ...
Physicalswitchesarenotidealandiftheydon’tswitchstateinsynchrony,thenumbermayappearas101.ThetransiWonmightlooklike011—001—101—100.
VillanovaCSC1300-DrPapalaskari 18
MoWvaWon
Problemhere:allthreebitsarechanging!
Graycodes
IfonlyonebitchangesataGraycodes(alsocalled“reflectedbinarycode”)solvethisproblembychangingonlyonebitataWmeSeealso:hjps://en.wikipedia.org/wiki/Gray_code VillanovaCSC1300-DrPapalaskari 19
MoWvaWon
Graphicby“Perlygatekeeper-Ownwork,CCBY-SA4.0,”hjps://commons.wikimedia.org/w/index.php?curid=59774462
HamiltonCycle:SufficientcondiWon(butNOTnecessary)
LetGbeagraphwithnverWceswheren≥3.Ifthedegreeofeveryvertexismorethann/2,thenGhasaHamiltoncycle.
VillanovaCSC1300-DrPapalaskari 20
IntuiWon:GraphswithlotsofedgesmusthaveaHamiltoncycle
HamiltonCycle:NecessarycondiWons(butNOTsufficient)
IfagraphhasaHamiltoncycle:• k(G)=1(i.e.,thenumberofcomponentsis1,soitisa
connectedgraph)• NoverWcesofdegree1• NocutverWces/edges• ForeverypropersubsetSofV(G),
k(G-S)≤|S|
VillanovaCSC1300-DrPapalaskari 21
Nosimplenecessaryandsufficientcondi9onsareknowntoverifyexistenceofaHamiltoncycleforgeneralgraphs.Thismeansthattheproblemhasnoknownefficientsolu9on.
IntuiWon:Inhamiltoniangraphs,youcanremovesomeverWceswithoutdisconnecWngthemintotoomanypieces(i.e.,morethanthenumberofverWcesremoved).
SecWon12.4,Problem16
VillanovaCSC1300-DrPapalaskari 22
Top Related