Graph & Tree Search and Traversals Algorithms Mark Redekopp - - PowerPoint PPT Presentation

graph tree search and
SMART_READER_LITE
LIVE PREVIEW

Graph & Tree Search and Traversals Algorithms Mark Redekopp - - PowerPoint PPT Presentation

1 CSCI 104 Graph & Tree Search and Traversals Algorithms Mark Redekopp David Kempe Sandra Batista 2 RECURSIVE TREE TRAVERSALS 3 Guiding Recursive Principle f(n) f(n-1) 1 0 1 2 3 4 A useful principle when trying to 50 51


slide-1
SLIDE 1

1

CSCI 104 Graph & Tree Search and Traversals Algorithms

Mark Redekopp David Kempe Sandra Batista

slide-2
SLIDE 2

2

RECURSIVE TREE TRAVERSALS

slide-3
SLIDE 3

3

Guiding Recursive Principle

  • A useful principle when trying to

develop recursive solutions is that the recursive code should handle

  • nly 1 element, which might be:

1. An element in an array 2. A node a linked list 3. A node in a tree 4. One choice in a sequence of choices

  • Then use recursion to handle the

remaining elements

  • And finally combine the solution(s)

to the recursive call(s) with the one element being handled

50 51 52 53 54 1 2 3 4

val next

3

0x1c0 val next

9

0x380 0x148 0x148 0x1c0 val next

7

0x0 NULL 0x380

f(n) f(n-1) f(head) f(head->next) f(n) f(left) f(right)

1 2 3

slide-4
SLIDE 4

4

Recursive Tree Traversals

  • A traversal iterates over all nodes of the tree

– Usually using a depth-first, recursive approach

  • Three general traversal orderings

– Pre-order [Process root then visit subtrees] – In-order [Visit left subtree, process root, visit right subtree] – Post-order [Visit left subtree, visit right subtree, process root]

60 80 30 25 50 20 10

