Presentation is loading. Please wait.

Presentation is loading. Please wait.

Depth-First Search Lecture 21: Graph Traversals

Similar presentations


Presentation on theme: "Depth-First Search Lecture 21: Graph Traversals"— Presentation transcript:

1 Depth-First Search Lecture 21: Graph Traversals
4/26/ :14 AM CSC 213 Lecture 21: Graph Traversals C B A E D L0 L1 F L2 C B A E D F

2 Subgraphs A subgraph S of a graph G is a graph such that
Depth-First Search 4/26/ :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

3 Graph with two connected components
Depth-First Search 4/26/ :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

4 Tree Tree is a graph Forest is graph containing a number of trees
Depth-First Search 4/26/ :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

5 Depth-First Search 4/26/ :14 AM Spanning Tree Spanning tree of a connected graph is a subgraph containing every vertex and no cycles Graph Spanning tree

6 Depth- & Breadth-First Search
Depth-First Search 4/26/ :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

7 DFS and Maze Traversal Classic strategy for exploring maze
Depth-First Search 4/26/ :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

8 Example unexplored vertex visited vertex unexplored edge
Depth-First Search 4/26/ :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

9 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/ :14 AM Example (cont.) D B A C E D B A C E D B A C E D B A C E

10 Properties of DFS Property 1 Property 2
Depth-First Search 4/26/ :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

11 Visitor Pattern Another commonly used coding method
Depth-First Search 4/26/ :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

12 Example unexplored vertex visited vertex unexplored edge
Depth-First Search 4/26/ :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

13 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/ :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

14 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/ :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

15 Properties of BFS Property 1 Property 2 Property 3
Depth-First Search 4/26/ :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

16 DFS vs. BFS Applications DFS BFS DFS BFS
Depth-First Search 4/26/ :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

17 DFS vs. BFS (cont.) Back edge (v,w) Cross edge (v,w) DFS BFS
Depth-First Search 4/26/ :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


Download ppt "Depth-First Search Lecture 21: Graph Traversals"

Similar presentations


Ads by Google