graph traversal Nov. 15/16, 2017 1 Today Recursive graph - - PowerPoint PPT Presentation

graph traversal
SMART_READER_LITE
LIVE PREVIEW

graph traversal Nov. 15/16, 2017 1 Today Recursive graph - - PowerPoint PPT Presentation

COMP 250 Lecture 29 graph traversal Nov. 15/16, 2017 1 Today Recursive graph traversal depth first Non-recursive graph traversal depth first breadth first 2 Heads up! There were a few mistakes in the slides for Sec. 001


slide-1
SLIDE 1

1

COMP 250

Lecture 29

graph traversal

  • Nov. 15/16, 2017
slide-2
SLIDE 2

Today

  • Recursive graph traversal
  • depth first
  • Non-recursive graph traversal
  • depth first
  • breadth first

2

slide-3
SLIDE 3

3

Heads up! There were a few mistakes in the slides for Sec. 001 for today’s

  • lecture. So if you are following

the lecture recordings and using these (corrected) slides, then you will notice some differences.

slide-4
SLIDE 4

depthfirst__Tree (root){ if (root is not empty){ root.visited = true // “preorder” for each child of root depthfirst__Tree( child ) } }

Recall: tree traversal (recursive)

4

slide-5
SLIDE 5

Graph traversal (recursive)

Need to specify a starting vertex. Visit all nodes that are “reachable” by a path from a starting vertex.

f g c d a e b h

5

slide-6
SLIDE 6

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E // w in v.adjList _______?___________ }

Graph traversal (recursive)

6

// Here “visiting” just means “reaching”

slide-7
SLIDE 7

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E // w in v.adjList if ! (w.visited) // avoids cycles depthFirst_Graph(w) }

Graph traversal (recursive)

7

// Here “visiting” just means “reaching”

slide-8
SLIDE 8

b e f f f f f c c c c c c c a f g c d a e b h

Call Stack for depthFirst(a)

8

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }

slide-9
SLIDE 9

b e f f f f f c a a f g c d a e b h

Call Stack for depthFirst(a)

9

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }

slide-10
SLIDE 10

b e f c c a a a f g c d a e b h

Call Stack for depthFirst(a)

10

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }

slide-11
SLIDE 11

b e f f f f f c c c c c c c a a a a a a a a a f g c d a e b h

Call Stack for depthFirst(a)

11

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }

slide-12
SLIDE 12

b e f f f f f c c c c c c c a a a a a a a a a f g c d a e b h

Call Stack for depthFirst(a)

12

depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }

slide-13
SLIDE 13

Call Tree

13

f g c d a e b h

root

b e f f f f f c c c c c c c a a a a a a a a a

slide-14
SLIDE 14

Example 2

14

g d a h e b i f c a - (b,d) b - (a,c,e) c - (b,f) d - (a,e,g) e - (b,d,f,h) f - (c,e,i) g - (d,h) h - (e,g,i) i - (f,h)

Adjacency List What is the call tree for depthFirst( a ) ?

slide-15
SLIDE 15

Example 2

15

g d a h e b i f c g d a h e b i f c

call tree for depthFirst(a)

slide-16
SLIDE 16

16

Q: Non-recursive graph traversal ? A: Similar to tree traversal: Use a stack or a queue.

slide-17
SLIDE 17

Recall: depth first tree traversal

(with a slight variation)

17

treeTraversalUsingStack(root){ initialize empty stack s visit root s.push(root) while s is not empty { cur = s.pop() for each child of cur{ visit child s.push(child) } } } Visit a node before pushing it onto the stack. Every node in the tree gets visited, pushed, and then popped.

slide-18
SLIDE 18

Generalize to graphs…

18

