l14: traversals and graphs -...

38
CSE373, Winter 2020 L14: Traversals and Graphs Traversals and Graphs CSE 373 Winter 2020 Instructor: Hannah C. Tang Teaching Assistants: Aaron Johnston Ethan Knutson Nathan Lipiarski Amanda Park Farrell Fileas Sam Long Anish Velagapudi Howard Xiao Yifan Bai Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan

Upload: others

Post on 10-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Traversals and GraphsCSE 373 Winter 2020

Instructor: Hannah C. Tang

Teaching Assistants:

Aaron Johnston Ethan Knutson Nathan Lipiarski

Amanda Park Farrell Fileas Sam Long

Anish Velagapudi Howard Xiao Yifan Bai

Brian Chan Jade Watkins Yuma Tou

Elena Spasova Lea Quan

Page 2: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Announcements

âť– Homework 5: k-d trees is released and due next Friday

▪ This is the first of our “hard” homeworks

▪ Suggestion: pretend it’s due Tuesday so you don’t panic while prepping for midterm. Start early!

▪ Hint: start with a version that doesn’t prune; then implement a version that chooses good/bad sides; then finally a pruning version

âť– Midterm is also next Friday

â–Ş If your student number ends in an odd number, go to KNE 210

â–Ş If your student ends in an even number, go to KNE 220

▪ We’ve released a practice midterm

â–Ş Review session Thursday night: 4:30-6:30 @ ARC 147

3

Page 3: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Feedback from the Reading Quiz

❖ The reading didn’t mention weighted/unweighted graphs

❖ I’m still confused about pre-, in- and post-order graphs

❖ It’s interesting how we can finally use a queue to implement BFS

▪ Why does BFS matter? It’s also really confusing

4

Page 4: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Lecture Outline

âť– Tree Traversals

âť– Introduction to Graphs

â–Ş Definitions

â–Ş Graph Problems

âť– Graph Traversals: DFS

5

Page 5: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Review: The Tree Data Structure

âť– A Tree is a collection of nodes; each node has <= 1 parent and

>= 0 children

▪ Root node: the “top” of the tree and the

only node with no parent

â–Ş Leaf node: a node with no children

â–Ş Edge: the connection between a parent

and child

â–Ş There is exactly one path between

any pair of nodes

6

class Node<Value> {

Value v;

List<Node> children;

}

Nodes

Edges Purple

Green Red

Blue Indigo OrangeYellow

Pink

Page 6: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Review: Trees We’ve Seen

âť– Binary Search Trees

â–Ş And one of its balanced

variants: the Left-Leaning Red-

Black Tree

âť– B-Trees

â–Ş Specifically, a 2-3 Tree

âť– Binary Heaps

7

9

5 17

8 311

u

s v

y

x

w

a

s u w

r y zt

e

b g

m

v

d f

b g

o

n p

m

e

31

9 17

8 15

Page 7: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Tree Applications

âť– Trees are a more general concept

â–Ş Organization charts

â–Ş Family lineages* including phylogenetic trees

â–Ş MOH Training Manual for Management of Malaria.

*: Not all family lineages are trees!

Page 8: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Tree Traversals

❖ Thus far, we’ve talked about searching a tree. Let’s back up and talk about traversing a tree

âť– A traversal:

â–Ş Iterates over every node in a tree in some defined ordering

▪ “Processes” or “visits” its contents

âť– There are several types of tree traversals

9

words.txt

data

tiles cities.txt

d1_x0_y0.jpgd0_x1_y0.jpgd0_x0_y0.jpg

Page 9: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Tree Traversal Types

âť– Level Order Traversal aka Breadth-First Traversal

âť– Depth-First Traversal

â–Ş Pre-order Traversal

â–Ş In-order Traversal

â–Ş Post-order Traversal

10

A C

B

D

E

F

G

Page 10: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Level-Order / Breadth-First Traversal

âť– Traverse and visit top-to-bottom, left-to-right

â–Ş Like reading in English

âť– Looks like how we converted our binary heap (ie, a complete tree) to its array representation

âť– Needs a supporting data structureto implement

â–Ş See next lecture!

11

A C

B

D

E

F

G

Page 11: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Depth-First Traversal

❖ Basic idea: traverse “deep nodes” (eg, A) before shallow ones (eg, F)

âť– Remember that traversing a node is different than visiting/processing a node

12

Page 12: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Depth-First: Pre-Order

❖ Pre-order “visits” the node before traversing its children

â–Ş DBACFEG

13A C

BD

EF

G

preOrder(BSTNode x) {

if (x == null)

return;

process(x.key)

preOrder(x.left)

preOrder(x.right)

}

Page 13: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Depth-First: In-Order

❖ Pre-order “visits” the node before traversing its children

â–Ş DBACFEG

âť– In-order traverses the left child, visits the node, then traverses the right child

â–Ş ABCDEF

14

preOrder(BSTNode x) {

if (x == null)

return;

process(x.key)

preOrder(x.left)

preOrder(x.right)

}

inOrder(BSTNode x) {

if (x == null)

return;

inOrder(x.left)

process(x.key)

inOrder(x.right)

}

A C

