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
mst.c
Go to the documentation of this file.
1
14#include "mst.h"
15
16
17void create_mst(MST * mst, const Node * nodes, unsigned short num_nodes) {
18 mst->isValid = false;
19 mst->cost = 0;
20 mst->num_nodes = num_nodes;
21 mst->num_edges = 0;
22 mst->prob = 0;
23
24 for (unsigned short i = 0; i < num_nodes; i++) {
25 mst->nodes[i].positionInGraph = nodes[i].positionInGraph;
26 mst->nodes[i].x = nodes[i].x;
27 mst->nodes[i].y = nodes[i].y;
28 mst->nodes[i].num_neighbours = 0;
29
30 for (unsigned short j = i; j < num_nodes; j++) {
31 mst->edges_matrix[i][j] = -1;
32 mst->edges_matrix[j][i] = -1;
33 }
34 }
35}
36
37
38void add_edge(MST * tree, const Edge * edge){
39
40 unsigned short src = edge->src;
41 unsigned short dest = edge->dest;
42
43 tree->edges[tree->num_edges].src = src;
44 tree->edges[tree->num_edges].dest = dest;
45 tree->edges[tree->num_edges].weight = edge->weight;
46 tree->edges[tree->num_edges].symbol = edge->symbol;
47 tree->edges[tree->num_edges].prob = edge->prob;
48 tree->edges[tree->num_edges].positionInGraph = tree->num_edges;
49 tree->nodes[src].neighbours[tree->nodes[src].num_neighbours] = dest;
50 tree->nodes[src].num_neighbours++;
51 tree->nodes[dest].neighbours[tree->nodes[dest].num_neighbours] = src;
52 tree->nodes[dest].num_neighbours++;
53 tree->edges_matrix[src][dest] = (short) tree->num_edges;
54 tree->edges_matrix[dest][src] = (short) tree->num_edges;
55
56 tree->num_edges++;
57 tree->cost += edge->weight;
58
59 if(HYBRID){
60 if(tree->num_edges == 1){
61 tree->prob = edge->prob;
62 }
63 else{
64 tree->prob = ((tree->prob * ((float) tree->num_edges -1)) + edge->prob) / ((float) tree->num_edges);
65 }
66 }
67}
68
69
70void print_mst(const MST * tree){
71 printf("\nMST or 1-Tree with cost: %lf and validity = %s\n", tree->cost, tree->isValid ? "TRUE" : "FALSE");
72
73 double dim = (log(tree->num_nodes) / log(10) + 1) * 2 + 7;
74 for (unsigned short i = 0; i < tree->num_edges; i++) {
75 char edge_print [(int) dim] ;
76 char edge_print_dest [(int) (dim-7)/2] ;
77 const Edge * curr = &tree->edges[i];
78 sprintf(edge_print, "%i", curr->src);
79 strcat(edge_print, " <--> ");
80 sprintf(edge_print_dest, "%i", curr->dest);
81 strcat(edge_print, edge_print_dest);
82 printf("Edge%i:\t%s\tweight = %lf\tprob = %lf\n",
83 curr->symbol,
84 edge_print,
85 curr->weight,
86 curr->prob);
87 }
88
89}
90
91
92void print_mst_original_weight(const MST * tree, const Graph * graph){
93 printf("\nMST or 1-Tree with cost: %f and validity = %s\n", tree->cost, tree->isValid ? "TRUE" : "FALSE");
94
95 double dim = (log(tree->num_nodes) / log(10) + 1) * 2 + 7;
96 for (unsigned short i = 0; i < tree->num_edges; i++) {
97 char edge_print [(int) dim] ;
98 char edge_print_dest [(int) (dim-7)/2] ;
99 const Edge * curr = &tree->edges[i];
100 sprintf(edge_print, "%i", curr->src);
101 strcat(edge_print, " <--> ");
102 sprintf(edge_print_dest, "%i", curr->dest);
103 strcat(edge_print, edge_print_dest);
104 printf("Edge%i: %s weight = %f prob = %f\n",
105 curr->symbol,
106 edge_print,
107 graph->edges_matrix[curr->src][curr->dest].weight,
108 curr->prob);
109 }
110}
void add_edge(MST *tree, const Edge *edge)
Add an Edge to the MST.
Definition: mst.c:38
void print_mst(const MST *tree)
Print the MST, printing all the information it contains.
Definition: mst.c:70
void create_mst(MST *mst, const Node *nodes, unsigned short num_nodes)
Create a Minimum Spanning Tree from a set of Nodes.
Definition: mst.c:17
void print_mst_original_weight(const MST *tree, const Graph *graph)
Print the MST, printing all the information it contains.
Definition: mst.c:92
This file contains the declaration of the Minimum Spanning Tree datastructure.
Structure of an Edge.
Definition: graph.h:40
unsigned short dest
ID of the destination vertex.
Definition: graph.h:42
unsigned short src
ID of the source vertex.
Definition: graph.h:41
double prob
Probability of the Edge to be in an optimal tour.
Definition: graph.h:45
double weight
Weight of the Edge, 1 if the data_structures is not weighted.
Definition: graph.h:44
unsigned short symbol
Symbol of the Edge, i.e. its unique ID.
Definition: graph.h:43
unsigned short positionInGraph
Position of the Edge in the list of Edges of the Graph.
Definition: graph.h:46
Structure of a Graph.
Definition: graph.h:51
Edge edges_matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]
Adjacency matrix of the Graph.
Definition: graph.h:59
Minimum Spanning Tree, or MST, and also a 1-Tree.
Definition: mst.h:28
unsigned short num_nodes
The number of Nodes in the MST.
Definition: mst.h:32
short edges_matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]
-1 if there is no Edge between the two Nodes, otherwise the index of the Edge in the MST.
Definition: mst.h:36
double prob
The probability of the MST, i.e. the average of the probabilities of its Edges.
Definition: mst.h:31
Edge edges[MAX_VERTEX_NUM]
The set of Edges in the MST, these are |V| because the MST can be a 1-Tree.
Definition: mst.h:35
Node nodes[MAX_VERTEX_NUM]
The set of Nodes in the MST.
Definition: mst.h:34
unsigned short num_edges
The number of Edges in the MST.
Definition: mst.h:33
bool isValid
True if the MST has the correct number of Edges, false otherwise.
Definition: mst.h:29
double cost
The total cost of the MST, i.e. the sum of the weights of the Edges.
Definition: mst.h:30
Structure of a Node.
Definition: graph.h:30
double x
x coordinate of the Node.
Definition: graph.h:31
double y
y coordinate of the Node.
Definition: graph.h:32
unsigned short num_neighbours
Number of neighbours of the Node.
Definition: graph.h:34
unsigned short positionInGraph
Position of the Node in the list of Nodes of the Graph, i.e. its unique ID.
Definition: graph.h:33
unsigned short neighbours[MAX_VERTEX_NUM - 1]
Array of IDs of the Node's neighbors.
Definition: graph.h:35