| 1 |
|
| /* |
| 2 |
|
| Search::ContextGraph - XS implementation |
| 3 |
|
| |
| 4 |
|
| (C) 2003 Schuyler Erle, Maciej Ceglowski |
| 5 |
|
| |
| 6 |
|
| This is free software, released |
| 7 |
|
| under the GPL. See LICENSE for more |
| 8 |
|
| information. |
| 9 |
|
| |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| # include "cns.h" |
| 13 |
|
| # include <unistd.h> |
| 14 |
|
| # include <stdlib.h> |
| 15 |
|
| # include <stdio.h> |
| 16 |
|
| # include <string.h> |
| 17 |
|
| # include <math.h> |
| 18 |
|
| |
| 19 |
|
| # define debug 0 |
| 20 |
|
| |
| 21 |
|
| |
| 22 |
|
| |
| 23 |
0 |
| Edge *new_edge ( Edge *edge, long sink, float weight ) { |
| 24 |
|
| /* Initialize a given edge struct. */ |
| 25 |
0 |
| edge->sink = sink; |
| 26 |
0 |
| edge->weight = weight; |
| 27 |
0 |
| return edge; |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
0 |
| Node *new_node ( Node *node, NodeType type, long capacity ) { |
| 31 |
|
| /* Initialize a given node struct. */ |
| 32 |
|
| //node->type = type; |
| 33 |
0 |
| node->degree = 0; |
| 34 |
0 |
| node->capacity = capacity; |
| 35 |
0 |
| node->edges = calloc( capacity, sizeof(Node) ); |
| 36 |
0 |
| memset( node->edges, 0, capacity * sizeof(Node) ); |
| 37 |
|
| if (debug) |
| 38 |
|
| fprintf(stderr, "%p new_node(%d) capacity = %ld\n", node, type, capacity ); |
| 39 |
0 |
| return node; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
| Graph *new_graph ( Graph *graph, long capacity, |
| 43 |
|
| float activationThreshold, float collectionThreshold, long maxDepth) { |
| 44 |
|
| /* Initialize a given graph struct. */ |
| 45 |
0 |
| graph->size = 0; |
| 46 |
0 |
| graph->capacity = capacity; |
| 47 |
0 |
| graph->activationThreshold = activationThreshold; |
| 48 |
0 |
| graph->collectionThreshold = collectionThreshold; |
| 49 |
0 |
| graph->maxDepth = maxDepth; |
| 50 |
0 |
| graph->nodes = calloc( capacity, sizeof(Node) ); |
| 51 |
0 |
| graph->energy = calloc( capacity, sizeof(float) ); |
| 52 |
0 |
| memset( graph->nodes, 0, capacity * sizeof(Node) ); |
| 53 |
0 |
| return graph; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
0 |
| void free_node (Node *node) { |
| 57 |
0 |
| free( node->edges ); |
| 58 |
0 |
| } |
| 59 |
|
| |
| 60 |
0 |
| void free_graph (Graph *graph) { |
| 61 |
|
| long n; |
| 62 |
|
| /*fprintf( stderr, "%p graph size: %ld\n", graph, graph->size );*/ |
| 63 |
0 |
▶ | for (n = 0; n < graph->size; n++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = 0; n < graph->size; n++) |
0 |
0 |
0 |
|
| 64 |
0 |
| free_node( graph->nodes + n ); |
| 65 |
0 |
| free( graph->energy); |
| 66 |
0 |
| free( graph->nodes ); |
| 67 |
0 |
| free( graph ); |
| 68 |
0 |
| } |
| 69 |
|
| |
| 70 |
|
| |
| 71 |
0 |
| void reset_graph( Graph *graph ) { |
| 72 |
0 |
| graph->energy = realloc( graph->energy, graph->size * sizeof(float) ); |
| 73 |
0 |
| memset( graph->energy, 0, graph->size * sizeof(float) ); |
| 74 |
0 |
| graph->numCalls = 0; |
| 75 |
0 |
| graph->maxDepth = -1; |
| 76 |
0 |
| } |
| 77 |
|
| |
| 78 |
0 |
| Edge *add_edge ( Node *node, long sink, float weight ) { |
| 79 |
0 |
| long deg = ++(node->degree); |
| 80 |
0 |
| long cap = node->capacity; |
| 81 |
|
| Edge *edge; |
| 82 |
|
| |
| 83 |
|
| if (debug) |
| 84 |
|
| printf( "* adding edge -> %ld(%f) to %p (%ld / %ld)\n", |
| 85 |
|
| sink, weight, node, deg, cap ); |
| 86 |
|
| |
| 87 |
|
| /* If we have more edges than we've allocated memory for, reallocate. */ |
| 88 |
0 |
▶ | if (deg > cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (deg > cap) { |
0 |
0 |
0 |
|
| 89 |
|
| |
| 90 |
|
| //if( cap < 255 ) { |
| 91 |
|
| |
| 92 |
|
| // node->capacity = cap + 256; |
| 93 |
|
| //fprintf( stderr, "\tincreasing node capacity %d , %d\n", node->capacity , cap); |
| 94 |
|
| //} else { |
| 95 |
|
| //printf( "\tdoubling node capacity %d\n", node->capacity); |
| 96 |
0 |
| node->capacity *= 2; |
| 97 |
|
| //} |
| 98 |
|
| |
| 99 |
0 |
| node->edges = realloc( node->edges, node->capacity * sizeof(Edge) ); |
| 100 |
0 |
| memset( node->edges + cap, 0, |
| 101 |
0 |
| ( node->capacity - cap ) * sizeof(Edge) ); |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| /* Initialize the new edge. */ |
| 105 |
0 |
| edge = node->edges + deg - 1; |
| 106 |
0 |
| new_edge( edge, sink, weight ); |
| 107 |
0 |
| return edge; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
0 |
| int preallocate ( Graph *graph, int nodecount ) { |
| 111 |
|
| |
| 112 |
|
| int original; |
| 113 |
|
| |
| 114 |
0 |
| original = graph->capacity; |
| 115 |
|
| |
| 116 |
0 |
▶ | if ( original < nodecount ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( original < nodecount ) { |
0 |
0 |
0 |
|
| 117 |
0 |
| printf( "Pre-allocationg graph capacity (was %d) to %d\n", original, nodecount ); |
| 118 |
0 |
| graph->capacity = nodecount; |
| 119 |
0 |
| graph->nodes = realloc( graph->nodes, graph->capacity * sizeof(Node)); |
| 120 |
0 |
| memset( graph->nodes + original, 0, (graph->capacity - original) * sizeof(Node) ); |
| 121 |
|
| } |
| 122 |
0 |
| return 1; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
0 |
| void presize_node( Graph *graph, long id, int size ) { |
| 126 |
|
| |
| 127 |
0 |
| Node *n = ( graph->nodes + id ); |
| 128 |
0 |
| long deg = n->degree; |
| 129 |
0 |
| long cap = n->capacity; |
| 130 |
|
| |
| 131 |
0 |
▶ | if ( cap < size ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( cap < size ) { |
0 |
0 |
0 |
|
| 132 |
0 |
| n->capacity = size; |
| 133 |
0 |
| n->edges = realloc( n->edges, size * sizeof( Edge )); |
| 134 |
0 |
| memset( n->edges + cap, 0, (n->capacity -cap ) * sizeof(Edge)); |
| 135 |
|
| //printf( "Presizing node capacity (was %d) to %d\n", cap, n->capacity ); |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
0 |
| } |
| 139 |
|
| |
| 140 |
|
| |
| 141 |
0 |
| Node *add_node ( Graph *graph, long id, NodeType type, long capacity ) { |
| 142 |
|
| Node *node; |
| 143 |
|
| |
| 144 |
|
| /* If the index of the to-be-created node is bigger than we've allocated, |
| 145 |
|
| * we need to reallocated. */ |
| 146 |
|
| if (debug) |
| 147 |
|
| fprintf( stderr, "%p add_node %ld/%d (%d)\n", |
| 148 |
|
| graph, id, graph->size, graph->capacity); |
| 149 |
|
| |
| 150 |
|
| |
| 151 |
0 |
▶ | if (id >= graph->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (id >= graph->size) { |
0 |
0 |
0 |
|
| 152 |
0 |
| long cap = graph->capacity; |
| 153 |
0 |
| graph->size = id + 1; |
| 154 |
|
| |
| 155 |
|
| /* Increase the capacity until it's bigger than the number of nodes |
| 156 |
|
| * we need, if such need be, and then resize the lists if the capacity |
| 157 |
|
| * changed. */ |
| 158 |
0 |
▶ | while (graph->size > graph->capacity ) |
Branch: 1 branch
| Branch | True | False | Total |
| while (graph->size > graph->capacity ) |
0 |
0 |
0 |
|
| 159 |
0 |
| graph->capacity *= 2; |
| 160 |
|
| |
| 161 |
0 |
▶ | if (graph->capacity > cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (graph->capacity > cap) { |
0 |
0 |
0 |
|
| 162 |
0 |
| graph->nodes = realloc( |
| 163 |
0 |
| graph->nodes, graph->capacity * sizeof(Node)); |
| 164 |
0 |
| memset( graph->nodes + cap, 0, (graph->capacity - cap) * sizeof(Node) ); |
| 165 |
|
| } |
| 166 |
|
| } |
| 167 |
|
| if (debug) |
| 168 |
|
| fprintf( stderr, "%p --> size: %d capacity: %d\n", |
| 169 |
|
| graph, graph->size, graph->capacity); |
| 170 |
|
| |
| 171 |
|
| /* Initialize the new node, unless there already is such a node. */ |
| 172 |
0 |
| node = graph->nodes + id; |
| 173 |
0 |
▶ | if ( node->capacity == 0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->capacity == 0 ){ |
0 |
0 |
0 |
|
| 174 |
0 |
| new_node( node, type, capacity ); |
| 175 |
|
| if (debug) |
| 176 |
|
| fprintf( stderr, "%p --> new node\n", |
| 177 |
|
| node); |
| 178 |
|
| } else { |
| 179 |
0 |
| fprintf( stderr, "node %p ", node ); |
| 180 |
|
| } |
| 181 |
0 |
| return node; |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
0 |
| int energize_node( Graph *graph, long id, float energy, int isStartingPoint) { |
| 185 |
0 |
| Node *node = graph->nodes + id; |
| 186 |
0 |
| float *slot = graph->energy + id; |
| 187 |
|
| |
| 188 |
0 |
| float subenergy = 0; |
| 189 |
|
| static int depth = 0; |
| 190 |
|
| long n; |
| 191 |
|
| |
| 192 |
|
| // Keep track of some search statistics |
| 193 |
0 |
| graph->numCalls++; |
| 194 |
0 |
▶ | if ( ++depth > graph->maxDepth ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ++depth > graph->maxDepth ) |
0 |
0 |
0 |
|
| 195 |
0 |
| graph->maxDepth = depth; |
| 196 |
|
| |
| 197 |
|
| |
| 198 |
|
| if (debug){ |
| 199 |
|
| int i; |
| 200 |
|
| for (i = 0; i< depth;i++) |
| 201 |
|
| fprintf( stderr, " "); |
| 202 |
|
| fprintf(stderr, "%ld: energizing %f + %f\n", id, node->energy, energy); |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
|
| /* Activate the node and calculate the propagating energy. */ |
| 206 |
0 |
| *slot += energy; |
| 207 |
0 |
▶ | if (node->degree) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->degree) { |
0 |
0 |
0 |
|
| 208 |
|
| |
| 209 |
|
| //printf( "* denominator is %f\n", denom ); |
| 210 |
0 |
| subenergy = energy / (log( node->degree ) + 1); |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* Special case handling for nodes with just one neighbor |
| 214 |
|
| Normally, we don't propagate energy at a singleton node |
| 215 |
|
| But if it's the query node, we want to continue the search */ |
| 216 |
|
| |
| 217 |
0 |
▶ | if ( node->degree == 1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->degree == 1 ) { |
0 |
0 |
0 |
|
| 218 |
|
| |
| 219 |
0 |
▶ | if ( isStartingPoint == 0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( isStartingPoint == 0 ){ |
0 |
0 |
0 |
|
| 220 |
0 |
| depth--; |
| 221 |
0 |
| return 0; |
| 222 |
|
| } else { |
| 223 |
0 |
| subenergy = energy; |
| 224 |
|
| } |
| 225 |
|
| } |
| 226 |
|
| /* **********************/ |
| 227 |
|
| |
| 228 |
|
| /* Bail if the subenergy isn't big enough to propagate. */ |
| 229 |
0 |
▶ | if (subenergy < graph->activationThreshold){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (subenergy < graph->activationThreshold){ |
0 |
0 |
0 |
|
| 230 |
0 |
| depth--; |
| 231 |
0 |
| return 0; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| /* Otherwise, recurse through our neighbors. */ |
| 235 |
|
| if (debug){ |
| 236 |
|
| int i; |
| 237 |
|
| for (i = 0; i< depth;i++) |
| 238 |
|
| fprintf( stderr, " "); |
| 239 |
|
| fprintf(stderr, "%ld: propagating subenergy %f to %d neighbors\n", |
| 240 |
|
| id, subenergy, node->degree); |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
0 |
▶ | for (n = 0; n < node->degree; n++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = 0; n < node->degree; n++) { |
0 |
0 |
0 |
|
| 244 |
0 |
| energize_node( graph, |
| 245 |
0 |
| node->edges[n].sink, node->edges[n].weight * subenergy , 0 ); |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
|
| if (debug){ |
| 249 |
|
| int i; |
| 250 |
|
| for (i = 0; i< depth;i++) |
| 251 |
|
| fprintf( stderr, " "); |
| 252 |
|
| fprintf(stderr, "%ld: finished\n", id); |
| 253 |
|
| } |
| 254 |
0 |
| depth--; |
| 255 |
0 |
| return 1; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
0 |
| int compare_results( const void *a, const void *b ) { |
| 259 |
0 |
| const Edge *x = a, *y = b; |
| 260 |
|
| /* Sort nodes in descending order by energy. */ |
| 261 |
0 |
▶ | return (x->weight > y->weight ? -1 : |
Branch: 1 branch
| Branch | True | False | Total |
| return (x->weight > y->weight ? -1 : |
0 |
0 |
0 |
|
| 262 |
0 |
| (x->weight < y->weight ? 1 : 0)); |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
0 |
| Edge *collect_results( Graph *graph ) { |
| 266 |
|
| |
| 267 |
|
| /* The result list will be a NULL-terminated list of edge structs. The |
| 268 |
|
| * weight entry in each struct will contain the accumulated activation energy |
| 269 |
|
| * of each sink. */ |
| 270 |
|
| long n, m; |
| 271 |
|
| |
| 272 |
0 |
| int result_size = 64; |
| 273 |
|
| |
| 274 |
0 |
| Edge *result = calloc( result_size, sizeof(Edge) ); |
| 275 |
|
| |
| 276 |
|
| /* Find the nodes that scored higher than the threshold. */ |
| 277 |
0 |
▶ | for (n = 0, m = 0; n < graph->size; n++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = 0, m = 0; n < graph->size; n++ ) { |
0 |
0 |
0 |
|
| 278 |
0 |
▶ | if ( *( graph->energy + n) > graph->collectionThreshold ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( *( graph->energy + n) > graph->collectionThreshold ) { |
0 |
0 |
0 |
|
| 279 |
|
| |
| 280 |
0 |
| Edge *e = result + m++; |
| 281 |
0 |
| e->sink = n; |
| 282 |
0 |
| e->weight = *( graph->energy + n); |
| 283 |
|
| |
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
|
| /* Expand as necessary to fit the result list */ |
| 287 |
0 |
▶ | while ( (m+1) > result_size ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( (m+1) > result_size ) { |
0 |
0 |
0 |
|
| 288 |
0 |
| result_size *= 2; |
| 289 |
0 |
| result = realloc( result, result_size * sizeof( Edge )); |
| 290 |
|
| } |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
0 |
| memset( result + m, 0, sizeof(Edge) ); |
| 294 |
0 |
| return result; |
| 295 |
|
| |
| 296 |
|
| } |
| 297 |
|
| |