/* Title: Graph generator utility functions file: utility.c Does: various functions: handle the edges in the adjacency matrix, random numbers, get graph degree statistics, shuffle a vertex permutation Source: Joseph Culberson's Coloring Page http://web.cs.ualberta.ca/~joe/Coloring/index.html Author: Joseph Culberson new functions added by Denis Papp in May, 1995 email: joe@cs.ualberta.ca dpapp@cs.ualberta.ca Compilation: C Code Copyright: Joseph Culberson. Permission is granted to use and distribute freely for non-commercial purposes with appropriate attribution. Warranty: NONE! Special Instructions: Have fun! Part of: The k-colorable graph generator program. */ #include "generate.h" #include #include extern void bzero(char *,int); /* EDGES IN GRAPHS */ char masks[ 8 ] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; /* set_edge() * GIVEN: vertices u/v, value to set x, graph * DESC: sets the appropriate bit in the graph to x * since the graph is symmetric it only sets the lower triangle */ void set_edge( register int u, register int v, char x, graphtype graph ) { register int i,j, byte, bit, mask; if (u> v) { i= u; j=v;} else {j=u;i=v;} bit = 7 - (j & 0x00000007); byte = j >> 3; mask = masks[bit]; if ( x == 1 ) graph[i][byte] |= mask; else graph[i][byte] &= ~mask; } /* get_edge() * GIVEN: vertices u/v, graph * RETURNS: what the appropriate bit in the graph is set to * NOTE: since the graph is symmetric it only looks at the lower triangle */ char get_edge( int u, int v, graphtype graph ) { int i,j, byte, bit; char mask; if (u>v) {i=u; j=v;} else {j=u; i=v;} bit = 7-(j & 0x00000007); byte = j >> 3; mask = masks[bit]; return( (graph[i][byte] & mask)==mask ); } /* cleargraph() * GIVEN: a graph * DESC: sets the graph to all 0s */ void cleargraph(graphtype graph ) { bzero((char *)graph,MAX_NR_VERTICES*MAX_NR_VERTICESdiv8); } /* * initrandom() * DESC: inputs a random seed and seeds the generator with xsrandom * RETURNS: the seed used */ int initrandom() { int seed; printf("Please enter a seed for the random number generator: "); scanf("%d",&seed); printf("The seed is %d\n",seed); xsrandom(seed); return(seed); } /* * dblrand() * RETURNS: uses xdrandom to return a double random between 0.0 and 1.0 */ double dblrand() { return (double) xdrandom(); } /* * rand() * RETURNS: users xrandom to return a long random between 0 and k-1 */ long rand(long k) { return (long) (xrandom() % k); } /* * create_and_shuffle() * GIVEN: an int array vset, the number of vertices (order) * DESC: creates a random order of vertices and stores in vset */ void create_and_shuffle(vset,order) int *vset,order; { long i,k,t; for(i=0;imax) max = degree[i]; total += degree[i]; totalsqr += degree[i] * degree[i]; } avg = total / order; intermediate =(totalsqr - (2*avg*total) + (avg * total))/order; if( intermediate >= 0.0 ) { std = sqrt(intermediate); } else { std = 0.0; } sprintf(temp, "c Degree Information:\nc Min:%d Avg:%f Max:%d Std:%f\n", (int)min,avg,(int)max,std); printf("%s\n",temp); strcat(string,temp); }