Is a Graph Connected? Algorithm 1: Breadth-first search - From a - - PowerPoint PPT Presentation

is a graph connected
SMART_READER_LITE
LIVE PREVIEW

Is a Graph Connected? Algorithm 1: Breadth-first search - From a - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Is a Graph Connected?

Algorithm 1: Breadth-first search - From a starting node, find closest nodes first.

a e b d c

Start at a:

1-1

Is a Graph Connected?

Algorithm 1: Breadth-first search - From a starting node, find closest nodes first.

a e b d c

Start at a:

a e b d c

Visit all nodes at distance 1 from a:

1-2

Is a Graph Connected?

Algorithm 1: Breadth-first search - From a starting node, find closest nodes first.

a e b d c

Start at a:

a e b d c

Visit all nodes at distance 1 from a:

a e b d c

Visit all nodes at distance 2 from a:

1-3 Slides05 BFS, DFS.key - February 6, 2019

slide-2
SLIDE 2

BFS Tree

If we keep only the edges traversed while doing a breadth-first search, we will have a tree.

a e b d c a e b d c a e b d c

Edges to layer 1: Plus edges to layer 2:

  • Done. Discard edges

not traversed.

2

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.

a e b d c

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

3

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 e b d c

{(a, ∅), (c, a), (d, a), (b, a), (e, c)} Shortest path from a to e: a-c-e new node edge traversed

4 Slides05 BFS, DFS.key - February 6, 2019

slide-3
SLIDE 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 e b d c

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 e b d c a e b d c

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 e b d c a e b d c a e b d c

5-3 Slides05 BFS, DFS.key - February 6, 2019

slide-4
SLIDE 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 e b d c a e b d c a e b d c a e b d c

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 e b d c a e b d c a e b d c a e b d c a e b d c

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 e b d c a e b d c a e b d c a e b d c a e b d c a e b d c

  • Done. Discard edges

not traversed.

5-6 Slides05 BFS, DFS.key - February 6, 2019

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?

a e b d c a e b d c

6

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.

7 Slides05 BFS, DFS.key - February 6, 2019