-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibHeap.cpp
257 lines (253 loc) · 6.87 KB
/
fibHeap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include<iostream>
#include<cmath>
#include<string>
#include<stdlib.h>
#include<vector>
#include "fibHeap.h"
using namespace std;
FibHeap::FibHeap()
{
max=NULL;
numNodes=0;
}
node* FibHeap::getNode(string hashtag,int freq)
{
node* newNode=new node();
newNode->degree=0;
newNode->hashtag=hashtag;
newNode->frequency=freq;
newNode->child=NULL;
newNode->left=newNode; // left pointer points to itself
newNode->right=newNode; // right pointer points to itslef
newNode->parent=NULL;
newNode->childCut=false;
return newNode;
}
void FibHeap::insert(node* newNode)
{
if(max==NULL) // heap is just starting
max=newNode;
else
{
// insert in top level list
node *next=max->right;
max->right=newNode;
newNode->left=max;
newNode->right=next;
next->left=newNode;
}
if(newNode->frequency > max->frequency) // update max pointer
max=newNode;
numNodes++; // increment number of nodes in heap
}
void FibHeap::increaseKey(node *ptr,int c)
{
// increase frequency of node pointer to by ptr by c
ptr->frequency+=c;
// if ptr is not root and new frequency of ptr is more than its parent
if(ptr->parent!=NULL && ptr->frequency>ptr->parent->frequency)
{
node *parent=ptr->parent; // need to store this because after cut this node will be at the top level list with parent pointer NULL
cut(ptr);
cascadingCut(parent);
}
// if after increase, current freq > max frequency, update max pointer
if(ptr->frequency > max->frequency)
max=ptr;
}
void FibHeap::cut(node *ptr)
{
// if single child
if(ptr->parent->degree==1)
{
ptr->parent->child=NULL;
ptr->parent->degree--;
}
else
{
// remove current node from list at this level
node *left=ptr->left;
node *right=ptr->right;
left->right=right;
right->left=left;
if(ptr->parent->child == ptr) // if ptr is the node the parent points to as the child
ptr->parent->child=left;
ptr->parent->degree--;
}
// parents child cut value will be examined/changed in cascadingCut method
// add to top level list
node *next=max->right;
max->right=ptr;
ptr->left=max;
ptr->right=next;
next->left=ptr;
ptr->childCut=false; // since this has become root node, childCut should be undef/false
ptr->parent=NULL;
}
void FibHeap::cascadingCut(node *ptr)
{
// if reached root node, no need to continue
if(ptr->parent!=NULL) // if reached a node which is not root
{
if(ptr->childCut==false) // if childCut is false, this ptr is losing its first child, make childCut true; nothing else to do
ptr->childCut=true;
else // if childCut was true
{
node *parent=ptr->parent; // need to store this because after cut, this node will be at the top level list
cut(ptr);
cascadingCut(parent);
}
}
}
node* FibHeap::removeMax()
{
node *ptr=max; // saving pointer to max node, to return it at the end
if(max!=NULL)
{
node *leftSibling=max->left;
node *rightSibling=max->right;
if(leftSibling!=max) // not only a single tree in the heap
{
node *curChild=max->child;
if(curChild!=NULL) // if atleast one child
{
node *firstChild=max->child;
node *lastChild=firstChild->left;
// insert children list as it is by changing the following
// right pointer of left sibling to firstChild
// left pointer of firstChild to leftSibling
// left pointer of right sibling to lastChild
// right pointer of lastChild to rightSibling
leftSibling->right=firstChild;
firstChild->left=leftSibling;
rightSibling->left=lastChild;
lastChild->right=rightSibling;
// reset ChildCut and parent pointer values for all children
for(int i=max->degree;i>0;i--)
{
curChild->childCut=false;
curChild->parent=NULL;
curChild=curChild->right;
}
}
else
{
leftSibling->right=rightSibling;
rightSibling->left=leftSibling;
}
max=rightSibling;
}
else // single tree in heap, reset parent pointers and childCut for children, remove the max node and reassign max
{
node* curChild=max->child;
node* firstChild=max->child;
if(curChild!=NULL) //atleast one child
{
do
{
curChild->parent=NULL;
curChild->childCut=false;
curChild=curChild->right;
}while(curChild!=firstChild);
}
max=curChild;
}
pairwiseCombine();
}
else // heap is empty
{
cout<<"Cant do removeMax() from an empty heap"<<endl;
}
numNodes>0?numNodes--:1; // cant reduce numNodes if there werent any nodes in the heap before
// resetting values for the max node that will be returned
ptr->degree=0;
ptr->child=NULL;
ptr->left=ptr;
ptr->right=ptr;
ptr->parent=NULL;
ptr->childCut=false;
return ptr;
}
void FibHeap::pairwiseCombine()
{
float phi=((1.0+sqrt(5))/(2.0));
int len=ceil(log((float)numNodes)/log(phi)); // log function returns natural log, converting it to log base phi
node **arr=new node*[len]; // array of pointers to structure node
int i;
for(i=0;i<len;i++)
arr[i]=NULL;
if(max!=NULL) // if no node exists in the heap - nothing more to do
{
// vector to iterate over all the root nodes
vector<node*> allNodes;
node* curNode=max;
do{
allNodes.push_back(curNode);
curNode=curNode->right;
}while(curNode!=max);
for(vector<node*>::iterator cur = allNodes.begin() ; cur != allNodes.end(); ++cur)
{
curNode=*cur;
node* parentNode=curNode;
int degree=curNode->degree;
while(arr[degree]!=NULL) // while a node exists with degree curNode->degree
{
node *otherNode = arr[degree];
if(otherNode->frequency > curNode->frequency)
{
parentNode=otherNode;
move(otherNode,curNode); // curNode becomes child of otherNode
}
else
{
parentNode=curNode;
move(curNode,otherNode); // otherNode becomes child of curNode
}
arr[degree]=NULL; // as there wont exist a node with this degree anymore(note that the new parents degree has incresed by 1)
degree++;
curNode=parentNode; // because whoever is the new parent will be compared next
}
arr[degree]=parentNode;
}
}
//reset max
max=NULL;
for(i=0;i<len;i++)
{
if(arr[i]!=NULL)
{
if(max==NULL)
max=arr[i];
else if(arr[i]->frequency > max->frequency)
max=arr[i];
}
}
}
void FibHeap::move(node *otherNode, node *curNode)
{
// curNode becomes child of otherNode
// remove curNode from top level list
curNode->left->right=curNode->right;
curNode->right->left=curNode->left;
// set curNode left and right pointers to itself
curNode->left=curNode;
curNode->right=curNode;
curNode->childCut=false;
if(otherNode->child==NULL) // if prospective parent had no child
{
otherNode->child=curNode;
curNode->parent=otherNode;
}
else
{
node* firstChild=otherNode->child;
node* secondChild=otherNode->child->right;
// insert new node on the right of first child
firstChild->right=curNode;
curNode->left=firstChild;
curNode->right=secondChild;
secondChild->left=curNode;
curNode->parent=otherNode;
}
otherNode->degree++;
}