470 likes | 634 Views
CSC 213 – Large Scale Programming. Lecture 32: Graph TRAVERSALS. Today’s Goals. Make Britney sad through my color choices Revisit issue of graph terminology and usage Subgraphs , spanning, & connected graphs, oh my! Problems solved or why you should care about these
E N D
CSC 213 – Large Scale Programming Lecture 32:Graph TRAVERSALS
Today’s Goals • Make Britney sad through my color choices • Revisit issue of graph terminology and usage • Subgraphs, spanning, & connected graphs, oh my! • Problems solved orwhy you should care about these • Just rats in a maze: using graphs to find the exit • Confident in your decisions: how to travel depth-first • Using bread-first searching to cover all the bases • 2 algorithms both alike in dignity: how do they differ?
Today’s Goals • Make Britney sad through my color choices • Revisit issue of graph terminology and usage • Subgraphs, spanning, & connected graphs, oh my! • Problems solved orwhy you should care about these • Just rats in a maze: using graphs to find the exit • Confident in your decisions: how to travel depth-first • Using bread-first searchingto cover all the bases • 2 algorithms both alike in dignity: how do they differ?
Subgraphs • Subgraphof G contains edges & vertices from G Graph
Subgraphs • Subgraphof G contains edges & vertices from G Subgraph
Subgraphs • Subgraphof G contains edges & vertices from G • Spanning subgraphis subgraphcontaining all vertices in G Subgraph Graph
Subgraphs • Subgraphof G contains edges & vertices from G • Spanning subgraphis subgraphcontaining all vertices in G Subgraph Spanning subgraph
Connected Graphs & Subgraphs • Connectedif path exists between all vertex pairs • Requires path, not edge, between vertices Connected Graph
Connected Graphs & Subgraphs • Connectedif path exists between all vertex pairs • Requires path,not edge, between vertices Disconnected Graph
Connected Graphs & Subgraphs • Connected if path exists between all vertex pairs • Requires path,not edge, between vertices • Connected component is subgraph containing all connected vertices • Often called “maximally connected” Disconnected Graph Graph
Connected Graphs & Subgraphs • Connectedif path exists between all vertex pairs • Requires path,not edge, between vertices • Connected component is subgraph containing all connected vertices • Often called “maximally connected” Disconnected Graph Graph with 2 connected components
Connected Graphs & Subgraphs • Connected if path exists between all vertex pairs • Requires path,not edge, between vertices • Connected component is subgraph containing all connected vertices • Often called “maximally connected” Disconnected Graph Graph with 2 connected components
Tree • Connected acyclic graph • Resembles Tree structure Graph
Tree • Connected acyclic graph • Resembles Tree structure Tree
Tree • Connected acyclic graph • Resembles Tree structure • Don’t lose forest for trees • Forestis graph with multiple trees Tree Graph
Tree • Connected acyclic graph • Resembles Tree structure • Don’t lose forest for trees • Forest is graph with multiple trees Tree Tree
Tree • Connected acyclic graph • Resembles Tree structure • Don’t lose forest for trees • Forest is graph with multiple trees Tree Forest
Spanning Tree • Subgraphthat is both spanning subgraph& tree • Contains all vertices in graph spanning subgraph • Tree connected without any cycles Graph
Spanning Tree • Subgraphthat is both spanning subgraph& tree • Contains all vertices in graph spanning subgraph • Tree connected without any cycles Tree
Spanning Tree • Subgraphthat is both spanning subgraph& tree • Contains all vertices in graph spanning subgraph • Tree connected without any cycles Spanning subgraph
Spanning Tree • Subgraphthat is both spanning subgraph& tree • Contains all vertices in graph spanning subgraph • Tree connected without any cycles Spanning tree
Spanning Tree • Subgraphthat is both spanning subgraph& tree • Contains all vertices in graph spanning subgraph • Tree connected without any cycles Tree Than Spans Royals?
Depth- & Breadth-First Search • Common graph traversal algorithms • DFS & BFS traversals have common traits • Visits all vertices and edges in G • Determines whether of not Gis connected • Finds connected components if G not connected • Heavily used to solve graph problems
DFS and Maze Traversal • Classic maze strategy • Intersection, corner, & dead endare vertices • Corridors become edges • Traveled corridors marked • Marks trace back to start
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge B D E C
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge B D E C
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge ??? B D E C
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge B D E C
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge B D E C
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge B D E C
DFS Example unexplored vertex A visited vertex A unexplored edge A discovery edge back edge B D E C
Properties of DFS • Connected component’svertices & edges visited • Edges visited form spanning tree for component A B D E C
Properties of DFS • Connected component’svertices & edges visited • Edges visited form spanning tree for component A B D E C
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge B D E C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E L2 C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E L2 C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E L2 C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E L2 C F
BFS Example unexplored vertex A visited vertex A unexplored edge L0 A discovery edge cross edge L1 B D E L2 C F
Properties of BFS • Some properties similar to DFS: • Visits all vertices & edgesin connected component • Component’s spanning tree from discovery edges • For each vertex v in Li • Spanning treehas exactlyi edgeson path from s to v • All paths in G from s to v have at least iedges L0 A L1 B D E L2 C C F
DFS vs. BFS DFS BFS Back edge (v,w) • w is ancestor of vin tree of discovery edges Cross edge (v,w) • v in same or previous level as win tree of discovery edges A L0 A B D E L1 B D E L2 C C F C
For Next Lecture • Weekly assignment available & due Tuesday • Gives you good chance to check your understanding • Next Fri. 1st check-in for program assignment #3 • Planning now saves time later! • Read 13.4.1 – 13.4.3 for class on Monday • When using directed edges, what changes needed? • Other computations possible for directed graphs?