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
graph.c
Go to the documentation of this file.
1
14#include "graph.h"
16
17
18void create_graph(Graph * graph, List *nodes_list, List *edges_list, GraphKind kind) {
19 graph->kind = kind;
20 graph->num_edges = 0;
21 graph->num_nodes = 0;
22 graph->orderedEdges = false;
23 graph->cost = 0;
24
25 ListIterator *nodes_iterator = create_list_iterator(nodes_list);
26 unsigned short numNodes = 0;
27 for (size_t j = 0; j < nodes_list->size; j++) {
28 Node *curr = list_iterator_get_next(nodes_iterator);
29 graph->nodes[numNodes].positionInGraph = numNodes;
30 graph->nodes[numNodes].num_neighbours = 0;
31 graph->nodes[numNodes].y = curr->y;
32 graph->nodes[numNodes].x = curr->x;
33 graph->num_nodes++;
34 numNodes++;
35 }
36 delete_list_iterator(nodes_iterator);
37
38 unsigned short numEdges = 0;
39 ListIterator *edges_iterator = create_list_iterator(edges_list);
40 for (size_t i = 0; i < edges_list->size ; i++) {
41 //add the source vertex to the data_structures
42 Edge * current_edge = list_iterator_get_next(edges_iterator);
43 unsigned short src = current_edge->src;
44 unsigned short dest = current_edge->dest;
45
46 graph->edges[numEdges].dest = dest;
47 graph->edges[numEdges].src = src;
48 graph->edges[numEdges].prob = current_edge->prob;
49 graph->edges[numEdges].weight = current_edge->weight;
50 graph->edges[numEdges].symbol = current_edge->symbol;
51 graph->edges[numEdges].positionInGraph = numEdges;
52
53 graph->nodes[src].neighbours[graph->nodes[src].num_neighbours] = dest;
54 graph->nodes[src].num_neighbours++;
55 graph->edges_matrix[src][dest] = graph->edges[numEdges];
56 graph->cost += current_edge->weight;
57
58 graph->edges_matrix[dest][src] = graph->edges_matrix[src][dest];
59 graph->nodes[dest].neighbours[graph->nodes[dest].num_neighbours] = src;
60 graph->nodes[dest].num_neighbours++;
61
62 numEdges++;
63 graph->num_edges++;
64 }
65 delete_list_iterator(edges_iterator);
66 del_list(edges_list);
67 del_list(nodes_list);
68}
69
70
71void create_euclidean_graph(Graph * graph, List *nodes) {
72 List *edges_list = new_list();
73
74 unsigned short z = 0;
75 Edge edges [MAX_EDGES_NUM];
76 ListIterator *i_nodes_iterator = create_list_iterator(nodes);
77 for (size_t i = 0; i < nodes->size; i++) {
78 Node *node_src = list_iterator_get_next(i_nodes_iterator);
79 for (size_t j = i + 1; j < nodes->size; j++) {
80 Node *node_dest = get_list_elem_index(nodes,j);
81
82 edges[z].src = node_src->positionInGraph;
83 edges[z].dest = node_dest->positionInGraph;
84 edges[z].symbol = z + 1;
85 edges[z].positionInGraph = z;
86 edges[z].prob = 0;
87 edges[z].weight = (float) sqrt(pow(fabs(node_src->x - node_dest->x), 2) +
88 pow(fabs(node_src->y - node_dest->y), 2));
89 add_elem_list_bottom(edges_list, &edges[z]);
90 z++;
91
92 }
93
94 }
95 delete_list_iterator(i_nodes_iterator);
96
97 create_graph(graph,nodes, edges_list, WEIGHTED_GRAPH);
98}
99
100
101void print_graph(const Graph *G) {
102 printf("Nodes: %i\n", G->num_nodes);
103 for (int i = 0; i < G->num_nodes; i++) {
104 Node curr = G->nodes[i];
105 printf("Node%i:\t(%.*f, %.*f)\t%i neighbours: ", curr.positionInGraph, DBL_DIG-1, curr.x, DBL_DIG-1, curr.y, curr.num_neighbours);
106
107 for (int z = 0; z < curr.num_neighbours; z++) {
108 printf("%i ", G->nodes[curr.neighbours[z]].positionInGraph);
109 }
110 printf("\n");
111 }
112
113 printf("\nCost: %lf\n", G->cost);
114 printf("\nEdges: %i\n", G->num_edges);
115
116 double dim = (log(G->num_nodes) / log(10) + 1) * 2 + 7;
117 for (unsigned short j = 0; j < G->num_edges; j++) {
118 char edge_print [(int) dim] ;
119 char edge_print_dest [(int) (dim-7)/2] ;
120 Edge curr = G->edges[j];
121 sprintf(edge_print, "%i", curr.src);
122 strcat(edge_print, " <--> ");
123 sprintf(edge_print_dest, "%i", curr.dest);
124 strcat(edge_print, edge_print_dest);
125
126 printf("Edge%i:\t%s\tweight = %.*f\tprob = %.*f\n",
127 curr.symbol,
128 edge_print,
129 DBL_DIG-1,
130 curr.weight,
131 DBL_DIG-1,
132 curr.prob);
133 }
134
135}
void print_graph(const Graph *G)
Print Nodes, Edges and other information of the Graph.
Definition: graph.c:101
void create_graph(Graph *graph, List *nodes_list, List *edges_list, GraphKind kind)
Create a new instance of a Graph with all the needed parameters.
Definition: graph.c:18
void create_euclidean_graph(Graph *graph, List *nodes)
Create a new instance of an euclidean graphs only the Nodes are necessary.
Definition: graph.c:71
The data structures to model the Graph.
GraphKind
Enum to specify the kind of the Graph.
Definition: graph.h:23
@ WEIGHTED_GRAPH
The Graph is weighted.
Definition: graph.h:24
void add_elem_list_bottom(List *list, void *element)
Adds an DllElem to the bottom of the List.
void del_list(List *list)
Delete an instance of a List.
List * new_list(void)
Create a new instance of a List.
void * get_list_elem_index(List *list, size_t index)
Retrieves a pointer to an DllElem from the List.
void * list_iterator_get_next(ListIterator *iterator)
Method that retrieves the current DllElem of an ListIterator and moves the pointer to the next object...
Definition: list_iterator.c:56
ListIterator * create_list_iterator(List *list)
Used for the creation of a new ListIterator.
Definition: list_iterator.c:18
void delete_list_iterator(ListIterator *iterator)
Delete the ListIterator given.
Definition: list_iterator.c:51
The declaration of the functions to manipulate the ListIterator.
#define MAX_EDGES_NUM
The maximum number of edges in the Graph.
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
unsigned short num_edges
Number of Edges in the Graph.
Definition: graph.h:55
bool orderedEdges
True if the Edges are ordered by weight, false otherwise.
Definition: graph.h:56
Edge edges[MAX_EDGES_NUM]
Array of Edges.
Definition: graph.h:58
GraphKind kind
Type of the Graph.
Definition: graph.h:52
Node nodes[MAX_VERTEX_NUM]
Array of Nodes.
Definition: graph.h:57
double cost
Sum of the weights of the Edges in the Graph.
Definition: graph.h:53
unsigned short num_nodes
Number of Nodes in the Graph.
Definition: graph.h:54
The iterator for the List.
Definition: linked_list.h:43
The double linked list.
Definition: linked_list.h:35
size_t size
The current size of the List.
Definition: linked_list.h:38
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