Professional Documents
Culture Documents
MST Prim
MST Prim
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.
• 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)
(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[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