Preorder(TNode* t) { if t == NULL return process(t) // print val. Preorder(t->left) Preorder(t->right) }

60 20 10 30 25 50 80

Inorder(TNode* t) { if t == NULL return Inorder(t->left) process(t) // print val. Inorder(t->right) } Postorder(TNode* t) { if t == NULL return Postorder(t->left) Postorder(t->right) process(t) // print val. }

10 20 25 30 50 60 80 10 25 50 30 20 80 60

// Node definition struct TNode { int val; TNode *left, *right; };

slide-5
SLIDE 5

5

Example 1: Count Nodes

  • Write a recursive function to count how many nodes are in

the binary tree

– Only process 1 node at a time – Determine pre-, in-, or post-order based on whose answers you need to compute the result for your node – For in- or post-order traversals, determine how to use/combine results from recursion on children

f(n) f(left) f(right)

// Node definition struct Tnode { int val; TNode *left, *right; }; int count(TNode* root) { if( root == NULL ) _______________; else { } }

slide-6
SLIDE 6

6

Example 2: Prefix Sums

  • Write a recursive function to have each node store the sum of

the values on the path from the root to each node.

– Only process 1 node at a time – Determine pre-, in-, or post-order based on whose answers you need to compute the result for your node

4 3 8 5 7 f(n) f(left) f(right)

void prefixH(TNode* root, int psum) void prefix(TNode* root) { prefixH(root, 0); } void prefixH(TNode* root, int psum) { if( root == NULL ) ________________; else { } }

4 7 12 12 14

slide-7
SLIDE 7

7

GENERAL GRAPH TRAVERSALS

slide-8
SLIDE 8

8

BREADTH-FIRST SEARCH

slide-9
SLIDE 9

9

Breadth-First Search

  • Given a graph with vertices, V, and

edges, E, and a starting vertex that we'll call u

  • BFS starts at u (‘a’ in the diagram to the

left) and fans-out along the edges to nearest neighbors, then to their neighbors and so on

  • Goal: Find shortest paths (a.k.a.

minimum number of hops or depth) from the start vertex to every other vertex

a b d c h e f g

Depth 0: a

slide-10
SLIDE 10

10

Breadth-First Search

  • Given a graph with vertices, V, and

edges, E, and a starting vertex, u

  • BFS starts at u (‘a’ in the diagram to the

left) and fans-out along the edges to nearest neighbors, then to their neighbors and so on

  • Goal: Find shortest paths (a.k.a.

minimum number of hops or depth) from the start vertex to every other vertex

a b d c h e f g

Depth 0: a Depth 1: c,e

1 1

slide-11
SLIDE 11

11

Breadth-First Search

  • Given a graph with vertices, V, and

edges, E, and a starting vertex, u

  • BFS starts at u (‘a’ in the diagram to the

left) and fans-out along the edges to nearest neighbors, then to their neighbors and so on

  • Goal: Find shortest paths (a.k.a.

minimum number of hops or depth) from the start vertex to every other vertex

a b d c h e f g

Depth 0: a Depth 1: c,e Depth 2: b,d,f,g

1 1 2 2 2 2

slide-12
SLIDE 12

12

Breadth-First Search

  • Given a graph with vertices, V, and

edges, E, and a starting vertex, u

  • BFS starts at u (‘a’ in the diagram to the

left) and fans-out along the edges to nearest neighbors, then to their neighbors and so on

  • Goal: Find shortest paths (a.k.a.

minimum number of hops or depth) from the start vertex to every other vertex

a b d c h e f g

Depth 0: a Depth 1: c,e Depth 2: b,d,f,g Depth 3: h

1 1 2 2 2 2 3

slide-13
SLIDE 13

13

Developing the Algorithm

  • Key idea: Must explore all nearer

neighbors before exploring further- away neighbors

  • From ‘a’ we find ‘e’ and ‘c’

– If we explore 'e' next and find 'f' who should we choose to explore from next: 'c' or 'f'?

  • Must explore all vertices at depth i

before any vertices at depth i+1

– Essentially, the first vertices we find should be the first ones we explore from – What data structure may help us? Depth 0: a Depth 1: c,e Depth 2: b,d,f,g Depth 3: h

a b d c h e f g

1 1 2

slide-14
SLIDE 14

14

Developing the Algorithm

  • Exploring all vertices in the order they are found

implies we will explore all vertices at shallower depth before greater depth

– Keep a first-in / first-out queue (FIFO) of neighbors found

  • Put newly found vertices in the back and pull out a vertex from

the front to explore next

  • We don’t want to put a vertex in the queue more than once…

– ‘mark’ a vertex the first time we encounter it – only allow unmarked vertices to be put in the queue

  • May also keep a ‘predecessor’ structure that indicates how each

vertex got discovered (i.e. which vertex caused this one to be found)

– Allows us to find a shortest-path back to the start vertex

slide-15
SLIDE 15

15

Breadth-First Search

Algorithm:

a b d c h e f g

nil,0 a Q: nil,inf nil,inf nil,inf nil,inf nil,inf nil,inf nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

slide-16
SLIDE 16

16

Breadth-First Search

Algorithm:

Q: a v = e c

a b d c h e f g

nil,0 a,1 a,1 nil,inf nil,inf nil,inf nil,inf nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

slide-17
SLIDE 17

17

Breadth-First Search

Algorithm:

Q: e v = c f

a b d c h e f g

nil,0 c e,2 nil,inf nil,inf nil,inf nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

a,1 a,1

slide-18
SLIDE 18

18

Breadth-First Search

Algorithm:

Q: c v = c b

a b d c h e f g

nil,0 f d g c,2 c,2 c,2 nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

e,2 a,1 a,1

slide-19
SLIDE 19

19

Breadth-First Search

Algorithm:

Q: f v = c

a b d c h e f g

nil,0 b d g nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

c,2 c,2 c,2 e,2 a,1 a,1

slide-20
SLIDE 20

20

Breadth-First Search

Algorithm:

Q: b v = c

a b d c h e f g

nil,0 d g b,3 h

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

c,2 c,2 c,2 e,2 a,1 a,1

slide-21
SLIDE 21

21

Breadth-First Search

Algorithm:

Q: d v = c g h

a b d c h e f g

nil,0

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

b,3 c,2 c,2 c,2 e,2 a,1 a,1

slide-22
SLIDE 22

22

Breadth-First Search

Algorithm:

Q: g v = c h

a b d c h e f g

nil,0

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

b,3 c,2 c,2 c,2 e,2 a,1 a,1

slide-23
SLIDE 23

23

Breadth-First Search

Algorithm:

Q: h v =

a b d c h e f g

nil,0

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

b,3 c,2 c,2 c,2 e,2 a,1 a,1

slide-24
SLIDE 24

24

Breadth-First Search

  • Shortest paths can be found be

walking predecessor value from any node backward

  • Example:

– Shortest path from a to h

– Start at h

– Pred[h] = b (so walk back to b) – Pred[b] = c (so walk back to c) – Pred[c] = a (so walk back to a) – Pred[a] = nil … no predecessor, Done!!

a b d c h e f g

nil,0 b,3 c,2 c,2 c,2 e,2 a,1 a,1

slide-25
SLIDE 25

25

Breadth-First Search Trees

  • BFS (and later DFS) will induce a tree subgraph (i.e.

acyclic, one parent each) from the original graph

– BFS is tree of shortest paths from the source to all other vertices (in connected component)

a b d c h e f g

nil,0

a c e d g b f h

Original graph, G BFS Induced Tree b,3 c,2 c,2 c,2 e,2 a,1 a,1

slide-26
SLIDE 26

26

Correctness

  • Define

– dist(s,v) = correct shortest distance – d[v] = BFS computed distance – p[v] = predecessor of v

  • Loop invariant

– What can we say about the nodes in the queue, their d[v] values, relationship between d[v] and dist[v], etc.?

Q: c f

a b d c h e f g

nil,0 a,1 a,1 nil,inf nil,inf nil,inf nil,inf nil,inf e

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

slide-27
SLIDE 27

27

Correctness

  • Define

– dist(s,v) = correct shortest distance – d[v] = BFS computed distance – p[v] = predecessor of v

  • Loop invariant

– All vertices with p[v] != nil (i.e. already in the queue or popped from queue) have d[v] = dist(s,v) – The distance of the nodes in the queue are sorted

  • If Q = {v1, v2, …, vr} then d[v1] <=

d[v2] <= … <= d[vr]

– The nodes in the queue are from 2 adjacent layers/levels

  • i.e. d[vk] <= d[v1] + 1
  • Suppose there is a node from a 3rd

level (d[v1] + 2), it must have been found by some, vi, where d[vi] = d[v1]+1

Q: c f

a b d c h e f g

nil,0 nil,inf nil,inf nil,inf nil,inf nil,inf e

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

a,1 a,1

slide-28
SLIDE 28

28

Breadth-First Search

  • Analyze the run time of

BFS for a graph with n vertices and m edges

– Find T(n,m)

  • How many times does

loop on line 5 iterate?

  • How many times loop on

line 7 iterate?

Q: e c

a b d c h e f g

nil,0 nil,inf nil,inf nil,inf nil,inf nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

a,1 a,1

slide-29
SLIDE 29

29

Breadth-First Search

  • Analyze the run time of BFS for a

graph with n vertices and m edges

– Find T(n)

  • How many times does loop on

line 5 iterate?

– N times (one iteration per vertex)

  • How many times loop on line 7

iterate?

– For each vertex, v, the loop executes deg(v) times – = σ𝑤∈𝑊 𝜄[1 + deg 𝑤 ] – = 𝜄 σ𝑤 1 + 𝜄 σ𝑤 deg(𝑤) – = Θ (n) + Θ (m)

  • Total = Θ(n+m)

Q: e c

a b d c h e f g

nil,0 nil,inf nil,inf nil,inf nil,inf nil,inf

BFS(G,u) 1 for each vertex v 2 pred[v] = nil, d[v] = inf. 3 Q = new Queue 4 Q.enqueue(u), d[u]=0 5 while Q is not empty 6 v = Q.front(); Q.dequeue() 7 foreach neighbor, w, of v: 8 if pred[w] == nil // w not found 9 Q.enqueue(w) 10 pred[w] = v, d[w] = d[v] + 1

a,1 a,1

slide-30
SLIDE 30

30

DEPTH FIRST SEARCH MOTIVATING EXAMPLE

Topological Search

slide-31
SLIDE 31

31

DFS Application: Topological Sort

  • Breadth-first search doesn't

solve all our problems.

  • Given a graph of dependencies

(tasks, prerequisities, etc.) topological sort creates a consistent ordering of tasks (vertices) where no dependencies are violated

  • Many possible valid topological
  • rderings exist

– EE 109, EE 209, EE 354, EE 454, EE 457, CS104, PHYS 152, CS 201,… – CS 104, EE 109, CS 170, EE 209,… EE 109 EE 209 EE 354 CS 104 CS 201 EE 457 EE 454L CS 350 CS 320 CS 170 CS 401 CS 360

slide-32
SLIDE 32

32

Topological Sort

  • Another example

– Getting dressed

  • More Examples:

– Project management scheduling – Build order in a Makefile or other compile project – Cooking using a recipe – Instruction execution on an out-

  • f-order pipelined CPU

– Production of output values in a simulation of a combinational gate network

Underwear Pants Belt Undershirt Shoes Tie Shirt Socks

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorith ms/GraphAlgor/topoSort.htm

Jacket

slide-33
SLIDE 33

33

Topological Sort

  • Does breadth-first search

work?

  • No. What if we started at CS 170…

– We'd go to CS 201L before CS 104

  • All parent nodes need to be

completed before any child node

  • BFS only guarantees some

parent has completed before child

  • Turns out a Depth-First Search

will be part of our solution

EE 109 EE 209 EE 354 EE 457 EE 454L CS 104 CS 201 CS 350 CS 320 CS 170 CS 401 CS 360

slide-34
SLIDE 34

34

Depth First Search

  • Explores ALL children

before completing a parent

– Note: BFS completes a parent before ANY children

  • For DFS let us assign:

– A start time when the node is first found – A finish time when a node is completed

  • If we look at our nodes in

reverse order of finish time (i.e. last one to finish back to first

  • ne to finish) we arrive at a…

– Topological ordering!!!

EE 109 EE 209 EE 354 CS 104 CS 201 EE 457 EE 454L CS 350 CS 320 CS 170 CS 401 CS 360

10 1 2 3 4 6 5 7 8 9 11 12 13 15 16 18 14 17 19 20 21 22 24 23 1 2 Start Time Finish Time CS 170, CS 104, CS 201, CS 320, CS 360, CS 477, CS 350, EE 109, EE 209L, EE 354, EE 454L, EE 457 Reverse Finish Time Order

slide-35
SLIDE 35

35

DFS Algorithm

  • DFS visits and completes all children before

completing (and going on to a sibling)

  • Process:

– Visit a node – Mark as visited (started) – For each visited neighbor, visit it and perform DFS on all of their children – Only then, mark as finished

  • Let's trace recursive DFS!!
  • If cycles in the graph, ensure we don’t get

caught visiting neighbors endlessly

– Use some status (textbooks use "colors" but really just some integer) – White = unvisited, – Gray = visited but not finished – Black = finished

DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list Source: "Introduction to Algorithms", Cormer, Leiserson, Rivest

slide-36
SLIDE 36

36

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-37
SLIDE 37

37

Depth First-Search

DFS-Visit (G, u,l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): u v

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-38
SLIDE 38

38

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): u v DFS-Visit(G,d,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-39
SLIDE 39

39

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): u v DFS-Visit(G,d,l): DFS-Visit(G,f,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-40
SLIDE 40

40

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): u DFS-Visit(G,d,l): DFS-Visit(G,f,l): DFS-Visit(G,h,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-41
SLIDE 41

41

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): u DFS-Visit(G,d,l): DFS-Visit(G,f,l): DFS-Visit(G,h,l): Finish_list: h

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-42
SLIDE 42

42

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): u DFS-Visit(G,d,l): DFS-Visit(G,f,l): Finish_list: h v

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-43
SLIDE 43

