Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank...

105
Adam Blank Winter 2017 Lecture 20 CSE 332 Data Structures and Parallelism

Transcript of Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank...

Page 1: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Adam Blank Winter 2017Lecture 20

CSE332

Data Structures and Parallelism

Page 2: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

CSE 332: Data Structures and Parallelism

Graphs 1:What is a Graph?

DFS and BFS

Page 3: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

LinkedLists are to Trees as Trees are to . . . 1

Where We’ve BeenEssential ADTs: Lists, Stacks, Queues, Priority Queues, Heaps,Vanilla Trees, BSTs, Balanced Trees, B-Trees, Hash TablesImportant Algorithms: Traversals, Sorting, buildHeap, Prefix Sum,“Divide and Conquer Algorithms”Concurrency: Parallelism, Synchronization

So, what’s next?

Graphs and Graph AlgorithmsA nearly universal data structure that will change the way you thinkabout the world. (Seriously.)

Graphs are more common than all the other data structures combined(this is in part true, because they’re a generalization of most of theother data structures).

Page 4: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

A Graph is a Thingy. . . 2

a

V = {a}, E =∅

b

c

V = {b,c},E = {{b,c}}

f

e d

V = {d,e, f},E = {{e, f},{ f ,d}}

g

h

ij

V = {g,h, i, j},E = {{x,y} ∣ x,y ∈V ∧ x ≠ y}

We call the circles vertices and the lines edges.

Definition (Graph)A Graph is a pair, G = (V,E), where:

V is a set of vertices, andE is a set of edges (pairs of vertices).

Page 5: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

A Graph is a Thingy. . . 2

a

V = {a}, E =∅

b

c

V = {b,c},E = {{b,c}}

f

e d

V = {d,e, f},E = {{e, f},{ f ,d}}

g

h

ij

V = {g,h, i, j},E = {{x,y} ∣ x,y ∈V ∧ x ≠ y}

We call the circles vertices and the lines edges.

Definition (Graph)A Graph is a pair, G = (V,E), where:

V is a set of vertices, andE is a set of edges (pairs of vertices).

Page 6: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Graphs are an ADT? 3

We can think of graphs as an ADT with operations likex.isNeighbor(y), but it’s not clear what should be included:

x.reachableFrom(y)?x.shortestPathTo(y)?x.centrality()?. . .

We will approach graphs differently:Graphs are an abstract concept that we can apply in different waysto the problem at hand.A “graph problem” is one that we can mathematically model as agraph. . .

Page 7: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 4

Consider the following questions:How can I allocate registers to variables in a program?

How popular am I?

What’s the minimum amount of wire I have to use to connect allthese homes?

Just how does Google work?

Can I automatically tag the words of a sentence with their part ofspeech?

How do I make look-ups in databases quick at Facebook’s scale?

Page 8: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? Finding Important People 5

If this graph is a social network, who is most important?Who has the most influence?

Page 9: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 6

What is the cheapest/shortest/etc. flight from location A to B?

http://allthingsgraphed.com/public/images/airline-google-earth.png

Page 10: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 7

How do words associate with each other? How easy are words toconfuse? How similar are spellings?

http://allthingsgraphed.com/public/images/wordnet-synonyms/good-evil-graph-path.png

Page 11: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 8

Where should we target cancer in a particular patient’s body?

https://gigaom.com/2013/03/26/how-researchers-are-fighting-lung-cancer-using-pagerank/

Page 12: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 9

How far am I from my friends? Why does my social network lookclustered? Why are all my friends more popular than I am?

http://allthingsgraphed.com/public/images/facebook/facebook-friend-graph.png

Page 13: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 10

What can we find out from political blog data? Who listens to whom?

http://allthingsgraphed.com/public/images/political-blogs-2004/left-right.png

Page 14: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 11

What effect did Robin Williams have on the people around him?

http://allthingsgraphed.com/public/images/imdb/robin-williams-graph.png

Page 15: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Why Graphs? 12

What happens when veteran teachers leave school networks?

http://www.mentalmunition.com/2013/05/using-social-network-analysis-to-find.html

Page 16: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

Maps

Vertices: regions; Edges: “is next to”

The Internet

Vertices: websites; Edges: “has a link to”

Social Networks

Vertices: people; Edges: “is friends with”

A Running Program

Vertices: methods; Edges: “calls”

A Chess Game

Vertices: boards; Edges: “can move to”

Telephone Lines

Vertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 17: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The Internet

Vertices: websites; Edges: “has a link to”

Social Networks

Vertices: people; Edges: “is friends with”

A Running Program

Vertices: methods; Edges: “calls”

A Chess Game

Vertices: boards; Edges: “can move to”

Telephone Lines

Vertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 18: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The InternetVertices: websites; Edges: “has a link to”

