-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
157 lines (124 loc) · 3.15 KB
/
heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* File name: heap.c
*
* Author: Francisco e Joel
*
* date: 2018/11
*
* Description: C-Implementation of heap related functions
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "heap.h"
#define DEBUG 0
#define MAXRPOINTS 8
//#define P (wt[v] + vect[t].wt)
#define lessPri(A, B) (key[A] > key[B])
#define exch(A, B) {int t = A; A = B; B = t; }
static unsigned int *queue;
static int pqfree; /* primeira posição livre */ /* número de elementos existentes no acervo */
static int hsize; /* tamanho da tabela */
static int maxWT = 1000;
static unsigned short *key;
static unsigned int *index;
void printQueue(){
int i;
for(i=0; i<pqfree; i++){
printf("%d - ", queue[i]);
}
printf("\n");
}
/*
* Função onde se desenvolve o algoritmo
*/
void GRAPHpfs(int s, int st[], unsigned short wt[], int vd, problem pb) {
int v, w, t;
vertice *vect;
key = wt;
vect = (vertice *) malloc(MAXRPOINTS * sizeof(vertice));
checkMalloc(vect);
int n_adj = 0; //para contagem do nº de vertices adjacentes
PQinit((pb.l * pb.c) + 1);
for (v = 0; v < (pb.l * pb.c); v++) {
st[v] = -1;
wt[v] = maxWT;
if(pb.city[getXCoord(v, pb)][getYCoord(v, pb)] != 0)
PQinsert(v);
}
wt[s] = 0;
PQdec(s);
while (!PQempty()){
if (wt[v = PQdelmax()] != maxWT){
if(v == vd){
cleanUp(vect);
return;
}
getAdjIndex(pb, v, &n_adj, vect); // vetor de adjacencias
for (t = 0; t < n_adj; t++)
if (wt[w = vect[t].v] > wt[v] + vect[t].wt) {
wt[w] = wt[v] + vect[t].wt;
PQdec(w);
st[w] = v;
}
}
}
cleanUp(vect);
}
BOOL PQempty() {
return pqfree == 0 ? TRUE : FALSE;
}
void PQdec(int s){
FixUp(queue, index[s]);
}
void PQinit(unsigned Size) {
queue = (unsigned int *)malloc(Size*sizeof(unsigned int));
hsize = Size; pqfree = 0;
index = (unsigned int *) calloc(Size, sizeof(unsigned int));
}
void PQinsert(int I) {
/* insere novo elemento no fim e restabelece ordenação com FixUp */
queue[pqfree] = I;
index[I] = pqfree;
FixUp(queue, pqfree);
pqfree++;
}
int PQdelmax() {
/* troca maior elemento com último da tabela e reordena com FixDown */
int max = queue[0];
pqfree--;
exch(queue[0], queue[pqfree]);
exch(index[queue[0]], index[queue[pqfree]]);
FixDown(queue, 0, pqfree);
/* ultimo elemento não considerado na reordenação */
return max;
}
void FixUp(unsigned int Heap[], int Idx) {
while (Idx > 0 && lessPri(Heap[(Idx-1)/2], Heap[Idx])) {
exch(Heap[Idx], Heap[(Idx-1)/2]);
exch(index[Heap[Idx]], index[Heap[(Idx-1)/2]]);
Idx = (Idx-1)/2;
}
}
void FixDown(unsigned int Heap[], int Idx, int N) {
int Child; /* índice de um nó descendente */
while(2*Idx < N-1) { /* enquanto não chegar às folhas */
Child = 2*Idx+1;
/* Selecciona o maior descendente. */
/* Nota: se índice Child é N-1, então só há um descendente */
if (Child < (N-1) && lessPri(Heap[Child], Heap[Child+1]))
Child++;
if (!lessPri(Heap[Idx], Heap[Child]))
break;
exch(Heap[Idx], Heap[Child]);
exch(index[Heap[Idx]], index[Heap[Child]]);
/* continua a descer a árvore */
Idx = Child;
}
}
void cleanUp(vertice* vect){
free(index);
free(queue);
free(vect);
}