0% found this document useful (0 votes)
29 views44 pages

DS Lecture 21 (Spanning Trees)

The document discusses spanning trees and minimum spanning trees in graphs. It defines spanning trees as subgraphs that connect all vertices without cycles. Minimum spanning trees have the minimum total weight among all spanning trees of a weighted graph. Prim's and Kruskal's algorithms are described for finding minimum spanning trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views44 pages

DS Lecture 21 (Spanning Trees)

The document discusses spanning trees and minimum spanning trees in graphs. It defines spanning trees as subgraphs that connect all vertices without cycles. Minimum spanning trees have the minimum total weight among all spanning trees of a weighted graph. Prim's and Kruskal's algorithms are described for finding minimum spanning trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

DISCRETE STRUCTURES

Lecture # 21
Spanning Trees

Summiya Alam
SPANNING TREES

• Spanning Trees: A subgraph T of a undirected graph G = (V , E) is a spanning


tree of G if it is a tree and contains every vertex of G.
WEIGHTED GRAPHS

Weighted Graphs: A weighted graph is a graph, in which each edge has a weight
(some real number).
Weight of a Graph: The sum of the weights of all edges.
WEIGHTED GRAPHS
MINIMUM SPANNING TREES

• A Minimum Spanning Tree in an undirected connected weighted graph is a


spanning tree of minimum weight (among all spanning trees).
MINIMUM SPANNING TREES
SPANNING TREES
SPANNING TREES
SPANNING TREES
SPANNING TREES
SPANNING TREES
SPANNING TREES
SPANNING TREES
MINIMUM SPANNING TREE
MINIMUM SPANNING TREES

• Remark: The minimum spanning tree may not be unique. However, if the
weights of all the edges are pairwise distinct, it is indeed unique
MINIMUM CONNECTOR ALGORITHMS

Prim’s algorithm

1. Select any vertex

2. Select the shortest edge


connected to that vertex

3. Select the shortest edge


connected to any vertex already
connected

4. Repeat step 3 until all vertices


have been connected
PRIM’S ALGORITHM
PRIM’S ALGORITHM
PRIM’S ALGORITHM
PRIM’S ALGORITHM
PRIM’S ALGORITHM
PRIM’S ALGORITHM
PRIM’S ALGORITHM
PRIM’S ALGORITHM
MINIMUM CONNECTOR
ALGORITHMS

Kruskal’s algorithm

1. Select the shortest edge in a


network

2. Select the next shortest edge


which does not create a cycle

3. Repeat step 2 until all vertices


have been connected
KRUSKAL’S ALGORITHM: TREE
CONSTRUCTION

{A,B} 1 E
E 6
7 {D,F} 2 6
5
A D {B,C} 4 A 5
D
1 8 5
2 {A,D} 5 1 2
B C {C,D} 5
4 F B 4 C F
{E,D} 6
{A,E} 7
{B,D} 8
EXAMPLE 2

• A cable company want to connect five villages to their network which currently
extends to the market town of Avonford. What is the minimum length of cable
needed?
EXAMPLE 2

Brinlei 5 Cornwel
gh l
3
6 4
8
8
Avonfor Fingle Donster
d 7 y
5
4 2

Edan
EXAMPLE 2

• We model the situation as a network, then the problem is to


find the minimum connector for the network

B 5 C
3
6 4
8
8
A F D
7
5
4 2
E
KRUSKAL’S ALGORITHM

B 5 C List the edges in order of size:

3 ED 2
6 4
8 AB 3
AE 4
8 CD 4
A F D BC 5
7
EF 5
5 CF 6
4 2 AF 7
BF 8
CF 8
E
KRUSKAL’S ALGORITHM

B 5 C Select the shortest


edge in the network
3
6 4
8 ED 2

8
A F D
7
5
4 2

E
KRUSKAL’S ALGORITHM

B 5 C Select the next shortest


edge which does not
3 create a cycle
6 4
8
ED 2
8 AB 3
A F D
7
5
4 2

E
KRUSKAL’S ALGORITHM

B 5 C Select the next shortest


edge which does not
3 create a cycle
6 4
8
ED 2
8 AB 3
A F D
7 CD 4 (or AE 4)
5
4 2

E
KRUSKAL’S ALGORITHM

B 5 C Select the next shortest


edge which does not
3 create a cycle
6 4
8
ED 2
8 AB 3
A F D
7 CD 4
AE 4
5
4 2

E
KRUSKAL’S ALGORITHM

B 5 C Select the next shortest


edge which does not
3 create a cycle
6 4
8
ED 2
8 AB 3
A F D
7 CD 4
AE 4
5 BC 5 – forms a cycle
4 2 EF 5

E
KRUSKAL’S ALGORITHM

B 5 C All vertices have been


connected.
3
6 4
8 The solution is
8 ED 2
A F D
7 AB 3
CD 4
5 AE 4
4 2 EF 5

E
Total weight of tree: 18
PRIM’S ALGORITHM

B 5 C Select any vertex

3 A
6 4
8
Select the shortest edge
8 connected to that vertex
A F D
7
AB 3
5
4 2

E
PRIM’S ALGORITHM

B 5 C Select the shortest


edge connected to
3 any vertex already
6 4
8 connected.
8 AE 4
A F D
7
5
4 2

E
PRIM’S ALGORITHM

B 5 C Select the shortest


edge connected to
3 any vertex already
6 4
8 connected.
8 ED 2
A F D
7
5
4 2

E
PRIM’S ALGORITHM

B 5 C Select the shortest


edge connected to
3 any vertex already
6 4
8 connected.
8 DC 4
A F D
7
5
4 2

E
PRIM’S ALGORITHM

B 5 C Select the shortest


edge connected to
3 any vertex already
6 4
8 connected.
8 EF 5
A F D
7
5
4 2

E
PRIM’S ALGORITHM

B 5 C All vertices have been


connected.
3
6 4
8 The solution is
8 AB 3
A F D
7 AE 4
ED 2
5 DC 4
4 2 EF 5

E Total weight of tree: 18


SOME POINTS TO NOTE

• Both algorithms will always give solutions with the same length.

• They will usually select edges in a different order – you must


show this in your workings.

• Occasionally they will use different edges – this may happen


when you have to choose between edges with the same length.
In this case there is more than one minimum connector for the
network.
THANK YOU

You might also like