Social Networks

Vertices: people; Edges: “is friends with”

A Running Program

Vertices: methods; Edges: “calls”

A Chess Game

Vertices: boards; Edges: “can move to”

Telephone Lines

Vertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 19: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The InternetVertices: websites; Edges: “has a link to”

Social NetworksVertices: people; Edges: “is friends with”

A Running Program

Vertices: methods; Edges: “calls”

A Chess Game

Vertices: boards; Edges: “can move to”

Telephone Lines

Vertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 20: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The InternetVertices: websites; Edges: “has a link to”

Social NetworksVertices: people; Edges: “is friends with”

A Running ProgramVertices: methods; Edges: “calls”

A Chess Game

Vertices: boards; Edges: “can move to”

Telephone Lines

Vertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 21: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The InternetVertices: websites; Edges: “has a link to”

Social NetworksVertices: people; Edges: “is friends with”

A Running ProgramVertices: methods; Edges: “calls”

A Chess GameVertices: boards; Edges: “can move to”

Telephone Lines

Vertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 22: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The InternetVertices: websites; Edges: “has a link to”

Social NetworksVertices: people; Edges: “is friends with”

A Running ProgramVertices: methods; Edges: “calls”

A Chess GameVertices: boards; Edges: “can move to”

Telephone LinesVertices: houses; Edges: “telephone line between”

CSE Courses

Vertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 23: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Modelling Problems with Graphs 13

To model a problem with a graph, you need to make two choices1 What are the vertices?2 What are the edges?

MapsVertices: regions; Edges: “is next to”

The InternetVertices: websites; Edges: “has a link to”

Social NetworksVertices: people; Edges: “is friends with”

A Running ProgramVertices: methods; Edges: “calls”

A Chess GameVertices: boards; Edges: “can move to”

Telephone LinesVertices: houses; Edges: “telephone line between”

CSE CoursesVertices: courses; Edges: “is a pre-requisite of”

With these in mind, let’s talk about more crucial definitions.

Page 24: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

More Important Graphs 14

Empty Grapha

b

cd

Complete Graph (Kn)a

b

cd

Some QuestionsHow many edges can a graph with ∣V ∣ = n have?

∣E ∣ = (n2) = n(n−1)

2

If we have ∣E ∣ = n, what is the smallest number of vertices we canhave? The largest?

Smallest:

v(v−1)2

= n Ô⇒ v2−v = n ∈O(√

n)

Largest:

There is no largest!

Page 25: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

More Important Graphs 14

Empty Grapha

b

cd

Complete Graph (Kn)a

b

cd

Some QuestionsHow many edges can a graph with ∣V ∣ = n have?

∣E ∣ = (n2) = n(n−1)

2

If we have ∣E ∣ = n, what is the smallest number of vertices we canhave? The largest?

Smallest:

v(v−1)2

= n Ô⇒ v2−v = n ∈O(√

n)

Largest:

There is no largest!

Page 26: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

More Important Graphs 14

Empty Grapha

b

cd

Complete Graph (Kn)a

b

cd

Some QuestionsHow many edges can a graph with ∣V ∣ = n have?

∣E ∣ = (n2) = n(n−1)

2

If we have ∣E ∣ = n, what is the smallest number of vertices we canhave? The largest?

Smallest:v(v−1)

2= n Ô⇒ v2−v = n ∈O(

√n)

Largest:

There is no largest!

Page 27: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

More Important Graphs 14

Empty Grapha

b

cd

Complete Graph (Kn)a

b

cd

Some QuestionsHow many edges can a graph with ∣V ∣ = n have?

∣E ∣ = (n2) = n(n−1)

2

If we have ∣E ∣ = n, what is the smallest number of vertices we canhave? The largest?

Smallest:v(v−1)

2= n Ô⇒ v2−v = n ∈O(

√n)

Largest: There is no largest!

Page 28: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Walks and Paths 15

Definition (Walk)A walk in a graph G = (V,E) is a list of vertices:v0,v1, . . . ,vn such that {vi,vi+1} ∈ E.Intuitively, a walk from u to v is a continuous line drawn without pickingup your pencil.

Definition (Path)A path in a graph G = (V,E) is a walk with no repeated vertices.

1

2

3

4 6

5

7

8

The blue edges are a path The red edges are a walk but not a path

Page 29: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Walks and Cycles 16

Definition (Walk)A walk in a graph G = (V,E) is a list of vertices:v0,v1, . . . ,vn such that {vi,vi+1} ∈ E.Intuitively, a path from u to v is a continuous line drawn without pickingup your pencil.

Definition (Cycle)A cycle in a graph G = (V,E) is a walk (v0,v1, . . . ,vn) with no repeatedvertices except v0 = vn.

