3.3 Spanning Trees

3.3 Spanning Trees
Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter
3.3 Spanning Trees

A spanning tree of a graph G is a subgraph of G that is
a tree containing all vertices of G. Spanning trees can be
constructed either by depth first (backtrack) search or by
breadth-first search.
2
Adjacency Matrix:
An adjacency matrix of an (undirected) graph is a (0,1)-matrix
with a 1 in entry (i, j ) if the vertex xi , and vertex x j are adjacent;
entry (i, j ) is 0 otherwise. These are sometimes used to test if a
graph is connected.
x2
x1
x1 x2 x3 x4 x5 x6 x7 x8
x3
x2 1 0 1 0 1 0 1 0
x3 1 1 0 0 0 0 0 0
x8
x4
x7
x5
x6
Graph G
x1 0 1 1 1 0 1 0 0
x4
x5
x6
x7
x8
1 0 0
0
0
0 1
0
0 1 0
0
0
0 1
0
1 0 0
0
0
0 0
0
0 1 0
1
1
0 0
1
0 0 0
0
0
0 1
0
Adjacency Matrix of G
3




Building a Depth-First spanning tree:
To check if G is connected.
Pick some vertex as the root and begin building a path from the
root composed of edges of the graph.
The path continues until it cannot go any further without
repeating a vertex already on the path.
The vertex where this path must stop is a leaf.
We now backtrack to the parent of this leaf and try to build a
path from the parent in another direction, and so on, as in the
depth-first search method in a later slide.
x1
x1 , x2 , x3 , x5 , x7 , x4 , x8 , x6
x2
x3
x5
x6
x7
x4
x8
4
Building a Breadth-First Spanning Tree
To check if G is connected.
● Pick some vertex x as the root and put all the edges leaving x
(along with the vertices at the ends of these edges) in the tree.
● Then we successively add to the tree the edges leaving the vertices
adjacent from x, unless such an edge goes to a vertex already in the
tree.
● We continue this process as in the breadth-first search method
described on the next slide.
x1
x1 , x2 , x3 , x4 , x6 , x5 , x7 , x8
x3 x4
x2
x5
x6
x7
x8
5
Minimal Spanning Trees


G is a graph with weights on the edges.
A minimal spanning tree, a spanning tree whose sum of the edge
weights is as small as possible.
B
2
7
C
A
3
4
2
1
10
5
F
9
D
6
E
6
3.3 Spanning Tree

Kruskal’s Algorithm
Add to T the shortest edge that does not form a circuit with edges
already in T.
Repeat this step until tree T has n-1 edges (initially T is empty). (n = # vertices)

Prim’s Algoritm
Add to T the shortest edge between a vertex in T and a vertex not in
T (initially pick any edge of shortest length).
Repeat this step until tree T had n-1 edges (n = # vertices)
7
3.3 Spanning Tree

a
Example 2 Kruskal’s Algorithm
1
5
g
3
h
7
d
4
1
l
m
n
q
10
3
s
v
3
3
8
u
7
1
r
w
3
2
4
5
o
2
7
4
9
j
p
5
5
5
2
3
2
8
i
3
e
2
2
4
k
3
6
8
c
7
4
2
b
f
Root at b, splits at l, s, m, and h.
Step 2: Add all edges of length 2
(in blue)
Step 3: Add almost all the edges of
length 3 (in green) except for (w,x)
because it would form a circuit.
x
2
6
t
Step 1: Include all edges of length 1
(in red)
y
Step 4: We would add all edges of
length 4 (purple) and either (m,n)
or (o,t) to obtain a minimal
spanning tree produced by Prim’s
algorithm.
8
3.3 Spanning Tree

a
Example 2 Prim’s Algorithm
1
5
g
3
h
7
d
4
1
l
m
n
q
10
3
s
v
3
3
8
u
7
1
r
w
Step 1: Pick an edge with shortest
length, in this example we’ll use (a,f ).
Step 2: Add the next edge of shortest
length (2): (a, b ).
Continuing adding the edge with the
next smallest length.
3
2
4
5
o
2
7
4
9
j
p
5
5
5
2
3
2
8
i
3
e
2
2
4
k
3
6
8
c
7
4
2
b
f
x
2
6
t
y
9
3.3 Spanning Tree

Theorem
Prim’s algorithm yields a minimal spanning tree.

Proof:
For simplicity, assume that the edges all have different lengths.
Let T’ be a minimal spanning tree chosen to have as many edges as
possible in common with the tree T* constructed by Prim’s algorithm.
10
3.3 Spanning Tree

Proof (continued)
If T* ≠ T’, let ek = (a,b) be the first edge chosen by Prim’s algorithm that is
not in T’. This means that the subtree Tk 1 , composed of the first k-1
edges composed by Prim’s algorithm, is part of the minimal tree T’. In
figure 1, edges of Tk 1 are in bold, ek ‘s edge is dashed, and the other
edges of T’ are drawn normally.
e*
ek
a
b
Since ek is not in the minimal spanning tree T’,
there is a path, call it P, in T’ that connects a to
b. At least one of the edges of P must not be
in Tk 1 , for otherwise P  ek would form a
circuit in the tree Tk formed by the first k
edges in Prim’s algorithm. Let e * be the first
edge along P (starting from a) that is not in Tk 1 .
Note that one end of vertex of e * is in Tk 1 .
11
3.3 Spanning Tree

Proof (continued)
If e * is shorter than ek , then on the kth iteration, Prim’s algorithm would have
incorporated e * not ek . If e * is greater than ek , we remove e * from T’ and
replace it with ek . The new T’ is still a spanning tree, but its length is
shorter—this contradicts the minimality of the original T’. ◊
e*
b
ek
a
12
3.3 Spanning Tree

Class Work
Exercise 1c, Pg. 114
For the following graph, construct the adjacency matrix and then find
a depth-first spanning tree.
b
5
a
3
c
2
2
h
5
3
1
2
1
3
g
d
4
4
e
6
8
f
13
3.3 Spanning Tree

Class Work
Exercise 1c, Pg. 114
The depth-first spanning tree is: a-b-c-d-e-f-g-h.
b
5
a
3
c
2
2
h
5
3
1
2
1
3
g
4
4
e
6
8
f
d
A B C D E
F G H
A
0
1
1
0
1
0
1
1
B
1
0
1
0
0
0
0
0
C
1
1
0
1
1
0
1
0
D
0
0
1
0
1
0
0
0
E
1
0
1
1
0
1
1
0
F
0
0
0
0
1
0
1
0
G
1
0
1
0
1
1
0
1
H
1
0
0
0
0
0
1
0
14
3.3 Spanning Tree

Class Work
Exercise 1c, Pg. 114
For the following graph, now find a minimal spanning tree, using
both Kruskal’s and Prim’s algorithms.
b
5
a
3
c
2
2
h
5
3
1
2
1
3
g
d
4
4
e
6
8
f
15
Kruskal’s Minimal Spanning Tree:
b
5
a
3
c
2
2
h
1
2
1
3
g
Prim’s Minimal Spanning Tree:
5
3
d
b
4
4
5
e
a
6
8
3
c
2
2
f
h
Now all the vertices are in the tree.
Weight = 19
5
3
1
2
1
3
g
4
4
e
6
8
Now all the vertices are included in this tree.
Weight = 19
d
f
16