Depth-First Search Lecture 21: Graph Traversals 4/26/2017 10:14 AM CSC 213 Lecture 21: Graph Traversals C B A E D L0 L1 F L2 C B A E D F
Subgraphs A subgraph S of a graph G is a graph such that Depth-First Search 4/26/2017 10:14 AM Subgraphs A subgraph S of a graph G is a graph such that All edges & vertices in S also exist within G Spanning subgraph of G contains all of G’s vertices Subgraph Spanning subgraph
Graph with two connected components Depth-First Search 4/26/2017 10:14 AM Connectivity Graph is connected path exists between every pair of vertices Does not require an edge between all vertices, however Connected component is a maximal connected subgraph So you could not add another connected node to the subgraph Connected graph Graph with two connected components
Tree Tree is a graph Forest is graph containing a number of trees Depth-First Search 4/26/2017 10:14 AM Tree Tree is a graph Must be undirected Must be connected Cannot contain a cycle Forest is graph containing a number of trees Tree Forest
Depth-First Search 4/26/2017 10:14 AM Spanning Tree Spanning tree of a connected graph is a subgraph containing every vertex and no cycles Graph Spanning tree
Depth- & Breadth-First Search Depth-First Search 4/26/2017 10:14 AM Depth- & Breadth-First Search Common techniques to traverse a graph DFS and BFS traversal of a graph G Visits all vertices and edges in G Computes if G is connected and, if it is not, finds connected components Computes spanning tree/spanning forest They help solve many graph problems
DFS and Maze Traversal Classic strategy for exploring maze Depth-First Search 4/26/2017 10:14 AM DFS and Maze Traversal Classic strategy for exploring maze Mark the intersections, corners and dead ends visited We mark corridors (edges) followed Can follow lines to get back to where we started the maze
Example unexplored vertex visited vertex unexplored edge Depth-First Search 4/26/2017 10:14 AM Example unexplored vertex D B A C E A A visited vertex unexplored edge discovery edge back edge D B A C E A B D E C
Example (cont.) D B A C E D B A C E D B A C E D B A C E Depth-First Search 4/26/2017 10:14 AM Example (cont.) D B A C E D B A C E D B A C E D B A C E
Properties of DFS Property 1 Property 2 Depth-First Search 4/26/2017 10:14 AM Properties of DFS Property 1 Visits all vertices and edges within a connected component Property 2 Edges followed during the depth-first search form a spanning tree for the connected component D B A C E
Visitor Pattern Another commonly used coding method Depth-First Search 4/26/2017 10:14 AM Visitor Pattern Another commonly used coding method Often uses DFS as a Template Method Defines at least 5 methods: initResult() – called at start of traversal startVisit() – called at start of vertex (node) processing 1 or more methods to analyze data during the traversal finishVisit() – called at end of vertex processing result() – called to get result of traversal
Example unexplored vertex visited vertex unexplored edge Depth-First Search 4/26/2017 10:14 AM Example C B A E D L0 L1 F A unexplored vertex A visited vertex unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F
Example (cont.) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B A E D Depth-First Search 4/26/2017 10:14 AM Example (cont.) C B A E D L0 L1 F C B A E D L0 L1 F L2 C B A E D L0 L1 F L2 C B A E D L0 L1 F L2
Example (cont.) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E F C B Depth-First Search 4/26/2017 10:14 AM Example (cont.) C B A E D L0 L1 F L2 L0 A L1 B C D L2 E F C B A E D L0 L1 F L2
Properties of BFS Property 1 Property 2 Property 3 Depth-First Search 4/26/2017 10:14 AM Properties of BFS Property 1 Visits all the vertices and edges within connected component Property 2 Discovery edges form a spanning tree of the component Property 3 For each vertex v in Li Path from s to v in spanning tree has exactly i edges Paths from s to v in G has at least i edges A B C D E F L0 A L1 B C D L2 E F
DFS vs. BFS Applications DFS BFS DFS BFS Depth-First Search 4/26/2017 10:14 AM DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles Shortest paths Biconnected components C B A E D L0 L1 F L2 A B C D E F DFS BFS
DFS vs. BFS (cont.) Back edge (v,w) Cross edge (v,w) DFS BFS Depth-First Search 4/26/2017 10:14 AM DFS vs. BFS (cont.) Back edge (v,w) w is an ancestor of v in the tree of discovery edges Cross edge (v,w) w is in the same level as v or in the next level in the tree of discovery edges C B A E D L0 L1 F L2 C B A E D F DFS BFS