DS Lecture 21 (Spanning Trees)
DS Lecture 21 (Spanning Trees)
Lecture # 21
Spanning Trees
Summiya Alam
SPANNING TREES
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
• 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
Kruskal’s algorithm
{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
B 5 C
3
6 4
8
8
A F D
7
5
4 2
E
KRUSKAL’S ALGORITHM
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
8
A F D
7
5
4 2
E
KRUSKAL’S ALGORITHM
E
KRUSKAL’S ALGORITHM
E
KRUSKAL’S ALGORITHM
E
KRUSKAL’S ALGORITHM
E
KRUSKAL’S ALGORITHM
E
Total weight of tree: 18
PRIM’S ALGORITHM
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
E
PRIM’S ALGORITHM
E
PRIM’S ALGORITHM
E
PRIM’S ALGORITHM
E
PRIM’S ALGORITHM
• Both algorithms will always give solutions with the same length.