graph traversal breadth first search breadth first search
play

Graph Traversal Breadth First Search Breadth First Search (BFS) - PowerPoint PPT Presentation

Graph Traversal Breadth First Search Breadth First Search (BFS) Idea Explore graph layer by layer from a start vertex s . s . . . Layer 0 Layer 1 Layer 2 3 / 16 BFS Applications Finding a shortest path. Determine distances.


  1. Graph Traversal

  2. Breadth First Search

  3. Breadth First Search (BFS) Idea ◮ Explore graph layer by layer from a start vertex s . s . . . Layer 0 Layer 1 Layer 2 3 / 16

  4. BFS Applications ◮ Finding a shortest path. ◮ Determine distances. ◮ Garbage collection (checking for connected components) ◮ Solving Puzzles ◮ Web crawling ◮ Base for other algorithms. 4 / 16

  5. BFS – Algorithm Preparation ◮ For every vertex v , set the distance dist ( v ) := ∞ and the parent par ( v ) := null . For the start vertex a , set dist ( a ) := 0 . ◮ Add a to an empty queue Q . b ∞ a 0 c ∞ ∞ d ∞ ∞ e f ∞ g ∞ ∞ i h Q a 5 / 16

  6. BFS – Algorithm Iteration ◮ Select and remove first vertex v from Q . b ∞ a 0 c ∞ ∞ d ∞ ∞ e f ∞ g ∞ ∞ i h Q 5 / 16

  7. BFS – Algorithm Iteration ◮ For each u ∈ N ( v ) with dist ( u ) = ∞ , ◮ set dist ( u ) := dist ( v ) + 1 , b ∞ a 0 c ∞ 1 d ∞ e 1 f ∞ g ∞ ∞ i h Q 5 / 16

  8. BFS – Algorithm Iteration ◮ For each u ∈ N ( v ) with dist ( u ) = ∞ , ◮ set dist ( u ) := dist ( v ) + 1 , ◮ set par ( u ) := v , and b ∞ a 0 c ∞ 1 d ∞ e 1 f ∞ g ∞ ∞ i h Q 5 / 16

  9. BFS – Algorithm Iteration ◮ For each u ∈ N ( v ) with dist ( u ) = ∞ , ◮ set dist ( u ) := dist ( v ) + 1 , ◮ set par ( u ) := v , and ◮ add u to Q . b ∞ a 0 c ∞ 1 d ∞ e 1 f ∞ g ∞ ∞ i h Q c e 5 / 16

  10. BFS – Algorithm Iteration ◮ Repeat until Q is empty. b a 0 2 c ∞ 1 d ∞ e 1 f 2 g ∞ ∞ i h g Q e b 5 / 16

  11. BFS – Algorithm Iteration ◮ Repeat until Q is empty. b a 0 2 c ∞ 1 d ∞ e 1 f 2 g ∞ 2 i h g Q b h 5 / 16

  12. BFS – Algorithm Iteration ◮ Repeat until Q is empty. b a 0 2 c 1 3 d e 1 3 f 2 g ∞ 2 i h g Q f h d 5 / 16

  13. BFS – Algorithm Iteration ◮ Repeat until Q is empty. b a 0 2 c 1 3 d e 1 3 f 2 g 2 3 i h Q f h d 5 / 16

  14. BFS Input : A graph G = ( V , E ) and a start vertex s ∈ V . 1 For Each v ∈ V Set dist ( v ) := ∞ and par ( v ) := null . 2 3 Create a new empty queue Q . 4 Set dist ( s ) := 0 and add s to Q . 5 While Q is not empty v := Q . deque () 6 For Each u ∈ N ( v ) with dist ( u ) = ∞ 7 Set dist ( u ) := dist ( v ) + 1 and set par ( u ) = v . 8 Add u to Q . 9 6 / 16

  15. BFS – Runtime Preparation ◮ Each vertex is accessed once. No edge is accessed. ◮ O ( | V | ) time Iteration ◮ Each vertex is added to the queue at most once. ◮ For each vertex removed from the queue, each neighbour is accessed once. ◮ Thus, runtime is � | N ( v ) | = 2 | E | v ∈ V Total runtime: O ( | V | + | E | ) 7 / 16

  16. Depth First Search

  17. Depth First Search Idea ◮ Follow path until you get stuck. ◮ If got stuck, backtrack path until reach unexplored neighbour. Continue on unexplored neighbour. Applications ◮ Tree-traversal ◮ Cycle detection ◮ Finding blocks and strongly connected components ◮ Mace generation ◮ Base for other algorithm, e. g. testing for planarity 9 / 16

  18. DFS – Algorithm (using recurrence) Preparation ◮ For every vertex v , set it as unvisited ( vis ( v ) := False ) and set the parent par ( v ) := null . ◮ Call DFS( s ) for the start vertex s . DFS( v ) ◮ Set vis ( v ) = True . ◮ For each u ∈ N ( v ) with vis ( u ) = False ◮ Set par ( u ) := v . ◮ Call DFS( u ). 10 / 16

  19. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i Stack: 11 / 16

  20. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i a Stack: 11 / 16

  21. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i a c Stack: 11 / 16

  22. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i a c Stack: b 11 / 16

  23. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i a c Stack: b 11 / 16

  24. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i a c f Stack: b 11 / 16

  25. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i g a c f Stack: b 11 / 16

  26. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i g a c f Stack: b 11 / 16

  27. DFS – Algorithm Example Run a DFS with start vertex s . b a c d e f g h i a c f Stack: b 11 / 16

  28. DFS – Runtime Runtime ◮ A single DFS( v ) call (without recurrence) accesses v and all its neighbours. Thus, runtime (for a single vertex v is O ( | N [ v ] | ) . ◮ For each vertex v , DFS( v ) is called only once. ◮ Total runtime: O ( | V | + | E | ) 12 / 16

  29. DFS – Edges A DFS partitions the edges in four groups. 1. Tree edges. Determined by parent pointers par ( · ) . 13 / 16

  30. DFS – Edges A DFS partitions the edges in four groups. 1. Tree edges. Determined by parent pointers par ( · ) . 2. Forward edges. From an ancestor to a descented 13 / 16

  31. DFS – Edges A DFS partitions the edges in four groups. 1. Tree edges. Determined by parent pointers par ( · ) . 2. Forward edges. From an ancestor to a descented 3. Back edges. From a descented to an ancestor 13 / 16

  32. DFS – Edges A DFS partitions the edges in four groups. 1. Tree edges. Determined by parent pointers par ( · ) . 2. Forward edges. From an ancestor to a descented 3. Back edges. From a descented to an ancestor 4. Cross edges. (only in directed graphs) Remaining edges 13 / 16

  33. DFS – Recognising Edges On the DFS-tree ◮ Make a preorder and a postorder traversal. ◮ For each vertex, store a vector ( i , j ) where i is the index of v in the preorder and j is the index of v in the postorder. For an edge uv ◮ Let ( i , j ) be the indices for u and ( x , y ) be the indices for v . ◮ Forward edge: i < x ◮ Backward edge: i > x and j < y ◮ Cross edge: i > x and j > y 14 / 16

  34. DFS – Detecting Cycles Theorem A graph G has a cycle if and only if any DFS has a back edge. Proof ( ⇒ ) ◮ Assume G has a cycle { v 1 , v 2 , . . . , v k } . W. l. o. g., let v 1 be the first visited by the DFS. v 1 v k v 2 v 3 v 4 ◮ Then, v k is an ancestor of v 1 in the DFS tree, i. e., v k v 1 is a back edge. � 15 / 16

  35. DFS – Detecting Cycles Theorem A graph G has a cycle if and only if any DFS has a back edge. Proof ( ⇐ ) ◮ Assume a DFS produces a back edge uv . v u ◮ Thus, v is an ancestor of u , i. e., the path from v to u using tree edges plus the edge uv form a cycle. � 16 / 16

Recommend


More recommend