I found the time complexity of Prims algorithm everywhere as O((V + E) log V) = E log V. But as we can see the algorithm:
It seems like the time complexity is O(V(log V + E log V)). But if its time complexity is O((V + E) log V). Then the nesting must have to be like this:
But the above nesting is seems to be wrong.
Using a Binary Heap
The time complexity required for one call to
EXTRACT-MIN(Q)
isO(log V)
using a min priority queue. The while loop at line 6 is executing total V times.soEXTRACT-MIN(Q)
is calledV
times. So the complexity ofEXTRACT-MIN(Q)
isO(V logV)
.The
for
loop at line 8 is executing total2E
times as length of each adjacency lists is2E
for an undirected graph. The time required to execute line 11 isO(log v)
by using theDECREASE_KEY
operation on the min heap. Line 11 also executes total2E
times. So the total time required to execute line 11 isO(2E logV) = O(E logV)
.The
for
loop at line 1 will be executedV
times. Using the procedure to perform lines 1 to 5 will require a complexity ofO(V)
.Total time complexity of
MST-PRIM
is the sum of the time complexity required to execute steps 1 through 3 for a total ofO((VlogV) + (E logV) + (V)) = O(E logV)
since|E| >= |V|
.Using a Fibonacci Heap
O(1)
amortized time. Line 11 executes a total of2E
times. So the total time complexity isO(E)
.So the total time complexity of
MST-PRIM
is the sum of executing steps 1 through 3 for a total complexity ofO(V logV + E + V)=O(E + V logV)
.