graphTraversalUsingStack(v){ initialize empty stack s v.visited = true s.push(v) while (!s.empty) { u = s.pop() for each w in u.adjList{ if (!w.visited){ // the only new part w.visited = true s.push(w) } } } }

slide-19
SLIDE 19

Example: graphTraversalUsingStack(a)

19

a g d a h e b i f c

a

slide-20
SLIDE 20

Example: graphTraversalUsingStack(a)

20

d a b g d a h e b i f c

d a b ‘a’ is popped and both ‘b’ and ‘d’ are pushed. The traversal defines a tree, but it is not a “call tree”. Why not?

slide-21
SLIDE 21

Example: graphTraversalUsingStack(a)

21

g d a e b g d a h e b i f c

g d e ‘d’ is popped and both ‘e’ and ‘g’ are pushed. a b b

slide-22
SLIDE 22

Example: graphTraversalUsingStack(a)

22

g d a e b g d a h e b i f c

g h d e e ‘g’ is popped and ‘h’ is pushed. a b b b

h

slide-23
SLIDE 23

Example: graphTraversalUsingStack(a)

23

g d a e b g d a h e b i f c

g h i d e e e ‘h’ is popped and ‘i’ is pushed. a b b b b

h i

slide-24
SLIDE 24

Example: graphTraversalUsingStack(a)

24

g d a e b g d a h e b i f c

g h i f d e e e e ‘i’ is popped and ‘f’ is pushed. a b b b b b

h i f

slide-25
SLIDE 25

Example: graphTraversalUsingStack(a)

25

g d a e b g d a h e b i f c

g h i f c d e e e e e ‘f’ is popped and ‘c’ is pushed. a b b b b b b

h i f c

slide-26
SLIDE 26

Example: graphTraversalUsingStack(a)

26

g d a e b g d a h e b i f c

g h i f c d e e e e e e a b b b b b b b b

h i f c

Order of nodes visited: abdeghifc

slide-27
SLIDE 27

a

b c d i e f h g j k

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

Recall: breadth first tree traversal

(see lecture 20)

27

for each level i visit all nodes at level i

slide-28
SLIDE 28

Breadth first graph traversal

28

Given an input vertex, find all vertices that can be reached by paths of length 1, 2, 3, 4, ….

slide-29
SLIDE 29

Breadth first graph traversal

29

graphTraversalUsingQueue(v){ initialize empty queue q v.visited = true q.enqueue(v) while (! q.empty) { u = q.dequeue() for each w in u.adjList{ if (!w.visited){ w.visited = true q.enqueue(w) } } } }

slide-30
SLIDE 30

Example

30

f c d a e b

graphTraversalUsingQueue(c)

queue c

slide-31
SLIDE 31

Example

31

f c d a e b

graphTraversalUsingQueue(c)

queue c f

slide-32
SLIDE 32

Example

32

f c d a e b

graphTraversalUsingQueue(c)

queue c f be

Both ‘b’, ‘e’ are visited and enqueued before ‘b’ is dequeued.

slide-33
SLIDE 33

Example

33

f c d a e b

graphTraversalUsingQueue(c)

queue c f be e

slide-34
SLIDE 34

34

f c d a e b

graphTraversalUsingQueue(c) It defines a tree whose root is the starting vertex. It finds the shortest path (number of vertices) to all vertices reachable from starting vertex.

slide-35
SLIDE 35

Example: graphTraversalUsingQueue(a)

35

g d a h e b i f c a

1

slide-36
SLIDE 36

Example: graphTraversalUsingQueue(a)

36

g d a h e b i f c a bd

1 2 3

slide-37
SLIDE 37

Example: graphTraversalUsingQueue(a)

37

g d a h e b i f c a bd dce

1 2 4 3 5

slide-38
SLIDE 38

Example: graphTraversalUsingQueue(a)

38

g d a h e b i f c a bd dce ceg

1 2 4 3 5 6

slide-39
SLIDE 39

Example: graphTraversalUsingQueue(a)

39

g d a h e b i f c a bd dce ceg egf

1 2 4 3 5 7 6

slide-40
SLIDE 40

Example: graphTraversalUsingQueue(a)

40

g d a h e b i f c a bd dce ceg egf gfh

1 2 4 3 5 7 6 8

slide-41
SLIDE 41

Example: graphTraversalUsingQueue(a)

41

g d a h e b i f c a bd dce ceg egf gfh fh hi i

1 2 4 3 5 7 6 8

slide-42
SLIDE 42

Example: graphTraversalUsingQueue(a)

42

g d a h e b i f c a bd dce ceg egf gfh fh hi

1 2 4 3 5 7 6 8 9

slide-43
SLIDE 43

Example: graphTraversalUsingQueue(a)

43

g d a h e b i f c a bd dce ceg egf gfh fh hi i

1 2 4 3 5 7 6 8 9

Note order of nodes visited: paths of length 1,2, 3, 4

slide-44
SLIDE 44

Example: graphTraversalUsingQueue(a)

44

g d a h e b i f c

The traversal defines a tree, but it is not a “call tree”. Why not?

slide-45
SLIDE 45

class Graph<T> { HashMap< String, Vertex<T> > vertexMap; class Vertex<T> { ArrayList<Edge> adjList; T element; boolean visited; } class Edge { Vertex endVertex; double weight; : } }

Recall: How to implement a Graph class in Java?

slide-46
SLIDE 46

46

HEADS UP ! Prior to traversal, …. for each w in V w.visited = false How to implement this ?

slide-47
SLIDE 47

47

class Graph<T> { HashMap< String, Vertex<T> > vertexMap; : public void resetVisited() { } }

HEADS UP ! Prior to traversal, …. for each w in V w.visited = false How to implement this ?

slide-48
SLIDE 48

48

class Graph<T> { HashMap< String, Vertex<T> > vertexMap; : public void resetVisited() { for( Vertex<T> v : vertexMap.values() ){ v.visited = false; } }

HEADS UP ! Prior to traversal, …. for each w in V w.visited = false How to implement this ?

[ASIDE: I did something unnecessarily complicated on the Sec.001 slides. What I have above is better. ]