cse 373: data structures and algorithms · graph traversal uses in addition to finding paths, we...

32
Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 16: Dijkstra’s Algorithm (Graphs)

Upload: others

Post on 29-Feb-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Instructor:LiliandeGreefQuarter:Summer2017

CSE373:DataStructuresandAlgorithmsLecture16:Dijkstra’sAlgorithm(Graphs)

Page 2: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Today

• Announcements• GraphTraversalsContinued• RemarksonDFS&BFS• Shortestpathsforweightedgraphs:Dijkstra’sAlgorithm!

Page 3: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Announcements:

Homework4isout!

• DuenextFriday(August4th)at5:00pm

• Maychoosetopair-programifyoulike!• Samecautionsaslasttimeapply:choosepartnersandwhentostartworkingwisely!

• Canalmostentirelycompleteusingmaterialbyendofthislecture

• Willdiscusssomesoftware-designconceptsnextweektohelpyoupreventsome(potentiallynon-obvious)bugs

Page 4: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Iwillhavethefinalexamquadruple-checked toavoidthesesituations!(Iamsosorry)

Anothermidtermcorrection… (&)

Bringyourmidtermto*any*officehourstogetyourpointback.

Page 5: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Graphs:TraversalsContinuedAndintroducingDijkstra’sAlgorithmforshortestpaths!

Page 6: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

GraphTraversals:Recap&RunningTime• Traversals:GeneralIdea

• Startingfromonevertex,repeatedlyexploreadjacentvertices• Markeachvertexwevisit,sowedon’tprocesseachmorethanonce(cycles!)

• ImportantGraphTraversalAlgorithms:

• Assuming“choosenextvertex”isO(1),entiretraversalis• Usegraphrepresentedwithadjacency

DepthFirstSearch(DFS) BreadthFirstSearch(BFS)

Explore… asfaraspossiblebeforebacktracking

allneighborsfirstbefore nextlevelofneighbors

Choosenextvertexusing… recursionorastack aqueue

Page 7: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Comparison(usefulforDesignDecisions!)

• Whichonefindsshortest paths?• i.e.whichisbetterfor“whatistheshortestpathfromx toy”whenthere’smorethanonepossiblepath?

• Whichonecanuselessspaceinfindingapath?

• Athirdapproach:• Iterativedeepening(IDFS):

• TryDFSbutdisallowrecursionmorethanK levelsdeep• Ifthatfails,incrementK andstarttheentiresearchover

• LikeBFS,findsshortestpaths.LikeDFS,lessspace.

Page 8: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

GraphTraversalUses

Inadditiontofindingpaths,wecanusegraphtraversalstoanswer:• Whatarealltheverticesreachable fromastartingvertex?• Isanundirectedgraphconnected?• Isadirectedgraphstronglyconnected?

• Butwhatifwewanttoactuallyoutputthepath?

• Howtodoit:• Insteadofjust“marking”anode,storethepreviousnodealongthepath• Whenyoureachthegoal,followpath fieldsbacktowhereyoustarted(andthenreversetheanswer)

• Ifjustwantedpathlength,couldputtheintegerdistanceateachnodeinsteadonce

Page 9: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Singlesourceshortestpaths

• Done:BFStofindtheminimumpathlengthfromv tou inO(|E|+|V|)

• Actually,canfindtheminimumpathlengthfromv toeverynode• StillO(|E|+|V|)• Nofasterwayfora“distinguished”destinationintheworst-case

• Now:Weightedgraphs

Givenaweightedgraphandnodev,findtheminimum-costpathfromv toeverynode

• Asbefore,asymptoticallynoharderthanforonedestination

Page 10: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

AFewApplicationsofShortestWeightedPath

• Drivingdirections

• Cheapflightitineraries

• Networkrouting

• Criticalpathsinprojectmanagement

Page 11: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

NotaseasyasBFS

WhyBFSwon’twork:Shortestpathmaynothavethefewestedges• Annoyingwhenthishappenswithcostsofflights

500

100100 100

100

Wewillassumetherearenonegativeweights• Problem isill-defined iftherearenegative-costcycles• Today’s algorithm iswrong ifedges canbenegative– Thereareother,slower(butnotterrible)algorithms

7

10 5

-11

Page 12: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Algorithm:GeneralIdeaGoal: Fromonestartingvertex,whataretheshortestpathstoeachoftheothervertices(foraweightedgraph)?

Idea: SimilartoBFS• Repeatedlyincreasea“setofverticeswithknownshortestdistances”• Anyvertexnotinthissetwillhavea“bestdistancesofar”• Eachvertexhasa“cost”torepresenttheseshortest/bestdistances• Updatecosts(i.e.“bestdistancessofar”)asweaddverticestoset

