cse 373: data structures and algorithms · graph traversal uses in addition to finding paths, we...
TRANSCRIPT
Instructor:LiliandeGreefQuarter:Summer2017
CSE373:DataStructuresandAlgorithmsLecture16:Dijkstra’sAlgorithm(Graphs)
Today
• Announcements• GraphTraversalsContinued• RemarksonDFS&BFS• Shortestpathsforweightedgraphs:Dijkstra’sAlgorithm!
Announcements:
Homework4isout!
• DuenextFriday(August4th)at5:00pm
• Maychoosetopair-programifyoulike!• Samecautionsaslasttimeapply:choosepartnersandwhentostartworkingwisely!
• Canalmostentirelycompleteusingmaterialbyendofthislecture
• Willdiscusssomesoftware-designconceptsnextweektohelpyoupreventsome(potentiallynon-obvious)bugs
Iwillhavethefinalexamquadruple-checked toavoidthesesituations!(Iamsosorry)
Anothermidtermcorrection… (&)
Bringyourmidtermto*any*officehourstogetyourpointback.
Graphs:TraversalsContinuedAndintroducingDijkstra’sAlgorithmforshortestpaths!
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
Comparison(usefulforDesignDecisions!)
• Whichonefindsshortest paths?• i.e.whichisbetterfor“whatistheshortestpathfromx toy”whenthere’smorethanonepossiblepath?
• Whichonecanuselessspaceinfindingapath?
• Athirdapproach:• Iterativedeepening(IDFS):
• TryDFSbutdisallowrecursionmorethanK levelsdeep• Ifthatfails,incrementK andstarttheentiresearchover
• LikeBFS,findsshortestpaths.LikeDFS,lessspace.
GraphTraversalUses
Inadditiontofindingpaths,wecanusegraphtraversalstoanswer:• Whatarealltheverticesreachable fromastartingvertex?• Isanundirectedgraphconnected?• Isadirectedgraphstronglyconnected?
• Butwhatifwewanttoactuallyoutputthepath?
• Howtodoit:• Insteadofjust“marking”anode,storethepreviousnodealongthepath• Whenyoureachthegoal,followpath fieldsbacktowhereyoustarted(andthenreversetheanswer)
• Ifjustwantedpathlength,couldputtheintegerdistanceateachnodeinsteadonce
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
AFewApplicationsofShortestWeightedPath
• Drivingdirections
• Cheapflightitineraries
• Networkrouting
• Criticalpathsinprojectmanagement
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
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
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
(extraspaceincaseyouwant/needit)
Thisiscalled… Dijkstra’sAlgorithm
NamedafteritsinventorEdsger Dijkstra (1930-2002)Trulyoneofthe“founders”ofcomputerscience;thisisjustoneofhismanycontributions
“Computerscienceisnomoreaboutcomputersthanastronomyisabouttelescopes.”
- Edsger Dijkstra
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
}
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
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
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
(spaceforscratchwork)
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
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
Example#3
• Howwillthe“best-cost-so-far”forYproceed?
• Isthisexpensive?
Y
X1 1 1 1
9080 70 60
10
... 1
becauseeachedge isprocessed
WhereareWe?
• Hadaproblem:Computeshortestpathsinaweightedgraphwithnonegativeweights
• Learnedanalgorithm:Dijkstra’s algorithm
• Whatshouldwedoafterlearninganalgorithm?• Proveitiscorrect
• Notobvious!• Wewillsketchthekeyideas
• Analyzeitsefficiency• Willdobetterbyusingadatastructurewelearnedearlier!
Correctness:Intuition
Roughintuition:
Allthe“known”verticeshavethecorrectshortestpath• Trueinitially:shortestpathtostartnodehascost0• Ifitstaystrueeverytimewemarkanode“known”,thenbyinductionthisholdsandeventuallyeverythingis“known”
Keyfactweneed:Whenwemarkavertex“known”wewon’tdiscoverashorterpathlater!• ThisholdsonlybecauseDijkstra’s algorithmpicksthenodewiththenextshortestpath-so-far
• Theproofisbycontradiction…
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
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
}}
Improvingasymptoticrunningtime
• Sofar:O(|V|2)
• Wehadasimilar“problem”withtopologicalsortbeingO(|V|2)duetoeachiterationlookingforthenodetoprocessnext• Wesolveditwithaqueueofzero-degreenodes• Buthereweneedthelowest-costnodeandcostscanchangeasweprocessedges
• Solution?• A holdingallunknownnodes,• Butmustsupport operation
• Mustmaintainareferencefromeachnodetoitscurrentpositioninthepriorityqueue• Conceptuallysimple,butcanbeapaintocodeup
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
}}
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|
PracticewithDesignDecisionsOurthree-eye-alienfrienduncoveredanimpressivelycompleteandup-to-datefamilytreetracingallthewaybacktotheancientemperorQinShiHuang.Thealienwantstofindadescendantofthisemperorwho’sstillalive,andcoulduseyouradvice!(AccordingtoWikipedia,QinShiHuanghad~50children,wow!)
Whatdatastructurewouldyourecommend?Why?
Whatalgorithmwouldyourecommend?Why?
(extraspacefornotes)