1

2

3

4 6

5

7

8

The blue edges are a cycle The red edges are not a cycle

Page 30: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Making A Connection! 17

Definition (Connected Graph)We say a graph is connected if for every pair of vertices, u,v ∈V , there isa path from u to v.Intuitively, if we pick up the graph and shake it around, if anything isn’tstill in the air, then the graph isn’t connected.

1

2

3

4 6

5

7

8

Connected! Not Connected!

Page 31: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Trees: Just A Kind of Graph! 18

Definition (Tree)A tree is a connected, acyclic graph.

a

b

c d

e

f

gh

a

b

c d

e

f

g h

Definition (Rooted Tree)A rooted tree is a tree with one special node called out as the “root”.

It can be equally useful to work with trees as it is to work with rootedtrees; we’re definitely more familiar with rooted trees so far. . .

Page 32: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

A “Worklist” 19

A very common type of algorithm on graphs is a worklist algorithm.

Recall the WorkList ADT:

WorkList ADT

add(v) Notifies the worklist that it must handle vnext() Returns the next vertex to work onhasWork() Returns true if there’s any work left and false other-

wise

Importantly, we do not care how the worklist manages the work.(Okay, we do, but not when coming up with the algorithm.)

Worklist algorithms will always look like the following:1 worklist = /* add initial work to worklist */2 while (worklist.hasWork()) {3 v = worklist.next();4 doWork(v);5 }

Page 33: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 34: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 35: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 36: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 37: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 38: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 39: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 40: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 41: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 42: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 43: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 44: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph 20

We said a graph is connected when there is a path between every pair ofvertices. What if we don’t know if a graph is connected?

Relatedly, we might ask the question:

Is a vertex w reachable (does there exist a path) from a vertex v?

We could use this algorithm to find a path, do something with everynode, search for a particular node, etc.

Unsurprisingly, this is a worklist algorithm:1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

Page 45: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

What Happened? 21

1 search(v) {2 worklist = [v];3 while (worklist.hasWork()) {4 v = worklist.next();5 doSomething(v);6 for (w : v.neighbors()) {7 worklist.add(w);8 }9 }

10 }

a

d

b

e

c

f

g

i

h

j

What Happened?We started searching paths in the graph and eventually went back andbetween a and b.This happened, because there were two distinct paths between a and b:

a → b and a → d → e → c → b

We followed the cycle in our graph!

A Corollary: This wouldn’t have happened on a tree!That is, we just found an algorithm to check for tree-ness!

Page 46: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 47: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 48: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 49: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 50: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 51: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 52: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 53: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 54: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 55: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 56: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 57: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 58: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 59: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 60: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 61: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 62: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 63: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a SortedList) 22

1 search(v) {2 worklist = [v];3 seen = {v};4 while (worklist.hasWork()) {5 v = worklist.next();6 doSomething(v);7 for (w : v.neighbors()) {8 if (w not seen) {9 worklist.add(w);

10 seen = seen ∪ {w};11 }12 }13 }14 }

We have two ways of fixing this:1 insist that the worklist take

care of duplicates, and2 avoid feeding duplicates to

the worklist

a

d

b

e

c

f

g

i

h

j

Page 64: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑a

a

d

b

e

c

f

g

i

h

j

Page 65: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑db

a

d

b

e

c

f

g

i

h

j

Page 66: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑db

a

d

b

e

c

f

g

i

h

j

Page 67: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑gfeb

a

d

b

e

c

f

g

i

h

j

Page 68: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑gfeb

a

d

b

e

c

f

g

i

h

j

Page 69: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑ihfeb

a

d

b

e

c

f

g

i

h

j

Page 70: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑ihfeb

a

d

b

e

c

f

g

i

h

j

Page 71: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑hfeb

a

d

b

e

c

f

g

i

h

j

Page 72: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑hfeb

a

d

b

e

c

f

g

i

h

j

Page 73: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑feb

a

d

b

e

c

f

g

i

h

j

Page 74: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑feb

a

d

b

e

c

f

g

i

h

j

Page 75: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑eb

a

d

b

e

c

f

g

i

h

j

Page 76: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑eb

a

d

b

e

c

f

g

i

h

j

Page 77: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑cb

a

d

b

e

c

f

g

i

h

j

Page 78: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑cb

a

d

b

e

c

f

g

i

h

j

Page 79: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑b

a

d

b

e

c

f

g

i

h

j

Page 80: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑b

a

d

b

e

c

f

g

i

h

j

Page 81: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Stack) 23

Okay, but we’d never actually use a SortedList. How about a Stack?

When we use a Stack:1 This algorithm is called DFS (depth-first search)2 We follow a path as far as possible, then back up3 It can be written recursively

worklist↓↑

