set students; set nodes ordered; # set of classes in this case set exams{students} within nodes; # set of exams for each student set arcs := {i in nodes, j in nodes: ord(i) < ord(j) and exists {s in students} ((i in exams[s]) and (j in exams[s]))}; # there is an arc between nodes (classes) i and j # if there is a student who takes both classes set colors; # set of exam times in this case var use{c in colors} binary; # is 1 if color c is used, 0 otherwise var assign{i in nodes, c in colors} binary; # is 1 if node i has color c, 0 otherwise minimize number_of_colors: sum{c in colors} use[c]; s.t. one_color_for_each_node{i in nodes}: sum{c in colors} assign[i,c] = 1; s.t. different_colors_for_adjacent_nodes {c in colors, i in nodes, j in nodes: (i,j) in arcs}: assign[i,c] + assign[j,c] <= 1; s.t. assign_only_chosen_colors{i in nodes, c in colors}: assign[i,c] <= use[c]; data; set colors:= 8am 10am 12pm 2pm 4pm 6pm; set nodes:= Math English Biology Chemistry Compscience Geography Psychology Spanish History French; set students:= 1 2 3 4 5 6 7; set exams[1]:= Math English Biology Chemistry ; set exams[2]:= Math English Compscience Geography; set exams[3]:= Biology Psychology Geography Spanish; set exams[4]:= Biology Compscience History French; set exams[5]:= English Psychology Compscience History; set exams[6]:= Psychology Chemistry Compscience French; set exams[7]:= Psychology Geography History Spanish;