cse 373: data structures and algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...what...
TRANSCRIPT
![Page 1: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/1.jpg)
Instructor:LiliandeGreefQuarter:Summer2017
CSE373:DataStructuresandAlgorithmsLecture15:GraphDataStructures,TopologicalSort,
andTraversals(DFS,BFS)
![Page 2: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/2.jpg)
Today:
• Announcements• Graphdatastructures• TopologicalSort• GraphTraversals• DepthFirstSearch(DFS)• BreadthFirstSearch(BFS)
![Page 3: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/3.jpg)
Announcement:ReceivedCourseFeedback
What’sworkingwell:• Walkingthroughin-classexamples• Posted,printed,andannotatedslides• Interactivequestions&in-classpartnerdiscussion
Thingstoaddress:• Amounttowriteonprintedslides• Whyusingpollingsystemforin-classexercises• Concernaboutnotgettingthroughentireslidedeck
![Page 4: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/4.jpg)
0
5
10
15
20
25
Freshman Sophomore Junior Senior 5thyear Graduatestudent
other
YearinProgramthisFall
![Page 5: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/5.jpg)
0510152025303540
LastTimeProgrammed/TakenCSCourse
![Page 6: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/6.jpg)
RepresentedMajors
• Engineering• Math• Science• Informatics• Geology• Spanish• AsianLanguage• Pre-major• Andmore!
![Page 7: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/7.jpg)
GraphDataStructuresAcoupleofdifferentwaystostoreadjacencies
![Page 8: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/8.jpg)
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
![Page 9: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/9.jpg)
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
![Page 10: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/10.jpg)
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)
![Page 11: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/11.jpg)
AdjacencyMatrixProperties
• Howwilltheadjacencymatrixvaryforanundirectedgraph?• Undirectedwillbesymmetricaroundthediagonal
• Howcanweadapttherepresentationforweightedgraphs?• InsteadofaBoolean,storeanumberineachcell• Needsomevaluetorepresent‘notanedge’
• Insome situations,0or-1works
![Page 12: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/12.jpg)
AdjacencyList
• Assigneachnodeanumberfrom0 to|V|-1• Anarrayoflength|V|inwhicheachentrystoresalistofalladjacentvertices(e.g.,linkedlist)
B S
M
E
(0) (1)(2)
(3)
0
1
2
3
![Page 13: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/13.jpg)
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)
![Page 14: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/14.jpg)
Algorithms
Okay,wecanrepresentgraphs
Nowwe’llimplementsomeusefulandnon-trivialalgorithms!• TopologicalSort• ShortestPaths• Related:Determiningifsuchapathexists• DepthFirstSearch• BreadthFirstSearch
![Page 15: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/15.jpg)
Graphs:TopologicalSortOrderingverticesinaDAG
![Page 16: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/16.jpg)
TopologicalSortTopologicalsort: GivenaDAG,orderalltheverticessothateveryvertexcomesbeforeallofitsneighbors
Oneexampleoutput:
CSE 142 CSE 143
CSE 374
CSE 373CSE 410
MATH 126
CSE 417
CSE 415
CSE 413
XYZ
![Page 17: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/17.jpg)
Questionsandcomments
• WhydoweperformtopologicalsortsonlyonDAGs?
• Istherealwaysauniqueanswer?
• DosomeDAGshaveexactly1answer?
• Terminology:ADAGrepresentsapartialorder andatopologicalsortproducesatotalorder thatisconsistentwithit
0
13
2
4
![Page 18: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/18.jpg)
Afewofitsuses
• Figuringouthowtograduate
• Computinganorderinwhichtorecompute cellsinaspreadsheet
• DetermininganordertocompilefilesusingaMakefile
• Ingeneral,takingadependencygraphandfindinganorderofexecution
![Page 19: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/19.jpg)
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
![Page 20: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/20.jpg)
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
![Page 21: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/21.jpg)
Notice
• Neededavertexwithin-degree0tostart• Willalwayshaveatleast1because
• Tiesamongverticeswithin-degreesof0canbebrokenarbitrarily• Canbemorethanonecorrectanswer,bydefinition,dependingonthegraph
![Page 22: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/22.jpg)
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--;
}
![Page 23: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/23.jpg)
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
![Page 24: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/24.jpg)
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:
![Page 25: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/25.jpg)
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);
}}
![Page 26: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/26.jpg)
GraphTraversalsDepth- andBreadth- FirstSearches!
![Page 27: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/27.jpg)
IntroductoryExample:GraphTraversals
Howwouldacomputersystematicallyfindapaththroughthemaze?
A B C D E
F G H I J
K L M N O
Source
Destination
![Page 28: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/28.jpg)
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.
![Page 29: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/29.jpg)
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
![Page 30: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/30.jpg)
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)
}
![Page 31: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/31.jpg)
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
![Page 32: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/32.jpg)
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
}}
![Page 33: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/33.jpg)
Practicetime!WhatisonepossibleorderofvisitingthenodesofthefollowinggraphwhenusingBreadthFirstSearch(BFS)?
A) MNOPQR
B) NQMPOR
M
R
N
Q
O
P
C)QMNPRO
D)QMNPOR
![Page 34: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures...What is the Data Structure? •So graphs are really useful for lots of data and questions](https://reader030.vdocuments.us/reader030/viewer/2022041119/5f31ed5cde66ac40176e0263/html5/thumbnails/34.jpg)