Chapter 3.23.4 Spanning Tree Algorithms Prof. Tesler Math 154 - - PowerPoint PPT Presentation

chapter 3 2 3 4 spanning tree algorithms
SMART_READER_LITE
LIVE PREVIEW

Chapter 3.23.4 Spanning Tree Algorithms Prof. Tesler Math 154 - - PowerPoint PPT Presentation

Chapter 3.23.4 Spanning Tree Algorithms Prof. Tesler Math 154 Winter 2020 Prof. Tesler Ch. 3.23.4: Spanning Tree Algorithms Math 154 / Winter 2020 1 / 56 Depth 4 Depth 0 Depth 1 3 8 5 6 2 Depth 2 7 10 9 1 Depth 3 The


slide-1
SLIDE 1

Chapter 3.2–3.4 Spanning Tree Algorithms

  • Prof. Tesler

Math 154 Winter 2020

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 1 / 56

slide-2
SLIDE 2

Depth

Depth 3 4 1 2 3 5 6 8 7 10 9 Depth 2 Depth 1 Depth 0

The depth of v is the length of the path from the root to v. The height of the tree is the maximum depth.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 2 / 56

slide-3
SLIDE 3

Ordered trees

6 4 1 2 3 5 6 8 7 10 9 4 5 8 7 10 9 3 2 1

An ordered tree puts the children of each node into a specific

  • rder (pictorially represented as left-to-right).

The diagrams shown above are the same as unordered trees, but are different as ordered trees. We’ll be looking at graphs and trees drawn in other ways; there will be other orderings besides left-to-right.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 3 / 56

slide-4
SLIDE 4

Depth first search of a tree

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 4 / 56

slide-5
SLIDE 5

Depth first search of a tree

We will number the vertices by a depth first search (DFS), also called a depth first traversal.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 5 / 56

slide-6
SLIDE 6

Depth first search of a tree

1

Start at the root r (the top vertex in this diagram), and assign it the number 1.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 6 / 56

slide-7
SLIDE 7

Depth first search of a tree

2 1

Visit 1’s first child (in left-to-right order), and assign it the number 2.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 7 / 56

slide-8
SLIDE 8

Depth first search of a tree

3 1 2

Visit 2’s first child (in left-to-right order), and assign it the number 3.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 8 / 56

slide-9
SLIDE 9

Depth first search of a tree

3 1 2

3 is a leaf, with no children! We explored as far as possible, but now have to backtrack to explore more. Back up how we came until a vertex (2) with at least one unnumbered child.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 9 / 56

slide-10
SLIDE 10

Depth first search of a tree

4 1 2 3

The next child of 2 is numbered 4. Continue going down this branch, choosing the leftmost option available.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 10 / 56

slide-11
SLIDE 11

Depth first search of a tree

5 1 2 3 4

Visit 4’s first child and assign it the number 5.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 11 / 56

slide-12
SLIDE 12

Depth first search of a tree

5 1 6 2 3 4

5 has no children. Back up to the first vertex (4) that has a child not yet numbered. Assign the first remaining child the number 6.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 12 / 56

slide-13
SLIDE 13

Depth first search of a tree

7 1 6 2 3 4 5

6 has no children. Back up to the first vertex (4) that has a child not yet numbered. Assign the first remaining child the number 7.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 13 / 56

slide-14
SLIDE 14

Depth first search of a tree

8 1 6 2 3 4 5 7

7 has no children. Back up to the first vertex (2) that has a child not yet numbered. Assign the first remaining child the number 8.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 14 / 56

slide-15
SLIDE 15

Depth first search of a tree

16 1 6 2 3 4 5 7 8 9 10 11 12 13 14 15

Continue in this fashion until all vertices are numbered.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 15 / 56

slide-16
SLIDE 16

Breadth first search of a tree

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 16 / 56

slide-17
SLIDE 17

Breadth first search of a tree

16 1 11 2 5 6 10 12 7 3 4 8 9 13 14 15

