Chapter 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Minimum Spanning Trees

Ali Balma
Tunis Business School
2020/2021
Minimum spanning tree
• A Minimum Spanning Tree (MST) is a subgraph of an
undirected graph such that the subgraph spans
(includes) all nodes, is connected, is acyclic, and has
minimum total edge weight
• A MST must be a connected graph
• Kruskal’s Algorithms works with undirected graphs
• Greedy algorithm that produce optimal solutions
• Property: for any pair of nodes, there is only one
path.
• http://www.youtube.com/watch?v=FPEMBWg_
WlY (from minute 24)
Algorithm
• Step 1. sort the edges in an increasing order in
a list L
• Step 2. place the edge on the top list. If it
makes up a cycle with the already placed
edges, then discard it. Otherwise, remove it
from L
• Step 3. If L is empty then stop, else go to step
2
Kruskal’s algorithm
T = empty spanning tree;
E = set of edges;
N = number of nodes in graph;
while T has fewer than N - 1 edges do
{
remove an edge (v, w) of lowest cost from E
if adding (v, w) to T would create a cycle
then discard (v, w)
else add (v, w) to T
}
Prim’s algorithm
• Prim’s algorithm finds a minimum cost spanning
tree by selecting edges from the graph one-by-
one as follows:
• It starts with a tree, T, consisting of a single
starting vertex, x.
• Then, it finds the shortest edge originating from x
that connects T to the rest of the graph (i.e., a
vertex not in the tree T).
• It adds this edge and the new vertex to the tree T.
• It then picks the shortest edge emanating from
the revised tree T that also connects T to the rest
of the graph and repeats the process.
• Complexity= O( |E| log|V| )
Prim’s algorithm
Consider a graph G=(V, E);
Let T be a tree consisting of only the starting
vertex x;
while (T has fewer than I V I vertices)
{
find a smallest edge connecting T to G-T;
add it to T;
}
Prim’s Algorithm

2 19
9
14 17 Start here
8 25
5

21 13 1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1?
1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1
Prim’s Algorithm

2 19
9
14 17
8 25
5

21 13 1
Prim’s and Kruskal’s Algorithms

• It is not necessary that Prim's and Kruskal's algorithm


generate the same minimum-cost spanning tree. For
example for the graph shown on the right:

• Kruskal's algorithm results in the following minimum


cost spanning tree:
– The same tree is generated by Prim's algorithm if
the start vertex is any of: A, B, or D.

• However if the start vertex is C the minimum cost


spanning tree generated by Prim’s algorithm is:

You might also like