last time
play

Last Time Both BFS and DFS are 2 ways to find all the connected - PowerPoint PPT Presentation

1 Last Time 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


  1. 1 Last Time 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. Implementing BFS (2) 2 initialize discovered to false for all nodes add s to the queue R = {s} discovered[s] = true while the queue is not empty { let u be the first node in the queue remove u from the queue for each edge (u, v) { if (!discovered[v]) { add v to the queue R = R ∪ {v} discovered[v] = true } } } 3 Implementing DFS (2) initialize explored to all false push s onto the stack while the stack is not empty { pop node u from the stack if (!explored[u]) { explored[u] = true R = R ∪ {u} for each edge (u,v) incident on u { push v onto the stack } } } Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  2. Representing Graphs: 4 Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. Two representations of each edge. How much memory? How long does it take to find out if a specific edge exists? How long does it take to find all edges incident on a node? 1 2 3 4 5 1 2 1 0 1 0 1 1 2 1 0 1 1 0 3 3 0 1 0 0 1 4 1 1 0 0 0 5 4 5 1 0 1 0 0 5 Representing Graphs: Adjacency List Adjacency list. Node indexed array of lists. Two representations of each edge. How much memory? How long to find a specific edge? How long to find all edges incident on a node? 1 2 4 5 1 2 2 1 3 4 3 3 2 5 4 1 2 5 4 5 1 3 Implementing BFS (2) 6 initialize discovered to false for all nodes add s to the queue R = {s} discovered[s] = true while the queue is not empty { let n be the first node in the queue remove n from the queue while there are more nodes in n’ s adjacency list { let n2 be the next node in n’ s adjacency list if (!discovered[n2]) { add n2 to the queue R = R ∪ {n2} discovered[n2] = true } } } Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  3. Runtime Analysis 7 initialize discovered to all false add s to the queue R = {s} discovered[s] = true while the queue is not empty { let n be the first node in the queue remove n from the queue while there are more nodes in n’ s adjacency list { let n2 be the next node in n’ s adjacency list if (!discovered[n2]) { add n2 to the queue R = R ∪ {n2} discovered[n2] = true } } } 8 Implementing DFS (2) initialize explored to all false push s onto the stack while the stack is not empty { pop node u from the stack if (!explored[u]) { explored[u] = true R = R ∪ {u} for each edge (u,v) incident on u { push v onto the stack } } } 9 Runtime Analysis initialize explored to all false push s onto the stack while the stack is not empty { pop node u from the stack if (!explored[u]) { explored[u] = true R = R ∪ {u} for each edge (u,v) incident on u { push v onto the stack } } } Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  4. 10 Summary Adjacency lists generally use less memory and are faster than adjacency matrices BFS and DFS differ only in the order in which nodes are visited and have cost O(m+n) 11 Party Problem You want to throw a party at which there are no pairs of guests that do not get along. You want to invite as many guests as possible. How would you solve this? 12 The party problem Represent each guest as a node Draw an edge between guests who do not get along Find the largest set of nodes where there is no edge between any pair of nodes in the set Brenda Cheryl Alice Freida Dorothy Greta Eunice Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  5. 13 Bipartite Graphs A bipartite graph is an undirected graph G = (V, E) in which the nodes can be colored red or blue such that every edge has one red and one blue end. is a bipartite graph is NOT a bipartite graph 14 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice 15 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  6. 16 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice 17 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice 18 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  7. 19 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice 20 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice 21 A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice Who should you invite? Slides06 BFS, DFS, Bipartite.key - February 11, 2019

  8. 22 BFS and Bipartite Graphs Lemma. Let G be a connected graph, and let L 0 , …, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Brenda Eunice Alice Dorothy Freida Greta Cheryl Layer 4 Layer 2 Layer 3 Layer 0 Layer 1 23 BFS and Bipartite Graphs Lemma. Let G be a connected graph, and let L 0 , …, L k be the layers produced by BFS starting at node s. Exactly one of the following holds. (i) No edge of G joins two nodes of the same layer, and G is bipartite. (ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not bipartite). Brenda Eunice Alice Dorothy Freida Greta Cheryl Layer 3 Layer 4 Layer 1 Layer 2 Layer 0 Slides06 BFS, DFS, Bipartite.key - February 11, 2019

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend