graphs lecture 2 today
play

Graphs Lecture 2 Today BFS/DFS Review; proof about DFS tree - PowerPoint PPT Presentation

Graphs Lecture 2 Today BFS/DFS Review; proof about DFS tree Implementation Running time Bipartite testing Topological sort BFS/DFS Review Examples on board Breadth-First Search Property. Let T be a BFS tree of G = (V , E), and let (x,


  1. Graphs Lecture 2

  2. Today BFS/DFS Review; proof about DFS tree Implementation Running time Bipartite testing Topological sort

  3. BFS/DFS Review Examples on board

  4. 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?

  5. 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 c a c e d e b d b Proof on board

  6. Graph Traversal Set explored[u] to be false for all u A = { s } / / set of discovered but not explored nodes while A is not empty Take a node u from A if explored[u] is false set explored[u] = true for each edge (u,v) incident to u add v to A end end end BFS: A is a queue (FIFO) DFS: A is a stack (LIFO)

  7. BFS: Alternate Implementation Set discovered[u] to be false for all u A = queue { s } / / set of discovered but not explored nodes layer[s] = 0 discovered[s] = true while A is not empty Take a node u from A When A is a queue, for each edge (u,v) incident to u if discovered[v] is false it is equivalent to add v to A check for duplicates layer[v] = layer[u] + 1 when adding to A discovered[v] = true add (u,v) to T end end end

  8. Running Time Set explored[u] to be false for all u A = { s } / / set of discovered but not explored nodes while A is not empty Take a node u from A if explored[u] is false set explored[u] = true for each edge (u,v) incident to u add v to A end end end Discuss on board: running time is O(n+m), pending correct data structure...

  9. 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

  10. Finding all Connected Components in a Graph Running BFS or DFS find all nodes connected to the start node How do we find all connected components? How expensive is that?

  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. Application: Bipartite Testing

  13. 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

  14. 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

  15. A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice

  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

  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

  22. A bipartite solution Brenda Cheryl Alice Freida Dorothy Greta Eunice Who should you invite?

  23. BFS and Bipartite Graphs Let G be a connected graph Lemma 1. G is bipartite if and only if G has no odd cycles Lemma 2. Let T be a BFS tree of G. Then G is bipartite if and only if there is no edge between any two nodes in the same layer. Brenda Eunice Alice Dorothy Freida Greta Cheryl Layer 4 Layer 3 Layer 2 Layer 1 Layer 0

  24. BFS and Bipartite Graphs Let G be a connected graph Lemma 1. G is bipartite if and only if G has no odd cycles Lemma 2. Let T be a BFS tree of G. Then G is bipartite if and only if there is no edge between any two nodes in the same layer. Brenda Eunice Alice Dorothy Freida Greta Cheryl Layer 4 Layer 3 Layer 2 Layer 1 Layer 0

  25. Directed Graphs Definitions: directed graph, DAG, topological order

  26. Topological Sort Lemma 1. If G has a topological order, then G is a DAG. Lemma 2. If G is a DAG, then G has a topological order. Proof by algorithm Find a node with no incoming edges Repeat...

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