From: Matthew Fernandez Date: Thu, 22 Dec 2022 03:33:05 +0000 (-0800) Subject: neatogen: push queue allocation into 'bfs_bounded' X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=213d22f7a68b423a8c85dc802a3561d73c66880e;p=graphviz neatogen: push queue allocation into 'bfs_bounded' Similar to the previous commit, the callee does not care about the contents of the queue on entry and the caller does not care about the contents of the queue on exit. Note that in this case we need to add an extra parameter because `bfs_bounded` did not have the queue size already. --- diff --git a/lib/neatogen/bfs.c b/lib/neatogen/bfs.c index 7b2ccf657..a5c19248a 100644 --- a/lib/neatogen/bfs.c +++ b/lib/neatogen/bfs.c @@ -73,7 +73,7 @@ void bfs(int vertex, vtx_data *graph, int n, DistType *dist) int bfs_bounded(int vertex, vtx_data * graph, DistType * dist, - Queue * Q, int bound, int *visited_nodes) + int bound, int *visited_nodes, int queue_size) /* compute vector 'dist' of distances of all nodes from 'vertex' */ /* ignore nodes whose distance to 'vertex' is more than bound */ { @@ -86,10 +86,12 @@ bfs_bounded(int vertex, vtx_data * graph, DistType * dist, dist[vertex] = 0; - initQueue(Q, vertex); + Queue Q; + mkQueue(&Q, queue_size); + initQueue(&Q, vertex); num_visit = 0; - while (deQueue(Q, &closestVertex)) { + while (deQueue(&Q, &closestVertex)) { closestDist = dist[closestVertex]; if (closestDist > bound) { dist[closestVertex] = -1; @@ -101,14 +103,15 @@ bfs_bounded(int vertex, vtx_data * graph, DistType * dist, 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); } } } + freeQueue(&Q); /* set distances of all nodes in Queue to -1 */ /* for next run */ - while (deQueue(Q, &closestVertex)) { + while (deQueue(&Q, &closestVertex)) { dist[closestVertex] = -1; } dist[vertex] = -1; diff --git a/lib/neatogen/bfs.h b/lib/neatogen/bfs.h index 8321d41aa..3807871dd 100644 --- a/lib/neatogen/bfs.h +++ b/lib/neatogen/bfs.h @@ -32,7 +32,7 @@ extern "C" { extern bool enQueue(Queue *, int); extern void bfs(int, vtx_data*, int, DistType*); - extern int bfs_bounded(int, vtx_data*, DistType *, Queue*, int, int*); + extern int bfs_bounded(int, vtx_data*, DistType*, int, int*, int); #ifdef __cplusplus } diff --git a/lib/neatogen/dijkstra.c b/lib/neatogen/dijkstra.c index 10d3f1bde..1cee13e59 100644 --- a/lib/neatogen/dijkstra.c +++ b/lib/neatogen/dijkstra.c @@ -185,20 +185,18 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist, { int num_visited_nodes; int i; - Queue Q; heap H; int closestVertex, neighbor; DistType closestDist; int num_found = 0; /* first, perform BFS to find the nodes in the region */ - mkQueue(&Q, n); /* remember that dist should be init. with -1's */ for (i = 0; i < n; i++) { dist[i] = -1; /* far, TOO COSTLY (O(n))! */ } num_visited_nodes = - bfs_bounded(vertex, graph, dist, &Q, bound, visited_nodes); + bfs_bounded(vertex, graph, dist, bound, visited_nodes, n); bitarray_t node_in_neighborhood = bitarray_new(n); for (i = 0; i < num_visited_nodes; i++) { bitarray_set(&node_in_neighborhood, visited_nodes[i], true); @@ -234,7 +232,6 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist, bitarray_reset(&node_in_neighborhood); freeHeap(&H); free(index); - freeQueue(&Q); return num_visited_nodes; } diff --git a/lib/neatogen/stress.c b/lib/neatogen/stress.c index 490a0d3a0..5c5bd279a 100644 --- a/lib/neatogen/stress.c +++ b/lib/neatogen/stress.c @@ -273,7 +273,6 @@ static int sparse_stress_subspace_majorization_kD(vtx_data * graph, /* Input gra /* if i is a pivot than CenterIndex[i] is its index, otherwise CenterIndex[i]= -1 */ int *CenterIndex; int *invCenterIndex; /* list the pivot nodes */ - Queue Q; float *old_weights; /* this matrix stores the distance between each node and each "center" */ DistType **Dij; @@ -332,7 +331,6 @@ static int sparse_stress_subspace_majorization_kD(vtx_data * graph, /* Input gra } invCenterIndex = NULL; - mkQueue(&Q, n); old_weights = graph[0].ewgts; if (reweight_graph) { @@ -441,7 +439,7 @@ static int sparse_stress_subspace_majorization_kD(vtx_data * graph, /* Input gra visited_nodes); } else { num_visited_nodes = - bfs_bounded(i, graph, dist, &Q, dist_bound, visited_nodes); + bfs_bounded(i, graph, dist, dist_bound, visited_nodes, n); } /* filter the pivots out of the visited nodes list, and the self loop: */ for (j = 0; j < num_visited_nodes;) { @@ -678,7 +676,6 @@ finish0: } free(subspace[0]); free(subspace); - freeQueue(&Q); return iterations; }