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
mfset.c
Go to the documentation of this file.
1
14#include "mfset.h"
15
16
17void create_forest_constrained(Forest *forest, const Node *nodes, unsigned short num_nodes, unsigned short candidateId) {
18 forest->num_sets = num_nodes - 1;
19
20 for (unsigned short i = 0; i < num_nodes; i++) {
21 if (i != candidateId) {
22 forest->sets[i].parentSet = NULL;
23 forest->sets[i].rango = 0;
24 forest->sets[i].curr = nodes[i];
25 forest->sets[i].num_in_forest = i;
26 }
27 }
28}
29
30
31void create_forest(Forest *forest, const Node *nodes, unsigned short num_nodes) {
32
33 forest->num_sets = num_nodes;
34 for (unsigned short i = 0; i < num_nodes; i++) {
35 forest->sets[i].parentSet = NULL;
36 forest->sets[i].rango = 0;
37 forest->sets[i].curr = nodes[i];
38 forest->sets[i].num_in_forest = i;
39 }
40
41}
42
43
44Set *find(Set *set) {
45 if (set->parentSet != NULL) {
46 set->parentSet = find(set->parentSet);
47 return set->parentSet;
48 }
49 return set;
50}
51
52
53void merge(Set *set1, Set *set2) {
54
55 Set *set1_root = find(set1);
56 Set *set2_root = find(set2);
57
58 //printf("\nThe root are %.2fd ,%d\n", set1_root->num_in_forest, set2_root->num_in_forest);
59 if (set1_root->num_in_forest != set2_root->num_in_forest) {
60 if (set1_root->rango > set2_root->rango) {
61 set2_root->parentSet = set1_root;
62 } else if (set1_root->rango < set2_root->rango) {
63 set1_root->parentSet = set2_root;
64 } else {
65 set2_root->parentSet = set1_root;
66 set1_root->rango++;
67 }
68 }
69}
70
71
72void print_forest(const Forest *forest) {
73 for (unsigned short i = 0; i < forest->num_sets; i++) {
74 Set set = forest->sets[i];
75
76 printf("Set %i: ", set.curr.positionInGraph);
77 if (set.parentSet != NULL) {
78 printf("Parent: %i, ", set.parentSet->curr.positionInGraph);
79 } else {
80 printf("Parent: NULL, ");
81 }
82 printf("Rango: %d, ", set.rango);
83 printf("Num in forest: %d\n", set.num_in_forest);
84
85 }
86}
void merge(Set *set1, Set *set2)
Merge two Sets in the Forest if they are not already in the same Set.
Definition: mfset.c:53
Set * find(Set *set)
Find the root of a Set.
Definition: mfset.c:44
void create_forest(Forest *forest, const Node *nodes, unsigned short num_nodes)
Create a new Forest with n Sets, each Set containing a Node, without constraints.
Definition: mfset.c:31
void create_forest_constrained(Forest *forest, const Node *nodes, unsigned short num_nodes, unsigned short candidateId)
Create a new Forest with n Sets, each Set containing a Node, with constraints.
Definition: mfset.c:17
void print_forest(const Forest *forest)
Print all the Forest.
Definition: mfset.c:72
This file contains the declaration of the Merge-Find Set datastructure for the Minimum Spanning Tree ...
A Forest is a list of Sets.
Definition: mfset.h:28
Set sets[MAX_VERTEX_NUM]
Array of Sets.
Definition: mfset.h:30
unsigned short num_sets
Number of Sets in the Forest.
Definition: mfset.h:29
Structure of a Node.
Definition: graph.h:30
unsigned short positionInGraph
Position of the Node in the list of Nodes of the Graph, i.e. its unique ID.
Definition: graph.h:33
A Set is a node in the Forest.
Definition: mfset.h:19
struct Set * parentSet
Pointer to the parent Set in a tree representation of the Forest.
Definition: mfset.h:20
Node curr
Current Node.
Definition: mfset.h:22
unsigned short num_in_forest
Number of the position of the Set in the Forest.
Definition: mfset.h:23
unsigned short rango
Rank of the Set, used to optimize the find operation.
Definition: mfset.h:21