-
Notifications
You must be signed in to change notification settings - Fork 1
/
ads_project.cpp
428 lines (418 loc) · 13.3 KB
/
ads_project.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//This code has been written by Karan Goel ,Student at University of Florida
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>
//All the header files have been added here
using namespace std;
//this is the basic node structure used
struct node {
int val;// this will keep the frequencies
int degree;//this will keep the number of children a node has
string key;//this will keep the name of the node
struct node *parent;//this will save the parent pointer
bool child_cut;//this will keep track of the child cut if it is true or not
struct node *link_child;//this will store the link list of the child pointer
struct node *link_list_right;//this will store the the link of the next pointer
struct node *link_list_left;//this will store the link of the previous node
}*head;
//making the map to be global so that it can be used within the functions as well
unordered_map<string, struct node *> umap;//this will be used to save the
std::map <int,struct node *>table;//this is used for pairwise combine
//Prototyping
//this code melds the two fibonacci heap into one
bool meld(struct node *root1,struct node *root2)
{
struct node *temp1=root1->link_list_right;
struct node *temp2=root2->link_list_left;
root1->link_list_right=root2;
root2->link_list_left=root1;
temp1->link_list_left=temp2;
temp2->link_list_right=temp1;
return true;
}
//this function returns the vector of the nodes that are that same level
//in our code it is level 0 and it is used for pairwise combine later.
vector<struct node *> check(struct node *root)
{
//here tell takes all the root nodes of max trees ,
//also we are setting the pointer of head node in this code only
vector <struct node *> tell;
struct node *temp=root;
do
{
tell.push_back(temp);
temp=temp->link_list_left;
}while(temp!=root);
return tell;
}
//this function is used to add a new node in the heap if the head is initially empty we will add a new node and later
//we will add to the head node
struct node * add(string name,int freq)
{
//if the head is null we are going a new node to the head and then making if a node , with left and right pointer
//pointing to itself
if(head==NULL)
{
head=new node();
head->child_cut=false;
head->val=freq;
head->key=name;
head->parent=NULL;
head->link_child=NULL;
head->link_list_right=head;
head->link_list_left=head;
head->degree=0;
return head;
}
//otherwise we will need to make a new node and meld it to the head node , i.e. the fibonacci heap
else
{
struct node *temp=new node();
temp->val=freq;
temp->key=name;
temp->parent=NULL;
temp->link_child=NULL;
temp->child_cut=false;
temp->degree=0;
temp->link_list_left=temp;
temp->link_list_right=temp;
meld(head,temp);
if(head->val<freq)
head=temp;
return temp;
}
return head;
};
//this is used for pairwise combine of the two node , it melds the smaller of the two as the root node and other as parent node.
//along with this it will check that if the child node is null it will add directly otherwise meld it to the node which is the
//child node of the bigger true , changing the value of the root node to be node
struct node * pairwise_comb(struct node *root1,struct node *root2)
{
//we will interchange the node if the root2 is bigger than root 1
if(root1==NULL || root2==NULL)
{
return root1;
}
if(root2->val>root1->val)
{
struct node *temp=root1;
root1=root2;
root2=temp;
}
//we will take out the root2 node (i.e. the smaller node (without changing the structure of the heap and then combine the two node
root2->parent=root1;
//root2->link_list_right->link_list_left=root2->link_list_left;
//root2->link_list_left->link_list_right=root2->link_list_right;
root1->degree=root1->degree+1;
root2->child_cut=false;
if(root1!=NULL && root1->link_child==NULL)
{
root1->link_child=root2;
root2->link_list_left=root2;
root2->link_list_right=root2;
return root1;
}
if(root1!=NULL && root1->link_child!=NULL)
{
struct node *temp=root1->link_child;
root2->link_list_left=root2;
root2->link_list_right=root2;
meld(temp,root2);
//root2->link_child=root2;
return root1;
}
return NULL;
};
// this will keep on working till it finds a empty place in the map , where it can be entered
struct node * pairwise_vec(struct node *root)
{
//we will keep looping the element till we are able to enter an element in the map
while(true)
{
std::map<int,struct node *>::iterator it;
it=table.find(root->degree);
if(it==table.end())
{
if(head->val<root->val)
{
head=root;
}
//here we will insert the element into hashmap and then return from the program
table.insert(std::pair<int,struct node *>(root->degree,root));
return root;
}
else if(root!=it->second)
{
struct node *p=it->second;
table.erase(it);
root=pairwise_comb(p,root);
}
}
return NULL;
}
//this code with the help of other funcationality such as check and pairwise combine
struct node * remove_max()
{
//temp is to keep the value of the maximum in case needed later
//temp2 is to create a new list
if(head==NULL)
{
//if head is null and nothing
return head;
}
//if head is the only element in the heap
if(head->link_child==NULL && head->link_list_left==head)
{
struct node *temp=head;
head=NULL;
return temp;//ts is done to remove the head from the node keeping the memory of the key intact and yet removing the array
}
//this is done to remove all the child of head and then meld it into head only
if(head->degree!=0)
{
struct node *temp=head->link_child;
head->degree=0;
head->link_child=NULL;
struct node *temp2=temp;
do
{
//temp->child_cut=false;
temp->parent=NULL;
temp=temp->link_list_left;
}
while(temp!=temp2);
//we will meld the children of head with head itself
meld(head,temp);
}
//this is to remove the head without changing the structure of the fibonacci heap
struct node *value_saver = head;//this is used to save the value which is returned later to be used by main program for later input in the heap
struct node *temp1=head->link_list_right;
head->link_list_left->link_list_right=head->link_list_right;
head->link_list_right->link_list_left=head->link_list_left;
struct node *temp2=temp1;
head=temp1;
vector<struct node *> l=check(temp2);
int i=0;
table.clear();
while(i<l.size())
{
l[i]->link_list_left=NULL;
l[i]->link_list_right=NULL;
pairwise_vec(l[i]);
i++;
}
std::map<int,struct node *>::iterator it=table.begin();
head=(*it).second;
head->link_list_left=head;
head->link_list_right=head;
while(it!=table.end())
{
(*it).second->link_list_left=(*it).second;
(*it).second->link_list_right=(*it).second;
meld(head,(*it).second);
if(head->val<(*it).second->val)
{
head=(*it).second;
}
++it;
}
return value_saver;
}
//it is used to increase a particular key and then check if it greater than parent and perform child cut if necessary
int increasekey(struct node *root,int freq)
{
//this is done to check if the root is not null
if(root==NULL)
{
return -1;
}
//this is done to increase the element which is already a root node and then you don't need to go for child cut operation
if(root->parent==NULL)
{
root->val=root->val+freq;
if(root->val>head->val)
{
head=root;
}
return 1;
}
if(root->parent!=NULL)
{
root->val=root->val+freq;
if(root->val<root->parent->val)
{
return 1;
}
else
{
do
{
if(root->link_list_left==root)
{
//if root is the only child in the parent then in order to remove it , we will need to make child pointer to be null
root->parent->link_child=NULL;
}
else
{
//if root has many other child as well then we will remove the pointer to be removed
root->parent->link_child=root->link_list_left;
root->link_list_left->link_list_right=root->link_list_right;
root->link_list_right->link_list_left=root->link_list_left;
root->link_list_left=root;
root->link_list_right=root;
}
//root's parent degree has to be reduced as one element has been taken out of the loop
root->parent->degree=root->parent->degree-1;
struct node *temp=root->parent;
root->parent=NULL;
meld(head,root);
if(head->val<root->val)
{
head=root;
}
root=temp;
} while(root!=NULL && root->parent!=NULL && root->child_cut!=false);
if(root!=NULL && root->parent!=NULL)
{
//make the last root accessed by the node to be true
root->child_cut=true;
}
}
}
return 0;
}
/*
int t;
int display_all(struct node *root,int i)
{
if(root==NULL)
{
return 0;
}
else
{
struct node *temp=root;
do
{
display_all(temp->link_child,i+1);
t++;
//std::cout<<temp->key<<temp->degree<<" "<<temp->val<<" "<<temp->child_cut<<" "<<i<<std::endl;
temp=temp->link_list_right;
}while(temp!=root);
return 0;
}
}*/
int main(int argc,char * args[])
{
fstream ofile;
ofstream ifile;
t=0;
ifile.open("output_file.txt");
if(!ifile)
{
std::cout<<"The File to write was not able to open";
return -1;
}
ofile.open(args[1]);
if(!ofile)
{
std::cout<<"The file can not be opened";
return -1;
}
while(!ofile.eof())
{
string temp;
getline(ofile,temp);
int flag=0;
int i=1;
if(temp=="stop"||temp=="STOP")
{
display_all(head,0);
std::cout<<t<<" "<<umap.size();
if(t==umap.size())
{
std::cout<<"sucess";
}
ifile.close();
ofile.close();
return 0;
}
if(temp[flag]=='#')
{
while(i<temp.length())
{
if(temp[i]==' ')
{
string temp1=temp.substr(flag+1,i-flag-1);//this is used to segregate the name from the string
int freq=atoi(temp.substr(i+1,temp.length()-1).c_str());
std::unordered_map<std::string,struct node *>::const_iterator got = umap.find (temp1);
if(got!=umap.end())
{
increasekey(got->second,freq);
++i;
}
else
{
struct node *temp=add(temp1,freq);
umap.insert(make_pair(temp1,temp));
}
}
++i;
}
}
else
{
vector<struct node *>saving;
saving.clear();
int freq=atoi(temp.substr(0,temp.length()).c_str());
while(freq--)
{
if(head!=NULL)
saving.push_back(remove_max());
}
int i=0;
while(i<saving.size())
{
ifile<<saving[i]->key;
//std::cout<<saving[i]->key<<","<<saving[i]->val<<",";
if(i<(saving.size()-1))
{
ifile<<",";
}
saving[i]->parent=NULL;
saving[i]->link_child=NULL;
saving[i]->child_cut=false;
saving[i]->degree=0;
saving[i]->link_list_right=saving[i];
saving[i]->link_list_left=saving[i];
if(head==NULL && saving[i]!=NULL)
{
head=saving[i];
}
else
{
if(saving[i]!=NULL)
{
meld(head,saving[i]);
if(saving[i]->val>head->val)
{
head=saving[i];
}
}
}
++i;
}
//std::cout<<"\n";
ifile<<"\n";
ifile.flush();
}
}
ifile.close();
ofile.close();
return 0;
}