Graph Convolutional Branch and Bound v1.0.0
A TSP solver that combines a graph convolutional network with a 1-Tree branch-and-bound.
Loading...
Searching...
No Matches
fibonacci_heap.c
Go to the documentation of this file.
1
13#include "fibonacci_heap.h"
14
15
17 heap->min_root = NULL;
18 heap->head_tree_list = NULL;
19 heap->tail_tree_list = NULL;
20 heap->num_nodes = 0;
21 heap->num_trees = 0;
22}
23
24
25void create_node(OrdTreeNode * node, unsigned short key, double value){
26 node->value = value;
27 node->key = key;
28 node->parent = NULL;
29 node->left_sibling = node;
30 node->right_sibling = node;
31
32 node->head_child_list = NULL;
33 node->tail_child_list = NULL;
34 node->num_children = 0;
35}
36
37
39
40 node->marked = false;
41 node->is_root = true;
42
43 if(heap->num_trees == 0){
44 heap->min_root = node;
45 heap->head_tree_list = node;
46 heap->tail_tree_list = node;
47 }
48 else{
49 if(node->value < heap->min_root->value){
50 heap->min_root = node;
51 }
52 heap->tail_tree_list->right_sibling = node;
53 heap->head_tree_list->left_sibling = node;
54 node->left_sibling = heap->tail_tree_list;
55 node->right_sibling = heap->head_tree_list;
56 heap->tail_tree_list = node;
57 }
58 heap->num_nodes++;
59 heap->num_trees++;
60}
61
62
63void create_insert_node(FibonacciHeap * heap, OrdTreeNode * node, unsigned short key, double value){
64 create_node(node, key, value);
65 insert_node(heap, node);
66}
67
68
69void link_trees(FibonacciHeap * heap, OrdTreeNode * child, OrdTreeNode * father){
70 child->marked = false;
73 child->is_root = false;
74 heap->num_trees--;
75
76 child->parent = father;
77 father->num_children++;
78 if(father->num_children == 1){
79 father->head_child_list = child;
80 father->tail_child_list = child;
81 child->left_sibling = child;
82 child->right_sibling = child;
83 }
84 else{
85 father->tail_child_list->right_sibling = child;
86 father->head_child_list->left_sibling = child;
87 child->left_sibling = father->tail_child_list;
88 child->right_sibling = father->head_child_list;
89 father->tail_child_list = child;
90 }
91
92}
93
94
95void swap_roots(FibonacciHeap * heap, OrdTreeNode * node1, OrdTreeNode * node2){
96 if(node1->key == node2->key){
97 return;
98 }
99 else{
100
101 OrdTreeNode * node1_left = node1->left_sibling;
102 OrdTreeNode * node1_right = node1->right_sibling;
103 OrdTreeNode * node2_left = node2->left_sibling;
104 OrdTreeNode * node2_right = node2->right_sibling;
105
106 node1->left_sibling = node2_left;
107 node1->right_sibling = node2_right;
108 node2->left_sibling = node1_left;
109 node2->right_sibling = node1_right;
110 }
111}
112
113
115
116 int dimension = ((int) ceil(log(heap->num_nodes) / log(2))) + 2;
117 OrdTreeNode * degree_list [dimension] ;
118
119 for (int i = 0; i < dimension; i++) {
120 degree_list[i] = NULL;
121 }
122
123 OrdTreeNode * iter = heap->head_tree_list;
124 int num_it = heap->num_trees;
125
126 for (int i = 0; i < num_it; i++) {
127 OrdTreeNode * deg_i_node = iter;
128 unsigned short deg = deg_i_node->num_children;
129 iter = iter->right_sibling;
130
131 while(degree_list[deg]!=NULL){
132
133 OrdTreeNode * same_deg_node = degree_list[deg];
134
135 if(deg_i_node->value > same_deg_node->value){
136 OrdTreeNode * temp = deg_i_node;
137 deg_i_node = same_deg_node;
138 same_deg_node = temp;
139 //iter = deg_i_node;
140 }
141
142 link_trees(heap, same_deg_node, deg_i_node);
143 // since same_deg_node is now a child of deg_i_node, it is no longer a root
144 degree_list[deg] = NULL;
145 deg++;
146 }
147 degree_list[deg] = deg_i_node;
148 }
149
150 heap->min_root = NULL;
151
152 for (int i = 0; i < dimension; i++) {
153 if(degree_list[i]!=NULL){
154 if(heap->min_root == NULL){
155 heap->min_root = degree_list[i];
156 heap->head_tree_list = degree_list[i];
157 heap->tail_tree_list = degree_list[i];
158 }
159 else{
160 if(heap->min_root->value > degree_list[i]->value){
161 heap->min_root = degree_list[i];
162 }
163 heap->tail_tree_list->right_sibling = degree_list[i];
164 heap->head_tree_list->left_sibling = degree_list[i];
165 degree_list[i]->left_sibling = heap->tail_tree_list;
166 degree_list[i]->right_sibling = heap->head_tree_list;
167 heap->tail_tree_list = degree_list[i];
168 }
169 }
170 }
171
172}
173
174
176
177 int min_pos;
178
179 if (heap->min_root != NULL) {
180
181 min_pos = heap->min_root->key;
182
183 OrdTreeNode *child = heap->min_root->head_child_list;
184 unsigned short num_children = heap->min_root->num_children;
185
186 for (unsigned short i = 0; i < num_children; i++) {
187 child->parent = NULL;
188 child->is_root = true;
189 child->marked = false;
190
191 if(num_children - i == 1){
192 heap->min_root->head_child_list = NULL;
193 heap->min_root->tail_child_list = NULL;
194 }
195 else{
196 if(heap->min_root->head_child_list->key == child->key){
197 heap->min_root->head_child_list = child->right_sibling;
198 }
199 if(heap->min_root->tail_child_list->key == child->key){
200 heap->min_root->tail_child_list = child->left_sibling;
201 }
203 child->right_sibling->left_sibling = child->left_sibling;
204 }
205
206 heap->tail_tree_list->right_sibling = child;
207 heap->head_tree_list->left_sibling = child;
208 child->left_sibling = heap->tail_tree_list;
209 child->right_sibling = heap->head_tree_list;
210 heap->tail_tree_list = child;
211
212 heap->num_trees++;
213
214 child = heap->min_root->head_child_list;
215 heap->min_root->num_children--;
216 }
217
218 heap->num_trees--;
219
220 if(heap->head_tree_list->key == heap->min_root->key){
222 }
223 if(heap->tail_tree_list->key == heap->min_root->key){
225 }
226
229
230 if (heap->min_root->key == heap->min_root->right_sibling->key) {
231 // the min root is the only tree in the heap, and it has no children, so the heap is now empty
232 heap->min_root = NULL;
233 heap->tail_tree_list = NULL;
234 heap->head_tree_list = NULL;
235 heap->num_trees = 0;
236 }
237 else{
238 // choose a new min root, arbitrarily
239 heap->min_root = heap->min_root->right_sibling;
240 // re-consolidate the heap
241 consolidate(heap);
242 }
243 heap->num_nodes--;
244 } else{
245 min_pos = -1;
246 }
247 return min_pos;
248}
249
250
251static void cut(FibonacciHeap * heap, OrdTreeNode * node, OrdTreeNode * parent){
252 node->parent = NULL;
253 node->is_root = true;
254 node->marked = false;
255 parent->num_children--;
256 if(parent->num_children == 0){
257 parent->head_child_list = NULL;
258 parent->tail_child_list = NULL;
259 }
260 else{
261 if(parent->head_child_list->key == node->key){
262 parent->head_child_list = node->right_sibling;
263 }
264 if(parent->tail_child_list->key == node->key){
265 parent->tail_child_list = node->left_sibling;
266 }
269 }
270
271 heap->tail_tree_list->right_sibling = node;
272 heap->head_tree_list->left_sibling = node;
273 node->left_sibling = heap->tail_tree_list;
274 node->right_sibling = heap->head_tree_list;
275 heap->tail_tree_list = node;
276 heap->num_trees++;
277}
278
279
280static void cascading_cut(FibonacciHeap * heap, OrdTreeNode * node){
281 if(node->parent != NULL){
282 if(!node->marked){
283 node->marked = true;
284 }
285 else{
286 OrdTreeNode * parent = node->parent;
287 cut(heap, node, parent);
288 cascading_cut(heap, parent);
289 }
290 }
291}
292
293
294void decrease_value(FibonacciHeap * heap, OrdTreeNode * node, double new_value){
295 if(new_value > node->value){
296 fprintf(stderr, "Error: new value is greater than current value\n");
297 exit(EXIT_FAILURE);
298 }
299 else{
300 node->value = new_value;
301 if(node->parent != NULL && node->value < node->parent->value){
302 OrdTreeNode * parent = node->parent;
303 cut(heap, node, node->parent);
304 cascading_cut(heap, parent);
305 }
306 if(node->value < heap->min_root->value){
307 heap->min_root = node;
308 }
309 }
310}
311
313 decrease_value(heap, node, -FLT_MAX);
314 extract_min(heap);
315}
void link_trees(FibonacciHeap *heap, OrdTreeNode *child, OrdTreeNode *father)
void insert_node(FibonacciHeap *heap, OrdTreeNode *node)
Insert a Node in the Fibonacci Heap.
void create_insert_node(FibonacciHeap *heap, OrdTreeNode *node, unsigned short key, double value)
A wrapper function to create a Node and insert it in the Fibonacci Heap.
void consolidate(FibonacciHeap *heap)
static void cascading_cut(FibonacciHeap *heap, OrdTreeNode *node)
int extract_min(FibonacciHeap *heap)
Extract the minimum Node from the Fibonacci Heap.
void create_fibonacci_heap(FibonacciHeap *heap)
Create an empty Fibonacci Heap.
void delete_node(FibonacciHeap *heap, OrdTreeNode *node)
static void cut(FibonacciHeap *heap, OrdTreeNode *node, OrdTreeNode *parent)
void create_node(OrdTreeNode *node, unsigned short key, double value)
Create a Node with a given key and value.
void swap_roots(FibonacciHeap *heap, OrdTreeNode *node1, OrdTreeNode *node2)
void decrease_value(FibonacciHeap *heap, OrdTreeNode *node, double new_value)
Decrease the value of a Node in the Fibonacci Heap.
This file contains the declaration of the Fibonacci Heap datastructure for the Minimum Spanning Tree ...
The Fibonacci Heap datastructure as collection of Heap-ordered Trees.
OrdTreeNode * min_root
The root of the Heap-ordered Tree with the minimum value.
OrdTreeNode * tail_tree_list
The root of the tail Tree in the Fibonacci Heap.
unsigned short num_trees
The number of Trees in the Heap.
OrdTreeNode * head_tree_list
The root of the head Tree in the Fibonacci Heap.
unsigned short num_nodes
The number of Nodes in the Heap.
A Heap-ordered Tree Node where the key of the parent is <= the key of its children.
struct OrdTreeNode * right_sibling
The right sibling of the Node.
bool marked
True if the Node has lost a child, false otherwise.
unsigned short num_children
The number of children of the Node.
struct OrdTreeNode * head_child_list
The head of the list of children of the Node.
struct OrdTreeNode * left_sibling
The left sibling of the Node.
unsigned short key
The key of the Node.
bool is_root
True if the Node is a root, false otherwise.
struct OrdTreeNode * parent
The parent of the Node.
double value
The value of the Node.
struct OrdTreeNode * tail_child_list
The tail of the list of children of the Node.