Download presentation
Presentation is loading. Please wait.
1
Subgraphs, Connected Components, Spanning Trees
Depth-First Search 2/4/2019 8:01 PM Subgraphs, Connected Components, Spanning Trees Depth-First Search
2
Subgraphs A subgraph S of a graph G is a graph such that
The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph Depth-First Search
3
Non connected graph with two connected components
Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components Depth-First Search
4
Trees and Forests A (free) tree is an undirected graph T such that
T is connected T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest Depth-First Search
5
Spanning Trees and Forests
A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree Depth-First Search
6
Cycles How to detect that a graph has a cycle? Hint: BFS (or DFS)
Depth-First Search
7
Connectivity Determines whether G is connected
How do we know G is not connected? Hint: BFS or DFS Depth-First Search
8
Connected Components Computes the connected components of G
How do we find all the connected components? Depth-First Search
9
BFS for an entire graph Algorithm BFS(G, s)
Queue new empty queue enqueue(Queue, s) while Queue.isEmpty() v dequeue(Queue) if getLabel(v) != VISITED visit v setLabel(v, VISITED) neighbors getAdjacentVertices(G, v) for each w neighbors if getLabel(w) != VISITED // “children” enqueue(Queue, w) The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for each u G.vertices() setLabel(u, UNEXPLORED) for each v G.vertices() if getLabel(v) = UNEXPLORED BFS(G, v) Breadth-First Search
10
DFS for an Entire Graph The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G, v) visit v setLabel(v, VISITED) neighbors getAdjacentVertices(G, v) for each w neighbors if getLabel(w) != VISITED // “children” DFS(G, w) Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for each u G.vertices() setLabel(u, UNEXPLORED) for each v G.vertices() if getLabel(v) = UNEXPLORED DFS(G, v) Depth-First Search
11
Spanning Tree Computes a spanning tree of G
How do we find a spanning tree? Depth-First Search
12
BFS Example unexplored vertex visited vertex unexplored edge
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 Breadth-First Search
13
BFS Example (cont.) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B A
Breadth-First Search
14
BFS Example (cont.) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E F
Breadth-First Search
15
DFS Example unexplored vertex visited vertex unexplored edge
B A C E A A visited vertex unexplored edge discovery edge back edge D B A C E D B A C E Depth-First Search
16
DFS 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
17
Spanning Forest Computes a spanning forest of G
How do we find a spanning forest? Depth-First Search
18
Path finding Given a start vertex s and a destination vertex z
Find a path from s to z Depth-First Search
19
Path finding in a maze Vertex intersection, corner and dead end Edge
Corridor Depth-First Search
20
BFS and path finding Hint: use the spanning tree Depth-First Search
21
BFS and path finding Use the spanning tree Start from destination
Go to its parent Until source is reached Depth-First Search
22
BFS and path finding Use the spanning tree
Start from destination Go to its parent Until source is reached The found path is the shortest in terms of number of edges --- Why? Depth-First Search
23
DFS and path finding Use the spanning tree
Start from destination Go to its parent Until source is reached The spanning tree could be large Use DFS but faster, ideas? Depth-First Search
24
Path Finding find a path between two given vertices v and z
Algorithm pathDFS(G, v, z, S) setLabel(v, VISITED) push(S, v) if v = z return S.elements() path = null neighbors getAdjacentVertices(G, v) for each w neighbors if getLabel(w) != VISITED // “child” path = pathDFS(G, w, z, S) if path != null // has path via a child return path pop(S, v) //no path via a child or no children return null find a path between two given vertices v and z call DFS(G, v) with v as the start vertex use a stack S to keep track of the path When destination vertex z is encountered, return the path as the contents of the stack Depth-First Search
25
DFS vs. BFS Applications DFS BFS DFS BFS
Spanning forest, connected components, paths, cycles Shortest paths Biconnected components (still connected after removing one vertex, algorithm not discussed) C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search
26
DFS vs. BFS (cont.) Back edge (v,w) Cross edge (v,w) DFS BFS
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 C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.