34
Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 15: Graph Data Structures, Topological Sort, and Traversals (DFS, BFS)

CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Instructor:LiliandeGreefQuarter:Summer2017

CSE373:DataStructuresandAlgorithmsLecture15:GraphDataStructures,TopologicalSort,

andTraversals(DFS,BFS)

Page 2: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Today:

• Announcements• Graphdatastructures• TopologicalSort• GraphTraversals• DepthFirstSearch(DFS)• BreadthFirstSearch(BFS)

Page 3: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Announcement:ReceivedCourseFeedback

What’sworkingwell:• Walkingthroughin-classexamples• Posted,printed,andannotatedslides• Interactivequestions&in-classpartnerdiscussion

Thingstoaddress:• Amounttowriteonprintedslides• Whyusingpollingsystemforin-classexercises• Concernaboutnotgettingthroughentireslidedeck

Page 4: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

0

5

10

15

20

25

Freshman Sophomore Junior Senior 5thyear Graduatestudent

other

YearinProgramthisFall

Page 5: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

0510152025303540

LastTimeProgrammed/TakenCSCourse

Page 6: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

RepresentedMajors

• Engineering• Math• Science• Informatics• Geology• Spanish• AsianLanguage• Pre-major• Andmore!

Page 7: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

GraphDataStructuresAcoupleofdifferentwaystostoreadjacencies

Page 8: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

AdjacencyMatrixProperties

• Howwilltheadjacencymatrixvaryforanundirectedgraph?• Undirectedwillbesymmetricaroundthediagonal

• Howcanweadapttherepresentationforweightedgraphs?• InsteadofaBoolean,storeanumberineachcell• Needsomevaluetorepresent‘notanedge’

• Insome situations,0or-1works

Page 12: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Algorithms

Okay,wecanrepresentgraphs

Nowwe’llimplementsomeusefulandnon-trivialalgorithms!• TopologicalSort• ShortestPaths• Related:Determiningifsuchapathexists• DepthFirstSearch• BreadthFirstSearch

Page 15: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Graphs:TopologicalSortOrderingverticesinaDAG

Page 16: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Questionsandcomments

• WhydoweperformtopologicalsortsonlyonDAGs?

• Istherealwaysauniqueanswer?

• DosomeDAGshaveexactly1answer?

• Terminology:ADAGrepresentsapartialorder andatopologicalsortproducesatotalorder thatisconsistentwithit

0

13

2

4

Page 18: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Afewofitsuses

• Figuringouthowtograduate

• Computinganorderinwhichtorecompute cellsinaspreadsheet

• DetermininganordertocompilefilesusingaMakefile

• Ingeneral,takingadependencygraphandfindinganorderofexecution

Page 19: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Notice

• Neededavertexwithin-degree0tostart• Willalwayshaveatleast1because

• Tiesamongverticeswithin-degreesof0canbebrokenarbitrarily• Canbemorethanonecorrectanswer,bydefinition,dependingonthegraph

Page 22: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

GraphTraversalsDepth- andBreadth- FirstSearches!

Page 27: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

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 Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”

Practicetime!WhatisonepossibleorderofvisitingthenodesofthefollowinggraphwhenusingBreadthFirstSearch(BFS)?

A) MNOPQR

B) NQMPOR

M

R

N

Q

O

P

C)QMNPRO

D)QMNPOR

Page 34: CSE 373: Data Structures and Algorithms · A First Algorithm for Topological Sort 1.Label (“mark”) each vertex with its in-degree • Could “write in a field in the vertex”