According to CLRS, the Prim's algorithms is implemented as below --
$\mathtt{\text{MST-PRIM}}(G,w,r)$
- for each $u \in V[G]$ do
- $\mathtt{\text{key}}[u] \leftarrow \infty$
- $\pi[u] \leftarrow \mathtt{\text{NIL}}$
- $\mathtt{\text{key}}[r] \leftarrow 0$
- $Q \leftarrow V[G]$
- while $Q \ne \emptyset$ do // ... $O(V)$
- $u$ $\leftarrow$ $\mathtt{\text{EXTRACT-MIN}}(u)$ // ... $O(\lg V)$
- for each $v \in \mathtt{\text{adj}}[u]$ do // ... $O(E)$
- if $v \in Q$ and $w(u,v) \gt \mathtt{\text{key}}[v]$
- then $\pi[v] \leftarrow u$
- $\mathtt{\text{key}} \leftarrow w(u,v)$ // $\mathtt{\text{DECREASE-KEY}}$ ... $O(\lg V)$
The book says the total complexity is $O(V \lg V + E \lg V) \approx O(E \lg V)$. However, what I understood is that the inner for
loop with the DECREASE-KEY
operation will cost $O(E \lg V)$, and the outer while
loop encloses both the EXTRACT-MIN
and the inner for
loop, so the total complexity should be $O(V (\lg V + E \lg V)) = O(V \lg V + EV \lg V) \approx O(EV \lg V)$.
Why the complexity analysis is not performed as such? and What is wrong with my formulation?