Sunteți pe pagina 1din 16

Prim’s Algorithm for minimun

spanning tree construction


Algorithm
MST-PRIM( G, w, r) 7. while Q = ∅
1.A={} 8. do u ← EXTRACT-MIN(Q)
2. for each u ∈ V[G] 9. Q=Q-u
3. do key[u]←∞ 10. If(π[u]!=NIL)
4. π[u]← NIL 11. A=AU(u, π[u])
12. for each v ∈ Adj[u]
5. key[r] ← 0 13. do if v ∈ Q and w(u, v) <
6. Q ← V[G] key[v]
14. then π[v]← u
15. key[v] ← w(u, v)
Explanation of terms used
• G=Graph
• W=weight function
• R=root vertex
• A= Solution Set
• key[v]←minimum weight is the minimum
weight of any edge connecting v to a vertex
in the tree
• π[u]=parent of u
Example

a b
7
10
32

c d
9 23 e
Initial steps of algorithm
MST-PRIM( G, w, r)
1. A={}
1.A={}
2. for each u ∈ V[G] V[G]={a,b,c,d,e}
3. do key[u]←∞
4.π[u]← NIL key[a,b,c,d,e]=∞,
5. key[r] ← 0 π[a,b,c,d,e]=NIL
6. Q ← V[G] Key[a]=0
7. while Q = ∅ Q={a,b,c,d,e}
8. do u ← EXTRACT-MIN(Q)
9. Q=Q-u
10. If(π[u]!=NIL)
11. A=AU(u, π[u])
12. for each v ∈ Adj[u]
13. do if v ∈ Q and w(u, v) < key[v]
14. then π[v]← u
15. key[v] ← w(u, v)
MST-PRIM( G, w, r) while Q = ∅
do u ← EXTRACT-MIN(Q)
A={}
Q=Q-u
for each u ∈ V[G] If(π[u]!=NIL)
do key[u]←∞ A=AU(u, π[u])
π[u]← NIL for each v ∈ Adj[u]
key[r] ← 0 do if v ∈ Q and w(u, v) <
key[v]
Q ← V[G] then π[v]← u
key[v] ← w(u, v)
1st iteration
while Q = ∅ Q={a,b,c,d,e}
do u ← EXTRACT-MIN(Q) • u=a
Q=Q-u • Q={b,c,d,e}
If(π[u]!=NIL)
• If(π[a]!=NIL) cond false
A=AU(u, π[u])
A=AU(u, π[u])
for each v ∈ Adj[u]
for each v ∈ Adj[a]
do if v ∈ Q and w(u, v) <
key[v] V={c,d}
then π[v]← u c(c in Q & d(d in Q &
key[v] ← w(u, v) w(a,c)(10)<key( w(a,d)(9)<key(
c)(∞) d)(∞)
b c d e
∞ 10 9 ∞ π[c]=a π[d]=a
Key[c]=10 Key[d]=9
2nd iteration
Q is b c d e
∞ 10 9 ∞

u=d
Q={b,c,e}
• If(π[d]!=NIL)(cond true)
A=AU(d,a)
Now A={(a,d)}
for each v ∈ Adj[d]
V={a,b,c,e}

a(a in Q (cond b(b in Q & c(c in Q & e(e in Q &


false)) w(b,d)(32)<key(b)( w(c,d)(9)<key(c)(10 w(e,d)(23)<key(e)(
∞) ) ∞)

π[b]=d π[c]=d π[e]=d


Key[b]=32 Key[c]=9 Key[e]=23
Now the graph and Q is

Q
b c e
32 9 23
3rd iteration
Q is b c e
32 9 23

u=c
Q={b,e}
• If(π[c]!=NIL)(cond true)
A=AU(c,d)
Now A={(a,d),(c,d)}
for each v ∈ Adj[c]
V={a,d}

a(a in Q (cond d(d in Q (cond


false)) false))
4th iteration
Q is b e
32 23

u=e
Q={b}
• If(π[e]!=NIL)(cond true)
A=AU(e,d)
A={(a,d),(c,d),(d,e)}
for each v ∈ Adj[e]
V={d}

d(d in Q (cond
false))
5th iteration
Q is b
32

u=b
Q={}
• If(π[b]!=NIL)(cond true)
A=AU(b,d)
A={(a,d),(c,d),(d,e),(b,d)}
for each v ∈ Adj[b]
V={d}

d(d in Q (cond
false))
6th iteration
Q is

• Q is empty.
• All vertices are
covered in A.
• Final A is:
A={(a,d),(c,d),(d,e),(b,d)}
Complexity calculations
• The performance of Prim’s algorithm depends on
how we implement the min priority queue Q.
• The initialization phase costs O(V).
• If Q is implemented as a binary min-heap:
• BUILD-MIN-HEAP procedure to perform the
initialization in lines 1–6 takes O(V) time.
• The body of the while loop is executed |V| times
• each EXTRACT-MIN operation takes O(lg V) time,
the total time for all calls to EXTRACT-MIN is
• O(V lg V) Go to Algorithm
• The for loop in lines 12-15 is executed O(E) times
as it will be executed degree(u)times.
• Within the for loop, the test for membership in Q
in line 13 can be implemented in constant time .
• The assignment in line 15 involves an implicit
DECREASE-KEY operation on the min-heap, which
can be implemented in a binary min-heap in O(lg
V) time. Thus, the total time for Prim’s algorithm
is
• O(V lg V + E lg V) = O(E lg V)
Go to Algorithm
• The actual complexity depends on the data
structure actually used in the algorithm.
• Using an array:
• EXTRACT−MIN=O(V),
• DECREASE−KEY=O(1),
• complexity is O(V*V) in the worst case.

S-ar putea să vă placă și