2.5 bfs & dfs 02

65
Graph Traversals Depth-First Search Think Stack Breadth-First Search Think Queue

Transcript of 2.5 bfs & dfs 02

Page 1: 2.5 bfs & dfs 02

Graph Traversals

Depth-First Search• Think Stack

Breadth-First Search• Think Queue

Page 2: 2.5 bfs & dfs 02

Overview

Goal To systematically visit the nodes of a graph

A tree is a directed, acyclic, graph (DAG) If the graph is a tree,

DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals

BFS is exhibited by level-order traversal

Page 3: 2.5 bfs & dfs 02

Depth-First Search

// recursive, preorder, depth-first searchvoid dfs (Node v) { if (v == null) return;

if (v not yet visited) visit&mark(v); // visit node before adjacent nodes

for (each w adjacent to v) if (w has not yet been visited) dfs(w);

} // dfs

Page 4: 2.5 bfs & dfs 02

Depth-First Search

// recursive, postorder, depth-first searchvoid dfs (Node v) { if (v == null) return;

tag(v); // mark v as having been considered for (each w adjacent to v) if (w has not yet been tagged) dfs(w);

visit(v); // postorder traversal: visit node after // adjacent nodes

} // dfs

Page 5: 2.5 bfs & dfs 02

Depth-First Search

// non-recursive, preorder, depth-first searchvoid dfs (Node v) { if (v == null) return;

push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v);

for (each w adjacent to v) if (w has not yet been visited) push(w); } // while

} // dfs

Page 6: 2.5 bfs & dfs 02

Depth-First Search

// non-recursive, postorder, depth-first searchvoid dfs (Node v) {

// Lex 20 (Do not need to submit)

} // dfs

Page 7: 2.5 bfs & dfs 02

Example

0

7

1

5

4

3

2

6

Policy: Visit adjacent nodes in increasing index order

Page 8: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7 6 4

Page 9: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6 5

Push 5

Page 10: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5

Pop/Visit/Mark 5

Page 11: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5

1

2

Push 2, Push 1

Page 12: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1

2

Pop/Visit/Mark 1

Page 13: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1

0

2

4

2

Push 4, Push 2, Push 0

Page 14: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0

2

4

2

Pop/Visit/Mark 0

Page 15: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0

3

7

2

4

2

Push 7, Push 3

Page 16: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3

7

2

4

2

Pop/Visit/Mark 3

Page 17: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3

2

7

2

4

2

Push 2

Page 18: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2

7

2

4

2

Pop/Mark/Visit 2

Page 19: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7

2

4

2

Pop/Mark/Visit 7

Page 20: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7

6

2

4

2

Push 6

Page 21: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7 6

2

4

2

Pop/Mark/Visit 6

Page 22: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7 6

4

2

Pop (don’t visit) 2

Page 23: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7 6 4

2

Pop/Mark/Visit 4

Page 24: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7 6 4

Pop (don’t visit) 2

Page 25: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 2 7 6 4

Done

Page 26: 2.5 bfs & dfs 02

Preorder DFS: Start with Node 5Note: edge (0,3) removed

0

7

1

5

4

3

2

6

5 1 0 7 6 2 4 3

Page 27: 2.5 bfs & dfs 02

Depth-First SearchPolicy: Don’t push nodes twice

// non-recursive, preorder, depth-first searchvoid dfs (Node v) { if (v == null) return;

push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v);

for (each w adjacent to v) if (w has not yet been visited && not yet stacked) push(w); } // while

} // dfs

Page 28: 2.5 bfs & dfs 02

Preorder DFS (Don’t push nodes twice). Start with Node 5

0

7

1

5

4

3

2

6

5 1 0 3 7 6 4 2

Page 29: 2.5 bfs & dfs 02

Postorder DFS: Start with Node 5

0

7

1

5

4

3

2

6

2 3 6 7 0 4 1 5

Page 30: 2.5 bfs & dfs 02

Breadth-first Search

Ripples in a pond Visit designated node Then visited unvisited nodes a distance i away, where i = 1,

2, 3, etc. For nodes the same distance away, visit nodes in systematic

manner (eg. increasing index order)

Page 31: 2.5 bfs & dfs 02

Breadth-First Search

// non-recursive, preorder, breadth-first searchvoid bfs (Node v) { if (v == null) return;

enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v);

for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while

} // bfs

Page 32: 2.5 bfs & dfs 02

BFS: Start with Node 5

7

1

5

4

3

2

6

5 1 2 0 4 3 7 6

0

Page 33: 2.5 bfs & dfs 02

BFS: Start with Node 5

7

1

5

4

3

2

6

5

0

Page 34: 2.5 bfs & dfs 02

BFS: Node one-away

7

1

5

4

3

2

6

5

0

Page 35: 2.5 bfs & dfs 02

