1 / 17

CSC 213

A. L 0. A. B. C. D. L 1. B. C. D. E. F. L 2. E. F. CSC 213. Lecture 21: Graph Traversals. 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.

Télécharger la présentation

CSC 213

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A L0 A B C D L1 B C D E F L2 E F CSC 213 Lecture 21: Graph Traversals

  2. 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’svertices Subgraph Spanning subgraph

  3. 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 Connectivity Connected graph Graph with two connected components

  4. 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. 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 • 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 • 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. A A B D E B D E C C Example unexplored vertex A visited vertex A unexplored edge discovery edge back edge A B D E C

  9. A A A B D E B D E B D E C C C A B D E C Example (cont.)

  10. 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 A B D E C Properties of DFS

  11. 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. L0 A L1 B C D E F Example unexplored vertex A visited vertex A unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F

  13. L0 L0 A A L1 L1 B C D B C D L2 E F E F L0 L0 A A L1 L1 B C D B C D L2 L2 E F E F Example (cont.)

  14. L0 L0 A A L1 L1 B C D B C D L2 L2 E F E F Example (cont.) L0 A L1 B C D L2 E F

  15. 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 treehas exactlyi edges Paths from s to v in G has at leasti edges Properties of BFS A B C D E F L0 A L1 B C D L2 E F

  16. L0 A L1 B C D L2 E F DFS vs. BFS A B C D E F DFS BFS

  17. 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 A L0 A B C D L1 B C D E F L2 E F DFS vs. BFS (cont.) DFS BFS

More Related