I am trying to implement Prim's algorithm for MST in C++. I have a design question
I implemented a min-heap which takes an integer and we can extract-min, decrease key and insert-key.
Now as i understand in Prim's I need to maintain weight, neighbor information for each vertex. Some ideas i have are:
1] Define a structure
struct node {
int vertex;
int weight;
int neighbor;
};
Use the min heap to return the node with minimum weight. But the problem is decrease key, since for decrease key the caller needs to pass the vertex for which he wants to decrease key. Since heap swaps elements too often, I have to go through the entire list to find the vertex and then decrease its key. This is O(n), I think Prim's would get worse if i do this.
2] The other way, would be to maintain another array, which tracks the position of a vertex in the min-heap queue. But it would be cumbersome to track positions during min-heap operations.
Basically, If i have to do decrease-key(v), how to find v in min-heap queue which is based on weight. So is there any elegant way to do this? which can still retain the complexity?