43

Depth First-Search

DFS-Visit (G, u,l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): DFS-Visit(G,d,l): DFS-Visit(G,f,l): Finish_list: h u DFS-Visit(G,g,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-44
SLIDE 44

44

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): DFS-Visit(G,d,l): DFS-Visit(G,f,l): Finish_list: h, g u DFS-Visit(G,g,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-45
SLIDE 45

45

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): DFS-Visit(G,d,l): DFS-Visit(G,f,l): Finish_list: h, g, f u

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-46
SLIDE 46

46

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): DFS-Visit(G,d,l): Finish_list: h, g, f, d u

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-47
SLIDE 47

47

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): Finish_list: h, g, f, d u v

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-48
SLIDE 48

48

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): Finish_list: h, g, f, d u v DFS-Visit(G,c,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-49
SLIDE 49

49

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): Finish_list: h, g, f, d u DFS-Visit(G,c,l): DFS-Visit(G,e,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-50
SLIDE 50

50

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): Finish_list: h, g, f, d. e u DFS-Visit(G,c,l): DFS-Visit(G,e,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-51
SLIDE 51

51

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): Finish_list: h, g, f, d. e, c u DFS-Visit(G,c,l):

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-52
SLIDE 52

52

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,a,l): Finish_list: h, g, f, d. e, c, a u

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-53
SLIDE 53

