(Sorry in advance for sloppy ASCII math, I don't think we can use LaTEX to answer the set)
The traditional way to implement the Prim algorithm with complexity O(V^2) is to have an array in addition to the adjacency matrix, you can call it distance , which has a minimum distance to this vertex from node.
Thus, we always check distance to find the next target, and since we do it V times and there are V distance members, our complexity is O(V^2) .
This would not be enough, since the initial values ββin distance quickly outdated. To update this array, everything we do is at the end of each step, iterate through our adjacency matrix and update the distance accordingly. This does not affect our time complexity, as it simply means that each step takes O(V+V) = O(2V) = O(V) . Therefore, our algorithm is O(V^2) .
Without using distance we have to iterate over all the edges of E every time, which in the worst case contains V ^ 2 edges, which means that our time complexity will be O(V^3) .
Evidence:
To prove that without the distance array it is impossible to calculate the MST time in O(V^2) , think that then at each iteration with a tree of size n there are potentially added vertices Vn .
To calculate which one to choose, we must check each of them to find their minimum distance from the tree, and then compare them with each other and find the minimum there.
In the worst case, each node contains a connection to each node in the tree, resulting in edges n * (Vn) and complexity O(n(Vn)) .
Since our sum would be the sum of each of these steps, since n goes from 1 to V, our final time complexity is as follows:
(sum O(n(Vn)) as n = 1 to V) = O(1/6(V-1) V (V+1)) = O(V^3)
QED