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

Minimum Spanning Tree

Introduction
• Given undirected graph G with positive edge weights. A
spanning tree of G is a subgraph T that is both a
tree(connected and acyclic) and spanning (includes all of the
vertices).
• Goal – To find a minimum weight spanning tree.
• Application – Communication Network Design
Introduction
• The minimum spanning tree may not be unique. However, if
the weights of all the edges are pairwise distinct, it is indeed
unique.
• Example
Binary Heap
• The (binary) heap data structure is an array object that can be
viewed as a nearly complete binary tree.

Max Heap Min Heap


Binary Heap contd..

• The min-heap property is that for every node i other than the
root, A[Parent[i]]<=A[i]
Binary Heap contd..
example

(a) (b)

Running time = O(h)


h is the height of the subtree
rooted at that node .
Height of tree = lgn

(c)
Build Heap

• Running Time
• Each call to MAX‐HEAPIFY costs O(lgn), and there are O(n)
such calls. Thus, the running time is O(nlgn). This upper
bound, through correct, is not asymptotically tight.
Build Heap contd..
• For an n‐element heap, height is ⌊lgn⌋ and at most ⌈n / 2h+1⌉
nodes of any height h. The time required by MAX‐HEAPIFY
when called on a node of height h is O(h). The total cost is
Build Heap Example
• Array A =

(a) (b)
Example contd..

(d)
(c)

(e) (f)
Example contd..

(g)
Max priority queue
• A max-priority queue is a data structure representing a
collection of elements which supports the following
operations:
• INSERT(Q, x) – Inserts element x into the set of elements Q
• MAXIMUM(Q) – Returns the element of Q with the largest key
• EXTRACT-MAX(Q) – Removes and returns the element with
the largest key
• INCREASE-KEY(Q, x, k) – Increases the value of x’s key to new
value k.
Max Priority Queue Operation
Max Priority Queue Operation
Min priority queue
• A min-priority queue is a data structure representing a
collection of elements which supports the following
operations:
• INSERT(Q, x) – Inserts element x into the set of elements Q
• MINIMUM(Q) – Returns the element of Q with the smallest
key
• EXTRACT-MIN(Q) – Removes and returns the element with
the smallest key
• DECREASE-KEY(Q, x, k) – Decreases the value of x’s key to new
value k.
Prim’s Algorithm
• Lines 1 to 3 take O(V)
time.
• Line 5 takes O(V) time to
build min priority queue.
• Each Extract-Min(Q)
operation takes O(lgV)
time. The body of while
loop is executed |V| times
thus line 7 takes total time
O(VlgV).
• Body of Loop at line 8
executes |E| times.
• At Line 11, Decrease key
operation takes O(lgV)
time and thus total time
taken by all the calls of
decrease key operation is
T= O(V) + O(VlgV) + O(ElgV) O(ElgV).
Prim’s Algorithm Example
• Example
• key[a] =0 and key value of all other nodes = infinity.
• Parent (π) of all nodes = Nil
• Q= [a(0), b(inf), c(inf), d(inf), e(inf), f(inf), g(inf), h(inf), i(inf)]
inf represents infinity.
• u = Extract_Min(Q) => u = a

• Adj(a) = {b, h} , v = b , w(a,b) = 4 < key(b) => key(b) = 4


• π(b) = a
• v = h , w(a,h) = 8 < key(h) => key(h) = 8 , π(h) = a
Example contd..
• Q = [b(4), h(8), c(inf), d(inf), e(inf), f(inf), g(inf), i(inf)]
• u = Extract_Min(Q) => u = b
• Adj(b) = {c, h} , v = c , w(b,c) = 8 < key(c) => key(c) = 8
• π(c)=b
• v = h , w(b,h) = 11 > key(h) (8) => key(h) = 8
Example contd..
• Q=[c(8), h(8), d(inf), e(inf), f(inf), g(inf), i(inf)]
• u = Extract_Min(Q) => u = c
• Adj(c) = {d, f, i}
• v= d, w(c,d)= 7 < key(d)=> key(d) = 7, π(d)=c
Example contd..
• v= f, w(c,f)= 4 < key(f)=> key(f) = 4, π(f)=c
• v= i, w(c,i)= 2 < key(i)=> key(i) = 2, π(i)=c
• Q=[i(2), f(4), d(7), h(8), e(inf), g(inf)]
• u = Extract_Min(Q) => u = i

• Adj[i]={g,h}
• v= g, w(i,g)=6 < key(g) => key(g) = 6, π(g)=i
• v= h, w(i,h)=7 < key(h) => key(h) = 7, π(h)=i
Example contd..
• Q=[f(4), g(6), d(7), h(7), e(inf)]
• u = Extract_Min(Q) => u = f

• Adj[f]={e, d}
• v= e, w(f,e)=10 < key(e) => key(e) = 10, π(e)=f
• v= d, w(f,d)=14 > key(d) => key(d) = 7, π(d)=c
Example contd..
• Q=[g(6), d(7), h(7), e(10)]
• u = Extract_Min(Q) => u = g
• Adj[g]={h}
• v= h, w(g,h)=1 < key(h) => key(h) = 1, π(h)=g
Example contd..
• Q=[h(1), d(7), e(10)]
• u = Extract_Min(Q) => u = h
• Q=[d(7), e(10)]
Example contd..
• u = Extract_Min(Q) => u = d

• Adj[d]={e}
• v= e, w(d,e)=9<key(e) => key(e)=9, π(e)=d
• Q=[e(9)]
• u = Extract_Min(Q) => u = e

You might also like