53

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,b,l): Finish_list: h, g, f, d. e, c, a u

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

May iterate through many complete vertices before finding b to launch a new search from

slide-54
SLIDE 54

54

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

DFS-Visit(G,b,l): Finish_list: h, g, f, d. e, c, a, b u

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-55
SLIDE 55

55

Depth First-Search

DFS-Visit (G, u, l) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 l.append(u)

a b c d e f g h

Finish_list: h, g, f, d. e, c, a, b u

DFS-All (G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) 7 return finish_list

slide-56
SLIDE 56

56

ANOTHER EXAMPLE (IF TIME)

With Cycles in the graph

slide-57
SLIDE 57

57

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

a b d c h e f g

slide-58
SLIDE 58

58

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

u v

slide-59
SLIDE 59

59

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): v u

slide-60
SLIDE 60

60

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): u v

slide-61
SLIDE 61

61

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): v u

slide-62
SLIDE 62

62

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): u v

slide-63
SLIDE 63

63

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): u v

slide-64
SLIDE 64

64

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): u v

slide-65
SLIDE 65

65

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFS-Visit(G,d): u v

slide-66
SLIDE 66

66

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFS-Visit(G,d): DFSQ: d u

slide-67
SLIDE 67

67

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFSQ: d u v

slide-68
SLIDE 68