While depth first search explores as deep as possible, Breadth first search (BFS) works one layer (depth) at a time. Depth 0: Number the root 1. Depth 1: Consecutively number all neighbors of 1. Depth 2: The depth 2 elements are all the neighbors of the depth 1 elements not yet accounted for. Number them consecutively. Continue in this way for depth 3, 4, etc.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 17 / 56

slide-18
SLIDE 18

Breadth first search of a tree

Depth first search Breadth first search

16 1 11 2 5 6 10 12 7 3 4 8 9 13 14 15 16 1 6 2 3 4 5 7 8 9 10 11 12 13 14 15

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 18 / 56

slide-19
SLIDE 19

Depth first search (DFS) in a connected graph

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 19 / 56

slide-20
SLIDE 20

Depth first search (DFS) in a connected graph

a g i h e d b c j f

For trees, we showed the root at the top, and children below in left-to-right order. That doesn’t apply for arbitrary graph drawings.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 20 / 56

slide-21
SLIDE 21

Depth first search (DFS) in a connected graph

a g i h e d b c j f

Set up a counter: time = 0

We’ll keep adding 1 to it as we number the vertices.

Pick starting vertex: a

The starting vertex can be any vertex; a is just an example. You may get a different tree, depending on where you start. This takes the place of the root in a rooted tree. In some applications, it’s called the source.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 21 / 56

slide-22
SLIDE 22

Depth first search (DFS) in a connected graph

d b c j f a g i h e

1

Current vertex: u = a Color u red: a is discovered Time stamp u: T(a) = time = 1 Neighbors of u: b, d, e (we’ll use alphabetical order) 1st undiscovered neighbor: v = b Draw red edge {u, v}: {a, b} Continue exploring: DFS(b)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 22 / 56

slide-23
SLIDE 23

Depth first search (DFS) in a connected graph

d b c j f a g i h e

2 1

Current vertex: u = b Color u red: b is discovered Time stamp u: T(b) = time = 2 Neighbors of u: a, c 1st undiscovered neighbor: v = c Draw red edge {u, v}: {b, c} Continue exploring: DFS(c)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 23 / 56

slide-24
SLIDE 24

Depth first search (DFS) in a connected graph

d b c j f a g i h e

3 2 1

Current vertex: u = c Color u red: c is discovered Time stamp u: T(c) = time = 3 Neighbors of u: b, f, h 1st undiscovered neighbor: v = f Draw red edge {u, v}: {c, f} Continue exploring: DFS(f)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 24 / 56

slide-25
SLIDE 25

Depth first search (DFS) in a connected graph

Skipping ahead a few steps . . .

d b c j f a g i h e

6 4 5 3 2 1

Current vertex: u = g Color u red: g is discovered Time stamp u: T(g) = time = 6 Neighbors of u: d, e, j 1st undiscovered neighbor: v = d Draw red edge {u, v}: {g, d} Continue exploring: DFS(d)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 25 / 56

slide-26
SLIDE 26

Depth first search (DFS) in a connected graph

d b c j f a g i h e

6 7 4 5 3 2 1

Current vertex: u = d Color u red: d is discovered Time stamp u: T(d) = time = 7 Neighbors of u: a, f, g But all neighbors of u have already been discovered! Backtrack to find a vertex (g) with an undiscovered neighbor.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 26 / 56

slide-27
SLIDE 27

Depth first search (DFS) in a connected graph

d b c j f a g i h e

6 7 4 5 3 2 1

Current vertex: u = g Neighbors of u: d, e, j 1st undiscovered neighbor: v = e Draw red edge {u, v}: {g, e} Continue exploring: DFS(e)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 27 / 56

slide-28
SLIDE 28

Depth first search (DFS) in a connected graph

d b c j f a g i h e

1 7 4 5 8 3 2 6

Current vertex: u = e Color u red: e is discovered Time stamp u: T(e) = time = 8 Neighbors of u: a, g, h 1st undiscovered neighbor: v = h Draw red edge {u, v}: {e, h} Continue exploring: DFS(h)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 28 / 56

