I have a graph with 4 nodes where the adjacency matrix are as follows: The numbers inside indicate the weights.
____1___2___3___4__
1 | | 10| 5 | 1
___________________
2 |10 | | |
___________________
3 | 5 | | |
___________________
4 | 1 | | |
___________________
I am using the fibonacci heap to store the edge weights. At each iteration, I will merge the pair of nodes with the lowest edge weight and update the edge weights of all nodes that are affected by this process. I am struggling on how I can keep a pointer to all affected nodes. I am aware of the handlers but.. well I am just really struggling. I hope someone can give me some pointers on how I can implement this and please, do not downvote this post. I have done my homework, installed boost, managed to get a simple heap working. I am just lost on how I can locate heaps given indices.
struct radig_distance
{
float distance;
int id1, id2;
radig_distance(int i, int j, float dist) : id1(i), id2(j), distance(dist) { }
};
struct compare_dist
{
//for min heap implementation
bool operator()(const radig_distance& n1, const radig_distance& n2) const
{
return n1.distance > n2.distance;
}
};
//fibonacci heap test
boost::heap::fibonacci_heap<radig_distance, boost::heap::compare<compare_dist>> test_heap;
vector<boost::heap::fibonacci_heap<radig_distance, boost::heap::compare<compare_dist>>::handle_type> test_handle;
test_handle.push_back(test_heap.push(radig_distance(1, 2, 10)));
test_handle.push_back(test_heap.push(radig_distance(1, 3, 5)));
test_handle.push_back(test_heap.push(radig_distance(1, 4, 1)));
// nodes that will be affected after merge of 1 and 4
// 2 and 3
int n[2] = { 2, 3 };
//remove edge with min element
test_heap.pop();
//update affected nodes. merged nodes will be given id of max + 1
//obviously wrong. how can i immediately access nodes whose id2 are 2 and 3
test_heap.update(test_handle[n[0]], radig_distance(5, n[0], 9.5));
test_heap.update(test_handle[n[1]], radig_distance(5, n[1], 4.5));
After the merge, edge weights will be updated and the merged node will have its id changed to total_number_of_nodes++.
__5___2___3___
5 | | 9.5| 4.5
______________
2 |9.5| |
______________
3 | 4.5| |
______________
I have the neighbors of each nodes stored in an adjacency map so I can easily determine which nodes, and hence edges will be affected. My main issue right now is accessing said nodes in the fibonacci heap.
I could just iterate through the entire heap every time a merge is performed but i was looking for a more efficient way