Callers of `bfs` were constructing `Queue` objects and passing them into `bfs`.
But in all of these cases neither the callee nor the caller care about the
contents of the queue. This appears to have been an optimization to hoist
allocation of this object outside loops in callers. This is nowhere close to the
most expensive operation these locations are performing. And replicating
operations like this led to opportunities for errors like that fixed in the
prior commit. It seems a win to undo this for readability.
#include <stdbool.h>
#include <stdlib.h>
-void bfs(int vertex, vtx_data * graph, int n, DistType * dist, Queue * Q)
+void bfs(int vertex, vtx_data *graph, int n, DistType *dist)
/* compute vector 'dist' of distances of all nodes from 'vertex' */
{
int i;
dist[i] = -1;
dist[vertex] = 0;
- initQueue(Q, vertex);
+ Queue Q;
+ mkQueue(&Q, n);
+ initQueue(&Q, vertex);
if (graph[0].ewgts == NULL) {
- while (deQueue(Q, &closestVertex)) {
+ while (deQueue(&Q, &closestVertex)) {
closestDist = dist[closestVertex];
for (i = 1; i < graph[closestVertex].nedges; i++) {
neighbor = graph[closestVertex].edges[i];
if (dist[neighbor] < -0.5) { /* first time to reach neighbor */
dist[neighbor] = closestDist + 1;
- enQueue(Q, neighbor);
+ enQueue(&Q, neighbor);
}
}
}
} else {
- while (deQueue(Q, &closestVertex)) {
+ while (deQueue(&Q, &closestVertex)) {
closestDist = dist[closestVertex];
for (i = 1; i < graph[closestVertex].nedges; i++) {
neighbor = graph[closestVertex].edges[i];
dist[neighbor] =
closestDist +
(DistType) graph[closestVertex].ewgts[i];
- enQueue(Q, neighbor);
+ enQueue(&Q, neighbor);
}
}
}
for (i = 0; i < n; i++)
if (dist[i] < -0.5) /* 'i' is not connected to 'vertex' */
dist[i] = closestDist + 10;
+
+ freeQueue(&Q);
}
int
extern bool deQueue(Queue *, int *);
extern bool enQueue(Queue *, int);
- extern void bfs(int, vtx_data *, int, DistType *, Queue *);
+ extern void bfs(int, vtx_data*, int, DistType*);
extern int bfs_bounded(int, vtx_data*, DistType *, Queue*, int, int*);
#ifdef __cplusplus
DistType *dist = N_GNEW(n, DistType); /* this vector stores the distances of
each nodes to the selected "pivots" */
float *old_weights = graph[0].ewgts;
- Queue Q;
DistType max_dist = 0;
/* this matrix stores the distance between each node and each "pivot" */
/* select the first pivot */
node = rand() % n;
- mkQueue(&Q, n);
if (reweight_graph) {
dijkstra(node, graph, n, coords[0]);
} else {
- bfs(node, graph, n, coords[0], &Q);
+ bfs(node, graph, n, coords[0]);
}
for (i = 0; i < n; i++) {
if (reweight_graph) {
dijkstra(node, graph, n, coords[i]);
} else {
- bfs(node, graph, n, coords[i], &Q);
+ bfs(node, graph, n, coords[i]);
}
max_dist = 0;
for (j = 0; j < n; j++) {
}
free(dist);
- freeQueue(&Q);
if (reweight_graph) {
restore_old_weights(graph, n, old_weights);
/* for unweighted graph */
int i;
DistType *storage = gv_calloc((size_t)(n * n), sizeof(DistType));
- Queue Q;
DistType **dij = gv_calloc(n, sizeof(DistType*));
for (i = 0; i < n; i++) {
dij[i] = storage + i * n;
}
- mkQueue(&Q, n);
for (i = 0; i < n; i++) {
- bfs(i, graph, n, dij[i], &Q);
+ bfs(i, graph, n, dij[i]);
}
- freeQueue(&Q);
return dij;
}
if (reweight_graph) {
dijkstra(node, graph, n, Dij[0]);
} else {
- bfs(node, graph, n, Dij[0], &Q);
+ bfs(node, graph, n, Dij[0]);
}
/* find the most distant node from first pivot */
if (reweight_graph) {
dijkstra(node, graph, n, Dij[i]);
} else {
- bfs(node, graph, n, Dij[i], &Q);
+ bfs(node, graph, n, Dij[i]);
}
max_dist = 0;
for (j = 0; j < n; j++) {
float *Dij = N_NEW(n * (n + 1) / 2, float);
DistType *Di = N_NEW(n, DistType);
- Queue Q;
-
- mkQueue(&Q, n);
count = 0;
for (i = 0; i < n; i++) {
- bfs(i, graph, n, Di, &Q);
+ bfs(i, graph, n, Di);
for (j = i; j < n; j++) {
Dij[count++] = (float)Di[j];
}
}
free(Di);
- freeQueue(&Q);
return Dij;
}