slide-29
SLIDE 29

Depth first search (DFS) in a connected graph

d b c j f a g i h e

1 6 7 4 5 8 3 2 9

Current vertex: u = h Color u red: h is discovered Time stamp u: T(h) = time = 9 Neighbors of u: c, e, i 1st undiscovered neighbor: v = i Draw red edge {u, v}: {h, i} Continue exploring: DFS(i)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 29 / 56

slide-30
SLIDE 30

Depth first search (DFS) in a connected graph

d b c j f a g i h e

10 6 7 4 5 8 3 2 1 9

Current vertex: u = i Color u red: i is discovered Time stamp u: T(i) = time = 10 Neighbors of u: h, j All neighbors are already discovered. Backtracking doesn’t give any new branch, so we’re done.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 30 / 56

slide-31
SLIDE 31

Depth first search pseudocode, using recursion

Initialize: discovered[· · · ] ← false parent[· · · ] ← null T[· · · ] ← ∞ ⊲ Time stamp time ← 0 Start with: DFS(root) procedure DFS(u)

1: discovered[u] ← true

⊲ We did this by coloring u red

2: time ← time +1

⊲ Visit u by doing something with it,

3: T[u] ← time

⊲ such as recording a time stamp.

4: for all v ∈ N(u) do 5:

if (not discovered[v]) then

6:

parent[v] ← u ⊲ Add tree edge

7:

DFS(v) ⊲ Recursively explore further

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 31 / 56

slide-32
SLIDE 32

Extending traversals to disconnected graphs

a g i h e d b c j f l k m n

4 8 3 2 1 12 13 11 14 10 9 6 7 5

DFS only finds vertices reachable from the source. To find a spanning forest for a disconnected graph, loop over all

  • vertices. If a vertex isn’t marked as discovered, do DFS starting

there to get a spanning tree for that component. This also lets you count the connected components. This also applies to breadth first search (BFS), coming up next. DFS and BFS also work for a directed graph (explore v ∈ N+(u)). Similar issues arise if it’s not strongly connected.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 32 / 56

slide-33
SLIDE 33

Breadth first search (BFS) in a connected graph

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 33 / 56

slide-34
SLIDE 34

Queues

Consider customers waiting in a line, called a queue. Queue: B, C, E, A, D, H The first customer is B. We dequeue B and process their order. Queue: C, E, A, D, H While processing B, customer J comes along and is added to the end of the queue (called enqueuing); no cuts allowed! Queue: C, E, A, D, H, J In Computer Science, a queue is a data structure that works in the same way.

New items are added to the end of the queue (enqueued). Items at the front of the line are dequeued and processed. We don’t have the complete sequence when we start. The queue grows and shrinks over time as items are enqueued and dequeued.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 34 / 56

slide-35
SLIDE 35

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

10 2 3 4 5 6 7 8 9 1

Q: a u: Pick a starting vertex. We’ll use a. Add a to the end of the queue, Q, and mark a as discovered.

The undiscovered nodes are white. The discovered nodes are pink. By hand, just make a small mark like a dot.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 35 / 56

slide-36
SLIDE 36

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1

Q: x a u: a Dequeue a vertex: u = a. Time stamp u: T(a) = time = 1

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 36 / 56

slide-37
SLIDE 37

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1

Q: x a, b, d, e u: a Add any undiscovered neighbors of u to the end of the queue: b, d, e. This is called enqueueing. Mark those neighbors as discovered (color them pink). Add an edge from u to each of those neighbors to the tree (color the edges red).

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 37 / 56

slide-38
SLIDE 38

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1

Not yet discovered Discovered Finished

1

Mark a as finished (we’ll color it red). By hand: Don’t literally color vertices. Just make a mark (like a dot) to show discovery, and write the number to show it’s finished. In software:

