csc 1300 – discrete structures villanova universitymap/1300/s19/14graphtraversals.pdf · euler...

22
Graph Traversals CSC 1300 – Discrete Structures Villanova University Villanova CSC 1300 - Dr Papalaskari 1

Upload: others

Post on 24-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

GraphTraversals

CSC1300–DiscreteStructuresVillanovaUniversity

VillanovaCSC1300-DrPapalaskari 1

Page 2: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

MajorThemesGraphtraversals:•  Eulercircuit/trail

–  Everyedgeexactlyonce

•  Hamiltoncycle– Everyvertexexactlyonce– Shortestcycle:

•  TravelingSalesmanProblem

– ShortestPath:•  Dijkstra’salgorithm

8

2

574

3 2

8

59

VillanovaCSC1300-DrPapalaskari 2

Page 3: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

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

Page 4: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

Euler’sSoluWontoTheBridgesofKönigsberg

A

C

B

D

LeonhardEuler(1707-1783)rephrasedthequesWonintermsofamul9graph(i.e.,mulWpleedgesallowed)

CADB

IsthereacircuitinthismulWgraphthattraversesallitsedgesexactlyonce?

VillanovaCSC1300-DrPapalaskari 4

Page 5: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

EulercircuitsandtrailsEulercircuit:traversesalltheedgesofthegraphexactlyonceEulertrail:atrailtraversingalltheedgesofthegraphexactlyonce

Examples:

HasanEulercircuit

HasanEulertrail

HasneitherEulercircuitnorEulertrail

VillanovaCSC1300-DrPapalaskari 5

Page 6: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

NecessaryandsufficientcondiWonsforEulercycles

Theorem:AconnectedgraphGhasanEulercycleifandonlyifeveryvertexofGhasevendegree.

(è)Anecessarycondi9onGhasanEulercycleonlyifeveryvertexhasevendegreeAssumeGhasanEulercycle.ObservethateveryWmethecyclepassesthroughavertex,itcontributes2tothevertex’sdegree,sincethecycleentersviaanedgeincidentwiththisvertexandleavesviaanothersuchedge.

VillanovaCSC1300-DrPapalaskari 6

Page 7: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

(ç)Asufficientcondi-onIfeveryvertexinGhasevendegree,GhasanEulercycle

LemmaAssumeeveryvertexinamulWgraphhasevendegree.Startatanarbitrarynon-isolatedvertexv0,chooseanarbitraryedge(v0,v1),thenchooseanarbitraryunusededgefromv1andsoon.ThenaeerafinitenumberofstepstheprocesswillarriveatthestarWngvertexv0,yieldingacyclewithdisWnctedges.ProofIntheaboveprocedure,onceyouenteredavertexv,therewillalwaysbeanotherunusededgetoexitvbecausevhasanevendegreeandonlyanevennumberoftheedgesincidentwithithadbeenusedbeforeyouenteredit.Theonlyedgefromwhichyoumaynotbeabletoexitaeerenteringitisv0(becauseanoddnumberofedgesincidentwithv0havebeenusedasyoudidn’tenteritatthebeginning),butifyouhavereachedv0,thenyouhavealreadyconstructedarequiredcycle.

VillanovaCSC1300-DrPapalaskari 7

Page 8: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

AprocedureforconstrucWnganEulercycle

AlgorithmEuler(G)//Input:ConnectedgraphGwithallverWceshavingevendegrees//Output:EulercycleConstructacycleinGusingtheprocedurefromLemmaRemovealltheedgesofcyclefromGtogetsubgraphHwhileHhasedgesfindanon-isolatedvertexvthatisbothincycleandinH//theexistenceofsuchavertexisguaranteedbyG’sconnecWvityconstructsubcycleinHusingLemma’sproceduresplicesubcycleintocycleatvremovealltheedgesofsubcyclefromHreturncycle

VillanovaCSC1300-DrPapalaskari 8

Page 9: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

ExampleG

cycle1231

H135724681357

2468

13572468

subcycle34653

13572468

splicing:346531231

1357

2468

13572468

VillanovaCSC1300-DrPapalaskari 9

Page 10: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

Example(cont.)

H

1357

2468

H

13572468

splicing678612346531

13572468

Eulercycleobtained12346786531

13572468

VillanovaCSC1300-DrPapalaskari 10

cycle12346531

Page 11: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

NecessaryandsufficientcondiWonsforEulertrails

TheoremAconnectedgraphGhasanEulertrailifandonlyififithasexactlytwoverWcesofodddegree.

VillanovaCSC1300-DrPapalaskari 11

Page 12: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

IcosianGameIcosianGameThispuzzlehasbeeninventedbytherenownedIrishmathemaWcianSirWilliamHamilton(1805-1865)andpresentedtotheworldunderthenameoftheIcosianGame.Thegame'sboardwasawoodenboardwithholesrepresenWngmajorworldciWeswithgroovesrepresenWngconnecWonsbetweenthem.TheobjectofthegamewastofindacircularroutethatwouldpassthroughalltheciWesexactlyoncebeforereturningtothestarWngpoint.

VillanovaCSC1300-DrPapalaskari 12

Page 13: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

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

Page 14: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

HamiltonCycle?

K6 C3 K4

VillanovaCSC1300-DrPapalaskari 14

Page 15: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

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

Page 16: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

FindaHamiltoncycle

Listthebitstrings,inorder:1.  __________2.  __________3.  __________4.  __________5.  __________6.  __________7.  __________8.  __________9.  __________

VillanovaCSC1300-DrPapalaskari 16

000

000

Page 17: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

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

Page 18: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

GraycodesDevicesindicateposiWonbyclosingandopeningswitchesorsensinganddigiWzingrotaWonalangle.Decimal Binary... ...2 0103 0114 100... ...

Physicalswitchesarenotidealandiftheydon’tswitchstateinsynchrony,thenumbermayappearas101.ThetransiWonmightlooklike011—001—101—100.

VillanovaCSC1300-DrPapalaskari 18

MoWvaWon

Problemhere:allthreebitsarechanging!

Page 19: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

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

Page 20: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

HamiltonCycle:SufficientcondiWon(butNOTnecessary)

LetGbeagraphwithnverWceswheren≥3.Ifthedegreeofeveryvertexismorethann/2,thenGhasaHamiltoncycle.

VillanovaCSC1300-DrPapalaskari 20

IntuiWon:GraphswithlotsofedgesmusthaveaHamiltoncycle

Page 21: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

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).

Page 22: CSC 1300 – Discrete Structures Villanova Universitymap/1300/s19/14graphtraversals.pdf · Euler circuits and trails Euler circuit: traverses all the edges of the graph exactly once

SecWon12.4,Problem16

VillanovaCSC1300-DrPapalaskari 22