0% found this document useful (0 votes)
57 views19 pages

DS Lecture17

The document discusses spanning trees in graphs. A spanning tree is a subgraph of a graph that contains every vertex of the graph and is a tree. The document provides examples of finding spanning trees in graphs and discusses Kirchhoff's theorem for calculating the number of spanning trees in a graph.

Uploaded by

Usama Mushtaq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views19 pages

DS Lecture17

The document discusses spanning trees in graphs. A spanning tree is a subgraph of a graph that contains every vertex of the graph and is a tree. The document provides examples of finding spanning trees in graphs and discusses Kirchhoff's theorem for calculating the number of spanning trees in a graph.

Uploaded by

Usama Mushtaq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

SPANNING TREE

Suppose it is required to develop a system of roads between six major cities.


A survey of the area revealed that only the roads shown in the graph could
be constructed.

b c

a d
e

f
For economic reasons, it is desired to construct the least possible number of
roads to connect the six cities.
One such set of roads is

b c

a
e d

Note that the subgraph representing these roads is a tree, it is connected &
circuit-free (six vertices and five edges) 1
SPANNING TREE

A spanning tree for a graph G is a subgraph of G that contains every vertex


of G and is a tree.
REMARK
1. Every connected graph has a spanning tree.
2. A graph may have more than one spanning trees.
3. Any two spanning trees for a graph have the same number of
edges.
4. If a graph is a tree, then its only spanning tree is itself.
EXERCISE

Find a spanning tree for the graph below:

b c

a d
e

f
SOLUTION
The graph has 6 vertices & 9 edges so we must delete 9 - 6 + 1 = 4 edges.
We delete an edge in each cycle. b c

1. Delete af 2. Delete fea


3. Delete be 4. Delete ed e d
The associated spanning tree is
f
2
EXERCISE

Find all the spanning trees of the graph given below.

v0 v1

v3 v2
SOLUTION
The graph has n = 4 vertices and e = 5 edges. So we must delete
e-v+1=5-4+1=2
edges from the cycles in the graph to obtain a spanning tree.

(1) Delete v0v1 & v1v2 to get

v0 v1

v3 v2
(2) Delete v0v1 & v1v3 to get

v0 v1

3
v3 v2
Original Graph
v0 v1

v3 v2
(3) Delete v0v1 & v2v3 to get

v0 v1

v3 v2

(4) Delete v0v3 & v1v2 to get

v0 v1

v3 v2

4
Original Graph

v0 v1

v3 v2

(5) Delete v0v3 & v1v3 to get

v0 v1

v3 v2

(6) Delete v0v3 & v2v3 to get

v0 v1

v3 v2

5
Original Graph

v0 v1

v3 v2

(7) Delete v1v3 & v1v2 to get

v0 v1

v3 v2

(8) Delete v1v3 & v2v3 to get

v0 v1

v3 v2

6
EXERCISE

Find a spanning tree for each of the following graphs.


(a) k1,5 (b) k4
SOLUTION
(a) k1,5 represents a complete bipartite graph on (1,5) vertices,
drawn below:

Clearly the graph itself is a tree (six vertices and five edges). Hence the
graph is itself a spanning tree.

(b) k4 represents a complete graph on four vertices.


a b

c d

Now
Number of vertices = n = 4 and number of edges = e = 6
Hence we must remove
e-v+1=6-4+1=3
edges to obtain a spanning tree.
7
Let ab, bd & cd edges are removed. The associated spanning tree is

a b

c d

KIRCHHOFF’S THEOREM
OR MATRIX - TREE THEOREM

Let M be the matrix obtained from the adjacency matrix of a connected


graph G by changing all 1’s to -1’s and replacing each diagonal 0 by the
degree of the corresponding vertex. Then the number of spanning trees of G
is equal to the value of any cofactor of M.

EXAMPLE

Find the number of spanning trees of the graph G.

a b c

8
a b c

