18static void swap(
Graph *graph,
unsigned short swap_1,
unsigned short swap_2) {
33 Edge temp = edges[swap_1];
34 edges[swap_1] = edges[swap_2];
41 Edge last_edge = edges[last];
42 Edge first_edge = edges[first];
43 unsigned short middle = (first + last) / 2;
44 Edge middle_edge = edges[middle];
45 double pivot_weight = first_edge.
weight;
46 double pivot_prob = first_edge.
prob;
54 pivot_weight = middle_edge.
weight;
55 pivot_prob = middle_edge.
prob;
56 swap(graph, first, middle);
59 pivot_weight = last_edge.
weight;
60 pivot_prob = last_edge.
prob;
61 swap(graph, first, last);
66 pivot_weight = last_edge.
weight;
67 pivot_prob = last_edge.
prob;
68 swap(graph, first, last);
72 pivot_weight = middle_edge.
weight;
73 pivot_prob = middle_edge.
prob;
74 swap(graph, first, middle);
77 unsigned short j = last;
78 unsigned short i = first + 1;
79 bool condition =
true;
81 Edge i_edge = edges[i];
82 while (i <= j && ((!HYBRID && pivot_weight >= i_edge.
weight) ||
83 (HYBRID && (pivot_weight > i_edge.
weight || (pivot_weight == i_edge.
weight && pivot_prob <= i_edge.
prob))))){
87 Edge j_edge = edges[j];
88 while (i <= j && (j_edge.
weight > pivot_weight
89 || (HYBRID && (j_edge.
weight == pivot_weight && j_edge.
prob < pivot_prob)))) {
102 swap( graph, first, j);
113 if(pivot -1 > first) {
116 if(pivot + 1 < last) {
136 unsigned short num_edges_inG = 0;
137 unsigned short num_edges_inMST = 0;
139 while (num_edges_inG < graph->num_edges && num_edges_inMST < graph->num_nodes - 1) {
141 Edge current_edge = graph->
edges[num_edges_inG];
142 unsigned short src = current_edge.
src;
143 unsigned short dest = current_edge.
dest;
149 merge(set1_root, set2_root);
156 if (num_edges_inMST == graph->
num_nodes - 1) {
static void swap(Graph *graph, unsigned short swap_1, unsigned short swap_2)
void kruskal(Graph *graph, MST *mst)
The Kruskal algorithm to find the Minimum Spanning Tree O(|E| log |V|)
static int pivot_quicksort(Graph *graph, unsigned short first, unsigned short last)
void wrap_quick_sort(Graph *graph)
The wrapper of the quick sort algorithm.
static void quick_sort(Graph *graph, unsigned short first, unsigned short last)
The declaration of the functions needed to compute the MST with Kruskal's algorithm.
void merge(Set *set1, Set *set2)
Merge two Sets in the Forest if they are not already in the same Set.
Set * find(Set *set)
Find the root of a Set.
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.
void add_edge(MST *tree, const Edge *edge)
Add an Edge to the MST.
void create_mst(MST *mst, const Node *nodes, unsigned short num_nodes)
Create a Minimum Spanning Tree from a set of Nodes.
unsigned short dest
ID of the destination vertex.
unsigned short src
ID of the source vertex.
double prob
Probability of the Edge to be in an optimal tour.
double weight
Weight of the Edge, 1 if the data_structures is not weighted.
unsigned short positionInGraph
Position of the Edge in the list of Edges of the Graph.
A Forest is a list of Sets.
Set sets[MAX_VERTEX_NUM]
Array of Sets.
Edge edges_matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]
Adjacency matrix of the Graph.
unsigned short num_edges
Number of Edges in the Graph.
bool orderedEdges
True if the Edges are ordered by weight, false otherwise.
Edge edges[MAX_EDGES_NUM]
Array of Edges.
Node nodes[MAX_VERTEX_NUM]
Array of Nodes.
unsigned short num_nodes
Number of Nodes in the Graph.
Minimum Spanning Tree, or MST, and also a 1-Tree.
bool isValid
True if the MST has the correct number of Edges, false otherwise.
A Set is a node in the Forest.
unsigned short num_in_forest
Number of the position of the Set in the Forest.