170 likes | 277 Views
This lecture delves into graph traversals and subgraphs, defining key concepts such as spanning subgraphs and connected components. We explore the characteristics of trees and forests, and the distinctions between depth-first search (DFS) and breadth-first search (BFS) techniques for traversing graphs. The lecture emphasizes the importance of such traversals in computing spanning trees and analyzing graph properties. Practical examples highlight how DFS and BFS work in various scenarios, including maze traversal and connected component analysis.
E N D
A L0 A B C D L1 B C D E F L2 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’svertices Subgraph Spanning subgraph
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
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
Spanning Tree • Spanning tree of a connected graph is a subgraph containing every vertex and no cycles Graph Spanning tree
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 • 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
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
A A A B D E B D E B D E C C C A B D E C Example (cont.)
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
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
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
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.)
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
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
L0 A L1 B C D L2 E F DFS vs. BFS A B C D E F DFS BFS
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