Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Instructor:LiliandeGreefQuarter:Summer2017
CSE373:DataStructuresandAlgorithmsLecture15:GraphDataStructures,TopologicalSort,
andTraversals(DFS,BFS)
Today:
• Announcements• Graphdatastructures• TopologicalSort• GraphTraversals• DepthFirstSearch(DFS)• BreadthFirstSearch(BFS)
Announcement:ReceivedCourseFeedback
What’sworkingwell:• Walkingthroughin-classexamples• Posted,printed,andannotatedslides• Interactivequestions&in-classpartnerdiscussion
Thingstoaddress:• Amounttowriteonprintedslides• Whyusingpollingsystemforin-classexercises• Concernaboutnotgettingthroughentireslidedeck
0
5
10
15
20
25
Freshman Sophomore Junior Senior 5thyear Graduatestudent
other
YearinProgramthisFall
0510152025303540
LastTimeProgrammed/TakenCSCourse
RepresentedMajors
• Engineering• Math• Science• Informatics• Geology• Spanish• AsianLanguage• Pre-major• Andmore!
GraphDataStructuresAcoupleofdifferentwaystostoreadjacencies
WhatistheDataStructure?
• Sographsarereallyusefulforlotsofdataandquestions• Forexample,“what’sthelowest-costpathfromxtoy”
• Butweneedadatastructurethatrepresentsgraphs
• The“bestone”candependon:• Propertiesofthegraph(e.g.,denseversussparse)• Thecommonqueries(e.g.,“is(u,v) anedge?”versus“whataretheneighborsofnodeu?”)
• Sowe’lldiscussthetwostandardgraphrepresentations• AdjacencyMatrix andAdjacencyList• Differenttrade-offs,particularlytimeversusspace
AdjacencyMatrix
• Assigneachnodeanumberfrom0 to|V|-1• A|V| x|V|matrix(i.e.,2-Darray)ofBooleans(or1vs.0)• IfM isthematrix,thenM[u][v] == truemeansthereisanedgefromu tov
B S
M
E
(0) (1)(2)
(3)
0 1 2 3
0
1
2
3
AdjacencyMatrixProperties
• Runningtimeto:• Getavertex’sout-edges:• Getavertex’sin-edges:• Decideifsomeedgeexists:• Insertanedge:• Deleteanedge:
• Spacerequirements:
• Bestforsparseordensegraphs?
F F F F
T F T T
F F T T
F T T F
0 1 2 3
0
1
2
3
B S
M
E
(0)(1)
(2)
(3)
AdjacencyMatrixProperties
• Howwilltheadjacencymatrixvaryforanundirectedgraph?• Undirectedwillbesymmetricaroundthediagonal
• Howcanweadapttherepresentationforweightedgraphs?• InsteadofaBoolean,storeanumberineachcell• Needsomevaluetorepresent‘notanedge’
• Insome situations,0or-1works
AdjacencyList
• Assigneachnodeanumberfrom0 to|V|-1• Anarrayoflength|V|inwhicheachentrystoresalistofalladjacentvertices(e.g.,linkedlist)
B S
M
E
(0) (1)(2)
(3)
0
1
2
3
AdjacencyListProperties
• Runningtimeto:• Getallofavertex’sout-edges:
whered isout-degreeofvertex• Getallofavertex’sin-edges:
(butcouldkeepasecondadjacencylistforthis!)• Decideifsomeedgeexists:
whered isout-degreeofsource• Insertanedge:
(unlessyouneedtocheckifit’sthere)• Deleteanedge:
whered isout-degreeofsource
• Spacerequirements: Bestforsparseordensegraphs?
0
1
2
3
0 3 /
/
3 /
1 2 /
2
B S
M
E
(0)(1)
(2)
(3)
Algorithms
Okay,wecanrepresentgraphs
Nowwe’llimplementsomeusefulandnon-trivialalgorithms!• TopologicalSort• ShortestPaths• Related:Determiningifsuchapathexists• DepthFirstSearch• BreadthFirstSearch
Graphs:TopologicalSortOrderingverticesinaDAG
TopologicalSortTopologicalsort: GivenaDAG,orderalltheverticessothateveryvertexcomesbeforeallofitsneighbors
Oneexampleoutput:
CSE 142 CSE 143
CSE 374
CSE 373CSE 410
MATH 126
CSE 417
CSE 415
CSE 413
XYZ
Questionsandcomments
• WhydoweperformtopologicalsortsonlyonDAGs?
• Istherealwaysauniqueanswer?
• DosomeDAGshaveexactly1answer?
• Terminology:ADAGrepresentsapartialorder andatopologicalsortproducesatotalorder thatisconsistentwithit
0
13
2
4
Afewofitsuses
• Figuringouthowtograduate
• Computinganorderinwhichtorecompute cellsinaspreadsheet
• DetermininganordertocompilefilesusingaMakefile
• Ingeneral,takingadependencygraphandfindinganorderofexecution
AFirstAlgorithmforTopologicalSort
1. Label(“mark”)eachvertexwithitsin-degree• Could“writeinafieldinthevertex”• Couldalsodothisviaadatastructure(e.g.,array)ontheside
2. Whilethereareverticesnotyetoutput:a) Chooseavertexv within-degreeof0b) Outputv andconceptually removeitfromthegraphc) Foreachvertexu adjacenttov (i.e.u suchthat(v,u)inE),
decrementthein-degree ofu
Example Output:
Node:126142143374373410413415417XYZRemoved?In-degree: 0021111113
CSE 142 CSE 143
CSE 374
CSE 373CSE 410
MATH 126
CSE 417
CSE 415
CSE 413
XYZ
Notice
• Neededavertexwithin-degree0tostart• Willalwayshaveatleast1because
• Tiesamongverticeswithin-degreesof0canbebrokenarbitrarily• Canbemorethanonecorrectanswer,bydefinition,dependingonthegraph
Runningtime?
•Whatistheworst-caserunningtime?• Initialization (assumingadjacencylist)• Sumofallfind-new-vertex (becauseeachO(|V|))• Sumofalldecrements (assumingadjacencylist)• Sototalis – notgoodforasparsegraph!
labelEachVertexWithItsInDegree();for(i = 0; i < numVertices; i++){v = findNewVertexOfDegreeZero();put v next in outputfor each u adjacent to vu.indegree--;
}
Doingbetter
Thetrickistoavoidsearchingforazero-degreenodeeverytime!• Keepthe“pending”zero-degreenodesinalist,stack,queue,bag,table,orsomething• Orderweprocessthemaffectsoutputbutnotcorrectnessorefficiency,providedthatadd/removearebothO(1)
Usingaqueue:
1. Labeleachvertexwithitsin-degree,enqueue 0-degreenodes2. Whilequeueisnotempty
a) v =dequeue()b) Outputv andremoveitfromthegraphc) Foreachvertexu adjacenttov (i.e.u suchthat(v,u)inE),decrementthein-degreeofu,
ifnewdegreeis0,enqueue it
Example:TopologicalSortUsingQueues
Thetrickistoavoidsearchingforazero-degreenodeeverytime!
1. Labeleachvertexwithitsin-degree,enqueue 0-degreenodes
2. Whilequeueisnotemptya) v =dequeue()
b) Outputv andremoveitfromthegraph
c) Foreachvertexu adjacenttov (i.e.u suchthat(v,u)inE),decrementthein-degreeofu,ifnewdegreeis0,enqueue it
A
D
BC
Node A B C D
Removed?
In-degree 0 1 1 2
Queue:
Output:
Runningtime?
• Whatistheworst-caserunningtime?• Initialization: (assumingadjacencylist)• Sumofallenqueues anddequeues:• Sumofalldecrements: (assumingadjacencylist)• Total: – muchbetterforsparsegraph!
labelAllAndEnqueueZeros();for(i=0; ctr < numVertices; ctr++){
v = dequeue();put v next in outputfor each u adjacent to v {u.indegree--;if(u.indegree==0) enqueue(u);
}}
GraphTraversalsDepth- andBreadth- FirstSearches!
IntroductoryExample:GraphTraversals
Howwouldacomputersystematicallyfindapaththroughthemaze?
A B C D E
F G H I J
K L M N O
Source
Destination
Note:underthehood,we’reusingagraphtorepresentthemaze
A B C D E
F G H I J
K L M N O
Source
Destination
A B C D E
F G H I J
N OK L M
Source
Destination
Ingraphterminology:findapath(ifany)fromonevertextoanother.
Findapath(ifany)fromonevertextoanother.
Idea:Repeatedlyexploreandkeeptrackofadjacentvertices.Markeachvertexwevisit,sowedon’tprocesseachmorethanonce.
A B C D E
F G H I J
K L M N O
Source
Destination
DepthFirstSearch(DFS)
DepthFirstSearch (DFS):Exploreasfaraspossiblealongeachbranchbeforebacktracking
Repeatedlyexploreadjacentverticesusing orMarkeachvertexwevisit,sowedon’tprocesseachmorethanonce.
Examplepseudocode: DFS(Node start) {mark and process startfor each node u adjacent to start
if u is not markedDFS(u)
}
Findapath(ifany)fromonevertextoanother.
Idea:Repeatedlyexploreandkeeptrackofadjacentvertices.Markeachvertexwevisit,sowedon’tprocesseachmorethanonce.
A B C D E
F G H I J
K L M N O
Source
Destination
BreadthFirstSearch(BFS)BreadthFirstSearch (BFS):Exploreneighborsfirst,beforemovingtothenextlevelofneighbors.
RepeatedlyexploreadjacentverticesusingMarkeachvertexwevisit,sowedon’tprocesseachmorethanonce.
Examplepseudocode:BFS(Node start) {initialize queue q and enqueue startmark start as visitedwhile(q is not empty) {next = q.dequeue() // and “process”for each node u adjacent to nextif(u is not marked)mark u and enqueue onto q
}}
Practicetime!WhatisonepossibleorderofvisitingthenodesofthefollowinggraphwhenusingBreadthFirstSearch(BFS)?
A) MNOPQR
B) NQMPOR
M
R
N
Q
O
P
C)QMNPRO
D)QMNPOR