Subgraphs Connected Components Spanning Trees 2014 Goodrich Tamassia
Subgraphs, Connected Components, Spanning Trees © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 1
Subgraphs q A subgraph S of a graph G is a graph such that n n q The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G Subgraph A spanning subgraph of G is a subgraph that contains all the vertices of G Spanning subgraph © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 2
Connectivity q q 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 © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 3
Trees and Forests q A (free) tree is an undirected graph T such that T is connected n T has no cycles This definition of tree is different from the one of a rooted tree n q q A forest is an undirected graph without cycles The connected components of a forest are trees © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search Tree Forest 4
Spanning Trees and Forests q A spanning tree of a connected graph is n q A spanning tree n q q a spanning subgraph that is a tree is not unique unless the graph is a tree Graph Spanning trees have applications to the design of communication networks A spanning forest of a graph n is a spanning subgraph that is a forest Spanning tree © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 5
Cycles q How to detect that a graph has a cycle? n Hint: BFS (or DFS) © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 6
Connectivity q Determines whether G is connected n How do we know G is not connected? w Hint: BFS or DFS © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 7
Connected Components q Computes the connected components of G n How do we find all the connected components? © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 8
BFS for an entire graph q 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() set. Label(u, UNEXPLORED) for each v G. vertices() if get. Label(v) = UNEXPLORED BFS(G, v) © 2014 Goodrich, Tamassia, Goldwasser Algorithm BFS(G, s) Queue new empty queue enqueue(Queue, s) while is. Empty(Queue) v dequeue(Queue) if get. Label(v) != VISITED visit v set. Label(v, VISITED) neighbors get. Adjacent. Vertices(G, v) for each w neighbors if get. Label(w) != VISITED // “children” enqueue(Queue, w) Breadth-First Search 9
DFS for an Entire Graph q The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for each u G. vertices() set. Label(u, UNEXPLORED) for each v G. vertices() if get. Label(v) = UNEXPLORED DFS(G, v) © 2014 Goodrich, Tamassia, Goldwasser Algorithm DFS(G, v) visit v set. Label(v, VISITED) neighbors get. Adjacent. Vertices(G, v) for each w neighbors if get. Label(w) != VISITED // “children” DFS(G, w) Depth-First Search 10
Spanning Tree q Computes a spanning tree of G n How do we find a spanning tree? © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 11
BFS Example unexplored vertex visited vertex unexplored edge discovery edge cross edge A A L 0 L 1 L 0 C E B D L 1 F © 2014 Goodrich, Tamassia, Goldwasser C E A B A F A B C E Breadth-First Search D D F 12
BFS Example (cont. ) L 0 L 1 L 0 A B C E L 0 L 1 B L 2 F L 0 A B L 2 D L 1 C E © 2014 Goodrich, Tamassia, Goldwasser D L 1 Breadth-First Search C E D F A B L 2 F A C E D F 13
BFS Example (cont. ) L 0 L 1 A B L 2 C E D L 1 B L 2 F A C E D F A B L 2 L 0 C E D F © 2014 Goodrich, Tamassia, Goldwasser Breadth-First Search 14
DFS Example unexplored vertex visited vertex unexplored edge discovery edge back edge A A A B E D E C A B D A D E C © 2014 Goodrich, Tamassia, Goldwasser B C Depth-First Search 15
DFS Example (cont. ) A B A D E B C C A A B D E C © 2014 Goodrich, Tamassia, Goldwasser B D E C Depth-First Search 16
Spanning Forest q Computes a spanning forest of G n How do we find a spanning forest? © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 17
Path finding q Given a start vertex s and a destination vertex z n Find a path from s to z © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 18
Path finding in a maze q q Vertex n intersection, corner and dead end Edge n Corridor © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 19
BFS and path finding q Hint: use the spanning tree © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 20
BFS and path finding q Use the spanning tree n n n Start from destination Go to its parent Until source is reached © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 21
BFS and path finding q Use the spanning tree n n n q Start from destination Go to its parent Until source is reached Faster, any ideas? n Hint: not the entire spanning tree © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 22
BFS and path finding q Use the spanning tree n n n q Faster, any ideas? n q Start from destination Go to its parent Until source is reached Hint: not the entire spanning tree The found path is the shortest in terms of number of edges --- Why? © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 23
DFS and path finding q Use the spanning tree n n n Start from destination Go to its parent Until source is reached © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 24
DFS and path finding q Use the spanning tree n n n q Start from destination Go to its parent Until source is reached Faster n Hint: not the entire spanning tree © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 25
DFS and path finding q Use the spanning tree n n n q Faster n q Start from destination Go to its parent Until source is reached Hint: not the entire spanning tree Less space, “no spanning tree, ” ideas? n Hint: use a stack © 2014 Goodrich, Tamassia, Goldwasser Depth-First Search 26
Path Finding q q 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, n return the path as the contents of the stack © 2014 Goodrich, Tamassia, Goldwasser Algorithm path. DFS(G, v, z, S) set. Label(v, VISITED) push(S, v) if v = z return elements(S) path = null neighbors get. Adjacent. Vertices(G, v) for each w neighbors if get. Label(w) != VISITED // “child” path = path. DFS(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 Depth-First Search 27
DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles Shortest paths Biconnected components (still connected after removing one vertex, algorithm not discussed) L 0 A B A C E D L 1 L 2 F DFS © 2014 Goodrich, Tamassia, Goldwasser B C E D F BFS Breadth-First Search 28
DFS vs. BFS (cont. ) Back edge (v, w) n Cross edge (v, w) w is an ancestor of v in the tree of discovery edges n L 0 A B w is in the same level as v or in the next level C E D L 1 DFS © 2014 Goodrich, Tamassia, Goldwasser B L 2 F A C E D F BFS Breadth-First Search 29
- Slides: 29