]> granicus.if.org Git - graphviz/commitdiff
neatogen: push queue allocation into 'bfs_bounded'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 22 Dec 2022 03:33:05 +0000 (19:33 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 25 Dec 2022 01:17:08 +0000 (17:17 -0800)
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.

lib/neatogen/bfs.c
lib/neatogen/bfs.h
lib/neatogen/dijkstra.c
lib/neatogen/stress.c

index 7b2ccf657a584fec3c6c7c382a29d238f3de2a8b..a5c19248a7fd17e3a706b0209cbed308c5a7e8c6 100644 (file)
@@ -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;
index 8321d41aa98c85ecc9f77328993a67401cbd690d..3807871ddff2cf786a1cf17121f335ff82f9af5c 100644 (file)
@@ -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
 }
index 10d3f1bdeac2017fdda89ab3e2a7034ea970c1bd..1cee13e594b1baf8f8e05ac7cc68218d72d273f6 100644 (file)
@@ -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;
 }
 
index 490a0d3a0ba2541917ff2b55bc95562b26fc9813..5c5bd279adadbf8e741ba33053724e1ae6254cd0 100644 (file)
@@ -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;
 }