A B

DC

F H

E

G

2 2 3

110 23

111

7

19

2

4 5

Page 13: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

ShortestPathExample#1vertex known? cost path

A

B

C

D

E

F

G

H

KnownSet(inorderadded):

A B

DC

F H

E

G

2 2 3

110 23

111

7

1

9

2

4 5

Page 14: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

(extraspaceincaseyouwant/needit)

Page 15: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Thisiscalled… Dijkstra’sAlgorithm

NamedafteritsinventorEdsger Dijkstra (1930-2002)Trulyoneofthe“founders”ofcomputerscience;thisisjustoneofhismanycontributions

“Computerscienceisnomoreaboutcomputersthanastronomyisabouttelescopes.”

- Edsger Dijkstra

Page 16: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Dijkstra’sAlgorithm(Pseudocode)

Dijkstra’sAlgorithm – thefollowingalgorithmforfindingsingle-sourceshortestpathsinaweightedgraph(directedorundirected)withnonegative-weightedges:

1. Foreachnodev, setv.cost = ¥ and v.known = false2. Setsource.cost = 03. Whilethereareunknownnodesinthegraph

a) Selecttheunknownnodev withlowestcostb) Markv asknownc) Foreachedge(v,u) withweightw,

c1 = v.cost + w //costofbestpaththroughv touc2 = u.cost //costofbestpathtou previouslyknownif(c1 < c2){ //ifthepaththroughv isbetteru.cost = c1u.path = v //forcomputingactualpaths

}

Page 17: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Dijkstra’sAlgorithm:Features

• Whenavertexismarkedknown,thecostoftheshortestpathtothatnodeisknown• Thepathisalsoknownbyfollowingback-pointers

• Whileavertexisstillnotknown,anothershorterpathtoit*might* stillbefound

Note:The“OrderAddedtoKnownSet”isnotimportant• Adetailabouthowthealgorithmworks(clientdoesn’tcare)• Notusedbythealgorithm(implementationdoesn’tcare)• Itissortedbypath-cost,resolvingtiesinsomeway

• Helpsgiveintuitionofwhythealgorithmworks

Page 18: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Dijkstra’sAlgorithm:CommentaryDijkstra’sAlgorithmisoneexampleof...

• Agreedyalgorithm:• Makealocallyoptimalchoiceateachstageto(hopefully)findaglobaloptimum• i.e.Settleonthebestlookingoptionateachrepeatedstep• Note:forsomeproblems,greedyalgorithmscannotfindbestanswer!

• Dynamicprogramming:• Solveacomplexproblembybreakingitdownintoacollectionofsimplersubproblems,solveeachofthosesubproblems justonce,andstoretheirsolutions.

• i.e.Savepartialsolutions,anduseittosolvefurtherpartstoavoidrepeatingwork

vertex known? cost path

A Y 0

B Y 2 A

C Y 1 A

D Y 4 A

E Y 11 G

F Y 4 B

G Y 8 H

H Y 7 F

Page 19: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Dijkstra’sAlgorithm:PracticeTime!

A B

CD

F

E

G

2

12 5

11

1

26

5 3

10

vertex known? cost path

A

B

C

D

E

F

G

Anorderofaddingverticestotheknownset:A) A,D,C,E,F,B,GB) A,D,C,E,B,F,GC) A,D,E,C,B,G,FD) A,D,E,C,B,F,G

Page 20: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

(spaceforscratchwork)

Page 21: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Dijkstra’sAlgorithm:PracticeTime!

A B

CD

F

E

G

2

12 5

11

1

26

5 3

10

vertex known? cost path

A

B

C

D

E

F

G

Anorderofaddingverticestotheknownset:A) A,D,C,E,F,B,GB) A,D,C,E,B,F,GC) A,D,E,C,B,G,FD) A,D,E,C,B,F,G

Page 22: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Dijkstra’sAlgorithm:PracticeTime!

A B

CD

F

E

G

2

12 5

11

1

26

5 3

10

vertex known? cost path

A Y 0

B £ 6 D

C £ 2 A

D Y 1 A

E £ 2 D

F £ 7 D

G £ 6 D

Anorderofaddingverticestotheknownset:A) A,D,C,E,F,B,GB) A,D,C,E,B,F,GC) A,D,E,C,B,G,FD) A,D,E,C,B,F,G

Page 23: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Example#3

• Howwillthe“best-cost-so-far”forYproceed?

• Isthisexpensive?

Y

X1 1 1 1

9080 70 60

10

... 1

becauseeachedge isprocessed

Page 24: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

WhereareWe?

