Minimum Spanning Tree. Minimum-Spanning-Tree Problem

You might also like

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

Minimum Spanning Tree

• Let G=(V,E) be a connected, undirected graph.


• For each edge (u,v) in E, we have a weight w(u,v) specifying
the cost (length of edge) to connect u and v.
• We wish to find a (acyclic) subset T of E that connects all of
the vertices in V and whose total weight is minimized.
• Thus, T is a tree. We call it a minimum spanning tree.
• The problem of determining the tree T is called the
minimum-spanning-tree problem.

1
Application of MST: an example
• In the design of electronic circuitry, it is often
necessary to make a set of pins electrically
equivalent by wiring them together.
• To interconnect n pins, we can use n-1 wires, each
connecting two pins.
• We want to minimize the total length of the wires.
• Minimum Spanning Trees can be used to model
this problem.

2
Here is an example of a connected graph
and its minimum spanning tree:

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2

Notice that the tree is not unique:


replacing (b,c) with (a,h) yields another spanning tree
with the same minimum weight.
3
Greedy Method
A greedy method is used to obtain the MST.
The next edge to include is taken according to
some optimization criteria that is choose an
edge that results in a minimum increase in the
sum of the cost of the edges so far included.
There are two well known algorithms to
interpret this idea.
1. Prim’s Algorithm
2. Kruskal’s Algorithm
4
Prim’s Algorithm
• The edges in set A always form a single tree
• Starts from an arbitrary “root”: VA = {a}
• At each step: b
8
c
7
d
4 9
– Find a light edge crossing (VA, V - VA) 2
a 11 i 4 14 e
– Add this edge to A 7 6
8 10
– Repeat until the tree spans all vertices h g f
1 2
• An edge crosses the cut
(S, V - S) if one endpoint is in S
and the other in V – S

5
Prim's algorithm
MST_PRIM(G,w,r)
1for each u in Q do
2 key[u]:=∞
3 parent[u]:=NIL
4 key[r]:=0
5QV[G]
6while Q!={} do
7 u:=EXTRACT_MIN(Q);
8 for each v in Adj[u] do
9 if v in Q and w(u,v)<key[v]
10 then parent[v]:=u
11 key[v]:=w(u,v)

Time complexity: O(ElgV)

6
Example
  
b
8
c
7
d 0  
4 9
 2  Q = {a, b, c, d, e, f, g, h, i}
a 11 i 4 14 e VA = 
7 6
8 10 Extract-MIN(Q)  a
h 1 g 2 f
  
4
8  7
 key [b] = 4  [b] = a
b c d
4 9 key [h] = 8  [h] = a
 2 
a 11 i 4 14 e
8
7 6
10 4 8
h 1 g 2 f Q = {b, c, d, e, f, g, h, i} VA = {a}
8  
Extract-MIN(Q)  b
7
Example
4 
8  key [c] = 8  [c] = b
8 7
b c d
9
key [h] = 8  [h] = a - unchanged
4
 2  8 8
a 11 i 4 14 e
7 6 Q = {c, d, e, f, g, h, i} VA = {a, b}
8 10
h g f Extract-MIN(Q)  c
1 2
8   key [d] = 7  [d] = c
4 8 7
8 7 key [f] = 4  [f] = c
b c d
4 9 key [i] = 2  [i] = c

2 2 
a 11 i 4 14 e
8
7 6
10 7 4 8 2
h 1 g 2 f Q = {d, e, f, g, h, i} VA = {a, b, c}
8  
4 8 Extract-MIN(Q)  i
Example
4 8 7 key [h] = 7  [h] = i
key [g] = 6  [g] = i
8 7
b c d
4 9
2 2  7 46 8
a 11 i 4 14 e
6
Q = {d, e, f, g, h} VA = {a, b, c, i}
7
Extract-MIN(Q)  f
8 10
h 1 g 2 f
87 
6 4
4 8 7 key [g] = 2  [g] = f
b
8
c
7
d key [d] = 7  [d] = c unchanged
key [e] = 10  [e] = f
4 9
2 2 
10
a 11 i 14 e
4 7 10 2 8
7 6
8 10 Q = {d, e, g, h} VA = {a, b, c, i, f}
h g f
1 2
Extract-MIN(Q)  g
7 26 4 9
Example
4 8 7 key [h] = 1  [h] = g
8 7
b c d 7 10 1
4 9
2 2 10 Q = {d, e, h} VA = {a, b, c, i, f, g}
a 11 i 14 e
7 6
4
Extract-MIN(Q)  h
8 10
h 1 g 2 f
71 2 4 7 10
4 8 7 Q = {d, e} VA = {a, b, c, i, f, g, h}
8 7
b c d Extract-MIN(Q)  d
4 9
2 2 10
a 11 i 4 14 e
7 6
8 10
h 1 g 2 f
1 2 4 10
Example

4 8 7
8 7
b c d
9
key [e] = 9  [e] = f
4
2 2 9
10 9
a 11 i 4 14 e
7 6 Q = {e} VA = {a, b, c, i, f, g, h, d}
8 10
h g f Extract-MIN(Q)  e
1 2
1 2 4 Q =  VA = {a, b, c, i, f, g, h, d, e}

11
Kruskal’s Algorithm
• Select edges in order of increasing cost.
• Accept an edge to expand tree or forest only
if it does not cause a cycle.

12
Kruskal's algorithm
MST_KRUSKAL(G,w)
1 A:={}
2 for each vertex v in V[G]
3 do MAKE_SET(v)
4 sort the edges of E by nondecreasing weight w
5 for each edge (u,v) in E, in order by
nondecreasing weight
6 do if FIND_SET(u) != FIND_SET(v)
7 then A:=A∪{(u,v)}
8 UNION(u,v)
9 return A
Time complexity: O(ElgV) 13
Disjoint-Set
• Keep a collection of sets S1, S2, .., Sk,
– Each Si is a set, e,g, S1={v1, v2, v8}.
• Three operations
– Make-Set(x)-creates a new set whose only member is x.
– Union(x, y) –unites the sets that contain x and y, say, Sx
and Sy, into a new set that is the union of the two sets.
– Find-Set(x)-returns a pointer to the representative of the
set containing x.
– Each operation takes O(log n) time.

14
The execution of Kruskal's algorithm

•The edges are considered by the algorithm in sorted


order by weight.
•The edge under consideration at each step is shown
with a red weight number.

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
15
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
16
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
17
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
18
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
19
8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2

8 7
b c d
4 9
2
a 11 i 4 14 e
7 6
8 10
h g f
1 2
20

You might also like