a

d

b

e

c

f

g

i

h

j

Page 82: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð a ←Ð

a

d

b

e

c

f

g

i

h

j

Page 83: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð b d ←Ð

a

d

b

e

c

f

g

i

h

j

Page 84: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð b d ←Ð

a

d

b

e

c

f

g

i

h

j

Page 85: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð d c e ←Ð

a

d

b

e

c

f

g

i

h

j

Page 86: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð d c e ←Ð

a

d

b

e

c

f

g

i

h

j

Page 87: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð c e f g ←Ð

a

d

b

e

c

f

g

i

h

j

Page 88: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð c e f g ←Ð

a

d

b

e

c

f

g

i

h

j

Page 89: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð e f g ←Ð

a

d

b

e

c

f

g

i

h

j

Page 90: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð e f g ←Ð

a

d

b

e

c

f

g

i

h

j

Page 91: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð f g h ←Ð

a

d

b

e

c

f

g

i

h

j

Page 92: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð f g h ←Ð

a

d

b

e

c

f

g

i

h

j

Page 93: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð g h ←Ð

a

d

b

e

c

f

g

i

h

j

Page 94: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð g h ←Ð

a

d

b

e

c

f

g

i

h

j

Page 95: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð h i ←Ð

a

d

b

e

c

f

g

i

h

j

Page 96: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð h i ←Ð

a

d

b

e

c

f

g

i

h

j

Page 97: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð i ←Ð

a

d

b

e

c

f

g

i

h

j

Page 98: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←Ð i ←Ð

a

d

b

e

c

f

g

i

h

j

Page 99: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph (with a Queue) 24

Any reason we shouldn’t use a Queue?

When we use a Queue:1 This algorithm is called BFS (breadth-first search)2 We increase our horizon away from the starting node

worklist ←

a

d

b

e

c

f

g

i

h

j

Page 100: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Searching a Graph: Recovering The Path 25

Use A Dictionary!

search(v) {worklist = [v];from = new Dictionary();from.put(v, null);while (worklist.hasWork()) {

v = worklist.next();doSomething(v);for (w : v.neighbors()) {

if (w not in from) {worklist.add(w);from.put(w, v);

}}

}return from;

}

findPath(v, w) {from = search(v);path = [];curr = w;while (curr != null) {

path.add(0, curr);curr = from[curr];

}return path;

}

Page 101: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Asymptotic Analysis of BFS and DFS 26

RuntimeBoth algorithms visit all nodes in the connected component: ∣V ∣Both algorithms can visit a node once for each edge in the graph: ∣E ∣

So, BFS and DFS are O(∣V ∣+ ∣E ∣) (this is called “graph linear”).

SpaceDFS: If the longest path has length p and the largest number ofneighbors is n, then DFS stores at most pn verticesBFS: Consider a tree. BFS will hold the entire bottom level which isO(∣V ∣).

Page 102: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

BFS and DFS Trade-Offs 27

Trade-OffsDFS has better space usage, but it might find a circuitous pathBFS will always find the shortest path to a node, but it will use morememory

Iterative DeepeningIterative Deepening is a DFS that bounds the depth:

1 int depth = 1;2 while (there are nodes to explore) {3 dfs(v, depth);4 depth++;5 }

Since most of the vertices are “leaves”, this actually doesn’t wastemuch time!

Page 103: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Generalizing Graphs: Direction & Edge Weight 28

Undirected vs. Directed (do the edges have arrows?)

Undirected

a

b

c

Directed

a

b

c

Weighted vs. Unweighted (do the edges have weights?)

Unweighted & Directed

a

b

c

Weighted & Undirected

100

50

1 a

b

c

Page 104: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

Generalizing Graphs: Multi-Edges 29

Simple vs. Multi (loops on vertices? multiple edges?)

Multi-graph

a

b

c

Graph with Loops

100

50

1 a

b

c

These generalizations are all useful in different domains. We’regoing to talk a lot more about them over the next few lectures.

Next lecture, we’ll be working mostly with directed graphs.

Page 105: Adam Blank Winter 2017Lecture 20 CSE 332courses.cs.washington.edu/courses/cse332/17wi/...Adam Blank Winter 2017Lecture 20 CSE 332 Data Structures and Parallelism

A Word about Sparsity 30

Back to counting edges. In a graph without multiple edges, if there are nvertices, there can be anywhere from 0 to n2 edges.

This is a very wide range. A graph with fewer edges is called sparse andone with closer to n2 is called dense.

We already saw that graph traversal was O(∣E ∣+ ∣V ∣):On a sparse graph, that’s O(∣V ∣)On a dense graph, that’s O(∣V ∣2).

Sparsity makes a huge difference!