Download presentation
Presentation is loading. Please wait.
Published byCora Turner Modified over 8 years ago
1
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals
2
Subgraphs Subgraph of a graph G must contain only edges & vertices from G Spanning subgraph contains all vertices from the graph Subgraph Spanning subgraph
3
Connected Graph Connected if path exists between every pair of vertices Requires a path, not an edge between vertices Connected component is subgraph containing all connected vertices Maximal connected Connected graph Graph with two connected components
4
Tree Connected, undirected, acyclic graph Just like Tree data structures Forest is graph containing multiple trees Tree Forest
5
Spanning Tree Subgraph that is spanning subgraph & tree Spanning subgraph must contain all vertices Tree must be connected without any cycles Spanning Tree
6
Depth- & Breadth-First Search Common graph traversal algorithms DFS & BFS traversals Visits all vertices and edges in G Computes if G is connected Finds connected components if G not connected Used frequently to solve graph problems
7
DFS and Maze Traversal Classic maze exploring strategy Each intersection, corner, & dead end is a vertex Corridors become edges DFS marks corridors traveled Marks lead back to start
8
DFS Example DB A C ED B A C E D B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge
9
DFS Example (cont.) DB A C E DB A C E DB A C E D B A C E
10
Properties of DFS Visits all vertices and edges in a connected component Edges followed form a spanning tree for the connected component DB A C E
11
BFS Example C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L0L0 L1L1 F CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F
12
BFS Example (cont.) CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2
13
CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2
14
Properties of BFS Also visits all the vertices and edges in a connected component Discovery edges create spanning tree of the component For each vertex v in L i Path from s to v in spanning tree has exactly i edges No paths from s to v in G has fewer than i edges CB A E D L0L0 L1L1 F L2L2
15
DFS vs. BFS Back edge (v,w) w is ancestor of v in tree of discovery edges Cross edge (v,w) v in same or previous level as w in tree of discovery edges CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS
16
For Next Lecture Review all our work with Graphs so far Come ready with questions you have Have fun & interesting problems to work on
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.