BD

EF

G

Page 14: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Depth-First: Post-Order

❖ Pre-order “visits” the node before traversing its children

â–Ş DBACFEG

❖ In-order traverses the left child, “visits” the node, then traverses the right child

â–Ş ABCDEF

❖ Post-order traverses its children before “visiting” the node

â–Ş ACBEGFD

15

preOrder(BSTNode x) {

if (x == null)

return;

process(x.key)

preOrder(x.left)

preOrder(x.right)

}

inOrder(BSTNode x) {

if (x == null)

return;

inOrder(x.left)

process(x.key)

inOrder(x.right)

}

A C

BD

EF

G

postOrder(BSTNode x) {

if (x == null)

return;

postOrder(x.left)

postOrder(x.right)

process(x.key)

}

Page 15: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

A Useful Visual Trick for Depth-First Traversals

âť– (Useful for humans, not algorithms)

âť– Trace a path around thegraph, from the top going counter-clockwise

▪ Pre-order: “Visit” when you pass the LEFT side of a node

▪ In-order: “Visit” when you pass the BOTTOM of a node

▪ Post-order: “Visit” when you pass the RIGHT side of a node.

9

4

2

1

3

6

8

5

7

Example: post-order4 7 8 5 2 9 6 3 1

Page 16: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Traversal Applications (1 of 2)

âť– Pre-order Traversal for printing directory listing

17

words.txt

data

tiles cities.txt

d1_x0_y0.jpgd0_x1_y0.jpgd0_x0_y0.jpg

data/

tiles/

d0_x0_y0.jpg

d0_x1_y0.jpg

d1_x0_y0.jpg

cities.txt

words.txt

Page 17: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Traversal Applications (2 of 2)

âť– Post-order Traversal for calculating directory size

18

words.txt

data

tiles cities.txt

d1_x0_y0.jpgd0_x1_y0.jpgd0_x0_y0.jpg

postOrder(BSTNode x) {

if (x == null)

return 0;

int total = 0;

for (BSTNode c : x.children())

total += postOrder(c)

total += x.fileSize();

return total;

}

256 256 256

65536 1024768

67328

Page 18: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Lecture Outline

âť– Tree Traversals

âť– Introduction to Graphs

â–Ş Definitions

â–Ş Graph Problems

âť– Graph Traversals: DFS

19

Page 19: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Trees are Hierarchical!

âť– Trees are fantastic for representing strict hierarchical relationships

â–Ş Not every relationship is hierarchical

â–Ş Eg: (Proposed) Light rail map

âť– This is not a tree: contains cycles!

20

Page 20: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Review (AGAIN?!?!): The Tree Data Structure

âť– A Tree is a collection of nodes; each node has <= 1 parent and

>= 0 children

▪ Root node: the “top” of the tree and the

only node with no parent

â–Ş Leaf node: a node with no children

â–Ş Edge: the connection between a parent

and child

â–Ş There is exactly one path between

any pair of nodes

21

class Node<Value> {

Value v;

List<Node> children;

}

Nodes

Edges Purple

Green Red

Blue Indigo OrangeYellow

Pink

Page 21: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

The Graph Data Structureâť– A Graph is a collection of nodes, and zero or more edges

connecting two nodes

â–Ş All trees are graphs!

âť– A Simple Graph has no self-loopsor parallel edges

â–Ş In a simple graph, E is O(V2)

â–Ş Unless otherwise stated, all graphsin this course are simple

22

Self-loopParallel

Page 22: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Graph Terminology (1 of 2)

Figure from Algorithms 4th Edition

âť– Graph:

â–Ş Set of vertices aka nodes

â–Ş Set of edges: pairs of vertices

â–Ş Vertices with an edge between them are adjacent

â–Ş Vertices or edges may have optional labels

• Numeric edge labels are sometimes called weights

Page 23: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Graph Terminology (2 of 2)

Figure from Algorithms 4th Edition

âť– Two vertices are connected if there is a path between them

â–Ş If all the vertices are connected, we say the graph is connected

â–Ş The number of edges leaving a vertex is its degree

âť– A path is a sequence of vertices connected by edges

â–Ş A simple path is a path without repeated vertices

â–Ş A cycle is a path whose first and last edges are the same

• A graph with a cycle is cyclic

Page 24: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Directed vs Undirected; Acyclic vs Cyclic

a

b

d

c

a

b

d

c

e

a

b

d

c

a

b

d

c

Acyclic:

Cyclic:

Directed Undirected

Page 25: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Labeled and Weighted Graphs

26

Edge Labels

a

b

c

d

Vertex Labels

b

d

c

e

a

Edge Labels(Edge Weights)

1

2

3

1

2

3

4

5

1

a

b

c

d

Vertex and Edge Labels

Page 26: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

pollev.com/uwcse373

This schematic map of the Paris MĂ©tro is a graph. It exhibits the following characteristics:

A. Undirected / Connected / Cyclic / Vertex-labeled

B. Directed / Connected / Cyclic / Vertex-labeled

C. Undirected / Connected / Cyclic / Edge-labeled

D. Directed / Connected / Cyclic / Edge-labeled

