In the primitive algorithm, there is a step where you should get the "closest" vertex. This step will cost O (N) when using a regular array, but to use a priority queue (for example, a bunch), you only need O (logN)
Therefore, the reason for using the priority queue is to reduce the complexity of the algorithm time (which means that the program is accelerating)
**
Update:
**
Here is a description of the Prim algorithm from Wikipedia . The bold part is the part for finding the nearest peak that I spoke about:
Input: A nonempty connected weighted graph with vertices V and edges E (weights can be negative).
Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew = {}
Repeat until Vnew = V: Select an edge (u, v) with a minimum weight such that u is in Vnew and v is not (if there are several edges with the same weight, any of them can be selected) Add v in Vnew and (u, v) so that Enew
Exit: Vnew and Enew describe a minimal spanning tree.
source share