Exemple #1
0
int main(int argc, char **argv){
	Graph g;
	int i;
	int j;
	
	g = graph_create(TEST_SIZE);
	assert(graph_vertex_count(g) == TEST_SIZE);
	
	for (i = 0; i < TEST_SIZE; i++){
		for (j = 0; j < TEST_SIZE; j++){
			assert(graph_has_edge(g, i, j) == 0);
		}
	}
	
	for (i = 0; i < TEST_SIZE; i++){
		assert(graph_out_degree(g, i) == 0);
		graph_foreach(g, i, match_sink, 0);
	}
	
	assert(graph_edge_count(g) == 0);
	
	for (i = 0; i < TEST_SIZE; i++){
		for (j = 0; j < TEST_SIZE; j++){
			if (i < j){
				graph_add_edge(g, i, j);
			}
		}
	}
	
	for (i = 0; i < TEST_SIZE; i++){
		for (j = 0; j < TEST_SIZE; j++){
			assert(graph_has_edge(g, i, j) == (i < j));
		}
	}
	
	assert(graph_edge_count(g) == (TEST_SIZE*(TEST_SIZE-1)/2));
	
	graph2dot(g);
	
	graph_destroy(g);
	
	return 0;
}
Exemple #2
0
boolean graph_write_dimacs_binary(graph_t *g, char *comment,FILE *fp) {
	char *buf;
	char *header=NULL;
	int headersize=0;
	int headerlength=0;
	int i,j;

	ASSERT((sizeof(setelement)*8)==ELEMENTSIZE);
	ASSERT(graph_test(g,NULL));
	ASSERT(fp!=NULL);

	buf=malloc(MAX(1024,g->n/8+1));
	header=malloc(1024);
	header[0]=0;
	headersize=1024;
	if (comment) {
		strcpy(buf,"c ");
		strncat(buf,comment,1000);
		strcat(buf,"\n");
		STR_APPEND(buf);
	}
	sprintf(buf,"p edge %d %d\n",g->n,graph_edge_count(g));
	STR_APPEND(buf);
	for (i=0; i < g->n; i++) {
		if (g->weights[i]!=1) {
			sprintf(buf,"n %d %d\n",i+1,g->weights[i]);
			STR_APPEND(buf);
		}
	}

	fprintf(fp,"%d\n",(int)strlen(header));
	fprintf(fp,"%s",header);
	free(header);

	for (i=0; i < g->n; i++) {
		memset(buf,0,i/8+1);
		for (j=0; j<i; j++) {
			if (GRAPH_IS_EDGE_FAST(g,i,j)) {
				buf[j/8] |= SET_BIT_MASK(7-j%8);
			}
		}
		fwrite(buf,1,i/8+1,fp);
	}
	free(buf);
	return TRUE;
}
Exemple #3
0
/*
 * graph_write_dimacs_ascii()
 *
 * Writes an ASCII dimacs-format file of graph g, with comment, to the
 * file stream fp.
 *
 * Returns TRUE if successful, FALSE if an error occurred.
 */
boolean graph_write_dimacs_ascii(graph_t *g, char *comment, FILE *fp) {
	int i,j;

	ASSERT((sizeof(setelement)*8)==ELEMENTSIZE);
	ASSERT(graph_test(g,NULL));
	ASSERT(fp!=NULL);

	if (comment)
		fprintf(fp,"c %s\n",comment);
	fprintf(fp,"p edge %d %d\n",g->n,graph_edge_count(g));
	for (i=0; i < g->n; i++)
		if (g->weights[i]!=1)
			fprintf(fp,"n %d %d\n",i+1,g->weights[i]);
	for (i=0; i < g->n; i++)
		for (j=0; j<i; j++)
			if (GRAPH_IS_EDGE_FAST(g,i,j))
				fprintf(fp,"e %d %d\n",i+1,j+1);
	return TRUE;
}
Exemple #4
0
/*
 * graph_print()
 *
 * Prints a representation of the graph g to stdout (along with any errors
 * noticed).  Mainly useful for debugging purposes and trivial output.
 *
 * The output consists of a first line describing the dimensions and then
 * one line per vertex containing the vertex number (numbered 0,...,n-1),
 * the vertex weight (if the graph is weighted), "->" and then a list
 * of all vertices it is adjacent to.
 */
void graph_print(graph_t *g) {
	int i,j;
	int asymm=0;
	int refl=0;
	int nonpos=0;
	int extra=0;
	unsigned int weight=0;
	boolean weighted;
	
	ASSERT((sizeof(setelement)*8)==ELEMENTSIZE);

	if (g==NULL) {
		printf("   WARNING: Graph pointer is NULL!\n");
		return;
	}
	if (g->n <= 0) {
		printf("   WARNING: Graph has %d vertices "
		       "(should be positive)!\n",g->n);
		return;
	}
	
	weighted=graph_weighted(g);

	printf("%s graph has %d vertices, %d edges (density %.2f).\n",
	       weighted?"Weighted":((g->weights[0]==1)?
				    "Unweighted":"Semi-weighted"),
	       g->n,graph_edge_count(g),
	       (float)graph_edge_count(g)/((float)(g->n - 1)*(g->n)/2));

	for (i=0; i < g->n; i++) {
		printf("%2d",i);
		if (weighted) {
			printf(" w=%d",g->weights[i]);
			if (g->weights[i] <= 0) {
				printf("*NON-POSITIVE*");
				nonpos++;
			}
		}
		if (weight < INT_MAX)
			weight+=g->weights[i];
		printf(" ->");
		for (j=0; j < g->n; j++) {
			if (SET_CONTAINS_FAST(g->edges[i],j)) {
				printf(" %d",j);
				if (i==j) {
					printf("*REFLEXIVE*");
					refl++;
				}
				if (!SET_CONTAINS_FAST(g->edges[j],i)) {
					printf("*ASYMMERTIC*");
					asymm++;
				}
			}
		}
		for (j=g->n; j < SET_ARRAY_LENGTH(g->edges[i])*ELEMENTSIZE;
		     j++) {
			if (SET_CONTAINS_FAST(g->edges[i],j)) {
				printf(" %d*NON-EXISTENT*",j);
				extra++;
			}
		}
		printf("\n");
	}

	if (asymm)
		printf("   WARNING: Graph contained %d asymmetric edges!\n",
		       asymm);
	if (refl)
		printf("   WARNING: Graph contained %d reflexive edges!\n",
		       refl);
	if (nonpos)
		printf("   WARNING: Graph contained %d non-positive vertex "
		       "weights!\n",nonpos);
	if (extra)
		printf("   WARNING: Graph contained %d edges to "
		       "non-existent vertices!\n",extra);
	if (weight>=INT_MAX)
		printf("   WARNING: Total graph weight >= INT_MAX!\n");
	return;
}