From my understanding, the use of heap helps by allowing constant cost for finding minimum value with the extra work of log n
every time we change a value. Change in value can happen a maximum of e
times. Time complexity is approximately O(e) + O(e log n) = O(e log n)
.
If it is a dense graph, e = n^2
.
But if we do the searching for minimum using linear search on the value array
, we will get a complexity of O(e) + O(n^2) = O(n^2)
which is better than elog n
for dense graphs. This is similar to what we get from using a 2D array for graph representation in prim's MST algorithm.
Then why do we use the 2D array representation at all for this specific algorithm?
e
= number of edges.
n
= number of nodes.
You're right; there's no point in using a fancy heap data structure for Prim on dense graphs. The defining feature of Prim, though, is not the heap so much as the idea of repeatedly extending a partial MST as cheaply as possible.
The point of an adjacency matrix is for space (no pointer overhead) and architectural reasons (sequential accesses are typically much more efficient than random).