30 #define MAX_DIST (double)INT_MAX
34 #define LOOP while(TRUE)
40 #define left(i) (2*(i))
41 #define right(i) (2*(i)+1)
42 #define parent(i) ((i)/2)
43 #define insideHeap(h,i) ((i)<h->heapSize)
44 #define greaterPriority(h,i,j,dist) (dist[h->data[i]]<dist[h->data[j]])
45 #define assign(h,i,j,index) {h->data[i]=h->data[j]; index[h->data[i]]=i;}
46 #define exchange(h,i,j,index) {int temp; \
48 h->data[i]=h->data[j]; \
50 index[h->data[i]]=i; \
51 index[h->data[j]]=j; \
59 static void heapify(
heap * h,
int i,
int index[],
Word dist[])
85 static void mkHeap(
heap * h,
int size)
92 static void freeHeap(
heap * h)
98 initHeap(
heap * h,
int startVertex,
int index[],
Word dist[],
int n)
107 for (count = 0, i = 0; i < n; i++)
108 if (i != startVertex) {
114 for (j = (n - 1) / 2; j >= 0; j--)
115 heapify(h, j, index, dist);
118 static boolean extractMax(
heap * h,
int *
max,
int index[],
Word dist[])
125 index[h->
data[0]] = 0;
127 heapify(h, 0, index, dist);
133 increaseKey(
heap * h,
int increasedVertex,
Word newDist,
int index[],
139 if (dist[increasedVertex] <= newDist)
142 placeInHeap = index[increasedVertex];
144 dist[increasedVertex] = newDist;
147 while (i > 0 && dist[h->
data[
parent(i)]] > newDist) {
151 h->
data[i] = increasedVertex;
152 index[increasedVertex] = i;
159 int closestVertex, neighbor;
166 index = (
int *) realloc(index, n *
sizeof(
int));
169 for (i = 0; i < n; i++)
172 for (i = 1; i < graph[vertex].
nedges; i++)
173 dist[graph[vertex].edges[i]] = (
DistType) graph[vertex].
ewgts[i];
175 initHeap(&H, vertex, index, dist, n);
177 while (extractMax(&H, &closestVertex, index, dist)) {
178 closestDist = dist[closestVertex];
181 for (i = 1; i < graph[closestVertex].
nedges; i++) {
182 neighbor = graph[closestVertex].
edges[i];
183 increaseKey(&H, neighbor,
185 (
DistType) graph[closestVertex].ewgts[i], index,
188 prevClosestDist = closestDist;
192 for (i = 0; i < n; i++)
194 dist[i] = prevClosestDist + 10;
201 int bound,
int *visited_nodes)
206 int num_visited_nodes;
208 static boolean *node_in_neighborhood =
NULL;
213 int closestVertex, neighbor;
220 for (i = 0; i < n; i++) {
224 bfs_bounded(vertex, graph, n, dist, &Q, bound, visited_nodes);
226 node_in_neighborhood =
227 (
boolean *) realloc(node_in_neighborhood, n *
sizeof(
boolean));
228 for (i = size; i < n; i++) {
229 node_in_neighborhood[i] =
FALSE;
233 for (i = 0; i < num_visited_nodes; i++) {
234 node_in_neighborhood[visited_nodes[i]] =
TRUE;
241 index = (
int *) realloc(index, n *
sizeof(
int));
244 for (i = 0; i < n; i++)
247 for (i = 1; i < graph[vertex].
nedges; i++)
248 dist[graph[vertex].edges[i]] = (
DistType) graph[vertex].
ewgts[i];
251 initHeap(&H, vertex, index, dist, n);
253 while (num_found < num_visited_nodes
254 && extractMax(&H, &closestVertex, index, dist)) {
255 if (node_in_neighborhood[closestVertex]) {
258 closestDist = dist[closestVertex];
261 for (i = 1; i < graph[closestVertex].
nedges; i++) {
262 neighbor = graph[closestVertex].
edges[i];
263 increaseKey(&H, neighbor,
265 (
DistType) graph[closestVertex].ewgts[i], index,
271 for (i = 0; i < num_visited_nodes; i++) {
272 node_in_neighborhood[visited_nodes[i]] =
FALSE;
276 return num_visited_nodes;
279 static void heapify_f(
heap * h,
int i,
int index[],
float dist[])
301 initHeap_f(
heap * h,
int startVertex,
int index[],
float dist[],
int n)
308 for (count = 0, i = 0; i < n; i++)
309 if (i != startVertex) {
315 for (j = (n - 1) / 2; j >= 0; j--)
316 heapify_f(h, j, index, dist);
319 static boolean extractMax_f(
heap * h,
int *max,
int index[],
float dist[])
326 index[h->
data[0]] = 0;
328 heapify_f(h, 0, index, dist);
334 increaseKey_f(
heap * h,
int increasedVertex,
float newDist,
int index[],
340 if (dist[increasedVertex] <= newDist)
343 placeInHeap = index[increasedVertex];
345 dist[increasedVertex] = newDist;
348 while (i > 0 && dist[h->
data[
parent(i)]] > newDist) {
352 h->
data[i] = increasedVertex;
353 index[increasedVertex] = i;
364 int closestVertex = 0, neighbor;
374 for (i = 0; i < n; i++)
377 for (i = 1; i < graph[vertex].
nedges; i++)
378 dist[graph[vertex].edges[i]] = graph[vertex].ewgts[i];
380 initHeap_f(&H, vertex, index, dist, n);
382 while (extractMax_f(&H, &closestVertex, index, dist)) {
383 closestDist = dist[closestVertex];
386 for (i = 1; i < graph[closestVertex].
nedges; i++) {
387 neighbor = graph[closestVertex].
edges[i];
388 increaseKey_f(&H, neighbor,
389 closestDist + graph[closestVertex].ewgts[i],
void dijkstra_f(int vertex, vtx_data *graph, int n, float *dist)
#define assign(h, i, j, index)
void freeQueue(Queue *qp)
int bfs_bounded(int vertex, vtx_data *graph, int n, DistType *dist, Queue *Q, int bound, int *visited_nodes)
#define greaterPriority(h, i, j, dist)
void dijkstra(int vertex, vtx_data *graph, int n, DistType *dist)
void mkQueue(Queue *qp, int size)
Agraph_t * graph(char *name)
#define exchange(h, i, j, index)
double dist(Site *s, Site *t)
int dijkstra_bounded(int vertex, vtx_data *graph, int n, DistType *dist, int bound, int *visited_nodes)