ch23 MST
ch23 MST
Chapter 23
Application of MST
– Find the least expensive way to connect a set of
cities, terminals, computers, etc.
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)
Application of MST
• A connected, undirected graph:
– Vertices = houses, Edges = roads
8 7
b c d
4 9
2
a 11 i 4 14 e
Find T Í E such that: 8
7 6
10
Properties of MST
• Minimum spanning tree is not unique
2 2
2 1 2 1
Growing a MST
• Grow a set A of edges (initially empty)
• Incrementally add edges to A such that they would
belong to a MST
Definitions
10
Definitions
11
Kruskal’s Algorithm
• Start with each vertex being its own component
13
14
15
Kruskal’s Algorithm
O(V)
O(ElgE)
O(E)
O(lgV)
17
18
19
20
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
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
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
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
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
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
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
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
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
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
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
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