You are on page 1of 32

Minimum Spanning Trees

Chapter 23

Mirza Mohammad Lutfe Elahi


CSE 373 Design and Analysis of Algorithms ECE@NSU
2

Minimum Spanning Trees


• Spanning Tree
– A tree (i.e., connected, acyclic graph) which contains all
the vertices of the graph
• Minimum Spanning Tree
– Spanning tree with the minimum sum of weights
• Spanning forest
– If a graph is not connected, then there is a spanning tree for
each connected component of the graph

CSE 373 Design and Analysis of Algorithms ECE@NSU


3

Minimum Spanning Trees


• A minimum spanning tree for a connected graph.

• Edges in a minimum spanning tree are shaded.


• Total weight - 37.
• This minimum spanning tree is not unique:
– removing the edge (b, c) and replacing it with the edge (a, h) yields
another spanning tree with weight 37.
3

CSE 373 Design and Analysis of Algorithms ECE@NSU


4

Application of MST
– Find the least expensive way to connect a set of
cities, terminals, computers, etc.

CSE 373 Design and Analysis of Algorithms ECE@NSU


5

Application of MST
Problem b
8
c
7
d
4 9
• A town has a set of houses 2
and a set of roads a 11 i 4 14 e
7 6
• A road connects 2 and only 8 10
2 houses h 1 g 2 f
• A road connecting houses u and v has a repair
cost w(u, v)

Goal: Repair enough (and no more) roads such


that:
1. Everyone stays connected
i.e., can reach every house from all other houses 5
2. Total repair cost is minimum
CSE 373 Design and Analysis of Algorithms ECE@NSU
6

Application of MST
• A connected, undirected graph:
– Vertices = houses, Edges = roads

• A weight w(u, v) on each edge (u, v) Î E

8 7
b c d
4 9
2
a 11 i 4 14 e
Find T Í E such that: 8
7 6
10

1. T connects all vertices h 1 g 2 f


6
2. w(T) = Σ(u, v)ÎT, w(u, v) is minimized
CSE 373 Design and Analysis of Algorithms ECE@NSU
7

Properties of MST
• Minimum spanning tree is not unique
2 2

2 1 2 1

• MST has no cycles – see why:


– We can take out an edge of a cycle, and still have the
vertices connected while reducing the cost

• No. of edges in a MST:


– |V| - 1
7

CSE 373 Design and Analysis of Algorithms ECE@NSU


8

Growing a MST
• Grow a set A of edges (initially empty)
• Incrementally add edges to A such that they would
belong to a MST

• Idea: add only “safe” edges


– An edge (u, v) is safe for A if and only if A È {(u, v)} is also a
subset of some MST

CSE 373 Design and Analysis of Algorithms ECE@NSU


9

Generic MST Algorithm

CSE 373 Design and Analysis of Algorithms ECE@NSU


10

Definitions

10

CSE 373 Design and Analysis of Algorithms ECE@NSU


11

Definitions

11

CSE 373 Design and Analysis of Algorithms ECE@NSU


12

Finding Safe Edges


• Let’s look at edge (h, g) b
8
c
7
d V-S
4 9
– Is it safe for A initially? 2
a 11 i 4 14 e
7 6
8 10
S h g f
1 2
• Later on:
– Let S Ì V be any set of vertices that includes h but not g
(so that g is in V - S)
– In any MST, there has to be one edge (at least) that
connects S with V - S
12
– Why not choose the edge with minimum weight (h, g)?
CSE 373 Design and Analysis of Algorithms ECE@NSU
13

Kruskal’s Algorithm
• Start with each vertex being its own component

• Repeatedly merge two components into one by


choosing the light edge that connects them

• Which components to consider at each iteration?


– Scan the set of edges in monotonically increasing order by
weight

13

CSE 373 Design and Analysis of Algorithms ECE@NSU


14

Implementation of Kruskal’s Algorithm


• Uses a disjoint-set data structure (see Chapter 21)
– to determine whether an edge connects vertices in different components
• MAKE-SET(u) – creates a new set whose only member is u
• FIND-SET(u) – returns a representative element from the set
that contains u
– Any of the elements of the set that has a particular property
– E.g.: Su = {r, s, t, u}, the property is that the element be the first one
alphabetically
FIND-SET(u) = r, FIND-SET(s) = r
– FIND-SET has to return the same value for a given set

