7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
1/16
2 4 . 2 - 6 D e s c r i b e a n e c e n t a l g o r i t h m t h a t , g i v e n a n
u n d i r e c t e d g r a p h G , d e t e r m i n e s a s p a n n i n g t r e e G w h o s e
l a r g e s t e d g e w e i g h t i s m i n i m u m o v e r a l l s p a n n i n g t r e e s
o f G
F i r s t , m a k e s u r e y o u u n d e r s t a n d t h e q u e s t i o n
5
5
5
5
5
1
6
1
11
Lower maximum edge weight Lower total weight
H e y , d o e s n ' t K r u s k a l ' s a l g o r i t h m d o s o m e t h i n g l i k e
t h i s . "
C e r t a i n l y ! S i n c e K r u s h a l ' s a l g o r i t h m c o n s i d e r s t h e e d g e s
i n o r d e r o f i n c r e a s i n g w e i g h t , a n d s t o p s t h e m o m e n t
t h e s e e d g e s f o r m a c o n n e c t e d g r a p h , t h e t r e e i t g i v e s
m u s t m i n i m i z e t h e e d g e w e i g h t .
H e y , b u t t h e n w h y d o e s n ' t P r i m ' s a l g o r i t h m a l s o w o r k ? "
I t g i v e s t h e s a m e t h i n g a s K r u s k a l ' s a l g o r i t h m , s o i t
m u s t b e t r u e t h a t a n y m i n i m u m s p a n n i n g t r e e m i n i -
m i z e s t h e m a x i m u m e d g e w e i g h t !
P r o o f : G i v e m e a M S T a n d c o n s i d e r t h e l a r g e s t e d g e
w e i g h t ,
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
2/16
D e l e t i n g i t d i s c o n n e c t s t h e M S T . I f t h e r e w a s a l o w e r
e d g e c o n n e c t s t h e t w o s u b t r e e s , I d i d n ' t h a v e a M S T !
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
3/16
M i n i m u m S p a n n i n g T r e e s
A t r e e i s a c o n n e c t e d g r a p h w i t h n o c y c l e s . A s p a n n i n g
t r e e i s a s u b g r a p h o f G w h i c h h a s t h e s a m e s e t o f
v e r t i c e s o f G a n d i s a t r e e .
A m i n i m u m s p a n n i n g t r e e o f a w e i g h t e d g r a p h G i s
t h e s p a n n i n g t r e e o f G w h o s e e d g e s s u m t o m i n i m u m
w e i g h t .
T h e r e c a n b e m o r e t h a n o n e m i n i m u m s p a n n i n g t r e e
i n a g r a p h ! c o n s i d e r a g r a p h w i t h i d e n t i c a l w e i g h t
e d g e s .
T h e m i n i m u m s p a n n i n g t r e e p r o b l e m h a s a l o n g h i s t o r y
t h e r s t a l g o r i t h m d a t e s b a c k a t l e a s t t o 1 9 2 6 ! .
M i n i m u m s p a n n i n g t r e e i s a l w a y s t a u g h t i n a l g o r i t h m
c o u r s e s s i n c e 1 i t a r i s e s i n m a n y a p p l i c a t i o n s , 2 i t i s
a n i m p o r t a n t e x a m p l e w h e r e g r e e d y a l g o r i t h m s a l w a y s
g i v e t h e o p t i m a l a n s w e r , a n d 3 C l e v e r d a t a s t r u c t u r e s
a r e n e c e s s a r y t o m a k e i t w o r k .
I n g r e e d y a l g o r i t h m s , w e m a k e t h e d e c i s i o n o f w h a t
n e x t t o d o b y s e l e c t i n g t h e b e s t l o c a l o p t i o n f r o m a l l
a v a i l a b l e c h o i c e s w i t h o u t r e g a r d t o t h e g l o b a l s t r u c -
t u r e .
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
4/16
A p p l i c a t i o n s o f M i n i m u m
S p a n n i n g T r e e s
M i n i m u m s p a n n i n g t r e e s a r e u s e f u l i n c o n s t r u c t i n g n e t -
w o r k s , b y d e s c r i b i n g t h e w a y t o c o n n e c t a s e t o f s i t e s
u s i n g t h e s m a l l e s t t o t a l a m o u n t o f w i r e . M u c h o f t h e
w o r k o n m i n i m u m s p a n n i n g a n d r e l a t e d S t e i n e r t r e e s
h a s b e e n c o n d u c t e d b y t h e p h o n e c o m p a n y .
M i n i m u m s p a n n i n g t r e e s p r o v i d e a r e a s o n a b l e w a y f o r
c l u s t e r i n g p o i n t s i n s p a c e i n t o n a t u r a l g r o u p s .
W h e n t h e c i t i e s a r e p o i n t s i n t h e E u c l i d i a n p l a n e , t h e
m i n i m u m s p a n n i n g t r e e p r o v i d e s a g o o d h e u r i s t i c f o r
t r a v e l l i n g s a l e s m a n p r o b l e m s . T h e o p t i m u m t r a v e l i n g
s a l e s m a n t o u r i s a t m o s t t w i c e t h e l e n g t h o f t h e m i n i -
m u m s p a n n i n g t r e e .
The Option Traveling System tour is at most twice
the length of the minimum spanning tree.
Note: There can be more than one minimum spanningtree considered as a group with identical weight
edges.
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
5/16
P r i m ' s A l g o r i t h m
I f G i s c o n n e c t e d , e v e r y v e r t e x w i l l a p p e a r i n t h e m i n i -
m u m s p a n n i n g t r e e . I f n o t , w e c a n t a l k a b o u t a m i n i -
m u m s p a n n i n g f o r e s t .
P r i m ' s a l g o r i t h m s t a r t s f r o m o n e v e r t e x a n d g r o w s t h e
r e s t o f t h e t r e e a n e d g e a t a t i m e .
A s a g r e e d y a l g o r i t h m , w h i c h e d g e s h o u l d w e p i c k ?
T h e c h e a p e s t e d g e w i t h w h i c h c a n g r o w t h e t r e e b y
o n e v e r t e x w i t h o u t c r e a t i n g a c y c l e .
S H O W E X A M P L E F I G U R E S T R A C I N G P R I M ' S A L -
G O R I T H M
D u r i n g e x e c u t i o n w e w i l l l a b e l e a c h v e r t e x a s e i t h e r i n
t h e t r e e , f r i n g e - m e a n i n g t h e r e e x i s t s a n e d g e f r o m
a t r e e v e r t e x , o r u n s e e n - m e a n i n g t h e v e r t e x i s m o r e
t h a n o n e e d g e a w a y .
S e l e c t a n a r b i t r a r y v e r t e x t o s t a r t .
W h i l e t h e r e a r e f r i n g e v e r t i c e s
s e l e c t m i n i m u m w e i g h t e d g e b e t w e e n t r e e a n d f r i n g e
a d d t h e s e l e c t e d e d g e a n d v e r t e x t o t h e t r e e
C l e a r l y t h i s c r e a t e s a s p a n n i n g t r e e , s i n c e n o c y c l e c a n
b e i n t r o d u c e d v i a e d g e s b e t w e e n t r e e a n d f r i n g e v e r -
t i c e s , b u t i s i t m i n i m u m ?
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
6/16
W h y i s P r i m ' s a l g o r i t h m
c o r r e c t ?
D o n ' t b e s c a r e d b y t h e p r o o f t h e r e a s o n i s r e a l l y q u i t e
b a s i c :
T h e o r e m : L e t G b e a c o n n e c t e d , w e i g h t e d g r a p h a n d
l e t E
0
E b e a s u b s e t o f t h e e d g e s i n a M S T T =
V ; E
T
. L e t V
0
b e t h e v e r t i c e s i n c i d e n t w i t h e d g e s i n
E
0
. I f x ; y i s a n e d g e o f m i n i m u m w e i g h t s u c h t h a t
x 2 V
0
a n d y i s n o t i n V
0
, t h e n E
0
f x ; y g i s a s u b s e t o f
a m i n i m u m s p a n n i n g t r e e .
P r o o f : I f t h e e d g e i s i n T , t h i s i s t r i v i a l .
S u p p o s e x ; y i s n o t i n T T h e n t h e r e m u s t b e a p a t h
i n T f r o m x t o y s i n c e T i s c o n n e c t e d . I f v ; w i s t h e
r s t e d g e o n t h i s p a t h w i t h o n e e d g e i n V
0
, i f w e d e l e t e
i t a n d r e p l a c e i t w i t h x ; y w e g e t a s p a n n i n g t r e e .
T h i s t r e e m u s t h a v e s m a l l e r w e i g h t t h a n T , s i n c e W v ; w
W x ; y . T h u s T c o u l d n o t h a v e b e e n t h e M S T .
Y
X
VU
Thus we cannot go wrong
with the greedy strategy the
way we could with the
traveling salesman.
PRIMs Algorithm is correct!
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
7/16
P r i m ' s A l g o r i t h m i s c o r r e c t !
T h u s w e c a n n o t g o w r o n g w i t h t h e g r e e d y s t r a t e g y t h e
w a y w e c o u l d w i t h t h e t r a v e l i n g s a l e s m a n p r o b l e m .
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
8/16
B u t h o w f a s t i s P r i m ' s ?
T h a t d e p e n d s o n w h a t d a t a s t r u c t u r e s a r e u s e d . I n
t h e s i m p l e s t i m p l e m e n t a t i o n , w e c a n s i m p l y m a r k e a c h
v e r t e x a s t r e e a n d n o n - t r e e a n d s e a r c h a l w a y s f r o m
s c r a t c h :
S e l e c t a n a r b i t r a r y v e r t e x t o s t a r t .
W h i l e t h e r e a r e n o n - t r e e v e r t i c e s
s e l e c t m i n i m u m w e i g h t e d g e b e t w e e n t r e e a n d f r i n g e
a d d t h e s e l e c t e d e d g e a n d v e r t e x t o t h e t r e e
T h i s c a n b e d o n e i n O n m t i m e , b y d o i n g a D F S o r
B F S t o l o o p t h r o u g h a l l e d g e s , w i t h a c o n s t a n t t i m e
t e s t p e r e d g e , a n d a t o t a l o f n i t e r a t i o n s .
C a n w e d o f a s t e r ? I f s o , w e n e e d t o b e a b l e t o i d e n t i f y
f r i n g e v e r t i c e s a n d t h e m i n i m u m c o s t e d g e a s s o c i a t e d
w i t h i t , f a s t . W e w i l l a u g m e n t a n a d j a c e n c y l i s t w i t h
e l d s m a i n t a i n i n g f r i n g e i n f o r m a t i o n .
V e r t e x :
f r i n g e l i n k p o i n t e r t o n e x t v e r t e x i n f r i n g e l i s t .
f r i n g e w e i g h t c h e a p e s t e d g e l i n k i n g v t o l
p a r e n t o t h e r v e r t e x w i t h v h a v i n g f r i n g e w e i g h t .
s t a t u s i n t r e e , f r i n g e , u n s e e n .
a d j a c e n c y l i s t t h e l i s t o f e d g e s .
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
9/16
N E E D P I C T U R E O F T H E D A T A S T R U C T U R E
N E E D D E S C R I P T I O N O F T H E A L G O R I T H M
F i n d i n g t h e m i n i m u m w e i g h t f r i n g e - e d g e t a k e s O n
t i m e j u s t b u m p t h r o u g h f r i n g e l i s t .
A f t e r a d d i n g a v e r t e x t o t h e t r e e , r u n n i n g t h r o u g h i t s
a d j a c e n c y l i s t t o u p d a t e t h e c o s t o f a d d i n g f r i n g e v e r -
t i c e s t h e r e m a y b e a c h e a p e r w a y t h r o u g h t h e n e w
v e r t e x c a n b e d o n e i n O n t i m e .
T o t a l t i m e i s O n
2
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
10/16
K r u s k a l ' s A l g o r i t h m
S i n c e a n e a s y l o w e r b o u n d a r g u m e n t s h o w s t h a t e v e r y
e d g e m u s t b e l o o k e d a t t o n d t h e m i n i m u m s p a n n i n g
t r e e , a n d t h e n u m b e r o f e d g e s m = O n
2
, P r i m ' s a l -
g o r i t h m i s o p t i m a l i n t h e w o r s t c a s e . I s t h a t a l l s h e
w r o t e ?
T h e c o m p l e x i t y o f P r i m ' s a l g o r i t h m i s i n d e p e n d e n t o f
t h e n u m b e r o f e d g e s . C a n w e d o b e t t e r w i t h s p a r s e
g r a p h s ? Y e s !
K r u s k a l ' s a l g o r i t h m i s a l s o g r e e d y . I t r e p e a t e d l y a d d s
t h e s m a l l e s t e d g e t o t h e s p a n n i n g t r e e t h a t d o e s n o t
c r e a t e a c y c l e . O b v i o u s l y , t h i s g i v e s a s p a n n i n g t r e e ,
b u t i s i t m i n i m a l ?
S H O W E X A M P L E F I G U R E S T R A C I N G K R U S K A L ' S
A L G O R I T H M
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
11/16
W h y i s K r u s k a l ' s a l g o r i t h m
c o r r e c t ?
T h e o r e m : L e t G b e a w e i g h t e d g r a p h a n d l e t E
0
E
I f E
0
i s c o n t a i n e d i n a M S T T a n d e i s t h e s m a l l e s t e d g e
i n E , E
0
w h i c h d o e s n o t c r e a t e a c y c l e , E
0
e T
P r o o f : A s b e f o r e , s u p p o s e e i s n o t i n T . A d d i n g e t o T
m a k e s a c y c l e . D e l e t i n g a n o t h e r e d g e f r o m t h i s c y c l e
l e a v e s a c o n n e c t e d g r a p h , a n d i f i t i s o n e f r o m E , E
0
t h e c o s t o f t h i s t r e e g o e s d o w n . S i n c e s u c h a n e d g e
e x i s t s , T c o u l d n o t b e a M S T .
12
3
4
5
6
7
8
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
12/16
H o w f a s t i s K r u s k a l ' s
a l g o r i t h m ?
W h a t i s t h e s i m p l e s t i m p l e m e n t a t i o n ?
S o r t t h e m e d g e s i n O m l g m t i m e .
F o r e a c h e d g e i n o r d e r , t e s t w h e t h e r i t c r e a t e s
a c y c l e t h e f o r e s t w e h a v e t h u s f a r b u i l t i f s o
d i s c a r d , e l s e a d d t o f o r e s t . W i t h a B F S D F S , t h i s
c a n b e d o n e i n O n t i m e s i n c e t h e t r e e h a s a t
m o s t n e d g e s .
T h e t o t a l t i m e i s O m n , b u t c a n w e d o b e t t e r ?
K r u s k a l ' s a l g o r i t h m b u i l d s u p c o n n e c t e d c o m p o n e n t s .
A n y e d g e w h e r e b o t h v e r t i c e s a r e i n t h e s a m e c o n -
n e c t e d c o m p o n e n t c r e a t e a c y c l e . T h u s i f w e c a n
m a i n t a i n w h i c h v e r t i c e s a r e i n w h i c h c o m p o n e n t f a s t ,
w e d o n o t h a v e t e s t f o r c y c l e s !
P u t t h e e d g e s i n a h e a p
c o u n t = 0
w h i l e c o u n t n , 1 d o
g e t n e x t e d g e v ; w
i f c o m p o n e n t v 6= c o m p o n e n t w
a d d t o T
c o m p o n e n t v = c o m p o n e n t w
I f w e c a n t e s t c o m p o n e n t s i n O l o g n , w e c a n n d t h e
M S T i n O m l o g m !
Q u e s t i o n : I s O m l o g n b e t t e r t h a n O m l o g m ?
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
13/16
U n i o n - F i n d P r o g r a m s
O u r a n a l y s i s t h a t K r u s k a l ' s M S T a l g o r i t h m i s O m l o g m
r e q u i r e s a f a s t w a y t o t e s t w h e t h e r a n e d g e l i n k s t w o
v e r t i c e s i n t h e s a m e c o n n e c t e d c o m p o n e n t .
T h u s w e n e e d a d a t a s t r u c t u r e f o r m a i n t a i n i n g s e t s
w h i c h c a n t e s t i f t w o e l e m e n t s a r e i n t h e s a m e a n d
m e r g e t w o s e t s t o g e t h e r . T h e s e c a n b e i m p l e m e n t e d
b y U N I O N a n d F I N D o p e r a t i o n s :
I s s
i
s
j
t = F i n d s
i
u = F i n d s
j
R e t u r n I s t = u ?
M a k e s
i
s
j
t = F i n d s
i
u = F i n d s
j
U n i o n t ; u
F i n d r e t u r n s t h e n a m e o f t h e s e t a n d U n i o n s e t s t h e
m e m b e r s o f t t o h a v e t h e s a m e n a m e a s u
W e a r e i n t e r e s t e d i n m i n i m i z i n g t h e t i m e i t t a k e s t o
e x e c u t e a n y s e q u e n c e o f u n i o n s a n d n d s .
A s i m p l e i m p l e m e n t a t i o n i s t o r e p r e s e n t e a c h s e t a s a
t r e e , w i t h p o i n t e r s f r o m a n o d e t o i t s p a r e n t . E a c h
e l e m e n t i s c o n t a i n e d i n a n o d e , a n d t h e n a m e o f t h e
s e t i s t h e k e y a t t h e r o o t :
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
14/16
s
FIND(S)
t
S
UNION (s, t)
I n t h e w o r s t c a s e , t h e s e s t r u c t u r e s c a n b e v e r y u n b a l -
a n c e d :
F o r i = 1 t o n = 2 d o
U N I O N i , i + 1
F o r i = 1 t o n = 2 d o
F I N D 1
W e w a n t t h e l i m i t t h e h e i g h t o f o u r t r e e s w h i c h a r e
e e c t e d b y U N I O N s . W h e n w e u n i o n , w e c a n m a k e
t h e t r e e w i t h f e w e r n o d e s t h e c h i l d .
S i n c e t h e n u m b e r o f n o d e s i s r e l a t e d t o t h e h e i g h t ,
t h e h e i g h t o f t h e n a l t r e e w i l l i n c r e a s e o n l y i f b o t h
s u b t r e e s a r e o f e q u a l h e i g h t !
L e m m a : I f U n i o n t ; v a t t a c h e s t h e r o o t o f v a s a s u b -
t r e e o f t i t h e n u m b e r o f n o d e s i n t i s g r e a t e r t h a n o r
e q u a l t o t h e n u m b e r i n v , a f t e r a n y s e q u e n c e o f u n i o n s ,
a n y t r e e w i t h h = 4 n o d e s h a s h e i g h t a t m o s t b l g h c
P r o o f : B y i n d u c t i o n o n t h e n u m b e r o f n o d e s k , k = 1
h a s h e i g h t 0 .
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
15/16
A s s u m e t r u e t o k , 1 n o d e s . L e t d
i
b e t h e h e i g h t o f
t h e t r e e t
i
d1
d2T2
k2 nodesk1 nodes
T1
k = k1+ k2 nodes
d is the height
I f d
1
d
2
t h e n d = d
1
b l o g k
1
c b l g k
1
+ k
2
c =
b l o g k c
I f d
1
d
2
, t h e n k
1
k
2
d = d
2
+ 1 b l o g k
2
c + 1 = b l o g 2 k
2
c b l o g k
1
+ k
2
c =
l o g k
7/29/2019 Lecture 17 - Minimum Spanning Tree.pdf
16/16
C a n w e d o b e t t e r ?
W e c a n d o u n i o n s a n d n d s i n O l o g n , g o o d e n o u g h
f o r K r u s k a l ' s a l g o r i t h m . B u t c a n w e d o b e t t e r ?
T h e i d e a l U n i o n - F i n d t r e e h a s d e p t h 1 :
N-1 leaves
... ...
O n a n d , i f w e a r e g o i n g d o w n a p a t h a n y w a y , w h y
n o t c h a n g e t h e p o i n t e r s t o p o i n t t o t h e r o o t ?
1
2
3
4 5 6 8 9
7 1112
13
1410
FIND(4)
1
4 3 7 1014
13
12115 6 8 9
2
T h i s p a t h c o m p r e s s i o n w i l l l e t u s d o b e t t e r t h a n O n l o g n
f o r n u n i o n - n d s .
O n ? N o t q u i t e : : : D i c u l t a n a l y s i s s h o w s t h a t i t
t a k e s O n n t i m e , w h e r e n i s t h e i n v e r s e A c k e r -
m a n f u n c t i o n a n d n u m b e r o f a t o m s i n t h e u n i v e r s e =
5