Orbiter 2022
Combinatorial Objects
graph_node.cpp
Go to the documentation of this file.
1// graph_node.cpp
2//
3// Anton Betten
4// December 30, 2013
5//
6//
7//
8//
9//
10
11#include "foundations.h"
12
13
14using namespace std;
15
16
17namespace orbiter {
18namespace layer1_foundations {
19namespace graph_theory {
20
21
23{
24 null();
25}
26
28{
29 freeself();
30}
31
33{
34 label = NULL;
35 id = -1;
36 layer = -1;
38 data1 = -1;
40 data2 = -1;
42 data3 = -1;
44 vec_data = NULL;
45 vec_data_len = 0;
48 nb_neighbors = 0;
49 neighbor_list = NULL;
51 child_id = NULL;
52 nb_children = 0;
56 radius_factor = 1.;
57}
58
60{
61 if (label) {
63 }
64 if (neighbor_list) {
66 }
67 if (f_has_vec_data) {
69 }
70 if (child_id) {
72 }
73 null();
74}
75
76void graph_node::add_neighbor(int l, int n, int id)
77{
78 int i;
79
81 int new_neighbor_list_allocated;
82 int *new_neighbor_list;
83
85 new_neighbor_list_allocated = 2 * neighbor_list_allocated;
86 }
87 else {
88 new_neighbor_list_allocated = 16;
89 }
90 new_neighbor_list = NEW_int(new_neighbor_list_allocated);
91 for (i = 0; i < nb_neighbors; i++) {
92 new_neighbor_list[i] = neighbor_list[i];
93 }
94 if (neighbor_list) {
96 }
97 neighbor_list = new_neighbor_list;
98 neighbor_list_allocated = new_neighbor_list_allocated;
99 }
101 nb_neighbors++;
102}
103
104void graph_node::add_text(const char *text)
105{
106 int l;
107 char *p;
108
109 l = strlen(text);
110 p = NEW_char(l + 1);
111 strcpy(p, text);
112 if (label) {
114 }
115 label = p;
116}
117
118void graph_node::add_vec_data(long int *v, int len)
119{
120 vec_data = NEW_lint(len);
121 vec_data_len = len;
122 Lint_vec_copy(v, vec_data, len);
124}
125
127{
130}
131
132
134{
136 data1 = data;
137}
138
140{
142 data2 = data;
143}
144
146{
148 data3 = data;
149}
150
152 orbiter_kernel_system::memory_object *m, int verbose_level)
153{
154 int f_v = (verbose_level >= 1);
155 int i;
156
157 if (f_v) {
158 cout << "graph_node::write_memory_object" << endl;
159 }
160 if (label == NULL) {
161 m->write_string("");
162 }
163 else {
165 }
166 m->write_int(id);
168 m->write_int(data1);
170 m->write_int(data2);
172 m->write_int(data3);
174 if (f_has_vec_data) {
176 for (i = 0; i < vec_data_len; i++) {
177 m->write_lint(vec_data[i]);
178 }
179 }
182 m->write_int(layer);
184 for (i = 0; i < nb_neighbors; i++) {
186 }
189 if (f_v) {
190 cout << "graph_node::write_memory_object "
191 "finished, data size (in chars) = "
192 << m->used_length << endl;
193 }
194}
195
197 orbiter_kernel_system::memory_object *m, int verbose_level)
198{
199 int f_v = (verbose_level >= 1);
200 int i;
201
202 if (f_v) {
203 cout << "graph_node::read_memory_object" << endl;
204 }
205 m->read_string(label);
206 m->read_int(&id);
208 m->read_int(&data1);
210 m->read_int(&data2);
212 m->read_int(&data3);
214 if (f_has_vec_data) {
217 for (i = 0; i < vec_data_len; i++) {
218 m->read_lint(&vec_data[i]);
219 }
220 }
221 else {
222 vec_data_len = 0;
223 vec_data = NULL;
224 }
225
228
229
230 m->read_int(&layer);
233 for (i = 0; i < nb_neighbors; i++) {
234 m->read_int(&neighbor_list[i]);
235 }
236
239 if (f_v) {
240 cout << "graph_node::read_memory_object finished" << endl;
241 }
242}
243
245{
246 int f_v = (verbose_level >= 1);
247
248 if (f_v) {
249 cout << "graph_node::allocate_tree_structure" << endl;
250 }
251
252 nb_children = 0;
255
256 if (f_v) {
257 cout << "graph_node::allocate_tree_structure done" << endl;
258 }
259}
260
261int graph_node::remove_neighbor(layered_graph *G, int id, int verbose_level)
262{
263 int f_v = (verbose_level >= 1);
264 int i, j;
265
266 if (f_v) {
267 cout << "graph_node::remove_neighbor" << endl;
268 }
269
270 for (i = 0; i < nb_neighbors; i++) {
271 if (neighbor_list[i] == id) {
272 for (j = i + 1; j < nb_neighbors; j++) {
273 neighbor_list[j - 1] = neighbor_list[j];
274 }
275 nb_neighbors--;
276 return TRUE;
277 }
278 }
279
280 if (f_v) {
281 cout << "graph_node::remove_neighbor done" << endl;
282 }
283 return FALSE;
284}
285
286void graph_node::find_all_parents(layered_graph *G, std::vector<int> &All_Parents, int verbose_level)
287{
288 int f_v = (verbose_level >= 1);
289 int i, id = 0, l, n, my_layer;
290
291 if (f_v) {
292 cout << "graph_node::find_parent layer = " << layer << endl;
293 }
294
295 G->find_node_by_id(graph_node::id, my_layer, n);
296 if (f_v) {
297 cout << "graph_node::find_parent my_layer = " << my_layer << endl;
298 }
299
300 for (i = 0; i < nb_neighbors; i++) {
301 id = neighbor_list[i];
302 G->find_node_by_id(id, l, n);
303
304 if (f_v) {
305 cout << "graph_node::find_parent i=" << i << " / " << nb_neighbors
306 << " id=" << id << " l=" << l << " n=" << n << endl;
307 }
308
309 if (l < my_layer) {
310 All_Parents.push_back(id);
311 }
312 }
313
314 if (f_v) {
315 cout << "graph_node::find_parent done" << endl;
316 }
317}
318
319int graph_node::find_parent(layered_graph *G, int verbose_level)
320{
321 int f_v = (verbose_level >= 1);
322 int i, id = 0, l, n;
323
324 if (f_v) {
325 cout << "graph_node::find_parent" << endl;
326 }
327
328 for (i = 0; i < nb_neighbors; i++) {
329 id = neighbor_list[i];
330 G->find_node_by_id(id, l, n);
331 if (l < layer) {
332 if (f_v) {
333 cout << "graph_node::find_parent done" << endl;
334 }
335 return id;
336 }
337 }
338 cout << "graph_node::find_parent did not find "
339 "a parent node" << endl;
340 cout << "layer = " << layer << endl;
341 cout << "id = " << id << endl;
342 exit(1);
343}
344
346 int id_child, int verbose_level)
347{
348 int f_v = (verbose_level >= 1);
349 int l, n;
350
351 if (f_v) {
352 cout << "graph_node::register_child" << endl;
353 }
356 int *child_id_new;
357
358 child_id_new = NEW_int(nb_children_allocated);
359 Int_vec_copy(child_id, child_id_new, nb_children);
361 child_id = child_id_new;
362 }
363 child_id[nb_children++] = id_child;
364
365 G->find_node_by_id(id_child, l, n);
367
368 if (f_v) {
369 cout << "graph_node::register_child done" << endl;
370 }
371}
372
374 double left, double right, int verbose_level)
375{
376 int f_v = (verbose_level >= 1);
377 int i, w, w0, w1;
378 int id_child, l, n;
379 double lft, rgt;
380 double dx;
381
382 if (f_v) {
383 cout << "graph_node::place_x_based_on_tree" << endl;
384 }
385
386 x_coordinate = (left + right) * .5;
388 width = right - left;
389 dx = (double) width / (double) (w - 1);
390 // the node itself counts for the
391 // weight, so we subtract one
392 w0 = 0;
393
394 for (i = 0; i < nb_children; i++) {
395 id_child = child_id[i];
396 G->find_node_by_id(id_child, l, n);
397
398 w1 = G->L[l].Nodes[n].weight_of_subtree;
399 lft = left + (double)w0 * dx;
400 rgt = left + ((double)(w0 + w1)) * dx;
401
402 G->L[l].Nodes[n].place_x_based_on_tree(G,
403 lft, rgt, verbose_level);
404 w0 += w1;
405 }
406
407
408 if (f_v) {
409 cout << "graph_node::place_x_based_on_tree done" << endl;
410 }
411}
412
414 layered_graph *G, int &r, int verbose_level)
415{
416 int f_v = (verbose_level >= 1);
417 int i, id_child, l, n;
418
419 if (f_v) {
420 cout << "graph_node::depth_first_rank_recursion" << endl;
421 }
422
424
425 for (i = 0; i < nb_children; i++) {
426 id_child = child_id[i];
427 G->find_node_by_id(id_child, l, n);
428
429 G->L[l].Nodes[n].depth_first_rank_recursion(G, r, verbose_level);
430 }
431 if (f_v) {
432 cout << "graph_node::depth_first_rank_recursion done" << endl;
433 }
434}
435
436void graph_node::scale_x_coordinate(double x_stretch, int verbose_level)
437{
438 x_coordinate *= x_stretch;
439}
440
441
442
443}}}
444
445
446
void place_x_based_on_tree(layered_graph *G, double left, double right, int verbose_level)
Definition: graph_node.cpp:373
void depth_first_rank_recursion(layered_graph *G, int &r, int verbose_level)
Definition: graph_node.cpp:413
void read_memory_object(orbiter_kernel_system::memory_object *m, int verbose_level)
Definition: graph_node.cpp:196
void find_all_parents(layered_graph *G, std::vector< int > &All_Parents, int verbose_level)
Definition: graph_node.cpp:286
void write_memory_object(orbiter_kernel_system::memory_object *m, int verbose_level)
Definition: graph_node.cpp:151
void scale_x_coordinate(double x_stretch, int verbose_level)
Definition: graph_node.cpp:436
void register_child(layered_graph *G, int id_child, int verbose_level)
Definition: graph_node.cpp:345
int remove_neighbor(layered_graph *G, int id, int verbose_level)
Definition: graph_node.cpp:261
int find_parent(layered_graph *G, int verbose_level)
Definition: graph_node.cpp:319
a data structure to store layered graphs or Hasse diagrams
Definition: graph_theory.h:654
#define Lint_vec_copy(A, B, C)
Definition: foundations.h:694
#define FREE_int(p)
Definition: foundations.h:640
#define NEW_char(n)
Definition: foundations.h:632
#define NEW_int(n)
Definition: foundations.h:625
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define Int_vec_copy(A, B, C)
Definition: foundations.h:693
#define FREE_char(p)
Definition: foundations.h:646
#define FREE_lint(p)
Definition: foundations.h:642
#define NEW_lint(n)
Definition: foundations.h:628
the orbiter library for the classification of combinatorial objects