14

CSE 373 Design and Analysis of Algorithms ECE@NSU


15

Implementation of Kruskal’s Algorithm


• UNION(u, v) – unites the dynamic sets that contain u
and v, say Su and Sv
– E.g.: Su = {r, s, t, u}, Sv = {v, x, y}

UNION (u, v) = {r, s, t, u, v, x, y}

• Running time for FIND-SET and UNION depends on


implementation. (see Chapter 21)

15

CSE 373 Design and Analysis of Algorithms ECE@NSU


16

Kruskal’s Algorithm

O(V)
O(ElgE)
O(E)

O(lgV)

Running time: O(V + ElgE + ElgV) = O(ElgE) – dependent on the


16
implementation of the disjoint-set data structure
CSE 373 Design and Analysis of Algorithms ECE@NSU
17

Kruskal’s Algorithm Example

17

CSE 373 Design and Analysis of Algorithms ECE@NSU


18

Kruskal’s Algorithm Example

18

CSE 373 Design and Analysis of Algorithms ECE@NSU


19

Kruskal’s Algorithm Example

19

CSE 373 Design and Analysis of Algorithms ECE@NSU


20

Kruskal’s Algorithm Example

20

CSE 373 Design and Analysis of Algorithms ECE@NSU


21

Prim’s Algorithm
• The edges in set A always form a single tree
• Starts from an arbitrary root vertex r
• At each step:
– Find a light edge that connects A to an isolated vertex
– Add this edge to A
– Repeat until the tree spans all vertices

21

CSE 373 Design and Analysis of Algorithms ECE@NSU


22

Prim’s Algorithm

O(V)

O(V)
O(lgV)
O(E)

O(lgV)

22
Overall complexity: O(V) + O(V lg V + E lg V) = O(E lg V)
CSE 373 Design and Analysis of Algorithms ECE@NSU
23

Prim’s Algorithm Example

V a b c d e f g h i
T 1 0 0 0 0 0 0 0 0
Key 0 - - - - - - - -
p -1 - - - - - - - -
23

CSE 373 Design and Analysis of Algorithms ECE@NSU


24

Prim’s Algorithm Example

V a b c d e f g h i
T 1 0 0 0 0 0 0 0 0
Key 0 4 - - - - - 8 -
p -1 a - - - - - a -
24

CSE 373 Design and Analysis of Algorithms ECE@NSU


25

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 0 0 0 0 0 0 0
Key 0 4 8 - - - - 8 -
p -1 a b - - - - a -
25

CSE 373 Design and Analysis of Algorithms ECE@NSU


26

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 0 0 0 0 0 0
Key 0 4 8 7 - 4 - 8 2
p -1 a b c - c - a c
26

CSE 373 Design and Analysis of Algorithms ECE@NSU


27

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 0 0 0 0 0 1
Key 0 4 8 7 - 4 6 7 2
p -1 a b c - c i i c
27

CSE 373 Design and Analysis of Algorithms ECE@NSU


28

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 0 0 1 0 0 1
Key 0 4 8 7 10 4 2 7 2
p -1 a b c f c f i c
28

CSE 373 Design and Analysis of Algorithms ECE@NSU


29

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 0 0 1 1 0 1
Key 0 4 8 7 10 4 2 1 2
p -1 a b c f c f g c
29

CSE 373 Design and Analysis of Algorithms ECE@NSU


30

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 0 0 1 1 1 1
Key 0 4 8 7 10 4 2 1 2
p -1 a b c f c f g c
30

CSE 373 Design and Analysis of Algorithms ECE@NSU


31

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 1 0 1 1 1 1
Key 0 4 8 7 9 4 2 1 2
p -1 a b c d c f g c
31

CSE 373 Design and Analysis of Algorithms ECE@NSU


32

Prim’s Algorithm Example

V a b c d e f g h i
T 1 1 1 1 1 1 1 1 1
Key 0 4 8 7 9 4 2 1 2
p -1 a b c d c f g c
32

CSE 373 Design and Analysis of Algorithms ECE@NSU

You might also like