35 typedef struct _HEAD_ADJ{
67 assert(tcliquegraph !=
NULL);
69 return tcliquegraph->nnodes;
75 assert(tcliquegraph !=
NULL);
77 return tcliquegraph->weights;
87 assert(tcliquegraph !=
NULL);
88 assert(tcliquegraph->ncachededges == 0);
89 assert(0 <= node1 && node1 < tcliquegraph->nnodes);
90 assert(0 <= node2 && node2 < tcliquegraph->nnodes);
102 if( currentadjedge > lastadjedge || *lastadjedge < node2 )
107 while( currentadjedge <= lastadjedge )
109 if( *currentadjedge >= node2 )
111 if( *currentadjedge == node2 )
132 assert(tcliquegraph !=
NULL);
133 assert(tcliquegraph->ncachededges == 0);
134 assert(0 <= node && node < tcliquegraph->nnodes);
135 assert(nnodes == 0 || nodes !=
NULL);
136 assert(adjnodes !=
NULL);
145 for( i = 0; i < nnodes; i++ )
147 assert(0 <= nodes[i] && nodes[i] < tcliquegraph->nnodes);
148 assert(i == 0 || nodes[i-1] < nodes[i]);
149 for( ; currentadjedge <= lastadjedge; currentadjedge++ )
151 if( *currentadjedge >= nodes[i] )
154 if( *currentadjedge == nodes[i] )
156 adjnodes[nadjnodes] = nodes[i];
179 assert(tcliquegraph !=
NULL);
183 (*tcliquegraph)->nnodes = 0;
184 (*tcliquegraph)->nedges = 0;
185 (*tcliquegraph)->weights =
NULL;
186 (*tcliquegraph)->degrees =
NULL;
187 (*tcliquegraph)->adjnodes =
NULL;
188 (*tcliquegraph)->adjedges =
NULL;
189 (*tcliquegraph)->sizenodes = 0;
190 (*tcliquegraph)->sizeedges = 0;
191 (*tcliquegraph)->cacheddegrees =
NULL;
192 (*tcliquegraph)->cachedorigs =
NULL;
193 (*tcliquegraph)->cacheddests =
NULL;
194 (*tcliquegraph)->ncachededges = 0;
195 (*tcliquegraph)->sizecachededges = 0;
205 assert(tcliquegraph !=
NULL);
207 if( *tcliquegraph !=
NULL )
209 if ( (*tcliquegraph)->adjedges !=
NULL )
216 if ( (*tcliquegraph)->cacheddegrees )
233 assert(tcliquegraph !=
NULL);
235 if( num > tcliquegraph->sizeedges )
239 newsize = 2*tcliquegraph->sizeedges;
244 tcliquegraph->sizeedges = newsize;
247 assert(num <= tcliquegraph->sizeedges);
259 assert(tcliquegraph !=
NULL);
261 if( num > tcliquegraph->sizecachededges )
265 newsize = 2*tcliquegraph->sizecachededges;
271 tcliquegraph->sizecachededges = newsize;
274 assert(num <= tcliquegraph->sizecachededges);
286 assert(tcliquegraph !=
NULL);
290 assert(tcliquegraph->adjnodes !=
NULL);
292 if( num > tcliquegraph->sizenodes )
297 newsize = 2*tcliquegraph->sizenodes;
305 for( i = tcliquegraph->sizenodes; i < newsize; i++ )
307 tcliquegraph->weights[i] = 0;
308 tcliquegraph->degrees[i] = 0;
309 tcliquegraph->adjedges[i].first = tcliquegraph->nedges;
310 tcliquegraph->adjedges[i].last = tcliquegraph->nedges;
313 if( tcliquegraph->ncachededges > 0 )
315 assert(tcliquegraph->cacheddegrees !=
NULL);
317 for( i = tcliquegraph->sizenodes; i < newsize; i++ )
318 tcliquegraph->cacheddegrees[i] = 0;
321 tcliquegraph->sizenodes = newsize;
323 assert(num <= tcliquegraph->sizenodes);
341 tcliquegraph->weights[node] = weight;
343 assert(tcliquegraph->degrees[node] == 0);
344 assert(tcliquegraph->adjedges[node].first <= tcliquegraph->nedges);
345 assert(tcliquegraph->adjedges[node].last == tcliquegraph->adjedges[node].first);
346 tcliquegraph->nnodes =
MAX(tcliquegraph->nnodes, node+1);
358 assert(0 <= node && node < tcliqueGetNNodes(tcliquegraph));
361 tcliquegraph->weights[node] = weight;
375 assert(tcliquegraph !=
NULL);
376 assert(0 <= node1 && node1 < tcliquegraph->nnodes);
377 assert(0 <= node2 && node2 < tcliquegraph->nnodes);
378 assert(node1 != node2);
384 if( tcliquegraph->ncachededges == 0 && tcliquegraph->sizenodes > 0 )
386 assert(tcliquegraph->cacheddegrees ==
NULL);
390 assert(tcliquegraph->cacheddegrees !=
NULL);
393 tcliquegraph->cachedorigs[tcliquegraph->ncachededges] = node1;
394 tcliquegraph->cacheddests[tcliquegraph->ncachededges] = node2;
395 tcliquegraph->ncachededges++;
396 tcliquegraph->cachedorigs[tcliquegraph->ncachededges] = node2;
397 tcliquegraph->cacheddests[tcliquegraph->ncachededges] = node1;
398 tcliquegraph->ncachededges++;
399 tcliquegraph->cacheddegrees[node1]++;
400 tcliquegraph->cacheddegrees[node2]++;
410 assert(tcliquegraph !=
NULL);
413 if( tcliquegraph->ncachededges > 0 )
423 assert(tcliquegraph->adjnodes !=
NULL);
424 assert(tcliquegraph->adjedges !=
NULL);
428 pos = tcliquegraph->nedges + tcliquegraph->ncachededges - 1;
429 for( n = tcliquegraph->nnodes-1; ; --n )
434 assert(tcliquegraph->adjedges[n].last - tcliquegraph->adjedges[n].first == tcliquegraph->degrees[n]);
437 olddegree = tcliquegraph->degrees[n];
438 tcliquegraph->degrees[n] += tcliquegraph->cacheddegrees[n];
441 pos -= tcliquegraph->cacheddegrees[n];
442 ninsertedholes += tcliquegraph->cacheddegrees[n];
443 assert(ninsertedholes <= tcliquegraph->ncachededges);
444 if( ninsertedholes == tcliquegraph->ncachededges )
449 for( i = tcliquegraph->adjedges[n].last - 1; i >= tcliquegraph->adjedges[n].first; --i, --pos )
451 assert(0 <= i && i < pos && pos < tcliquegraph->nedges + tcliquegraph->ncachededges);
452 tcliquegraph->adjnodes[pos] = tcliquegraph->adjnodes[i];
456 tcliquegraph->adjedges[n].first = pos+1;
457 tcliquegraph->adjedges[n].last = pos+1 + olddegree;
459 assert(n == tcliquegraph->nnodes-1
460 || tcliquegraph->adjedges[n].first + tcliquegraph->degrees[n] == tcliquegraph->adjedges[n+1].first);
462 assert(ninsertedholes == tcliquegraph->ncachededges);
463 assert(tcliquegraph->adjedges[n].last == pos+1);
465 for( --n; n >= 0; --n )
466 assert(tcliquegraph->cacheddegrees[n] == 0);
470 for( i = 0; i < tcliquegraph->ncachededges; ++i )
474 n = tcliquegraph->cachedorigs[i];
475 dest = tcliquegraph->cacheddests[i];
476 assert(0 <= n && n < tcliquegraph->nnodes);
477 assert(0 <= dest && dest < tcliquegraph->nnodes);
478 assert(tcliquegraph->adjedges[n].last <= tcliquegraph->nedges + tcliquegraph->ncachededges);
479 assert(n == tcliquegraph->nnodes-1 || tcliquegraph->adjedges[n].last <= tcliquegraph->adjedges[n+1].first);
480 assert(n == tcliquegraph->nnodes-1
481 || tcliquegraph->adjedges[n].first + tcliquegraph->degrees[n] == tcliquegraph->adjedges[n+1].first);
484 for( pos = tcliquegraph->adjedges[n].last;
485 pos > tcliquegraph->adjedges[n].first && dest < tcliquegraph->adjnodes[pos-1]; --pos )
487 tcliquegraph->adjnodes[pos] = tcliquegraph->adjnodes[pos-1];
489 tcliquegraph->adjnodes[pos] = dest;
490 tcliquegraph->adjedges[n].last++;
492 assert(n == tcliquegraph->nnodes-1 || tcliquegraph->adjedges[n].last <= tcliquegraph->adjedges[n+1].first);
496 tcliquegraph->nedges += tcliquegraph->ncachededges;
502 tcliquegraph->ncachededges = 0;
503 tcliquegraph->sizecachededges = 0;
507 assert(tcliquegraph->ncachededges == 0);
508 assert(tcliquegraph->sizecachededges == 0);
509 assert(tcliquegraph->cacheddegrees ==
NULL);
510 assert(tcliquegraph->cachedorigs ==
NULL);
511 assert(tcliquegraph->cacheddests ==
NULL);
520 for( n = 0; n < tcliquegraph->nnodes; ++n )
524 assert(tcliquegraph->adjedges[n].first == pos);
525 assert(tcliquegraph->adjedges[n].last == tcliquegraph->adjedges[n].first + tcliquegraph->degrees[n]);
527 for( i = tcliquegraph->adjedges[n].first; i < tcliquegraph->adjedges[n].last-1; ++i )
529 assert(tcliquegraph->adjnodes[i] < tcliquegraph->adjnodes[i+1]);
531 pos = tcliquegraph->adjedges[n].last;
533 assert(pos == tcliquegraph->nedges);
543 const char* filename,
559 assert(tcliquegraph !=
NULL);
560 assert(scaleval > 0.0);
563 if( (file = fopen(filename,
"r")) ==
NULL )
565 if( (file = fopen(
"default.dat",
"r")) ==
NULL )
579 charresult = fgets(probname, sizeofprobname, file);
580 if( charresult ==
NULL )
582 infoMessage(
"Error while reading probname in file %s", filename);
590 probname[sizeofprobname-1] =
'\0';
591 tmp[sizeofprobname] =
'\0';
594 while( (
int) strlen(tmp) == sizeofprobname && tmp[strlen(tmp)-1] !=
'\n' )
596 charresult = fgets(tmp, sizeofprobname, file);
598 if( charresult ==
NULL )
600 infoMessage(
"Error while reading probname in file %s", filename);
610 result = fscanf(file,
"%d", &(*tcliquegraph)->nnodes);
613 infoMessage(
"Error while reading number of nodes in file %s", filename);
618 result = fscanf(file,
"%d", &(*tcliquegraph)->nedges);
621 infoMessage(
"Error while reading number of edges in file %s", filename);
626 if( (*tcliquegraph)->nnodes < 0 || (*tcliquegraph)->nedges < 0 )
628 infoMessage(
"\nInvalid number of %s (%d) in file: %s", (*tcliquegraph)->nnodes < 0 ?
"nodes" :
"edges",
629 (*tcliquegraph)->nnodes < 0 ? (*tcliquegraph)->nnodes : (*tcliquegraph)->nedges, filename);
638 infoMessage(
"Run out of memory while reading file %s", filename);
645 infoMessage(
"Run out of memory while reading file %s", filename);
652 infoMessage(
"Run out of memory while reading file %s", filename);
659 infoMessage(
"Run out of memory while reading file %s", filename);
665 for( i = 0; i < (*tcliquegraph)->nnodes; i++ )
667 result = fscanf(file,
"%lf", &weight);
670 infoMessage(
"Error while reading weights of nodes in file %s", filename);
675 (*tcliquegraph)->weights[i] = (
TCLIQUE_WEIGHT)(weight * scaleval);
676 assert((*tcliquegraph)->weights[i] >= 0);
681 for( i = 0; i < (*tcliquegraph)->nedges; i++ )
684 result = fscanf(file,
"%d%d", &node1, &node2);
687 infoMessage(
"Error while reading edges in file %s", filename);
692 if( node1 < 0 || node2 < 0 )
694 infoMessage(
"\nInvalid node index (%d) in file: %s", node1 < 0 ? node1 : node2, filename);
700 if( node1 != currentnode )
703 (*tcliquegraph)->degrees[currentnode] = 0;
704 (*tcliquegraph)->adjedges[currentnode].first = i;
705 (*tcliquegraph)->adjedges[currentnode].last = (*tcliquegraph)->adjedges[currentnode].first;
707 (*tcliquegraph)->degrees[currentnode]++;
708 (*tcliquegraph)->adjnodes[i] = node2;
709 (*tcliquegraph)->adjedges[currentnode].last++;
721 const char* filename,
730 assert(tcliquegraph !=
NULL);
731 assert(scaleval > 0.0);
734 if( (file = fopen(filename,
"w")) ==
NULL )
741 fprintf(file,
"%s\n", probname);
742 fprintf(file,
"%d\n", tcliquegraph->nnodes);
743 fprintf(file,
"%d\n", tcliquegraph->nedges);
746 for( i = 0; i < tcliquegraph->nnodes; i++ )
747 fprintf(file,
"%f\n", (
double)tcliquegraph->weights[i]/scaleval);
750 for( i = 0; i < tcliquegraph->nnodes; i++ )
752 for( j = tcliquegraph->adjedges[i].first; j < tcliquegraph->adjedges[i].last; j++ )
753 fprintf(file,
"%d %d\n", i, tcliquegraph->adjnodes[j]);
767 assert(tcliquegraph !=
NULL);
769 return tcliquegraph->nedges + tcliquegraph->ncachededges;
777 assert(tcliquegraph !=
NULL);
778 assert(tcliquegraph->ncachededges == 0);
780 return tcliquegraph->degrees;
788 assert(tcliquegraph !=
NULL);
789 assert(tcliquegraph->ncachededges == 0);
791 return tcliquegraph->adjnodes;
803 assert(tcliquegraph !=
NULL);
804 assert(tcliquegraph->ncachededges == 0);
805 assert(0 <= node && node < tcliquegraph->nnodes);
807 adjedges = tcliquegraph->adjedges;
808 assert(adjedges !=
NULL);
809 assert(adjedges[node].first >= 0);
813 assert(adjnodes !=
NULL);
815 return &adjnodes[adjedges[node].first];
830 assert(tcliquegraph !=
NULL);
831 assert(tcliquegraph->ncachededges == 0);
832 assert(0 <= node && node < tcliquegraph->nnodes);
834 adjedges = tcliquegraph->adjedges;
838 assert(adjedges !=
NULL);
839 assert(degrees[node] == 0 || adjedges[node].last-1 >= 0);
842 assert(adjedges[node].last - adjedges[node].first == degrees[node]);
845 assert(adjnodes !=
NULL);
847 return &adjnodes[adjedges[node].last-1];
859 assert(tcliquegraph !=
NULL);
860 assert(tcliquegraph->ncachededges == 0);
863 weights = tcliqueGetWeights(tcliquegraph);
866 for( i = 0; i < tcliqueGetNNodes(tcliquegraph); i++ )
871 infoMessage(
"node %d: weight=%d, degree=%d, adjnodes=\n[ ", i, weights[i], degrees[i]);
875 assert(lastadjedge + 1 - currentadjedge == degrees[i]);
877 for( ; currentadjedge <= lastadjedge; currentadjedge++ )