/* * The author of this software is Michael Trick. Copyright (c) 1994 by * Michael Trick. * * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR DOES NOT MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ /* COLOR.C: Easy code for graph coloring Author: Michael A. Trick, Carnegie Mellon University, trick+@cmu.edu Last Modified: November 2, 1994 Graph is input in a file. First line contains the number of nodes and edges. All following contain the node numbers (from 1 to n) incident to each edge. Sample: 4 4 1 2 2 3 3 4 1 4 represents a four node cycle graph. Code is probably insufficiently debugged, but may be useful to some people. For more information on this code, see Anuj Mehrotra and Michael A. Trick, "A column generation approach to graph coloring", GSIA Technical report series. */ #include #include #include #include #include #define MAX_RAND (2.0*(1 << 30)) #define MAX_NODE 600 #define TRUE 1 #define FALSE 0 #define INF 100000.0 struct tms buffer; /* structure for timing */ int current_time,start_time; double utime; int adj[MAX_NODE][MAX_NODE]; int BestColoring; int num_node; int ColorClass[MAX_NODE]; int prob_count; int Order[MAX_NODE]; int Handled[MAX_NODE]; int ColorAdj[MAX_NODE][MAX_NODE]; int ColorCount[MAX_NODE]; int lb; int num_prob,max_prob; int best_clique; int greedy_clique(valid,clique) int *valid,*clique; { int i,j,k; int max; int place,done; int *order; int weight[MAX_NODE]; for (i=0;i max_prob) return -1; for (j=0;j=target) return incumb; if (incumb > best_clique) { best_clique=incumb; /* printf("Clique of size %5d found.\n",best_clique);*/ } /* printf("Greedy gave %f\n",incumb);*/ place = 0; for (i=0;i incumb) { /* printf("Taking new\n");*/ incumb = new_weight+1; for (k=0;k best_clique) { best_clique=incumb; /* printf("Clique of size %5d found.\n",best_clique);*/ } } /* else printf("Taking incumb\n");*/ free(valid1); free(clique1); if (incumb >=target) break; } free(order); return(incumb); } AssignColor(node,color) int node,color; { int node1; /* printf(" %d color +%d\n",node,color);*/ ColorClass[node] = color; for (node1=0;node1=BestColoring) return(current_color); if (BestColoring <=lb) return(BestColoring); if (i >= num_node) return(current_color); /* printf("Node %d, num_color %d\n",i,current_color);*/ /* Find node with maximum color_adj */ max = -1; place = -1; for(k=0;k 0) count++; if (count!=ColorCount[k]) printf("Trouble with color count\n"); */ /* printf("ColorCount[%3d] = %d\n",k,ColorCount[k]);*/ if ((ColorCount[k] > max) || ((ColorCount[k]==max)&&(ColorAdj[k][0]>ColorAdj[place][0]))) { /* printf("Best now at %d\n",k);*/ max = ColorCount[k]; place = k; } } if (place==-1) { printf("Graph is disconnected. This code needs to be updated for that case.\n"); exit(1); } Order[i] = place; Handled[place] = TRUE; /* printf("Using node %d at level %d\n",place,i);*/ for (j=1;j<=current_color;j++) { if (!ColorAdj[place][j]) { ColorClass[place] = j; AssignColor(place,j); new_val = color(i+1,current_color); if (new_val < BestColoring){ BestColoring = new_val; print_colors(); } RemoveColor(place,j); if (BestColoring<=current_color) { Handled[place] = FALSE; return(BestColoring); } } } if (current_color+1 < BestColoring) { ColorClass[place] = current_color+1; AssignColor(place,current_color+1); new_val = color(i+1,current_color+1); if (new_val < BestColoring) { BestColoring = new_val; print_colors(); } RemoveColor(place,current_color+1); } Handled[place] = FALSE; return(BestColoring); } print_colors() { int i,j; times(&buffer); current_time = buffer.tms_utime; printf("Best coloring is %d at time %7.1f\n",BestColoring,(current_time-start_time)/60.0); /* for (i=0;i=max_prob) printf(" (not confirmed)\n"); else printf("\n"); val = color(place,place); times(&buffer); current_time=buffer.tms_utime; printf("Best coloring has value %d, subproblems: %d time:%7.1f\n",val,prob_count,(current_time-start_time)/60.0); }