d
SOLUTION
We will use the Kirchoff`s Theorem for which first
we will calculate the adjacency matrix of G is
a b c d
a 0 1 0 1
b 1 0 1 1
A(G ) 
c 0 1 0 1
 
d 1 1 1 0

The matrix specified in Kirchhoff’s theorem is


2  1 0  1
 1 3  1  1
M 
0  1 2  1
 
 1  1  1 3
Note the matrix is obtained by changing “1” to “-1”in the adjacency matrix
and replacing zero of diagonals to the corresponding degree of the vertex.
For example first diagonal is correspond to the vertex v 1 and v1 has v1 has
degree 2 so we replace 0 by 2. Similarly you can see that the second zero is
corresponding to “b” and “b” has degree 3 in the graph so we replace this
zero by three. Similarly deal with all zeros in the diagonal then we will get
the above matrix.

9
Now cofactor of the element at (1,1) in M is

3 1 1
1 2 1
1 1 3

Expanding by first row, we get

2 1 1 1 1 2
3  (1)  (1)
1 3 1 3 1 1
 3(6  1)  (3  1)  (1)(1  2)
 15  4  3
8

Hence there will be 8 Spanning trees for the given graph. You can also find
out the number of spanning trees of the example for which we find the
spanning trees and you will see that these are also 8 as we counted them.

EXERCISE

How many non-isomorphic spanning trees does the following simple graph
has?

10
SOLUTION There are eight spanning tree of the graph

1 2 3 4

5 6 7 8

Clearly 1 & 6 are isomorphic, and 2,3,4,5,7,8 are isomorphic. Hence there
are only two non-isomorphic spanning trees of the given graph.
EXERCISE

Suppose an oil company wants to build a series of pipelines between six


storage facilities in order to be able to move oil from one storage facility to
any of the other five. For environmental reasons it is not possible to build a
pipeline between some pairs of storage facilities. The possible pipelines that
can be build are.
a b c

11
f e d
Because the construction of a pipeline is very expensive, construct as few
pipelines as possible.
(The company does not mind if oil has to be routed through one or more
intermediate facilities)

SOLUTION

The task is to find a set of edges which together with the incident vertices
from a connected graph containing all the vertices and having no cycles.
This will allow oil to go from any storage facility to any other without
unnecessary building costs. Thus, a tree containing all the vertices of the
graph is to be soughed. One selection of edges is

a b c

f e d

DEFINITION

A weighted graph is a graph for which each edge has an associated real
number weight.
The sum of the weights of all the edges is the total weight of the graph.

12
EXAMPLE
The figure shows a weighted graph with total weight
2 + 6 + 3 + 2 + 3 + 1 = 17

2 6
2 1

MINIMAL SPANNING TREE

A minimal spanning tree for a weighted graph is a spanning tree that has the
least possible total weight compared to all other spanning trees of the graph.
If G is a weighted graph and e is an edge of G then w(e) denotes the weight
of e and w(G) denotes the total weight of G.

EXERCISE

Find the three spanning trees of the weighted graph below. Also indicate the
minimal spanning tree.

2 6
2 1

13
3
SOLUTION

First Spanning tree we obtained by removing edge of weight 3.


3
T1

6
2 w(T1)=14
2 1

Second Spanning tree we obtained by removing edge of weight 6.


3
T2

2 2 1
w(T2)=11

Third Spanning tree we obtained by removing edge of weight 2.

3
T3

2 6 1 w(T3)=15

T2 is the minimal spanning tree, since it has the minimum weight among the
spanning trees.
14
KRUSKAL’S ALGORITHM

Input: G [a weighted graph with n vertices]


Algorithm:
1. Initialize T(the minimal spanning tree of G) to have all the
vertices of G and no edges.
2. Let E be the set of all edges of G and let m: = 0.
3. While (m < n - 1)
3a. Find an edge e in E of least weight.
3b. Delete e from E.
3c. If addition of e to the edge set of T does not produce
a circuit then add e to the edge set of T and set m: =
m+1
end while
Output T
end Algorithm PRIM’S ALGORITHM

Input: G[a weighted graph with n vertices]


Algorithm Body:
1. Pick a vertex v of G and let T be the graph with one vertex, v,
and no edges.
2. Let V be the set of all vertices of G except v
3. for i: = 1 to n - 1
3a. Find an edge e of G such that
(1) e connects T to one of the vertices in V
and (2) e has the least weight of all edges
connecting T to a vertex in V.
Let w be the end point of e that is in V.
3b. Add e and w to the edge and vertex sets of T and
delete w from V.
next i
Output: T 15
end Algorithm
EXERCISE

Use Kruskal’s algorithm to find a minimal spanning tree for the graph
below. Indicate the order in which edges are added to form the tree.
b 7 c
1 4
8 e
a 10 d
9 3
2
5
g 6 f
SOLUTION
Minimal spanning tree:

b c
7
1 4
e 3
a d
2

g 6 f

Order of adding the edges:


{a,b}, {e,f}, {e,d}, {c,d}, {g,f}, {b,c}
16
EXERCISE

Use Prim’s algorithm starting with vertex a to find a minimal spanning


tree of the graph below. Indicate the order in which edges are added to form
the tree.

b 7 c

1 4
8 e
10 d
a
3
9
2
5

g 6 f

SOLUTION:
Minimal spanning tree
b c
7
1 4
e 3
a d
2

g 6 f

Order of adding the edges:


{a,b}, {b.c}, {c,d}, {d,e}, {e,f}, {f,g}
17
EXERCISE

Find all minimal spanning trees that can be obtained using


(a) Kruskal’s algorithm
(b) Prim’s algorithm starting with vertex a

a 9 b

6
8 c 7
3 5

d 5 e

SOLUTION
(a) When Kruskal’s algorithm is applied, edges are added in one of
the following two orders:

1. {c,d}, {c,e}, {c,b}, {d,a}


2. {c,d}, {d,e}, {c,b}, {d,a}
Thus, there are two distinct minimal spanning trees:
b a b
a

c c

18
d e d e
a 9 b

6
8 c 7
3 5

d 5 e

(b) When Prim’s algorithm is applied starting at a, edges are


added in one of the following two orders:
1. {a,d}, {d,c}, {c,e}, {c,b}
2. {a,d}, {d,c}, {d,e}, {c,b}

Thus, the two distinct minimal spanning trees are:

b a b
a

c c

d e d e

19

You might also like