68

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFSQ: d DFS-Visit(G,e): u v v

slide-69
SLIDE 69

69

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFSQ: d e DFS-Visit(G,e): u

slide-70
SLIDE 70

70

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFSQ: d e f DFS-Visit(G,f): u

slide-71
SLIDE 71

71

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFSQ: d e f g h b c u

slide-72
SLIDE 72

72

Depth First-Search

a b d c h e f g

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFSQ: d e f g h b c a

slide-73
SLIDE 73

73

ITERATIVE VERSION

slide-74
SLIDE 74

74

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

st: a

slide-75
SLIDE 75

75

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

st: a c e

slide-76
SLIDE 76

76

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

st: a c e c f

slide-77
SLIDE 77

77

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

st: a c e c f d g

slide-78
SLIDE 78

78

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

st: a c e c f d g c h

slide-79
SLIDE 79

79

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-80
SLIDE 80

80

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-81
SLIDE 81

81

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-82
SLIDE 82

82

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-83
SLIDE 83

83

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-84
SLIDE 84

84

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-85
SLIDE 85

85

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

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

slide-86
SLIDE 86

86

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 st = new Stack 4 st.push_back(s) 5 while st not empty 6 u = st.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 st.push_back(v) 12 else if u.color != WHITE 13 u.color = BLACK 14 st.pop_back() a b d c h e f g

st: a

slide-87
SLIDE 87

87

BFS vs. DFS Algorithm

  • BFS and DFS are more similar than you think

– Do we use a FIFO/Queue (BFS) or LIFO/Stack (DFS) to store vertices as we find them

BFS-Visit (G, start_node) 1 for each vertex u 2 u.color = WHITE 3 u.pred = nil 4 bfsq = new Queue 5 bfsq.push_back(start_node) 6 while bfsq not empty 7 u = bfsq.pop_front() 8 if u.color == WHITE 9 u.color = GRAY 10 foreach vertex v in Adj(u) do 11 bfsq.push_back(v) DFS-Visit (G, start_node) 1 for each vertex u 2 u.color = WHITE 3 u.pred = nil 4 st = new Stack 5 st.push_back(start_node) 6 while st not empty 7 u = st.top(); st.pop() 8 if u.color == WHITE 9 u.color = GRAY 10 foreach vertex v in Adj(u) do 11 st.push_back(v)

slide-88
SLIDE 88

88

SOLUTIONS

slide-89
SLIDE 89

89

Example 1: Count Nodes

  • Write a recursive function to count how many nodes are in

the binary tree

– Only process 1 node at a time – Determine pre-, in-, or post-order based on whose answers you need to compute the result for your node – For in- or post-order traversals, determine how to use/combine results from recursion on children

f(n) f(left) f(right)

// Node definition struct Tnode { int val; TNode *left, *right; }; int count(TNode* root) { if( root == NULL ) return 0; else { return 1 + count(root->left) + count(root->right); } }

slide-90
SLIDE 90

90

Example 2: Prefix Sums

  • Write a recursive function to have each node store the sum of

the values on the path from the root to each node.

– Only process 1 node at a time – Determine pre-, in-, or post-order based on whose answers you need to compute the result for your node

4 3 8 5 7 f(n) f(left) f(right)

void prefixH(TNode* root, int psum) void prefix(TNode* root) { prefixH(root, 0); } void prefixH(TNode* root, int psum) { if( root == NULL ) return; else { root->val += psum; prefixH(root->left, root->val); prefixH(root->right, root->val); } }

4 7 12 12 14