BFS: Visit 1 and 2

7

1

5

4

3

2

6

5 1 2

0

Page 36: 2.5 bfs & dfs 02

BFS: Nodes two-away

7

1

5

4

3

2

6

5 1 2

0

Page 37: 2.5 bfs & dfs 02

BFS: Visit 0 and 4

7

1

5

4

3

2

6

5 1 2 0 4

0

Page 38: 2.5 bfs & dfs 02

BFS: Nodes three-away

7

1

5

4

3

2

6

5 1 2 0 4

0

Page 39: 2.5 bfs & dfs 02

BFS: Visit nodes 3 and 7

7

1

5

4

3

2

6

5 1 2 0 4 3 7

0

Page 40: 2.5 bfs & dfs 02

BFS: Node four-away

7

1

5

4

3

2

6

5 1 2 0 4 3 7

0

Page 41: 2.5 bfs & dfs 02

BFS: Visit 6

7

1

5

4

3

2

6

5 1 2 0 4 3 7 6

0

Page 42: 2.5 bfs & dfs 02

BiconnectivityA connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph.

Example :

If the nodes are computers and the edges are links, then if any computer goes down, network mail is unaffected, except, of course, at the down computer.

Page 43: 2.5 bfs & dfs 02

Biconnectivity If a graph is not biconnected, the vertices whose removal

would disconnect the graph are known as articulation points. These nodes are critical in many applications.

Page 44: 2.5 bfs & dfs 02

BiconnectivityThe graph below is not biconnected: C and D are articulation points. The removal of C would disconnect G, and the removal of D would disconnect E and F, from the rest of the graph

Page 45: 2.5 bfs & dfs 02

Biconnectivity• Depth-first search provides a linear-time algorithm to find all

articulation points in a connected graph.

1.Starting at any vertex, perform a depth-first search and number the nodes as they are visited: num(v).

2.For every vertex v in the depth-first spanning tree, compute the lowest-numbered vertex called low(v), that is reachable from v by taking zero or more tree edges and then possibly one back edge (in that order).

Page 46: 2.5 bfs & dfs 02

Biconnectivity The depth-first spanning tree shows the preorder number

first, and then the lowest-numbered vertex reachable

(For each node num/low is given in the tree)

Page 47: 2.5 bfs & dfs 02

Biconnectivity• The lowest-numbered vertex , low(v), can be efficiently

computed by performing a postorder traversal of the

depth-first spanning tree, and it is the minimum of

1)num(v)

2)the lowest num(w) among all back edges (v, w)

3)the lowest low(w) among all tree edges (v, w)

Page 48: 2.5 bfs & dfs 02

Biconnectivity: How to decide Articulation Points

The root is an articulation point if and only if it has more than one child (because if it has two children, removing the root disconnects nodes in different subtrees, and if it has only one child, removing the root merely disconnects the root)

Any other vertex v is an articulation point if and only if v has some child w such that low(w) ≥ num(v).Example:Vertex D is an articulation point

low(E)= 4 ≥ num(D)=4Vertex C is an articulation point

low(G)= 7 ≥ num(C)=3

Page 49: 2.5 bfs & dfs 02

                                                                                             

How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot?

TRIANGLES: 

How many triangles are located in the image below?

Page 50: 2.5 bfs & dfs 02

There are 11 squares total; 5 small, 4 medium, and 2 large.

27 triangles.  There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.

Page 51: 2.5 bfs & dfs 02

GUIDED READING

Page 52: 2.5 bfs & dfs 02
Page 53: 2.5 bfs & dfs 02

ASSESSMENT

Page 54: 2.5 bfs & dfs 02

Do the DFS and BFS for this graph

12

43

57

6

Page 55: 2.5 bfs & dfs 02

DIFFERENT TRAVERSALS STARTING FROM VERTEX 1

PRE ORDER TRAVERSAL (ALSO CALLED DEPTH FIRST SEARCH)

1 2 3 4 5 6 7 POST ORDER TRAVERSAL

6 5 4 7 3 2 1 BREADTH FIRST SEARCH

1 2 4 3 5 6 7

Page 56: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

Page 57: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2

3

6

5

4

7

Page 58: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2

3

6 4

5

4

7

Page 59: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2

3

6 4

5 4

4

7

Page 60: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2

3

6 4

5 4

4 1

7

Page 61: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2

3

6 4

5 4

4 1

7 7

Page 62: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2

3 1

6 4

5 4

4 1

7 7

Page 63: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1

2 1

3 1

6 4

5 4

4 1

7 7

Page 64: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1 1

2 1

3 1

6 4

5 4

4 1

7 7

Page 65: 2.5 bfs & dfs 02

DEPTH FIRST SPANNING TREE

6•1

•4

•2

•5

•3

•6•7

1 1

2 1

3 1

6 4

5 4

4 1

7 7