1-1 Is a Graph Connected? Algorithm 1: Breadth-first search - From a starting node, find closest nodes first. a c Start at a: e b d 1-2 Is a Graph Connected? Algorithm 1: Breadth-first search - From a starting node, find closest nodes first. a c Start at a: e b d Visit all nodes at a c e distance 1 from a: b d 1-3 Is a Graph Connected? Algorithm 1: Breadth-first search - From a starting node, find closest nodes first. a c Start at a: e b d Visit all nodes at a c e distance 1 from a: b d Visit all nodes at a c e distance 2 from a: b d Slides05 BFS, DFS.key - February 6, 2019
2 BFS Tree If we keep only the edges traversed while doing a breadth-first search, we will have a tree. a c Edges to layer 1: e b d a c Plus edges to layer 2: e b d Done. Discard edges a c e not traversed. b d 3 Breadth-First Search Property. Let T be a BFS tree of G = (V , E), and let (x, y) be an edge of G. Then the layer of x and y differ by at most 1. Layer 0: {a} a c Layer 1: {b, c, d} e Layer 2: {e} b d Proof? 4 Shortest Path When we use an edge to add a node in BFS, remember the other endpoint. To find the shortest path, start at the end and walk backwards. a c {(a, ∅ ), (c, a), (d, a), (b, a), (e, c)} e b d new node edge traversed Shortest path from a to e: a-c-e Slides05 BFS, DFS.key - February 6, 2019
5-1 Is a Graph Connected? Algorithm 2: Depth-first search - When adding a node to the visited set, recursively add nodes from that node a c e b d 5-2 Is a Graph Connected? Algorithm 2: Depth-first search - When adding a node to the visited set, recursively add nodes from that node a c a c e e b d b d 5-3 Is a Graph Connected? Algorithm 2: Depth-first search - When adding a node to the visited set, recursively add nodes from that node a c a c a c e e e b d b d b d Slides05 BFS, DFS.key - February 6, 2019
5-4 Is a Graph Connected? Algorithm 2: Depth-first search - When adding a node to the visited set, recursively add nodes from that node a c a c a c e e e b d b d b d a c e b d 5-5 Is a Graph Connected? Algorithm 2: Depth-first search - When adding a node to the visited set, recursively add nodes from that node a c a c a c e e e b d b d b d a c a c e e b d b d 5-6 Is a Graph Connected? Algorithm 2: Depth-first search - When adding a node to the visited set, recursively add nodes from that node a c a c a c e e e b d b d b d a c a c a c e e e b d b d b d Done. Discard edges not traversed. Slides05 BFS, DFS.key - February 6, 2019
6 Depth First Search Theorem: Let T be a depth-first search tree. Let x and y be 2 nodes in the tree. Let (x, y) be an edge that is in G but not in T. Then either x is an ancestor of y or y is an ancestor of x in the DFS tree. a a c c e d e b d b Proof? 7 Summary Both BFS and DFS are 2 ways to find all the connected components in a graph. It doesn’ t matter what node we start at when we look for a connected component. The trees constructed will generally look different based on the algorithm we use and the order in which we visit nodes. Slides05 BFS, DFS.key - February 6, 2019
Recommend
More recommend