There’s a variable discovered or visited for each vertex. Various algorithms using DFS or BFS use 0, 2, or 3 of these states.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 38 / 56

slide-39
SLIDE 39

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1 2

Q: xxx a, b, d, e u: b Dequeue: u = b Time stamp u: T(b) = time = 2 Undiscovered neighbors: c

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 39 / 56

slide-40
SLIDE 40

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1 2

Q: xxx a, b, d, e, c u: b Dequeue: u = b Time stamp u: T(b) = time = 2 Undiscovered neighbors: c Append them to queue, mark them discovered (pink), and add tree edges to them (red)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 40 / 56

slide-41
SLIDE 41

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1 2

Q: xxx a, b, d, e, c u: b Dequeue: u = b Time stamp u: T(b) = time = 2 Undiscovered neighbors: c Append them to queue, mark them discovered (pink), and add tree edges to them (red) Mark u finished (red)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 41 / 56

slide-42
SLIDE 42

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1 2 3

Q: xxxxx a, b, d, e, c u: d Dequeue: u = d Time stamp u: T(d) = time = 3 Undiscovered neighbors: f, g

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 42 / 56

slide-43
SLIDE 43

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1 2 3

Q: xxxxx a, b, d, e, c, f, g u: d Dequeue: u = d Time stamp u: T(d) = time = 3 Undiscovered neighbors: f, g Append them to queue, mark them discovered (pink), and add tree edges to them (red)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 43 / 56

slide-44
SLIDE 44

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

1 2 3

Q: xxxxx a, b, d, e, c, f, g u: d Dequeue: u = d Time stamp u: T(d) = time = 3 Undiscovered neighbors: f, g Append them to queue, mark them discovered (pink), and add tree edges to them (red) Mark u finished (red)

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 44 / 56

slide-45
SLIDE 45

Breadth first search (BFS) in a connected graph

d b c j f a g i h e

10 2 3 4 5 6 7 8 9 1

Q: xxxxxxxxxxxxxxxxx a, b, d, e, c, f, g, h, j, i u: Continue until forced to stop (no element to dequeue). All vertices reachable from a are included in the tree. If it’s a connected graph, then it’s a spanning tree reaching all vertices.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 45 / 56

slide-46
SLIDE 46

Distance (in edges) from a vertex to all other vertices

d b c j f a g i h e

3 1 1 1 2 2 2 2 3

Q: a b d e c f g h j i Depth: 0 1 1 1 2 2 2 2 3 3 The BFS queue is in weakly increasing order of distance from a. Instead of marking time stamps, we could have marked the distance from a (= parent’s distance + 1 = depth in tree). Processing vertices in order by layer assures we get shortest paths from a, although there may be ties for the shortest path to each vertex, e.g., a, d, g and a, e, g.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 46 / 56

slide-47
SLIDE 47

Distance (in edges) from a vertex to all other vertices

d b c j f a g i h e

3 1 1 1 2 2 2 2 3

Let Ni(v) be the set of all vertices of G a distance i from v: N0(a) = {a} N2(a) = {c, f, g, h} N1(a) = {b, d, e} N3(a) = {i, j} We used breadth first search to compute that for v = a.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 47 / 56

slide-48
SLIDE 48

DFS vs. BFS

DFS from a BFS from a

d b c j f a g i h e

10 6 7 4 5 8 3 2 1 9

d b c j f a g i h e

10 2 3 4 5 6 7 8 9 1

depth = 8 depth = 3 DFS tends to give longer paths, and to branch out less. BFS tends to give shorter paths, and to branch out more.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 48 / 56

slide-49
SLIDE 49

Maze

Start End

A maze can be represented by a graph. Vertex for each cell and for Start and End. Edge between adjacent cells w/o wall in-between. Pick an ordering of neighbors of the cells:

Could go up, left, down, or right one cell. Or, best first: Use a heuristic to guess best neighbor (may not be right). Order neighbors by distance to End, using Manhattan distance d((x, y), (x′, y′)) = |x − x′| + |y − y′| Break ties with a rule like down, left, right, up.