• Hadaproblem:Computeshortestpathsinaweightedgraphwithnonegativeweights

• Learnedanalgorithm:Dijkstra’s algorithm

• Whatshouldwedoafterlearninganalgorithm?• Proveitiscorrect

• Notobvious!• Wewillsketchthekeyideas

• Analyzeitsefficiency• Willdobetterbyusingadatastructurewelearnedearlier!

Page 25: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Correctness:Intuition

Roughintuition:

Allthe“known”verticeshavethecorrectshortestpath• Trueinitially:shortestpathtostartnodehascost0• Ifitstaystrueeverytimewemarkanode“known”,thenbyinductionthisholdsandeventuallyeverythingis“known”

Keyfactweneed:Whenwemarkavertex“known”wewon’tdiscoverashorterpathlater!• ThisholdsonlybecauseDijkstra’s algorithmpicksthenodewiththenextshortestpath-so-far

• Theproofisbycontradiction…

Page 26: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Correctness:TheCloud(RoughSketch)

• Supposev isthenextnodetobemarkedknown(nexttoaddto“thecloudofknownvertices”)• Thebest-knownpathtovmusthaveonlynodes“inthecloud”

– Elsewewouldhavepickedanodeclosertothecloudthanv• Supposetheactualshortestpath tov isdifferent

– Itwon’tuseonlycloudnodes,orwewouldknowaboutit– Soitmustusenon-cloudnodes.Letw bethefirst non-cloudnodeonthispath.– Thepartofthepathuptow isalreadyknownandmustbeshorterthanthebest-knownpathtov.– Sovwouldnothavebeenpicked.Contradiction!

The Cloud of Known Vertices

Nextshortestpathfrominsidetheknowncloudv

Betterpathtov?

Source

w

Page 27: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Efficiency,firstapproach

Usepseudocode todetermineasymptoticrun-time• Noticeeachedgeisprocessedonlyonce

dijkstra(Graph G, Node start) {for each node: x.cost=infinity, x.known=falsestart.cost = 0while(not all nodes are known) {

b = find unknown node with smallest costb.known = truefor each edge (b,a) in Gif(!a.known)

if(b.cost + weight((b,a)) < a.cost){a.cost = b.cost + weight((b,a))a.path = b

}}

Page 28: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Improvingasymptoticrunningtime

• Sofar:O(|V|2)

• Wehadasimilar“problem”withtopologicalsortbeingO(|V|2)duetoeachiterationlookingforthenodetoprocessnext• Wesolveditwithaqueueofzero-degreenodes• Buthereweneedthelowest-costnodeandcostscanchangeasweprocessedges

• Solution?• A holdingallunknownnodes,• Butmustsupport operation

• Mustmaintainareferencefromeachnodetoitscurrentpositioninthepriorityqueue• Conceptuallysimple,butcanbeapaintocodeup

Page 29: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Efficiency,secondapproachUsepseudocode todetermineasymptoticrun-time

dijkstra(Graph G, Node start) {for each node: x.cost=infinity, x.known=falsestart.cost = 0build-heap with all nodeswhile(heap is not empty) {

b = deleteMin()b.known = truefor each edge (b,a) in Gif(!a.known)if(b.cost + weight((b,a)) < a.cost){

decreaseKey(a,“new cost – old cost”)a.path = b

}}

Page 30: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

Densevs.Sparse(again!)

• Firstapproach:O(|V|2)

• Secondapproach:O(|V|log|V|+|E|log|V|)

• Sowhichisbetter?• DenseorSparse?O(|V|log|V|+|E|log|V|)(if|E|>|V|,thenit’sO(|E|log|V|))• DenseorSparse?O(|V|2)

• But,remembertheseareworst-caseandasymptotic• Priorityqueuemighthaveslightlyworseconstantfactors• Ontheotherhand,for“normalgraphs”,wemightcalldecreaseKey rarely(ornotpercolatefar),making|E|log|V|morelike|E|

Page 31: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

PracticewithDesignDecisionsOurthree-eye-alienfrienduncoveredanimpressivelycompleteandup-to-datefamilytreetracingallthewaybacktotheancientemperorQinShiHuang.Thealienwantstofindadescendantofthisemperorwho’sstillalive,andcoulduseyouradvice!(AccordingtoWikipedia,QinShiHuanghad~50children,wow!)

Whatdatastructurewouldyourecommend?Why?

Whatalgorithmwouldyourecommend?Why?

Page 32: CSE 373: Data Structures and Algorithms · Graph Traversal Uses In addition to finding paths, we can use graph traversals to answer: •What are all the vertices reachablefrom a starting

(extraspacefornotes)