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

graphs lecture 2 today
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 1

Graphs Lecture 2

slide-2
SLIDE 2

Today

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

slide-3
SLIDE 3

BFS/DFS Review

Examples on board

slide-4
SLIDE 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

  • f x and y differ by at most 1.

a e b d c

Layer 0: {a} Layer 1: {b, c, d} Layer 2: {e} Proof?

slide-5
SLIDE 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

  • f x in the DFS tree.

Proof on board

a e b d c a e b d c

slide-6
SLIDE 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)

slide-7
SLIDE 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 for each edge (u,v) incident to u if discovered[v] is false add v to A layer[v] = layer[u] + 1 discovered[v] = true add (u,v) to T end end end

When A is a queue, it is equivalent to check for duplicates when adding to A

slide-8
SLIDE 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...

slide-9
SLIDE 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 3 5 4 2

1 2 3 4 5 2 4 5 1 3 4 2 5 1 2 1 3

slide-10
SLIDE 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?

slide-11
SLIDE 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?

slide-12
SLIDE 12

Application: Bipartite Testing

slide-13
SLIDE 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

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-14
SLIDE 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

slide-15
SLIDE 15

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-16
SLIDE 16

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-17
SLIDE 17

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-18
SLIDE 18

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-19
SLIDE 19

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-20
SLIDE 20

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-21
SLIDE 21

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-22
SLIDE 22

A bipartite solution

Alice Greta Freida Eunice Dorothy Cheryl Brenda

Who should you invite?

slide-23
SLIDE 23

Layer 1 Layer 2 Layer 3 Layer 4 Layer 0

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

  • nly if there is no edge between any two nodes in the same layer.

Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-24
SLIDE 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

  • nly if there is no edge between any two nodes in the same layer.

Layer 1 Layer 2 Layer 3 Layer 4 Layer 0 Alice Greta Freida Eunice Dorothy Cheryl Brenda

slide-25
SLIDE 25

Directed Graphs

Definitions: directed graph, DAG, topological

  • rder
slide-26
SLIDE 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...