Use DFS or BFS. But instead of exploring all cells, you can stop when you reach the goal. DFS may be better since you need to go deep rather than to find the shortest solution.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 49 / 56

slide-50
SLIDE 50

Diameter and radius of an undirected graph

(0,2) A F E B C D G (1,0) (2,0) (0,0) (0,1)

diameter 3 diameter 4 radius 2 radius 2 The eccentricity of a vertex is the largest distance from the vertex.

Eccentricity of A is 2. Eccentricity of F is 3.

The BFS algorithm we just used gives all the distances from a vertex to the other vertices, so it can be used to compute this.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 50 / 56

slide-51
SLIDE 51

Diameter and radius of an undirected graph

(0,2) A F E B C D G (1,0) (2,0) (0,0) (0,1)

diameter 3 diameter 4 radius 2 radius 2 The diameter of a graph is maximum eccentricity: it’s the largest distance between two vertices. The radius of a graph is the minimum eccentricity. Left graph: diameter 3 (from C to F); radius 2 (using G) Right graph: diameter 4 (from (0,0) to (2,2)); radius 2 (using (1,1)).

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 51 / 56

slide-52
SLIDE 52

Is a connected graph bipartite?

First solution

We’ll label all vertices as being in part A or part B. Pick a starting vertex and label it A. Use DFS, BFS, or any other traversal that adds on one edge at a time to form a spanning tree. As you add new vertices, label them A/B opposite of their parent. As you explore neighbors of u, if any neighbor v was already discovered and has the same label A/B as u, then it’s not bipartite. If it is bipartite, then A and B are its two parts.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 52 / 56

slide-53
SLIDE 53

Is a connected graph bipartite?

Second solution

Pick any starting vertex, u. Do BFS starting at u, and set A = N0(u) ∪ N2(u) ∪ N4(u) ∪ · · · (even distance) B = N1(u) ∪ N3(u) ∪ N5(u) ∪ · · · (odd distance) In BFS, all edges either connect vertices at two consecutive layers, or two vertices in the same layer. E.g., if d(u, v) = 8 and {v, w} is an edge, then d(u, w) ∈ {7, 8, 9}. If there is any edge between two vertices of A (or two vertices of B), then there is an odd-length cycle, so it’s not bipartite. Otherwise, the graph is bipartite and A and B are its two parts.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 53 / 56

slide-54
SLIDE 54

Is a connected graph bipartite?

Second solution

d b c j f a g i h e

3 1 1 1 2 2 2 2 3

Vertices are marked with their distance from a, found from BFS. There are edges between vertices at the same level (such as {c, f} in N2(a), as well as others), so it’s not bipartite.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 54 / 56

slide-55
SLIDE 55

Is a disconnected graph bipartite?

Apply either procedure starting from any vertex in each component. If any component isn’t bipartite, then the graph isn’t bipartite. Otherwise, A and B gives a bipartition of the graph. Inverting A/B in any component gives other solutions.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 55 / 56

slide-56
SLIDE 56

Does a graph have a cycle?

Undirected simple graph: In BFS or DFS, when checking the neighbors of a vertex, if any neighbor besides the parent was already discovered, then there’s a cycle. Multigraphs/pseudographs: Loops are cycles of length 1. Multiple edges give cycles of length 2. If there are no loops or multiple edges, use the test for a simple graph. Directed graph: DFS can be used to determine if there’s a directed cycle, but it’s more involved.

Use DFS with three vertex colors. Initialize all vertices to white (not discovered). When entering a vertex, color it pink (discovered). It stays pink while recursing to its out-neighbors. After returning from the recursion, color it red (finished). When checking out-neighbors of a vertex, if any is pink (discovered), it must be an ancestor in the tree, so there’s a cycle.

  • Prof. Tesler
  • Ch. 3.2–3.4: Spanning Tree Algorithms

Math 154 / Winter 2020 56 / 56