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
b_and_b_data.c
Go to the documentation of this file.
1
14#include "b_and_b_data.h"
15
16
18 list->size = 0;
19 list->head = list->tail = NULL;
20}
21
22
24 if (!list) {
25 return;
26 }
27
28 SubProblemElem *current = list->head;
29 SubProblemElem *next;
30
31 while (current) {
32 next = current->next;
33 free(current);
34 current = next;
35 }
36}
37
38
40 return (list == NULL || list->size == 0);
41}
42
43
45 SubProblemElem *e = malloc(sizeof(SubProblemElem));
46 e->subProblem = *value;
47 e->next = next;
48 e->prev = prev;
49
50 return e;
51}
52
53
55 return (list != NULL) ? list->size : 0;
56}
57
58
60 if (list == NULL) {
61 return;
62 }
63
64 SubProblemElem *e = build_list_elem(element, NULL, list->tail);
65
67 list->head = e;
68 else
69 list->tail->next = e;
70 list->tail = e;
71 list->size++;
72}
73
74
75void add_elem_SubProblemList_index(SubProblemsList *list, SubProblem *element, size_t index){
77 if (!list || index > get_SubProblemList_size(list)) {
78 return;
79 }
80
81// support element is a temporary pointer which avoids losing data
83 SubProblemElem *supp = list->head;
84
85 for (size_t i = 0; i < index; ++i)
86 supp = supp->next;
87
88 if (supp == list->head) {
89 e = build_list_elem(element, supp, NULL);
90
91 if (supp == NULL) {
92 list->head = list->tail = e;
93 } else {
94// e->next->prev = e;
95 list->head->prev = e;
96 list->head = e;
97 }
98 } else {
99 if (supp == NULL) {
100 e = build_list_elem(element, NULL, list->tail);
101 list->tail->next = e;
102 } else {
103 e = build_list_elem(element, supp, supp->prev);
104 e->next->prev = e;
105 e->prev->next = e;
106 }
107 }
108
109 list->size++;
110}
111
112
114 if (list == NULL || is_SubProblemList_empty(list) || index >= get_SubProblemList_size(list)) {
115 return;
116 }
117
118 SubProblemElem *oldElem;
119 oldElem = list->head;
120
121 for (size_t i = 0; i < index; ++i)
122 oldElem = oldElem->next;
123
124 // Found index to remove!!
125 if (oldElem != list->head) {
126 oldElem->prev->next = oldElem->next;
127 if (oldElem->next != NULL) {
128 oldElem->next->prev = oldElem->prev;
129 } else {
130 list->tail = oldElem->prev;
131 }
132 } else {
133 if (list->head == list->tail) {
134 list->head = list->tail = NULL;
135 } else {
136 list->head = list->head->next;
137 list->head->prev = NULL;
138 }
139 }
140
141 free(oldElem);
142 list->size--;
143}
144
145
147 if (list == NULL || index >= get_SubProblemList_size(list)) {
148 return NULL;
149 }
150
151 SubProblemElem *supp; // iteration support element
152 supp = list->head;
153
154 for (size_t i = 0; i < index; ++i)
155 supp = supp->next;
156 return &supp->subProblem;
157}
158
160 if (!list)
161 return NULL;
162
163 SubProblemsListIterator *new_iterator = malloc(sizeof(SubProblemsListIterator));
164 new_iterator->list = list;
165 new_iterator->curr = new_iterator->list->head;
166 new_iterator->index = 0;
167 return new_iterator;
168}
169
171 return (iterator) ? iterator->index < get_SubProblemList_size(iterator->list) : 0;
172}
173
175 return (iterator && iterator->curr) ? &iterator->curr->subProblem : NULL;
176}
177
179 if (is_SubProblemList_iterator_valid(iterator)) {
180 iterator->index++;
181
182 if (is_SubProblemList_iterator_valid(iterator)) {
183 iterator->curr = iterator->curr->next;
184 }
185 }
186}
187
189 if (!is_SubProblemList_iterator_valid(iterator)) {
190 return NULL;
191 }
192
195 return element;
196}
197
199 free(iterator);
200}
void add_elem_SubProblemList_index(SubProblemsList *list, SubProblem *element, size_t index)
Add a SubProblem at a specific index of a SubProblem List.
Definition: b_and_b_data.c:75
SubProblemElem * build_list_elem(SubProblem *value, SubProblemElem *next, SubProblemElem *prev)
Definition: b_and_b_data.c:44
void list_openSubProblemList_next(SubProblemsListIterator *iterator)
Definition: b_and_b_data.c:178
bool is_SubProblemList_iterator_valid(SubProblemsListIterator *iterator)
Check if a SubProblem List iterator is valid.
Definition: b_and_b_data.c:170
SubProblem * get_SubProblemList_elem_index(SubProblemsList *list, size_t index)
Get a SubProblem from a specific index of a SubProblem List.
Definition: b_and_b_data.c:146
void delete_SubProblemList_iterator(SubProblemsListIterator *iterator)
Delete a SubProblem List iterator.
Definition: b_and_b_data.c:198
size_t get_SubProblemList_size(SubProblemsList *list)
Get the size of a SubProblem List.
Definition: b_and_b_data.c:54
void delete_SubProblemList_elem_index(SubProblemsList *list, size_t index)
Remove a SubProblem from a specific index of a SubProblem List.
Definition: b_and_b_data.c:113
void add_elem_SubProblemList_bottom(SubProblemsList *list, SubProblem *element)
Add a SubProblem to the bottom of a SubProblem List.
Definition: b_and_b_data.c:59
SubProblemsListIterator * create_SubProblemList_iterator(SubProblemsList *list)
Create a new SubProblem List iterator on a SubProblem List.
Definition: b_and_b_data.c:159
void new_SubProblemList(SubProblemsList *list)
Create a new SubProblem List.
Definition: b_and_b_data.c:17
bool is_SubProblemList_empty(SubProblemsList *list)
Check if a SubProblem List is empty.
Definition: b_and_b_data.c:39
void delete_SubProblemList(SubProblemsList *list)
Delete a SubProblem List.
Definition: b_and_b_data.c:23
SubProblem * get_current_openSubProblemList_iterator_element(SubProblemsListIterator *iterator)
Definition: b_and_b_data.c:174
SubProblem * SubProblemList_iterator_get_next(SubProblemsListIterator *iterator)
Get the next element of a SubProblem List iterator.
Definition: b_and_b_data.c:188
The data structures used in the Branch and Bound algorithm.
The element of the list of SubProblems.
Definition: b_and_b_data.h:79
SubProblem subProblem
The SubProblem.
Definition: b_and_b_data.h:80
struct SubProblemElem * next
The next element of the list.
Definition: b_and_b_data.h:81
struct SubProblemElem * prev
The previous element of the list.
Definition: b_and_b_data.h:82
The struct used to represent a SubProblem or node of the Branch and Bound tree.
Definition: b_and_b_data.h:42
The iterator of the list of SubProblems.
Definition: b_and_b_data.h:95
size_t index
The index of the current element of the list.
Definition: b_and_b_data.h:98
SubProblemsList * list
The list to iterate.
Definition: b_and_b_data.h:96
SubProblemElem * curr
The current element of the list.
Definition: b_and_b_data.h:97
The list of open SubProblems.
Definition: b_and_b_data.h:87
SubProblemElem * tail
The tail of the list.
Definition: b_and_b_data.h:89
SubProblemElem * head
The head of the list.
Definition: b_and_b_data.h:88
size_t size
The size of the list.
Definition: b_and_b_data.h:90