I am trying to optimize the graph using the Prim Min Spanning Tree algorithm. But I do not get the desired answer.
Algorithm:
1. Construct min heap array. The array consists of nodes which have a vertex value and a key value. The key values are initialized to INT_MAX initially. 2. Make the zeroth node key 0, as this is the starting node. 3. I iterate over the heap, till it becomes empty, and in every step following is done: - Extract the minimum element out of the min heap. This is done by extractMin() function in the class MinHeap. 4. Look for this extracted element neighbors and update their keys based on the weight of the corresponding edge. 5. Then decrease the key value in the minHeap by using decreaseKey() function in class MinHeap. 6. Store the parent and child for which the condition satisfies in a map called parent.
This is the code description:
1. The code contains two header files, Graph.h and MinHeap.h. The functions are all std f functions in these files. So there won't be any problem in understanding them. 2. The Graph.cpp file contains the PrimMST() function which does all the job and performs the entire algorithm.
Here's the problem:
1. When I extract a node from heap in PrimMST() function, I call extractMin() function defined in MinHeap.cpp file. This function swaps the top most node in the heap with the bottom most node. And then performs the heapify operation. But, it is not performing this operation though I have called it in extractMin(). There's no problem with minHeapify function which does the heapify operation as it does perform its job else where is the program.
This is the graph I'm trying to optimize: 
This is the program: PS: I place all the code with all the header files so that it can be easily understood. But skip the code and please pay attention to the PrimMST () function in the Graph.cpp file.
With this input, I get an erroneous output. Note that this output is obtained by calling printHeap before and after calling extractMin (). And as you can see, although minHeapify (0) is called in extractMin () every time a node is retrieved. This somehow does not perform the operation, and therefore, the heap does not accumulate, which leads to an erroneous result Example output for the first three iterations:
First Iteration: 0, 0 1, 2147483647 2, 2147483647 3, 2147483647 4, 2147483647 5, 2147483647 6, 2147483647 7, 2147483647 8, 2147483647 8, 2147483647 1, 2147483647 2, 2147483647 3, 2147483647 4, 2147483647 5, 2147483647 6, 2147483647 7, 214748364 Second Iteration: 1, 4 7, 8 2, 2147483647 8, 2147483647 4, 2147483647 5, 2147483647 6, 2147483647 3, 2147483647 3, 2147483647 7, 8 2, 2147483647 8, 2147483647 4, 2147483647 5, 2147483647 6, 2147483647 Third Iteration: 2, 8 7, 8 3, 2147483647 8, 2147483647 4, 2147483647 5, 2147483647 6, 2147483647 6, 2147483647 7, 8 3, 2147483647 8, 2147483647 4, 2147483647 5, 2147483647
Please pay attention to the second and third iterations, they are not killed at all, although I called the minHeapify function in the extractMin () function at the end.
I desperately need help with this.