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
prim.c
Go to the documentation of this file.
1
14#include "prim.h"
15
16void prim(const Graph * graph, MST * mst){
17 create_mst(mst, graph->nodes, graph->num_nodes);
18 FibonacciHeap heap;
20
21 OrdTreeNode tree_nodes[graph->num_nodes];
22 bool in_heap [graph->num_nodes];
23 int fathers [graph->num_nodes];
24 bool start = true;
25
26 for (unsigned short i = 0; i < graph->num_nodes; i++) {
27 if(start){
28 create_insert_node(&heap, &tree_nodes[i], i, 0);
29 start = false;
30 } else{
31 create_insert_node(&heap, &tree_nodes[i], i, DBL_MAX);
32 }
33 in_heap[i] = true;
34 fathers[i] = -1;
35 }
36
37 while(heap.num_nodes != 0){
38 int min_pos = extract_min(&heap);
39 if(min_pos == -1){
40 fprintf(stderr, "Error: min_pos == -1\n");
41 exit(1);
42 }
43 else{
44 in_heap[min_pos] = false;
45 for(unsigned short i = 0; i < graph->nodes[min_pos].num_neighbours; i++){
46 unsigned short neigh = graph->nodes[min_pos].neighbours[i];
47 if(neigh != min_pos && in_heap[neigh]) {
48 double weight = graph->edges_matrix[min_pos][neigh].weight;
49 if (weight < tree_nodes[neigh].value) {
50 fathers[neigh] = min_pos;
51 decrease_value(&heap, &tree_nodes[neigh], weight);
52 }
53 }
54 }
55 }
56 }
57
58 for(unsigned short i = 0; i < graph->num_nodes; i++){
59 if(fathers[i] != -1){
60 add_edge(mst, &graph->edges_matrix[i][fathers[i]]);
61 }
62 }
63
64 if(mst->num_edges == graph->num_nodes - 1){
65 mst->isValid = true;
66 }
67 else{
68 mst->isValid = false;
69 }
70}
void create_insert_node(FibonacciHeap *heap, OrdTreeNode *node, unsigned short key, double value)
A wrapper function to create a Node and insert it in the Fibonacci Heap.
int extract_min(FibonacciHeap *heap)
Extract the minimum Node from the Fibonacci Heap.
void create_fibonacci_heap(FibonacciHeap *heap)
Create an empty Fibonacci Heap.
void decrease_value(FibonacciHeap *heap, OrdTreeNode *node, double new_value)
Decrease the value of a Node in the Fibonacci Heap.
void add_edge(MST *tree, const Edge *edge)
Add an Edge to the MST.
Definition: mst.c:38
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 prim(const Graph *graph, MST *mst)
The Prim algorithm to find the Minimum Spanning Tree O(|E| + |V| log |V|)
Definition: prim.c:16
The declaration of the functions needed to compute the MST with Prim's algorithm.
double weight
Weight of the Edge, 1 if the data_structures is not weighted.
Definition: graph.h:44
The Fibonacci Heap datastructure as collection of Heap-ordered Trees.
unsigned short num_nodes
The number of Nodes in the Heap.
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
Node nodes[MAX_VERTEX_NUM]
Array of Nodes.
Definition: graph.h:57
unsigned short num_nodes
Number of Nodes in the Graph.
Definition: graph.h:54
Minimum Spanning Tree, or MST, and also a 1-Tree.
Definition: mst.h:28
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
unsigned short num_neighbours
Number of neighbours of the Node.
Definition: graph.h:34
unsigned short neighbours[MAX_VERTEX_NUM - 1]
Array of IDs of the Node's neighbors.
Definition: graph.h:35
A Heap-ordered Tree Node where the key of the parent is <= the key of its children.