examples/color.mod
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

- Generated files and doc/notes are removed
alpar@1
     1
/* COLOR, Graph Coloring Problem */
alpar@1
     2
alpar@1
     3
/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
alpar@1
     4
alpar@1
     5
/* Given an undirected loopless graph G = (V, E), where V is a set of
alpar@1
     6
   nodes, E <= V x V is a set of arcs, the Graph Coloring Problem is to
alpar@1
     7
   find a mapping (coloring) F: V -> C, where C = {1, 2, ... } is a set
alpar@1
     8
   of colors whose cardinality is as small as possible, such that
alpar@1
     9
   F(i) != F(j) for every arc (i,j) in E, that is adjacent nodes must
alpar@1
    10
   be assigned different colors. */
alpar@1
    11
alpar@1
    12
param n, integer, >= 2;
alpar@1
    13
/* number of nodes */
alpar@1
    14
alpar@1
    15
set V := {1..n};
alpar@1
    16
/* set of nodes */
alpar@1
    17
alpar@1
    18
set E, within V cross V;
alpar@1
    19
/* set of arcs */
alpar@1
    20
alpar@1
    21
check{(i,j) in E}: i != j;
alpar@1
    22
/* there must be no loops */
alpar@1
    23
alpar@1
    24
/* We need to estimate an upper bound of the number of colors |C|.
alpar@1
    25
   The number of nodes |V| can be used, however, for sparse graphs such
alpar@1
    26
   bound is not very good. To obtain a more suitable estimation we use
alpar@1
    27
   an easy "greedy" heuristic. Let nodes 1, ..., i-1 are already
alpar@1
    28
   assigned some colors. To assign a color to node i we see if there is
alpar@1
    29
   an existing color not used for coloring nodes adjacent to node i. If
alpar@1
    30
   so, we use this color, otherwise we introduce a new color. */
alpar@1
    31
alpar@1
    32
set EE := setof{(i,j) in E} (i,j) union setof{(i,j) in E} (j,i);
alpar@1
    33
/* symmetrisized set of arcs */
alpar@1
    34
alpar@1
    35
param z{i in V, case in 0..1} :=
alpar@1
    36
/* z[i,0] = color index assigned to node i
alpar@1
    37
   z[i,1] = maximal color index used for nodes 1, 2, ..., i-1 which are
alpar@1
    38
            adjacent to node i */
alpar@1
    39
(  if case = 0 then
alpar@1
    40
   (  /* compute z[i,0] */
alpar@1
    41
      min{c in 1..z[i,1]}
alpar@1
    42
      (  if not exists{j in V: j < i and (i,j) in EE} z[j,0] = c then
alpar@1
    43
            c
alpar@1
    44
         else
alpar@1
    45
            z[i,1] + 1
alpar@1
    46
      )
alpar@1
    47
   )
alpar@1
    48
   else
alpar@1
    49
   (  /* compute z[i,1] */
alpar@1
    50
      if not exists{j in V: j < i} (i,j) in EE then
alpar@1
    51
         1
alpar@1
    52
      else
alpar@1
    53
         max{j in V: j < i and (i,j) in EE} z[j,0]
alpar@1
    54
   )
alpar@1
    55
);
alpar@1
    56
alpar@1
    57
check{(i,j) in E}: z[i,0] != z[j,0];
alpar@1
    58
/* check that all adjacent nodes are assigned distinct colors */
alpar@1
    59
alpar@1
    60
param nc := max{i in V} z[i,0];
alpar@1
    61
/* number of colors used by the heuristic; obviously, it is an upper
alpar@1
    62
   bound of the optimal solution */
alpar@1
    63
alpar@1
    64
display nc;
alpar@1
    65
alpar@1
    66
var x{i in V, c in 1..nc}, binary;
alpar@1
    67
/* x[i,c] = 1 means that node i is assigned color c */
alpar@1
    68
alpar@1
    69
var u{c in 1..nc}, binary;
alpar@1
    70
/* u[c] = 1 means that color c is used, i.e. assigned to some node */
alpar@1
    71
alpar@1
    72
s.t. map{i in V}: sum{c in 1..nc} x[i,c] = 1;
alpar@1
    73
/* each node must be assigned exactly one color */
alpar@1
    74
alpar@1
    75
s.t. arc{(i,j) in E, c in 1..nc}: x[i,c] + x[j,c] <= u[c];
alpar@1
    76
/* adjacent nodes cannot be assigned the same color */
alpar@1
    77
alpar@1
    78
minimize obj: sum{c in 1..nc} u[c];
alpar@1
    79
/* objective is to minimize the number of colors used */
alpar@1
    80
alpar@1
    81
data;
alpar@1
    82
alpar@1
    83
/* These data correspond to the instance myciel3.col from:
alpar@1
    84
   http://mat.gsia.cmu.edu/COLOR/instances.html */
alpar@1
    85
alpar@1
    86
/* The optimal solution is 4 */
alpar@1
    87
alpar@1
    88
param n := 11;
alpar@1
    89
alpar@1
    90
set E :=
alpar@1
    91
 1 2
alpar@1
    92
 1 4
alpar@1
    93
 1 7
alpar@1
    94
 1 9
alpar@1
    95
 2 3
alpar@1
    96
 2 6
alpar@1
    97
 2 8
alpar@1
    98
 3 5
alpar@1
    99
 3 7
alpar@1
   100
 3 10
alpar@1
   101
 4 5
alpar@1
   102
 4 6
alpar@1
   103
 4 10
alpar@1
   104
 5 8
alpar@1
   105
 5 9
alpar@1
   106
 6 11
alpar@1
   107
 7 11
alpar@1
   108
 8 11
alpar@1
   109
 9 11
alpar@1
   110
 10 11
alpar@1
   111
;
alpar@1
   112
alpar@1
   113
end;