E. I’m not sure …

Page 27: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Lecture Outline

âť– Tree Traversals

âť– Introduction to Graphs

â–Ş Definitions

â–Ş Graph Problems

âť– Graph Traversals: DFS

28

Page 28: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

ST

Graph Queries

âť– There are lots of interesting questions we can ask about a graph:

â–Ş What is the shortest route from S to T? What is the longest without cycles?

â–Ş Are there cycles?

â–Ş Is there a tour you can take that only uses each node (station) exactly once?

â–Ş Is there a tour that uses each edge exactly once?

Page 29: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Graph Queries More Theoreticallyâť– Some well known graph problems and their common names:

â–Ş s-t Path. Is there a path between vertices s and t?

â–Ş Connectivity. Is the graph connected?

â–Ş Biconnectivity. Is there a vertex whose removal disconnects the graph?

â–Ş Shortest s-t Path. What is the shortest path between vertices s and t?

â–Ş Cycle Detection. Does the graph contain any cycles?

â–Ş Euler Tour. Is there a cycle that uses every edge exactly once?

â–Ş Hamilton Tour. Is there a cycle that uses every vertex exactly once?

â–Ş Planarity. Can you draw the graph on paper with no crossing edges?

â–Ş Isomorphism. Are two graphs the same graph (in disguise)?

❖ Often can’t tell how difficult a graph problem is without very deep consideration.

Page 30: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Graph Problem Difficulty

âť– Some well known graph problems:

â–Ş Euler Tour: Is there a cycle that uses every edge exactly once?

â–Ş Hamilton Tour: Is there a cycle that uses every vertex exactly once?

âť– Difficulty can be deceiving

â–Ş An efficient Euler tour algorithm O(# edges) was found as early as 1873 [Link].

â–Ş Despite decades of intense study, no efficient algorithm for a Hamilton tour exists. Best algorithms are exponential time.

âť– Graph problems are among the most mathematically rich areas of CS theory

Page 31: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

Lecture Outline

âť– Tree Traversals

âť– Introduction to Graphs

â–Ş Definitions

â–Ş Graph Problems

âť– Graph Traversals: DFS

32

Page 32: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

s-t Connectivity Problem

âť– s-t connectivity problem

â–Ş Given source vertex s and a target vertex t, does there exist a path between s and t?

âť– Try to come up with an algorithm for connected(s, t)

33

1

2

3

4

5

6

7

8

0st

Page 33: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

s-t Connectivity Problem: Proposed Solution

34

connected(Node s, Node t) {

if (s == t) {

return true;

} else {

for (Node n : s.neighbors) {

if (connected(n, t)) {

return true;

}

}

return false;

}

}

1

2

3

4

5

6

7

8

0st

Page 34: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

pollev.com/uwcse373

âť– What is wrong with the proposed algorithm?

35

connected(Node s, Node t) {

if (s == t) {

return true;

} else {

for (Node n : s.neighbors) {

if (connected(n, t)) {

return true;

}

}

return false;

}

}

1

2

3

4

5

6

7

8

0st

Page 35: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

s-t Connectivity Problem

âť– What is wrong with the proposed algorithm?

â–Ş Does 0 == 7? No; if(connected(1, 7) return true;

â–Ş Does 1 == 7? No; if(connected(0, 7) return true;

â–Ş Does 0 == 7?

36

connected(Node s, Node t) {

if (s == t) {

return true;

} else {

for (Node n : s.neighbors) {

if (connected(n, t)) {

return true;

}

}

return false;

}

}

1

2

3

4

5

6

7

8

0st

Page 36: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

s-t Connectivity Problem: Depth-First Search

âť– Mark each node as visited!

37

connected(Node s, Node t) {

if (s == t) {

return true;

} else {

s.visited = true;

for (Node n : s.neighbors) {

if (n.visited) {

continue;

}

if (connected(n, t)) {

return true;

}

}

return false;

}

}

1

2

3

4

5

6

7

8

0st

Is this a pre-order traversal or a post-order traversal?

Do in-order traversals exist for graphs?

Page 37: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

s-t Connectivity Problem: Depth-First Search

âť– Demo: https://docs.google.com/presentation/d/1OHRI7Q_f8hlwjRJc8NPBUc1cMu5KhINH1xGXWDfs_dA/present?ueb=true&slide=id.g76e0dad85_2_380

âť– Is this a pre-order traversal or a post-order traversal?

â–Ş Do in-order traversals exist for graphs?

38

Page 38: L14: Traversals and Graphs - courses.cs.washington.educourses.cs.washington.edu/courses/cse373/20wi/lectures/14-treetraversals...Graph Terminology (2 of 2) Figure from Algorithms 4th

CSE373, Winter 2020L14: Traversals and Graphs

tl;dr

âť– Traversals are an order in which you visit/process vertices

âť– Trees have level-order traversals and 3 depth-first traversals

âť– Graphs are a more general idea than a tree

â–Ş Key terms: Directed/Undirected, Cyclic/Acylic, Path, Cycle

â–Ş Traversals are a common tool for solving almost all graph problems

• DFS pre-order, DFS post-order, BFS (next lecture!)

39