examples/mfvsp.mod
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
/* MFVSP, Minimum Feedback Vertex Set Problem */
alpar@1
     2
alpar@1
     3
/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
alpar@1
     4
alpar@1
     5
/* The Minimum Feedback Vertex Set Problem for a given directed graph
alpar@1
     6
   G = (V, E), where V is a set of vertices and E is a set of arcs, is
alpar@1
     7
   to find a minimal subset of vertices, which being removed from the
alpar@1
     8
   graph make it acyclic.
alpar@1
     9
alpar@1
    10
   Reference:
alpar@1
    11
   Garey, M.R., and Johnson, D.S. (1979), Computers and Intractability:
alpar@1
    12
   A guide to the theory of NP-completeness [Graph Theory, Covering and
alpar@1
    13
   Partitioning, Minimum Feedback Vertex Set, GT8]. */
alpar@1
    14
alpar@1
    15
param n, integer, >= 0;
alpar@1
    16
/* number of vertices */
alpar@1
    17
alpar@1
    18
set V, default 1..n;
alpar@1
    19
/* set of vertices */
alpar@1
    20
alpar@1
    21
set E, within V cross V,
alpar@1
    22
default setof{i in V, j in V: i <> j and Uniform(0,1) <= 0.15} (i,j);
alpar@1
    23
/* set of arcs */
alpar@1
    24
alpar@1
    25
printf "Graph has %d vertices and %d arcs\n", card(V), card(E);
alpar@1
    26
alpar@1
    27
var x{i in V}, binary;
alpar@1
    28
/* x[i] = 1 means that i is a feedback vertex */
alpar@1
    29
alpar@1
    30
/* It is known that a digraph G = (V, E) is acyclic if and only if its
alpar@1
    31
   vertices can be assigned numbers from 1 to |V| in such a way that
alpar@1
    32
   k[i] + 1 <= k[j] for every arc (i->j) in E, where k[i] is a number
alpar@1
    33
   assigned to vertex i. We may use this condition to require that the
alpar@1
    34
   digraph G = (V, E \ E'), where E' is a subset of feedback arcs, is
alpar@1
    35
   acyclic. */
alpar@1
    36
alpar@1
    37
var k{i in V}, >= 1, <= card(V);
alpar@1
    38
/* k[i] is a number assigned to vertex i */
alpar@1
    39
alpar@1
    40
s.t. r{(i,j) in E}: k[j] - k[i] >= 1 - card(V) * (x[i] + x[j]);
alpar@1
    41
/* note that x[i] = 1 or x[j] = 1 leads to a redundant constraint */
alpar@1
    42
alpar@1
    43
minimize obj: sum{i in V} x[i];
alpar@1
    44
/* the objective is to minimize the cardinality of a subset of feedback
alpar@1
    45
   vertices */
alpar@1
    46
alpar@1
    47
solve;
alpar@1
    48
alpar@1
    49
printf "Minimum feedback vertex set:\n";
alpar@1
    50
printf{i in V: x[i]} "%d\n", i;
alpar@1
    51
alpar@1
    52
data;
alpar@1
    53
alpar@1
    54
/* The optimal solution is 3 */
alpar@1
    55
alpar@1
    56
param n := 15;
alpar@1
    57
alpar@1
    58
set E := 1 2, 2 3, 3 4, 3 8, 4 9, 5 1, 6 5, 7 5, 8 6, 8 7, 8 9, 9 10,
alpar@1
    59
         10 11, 10 14, 11 15, 12 7, 12 8, 12 13, 13 8, 13 12, 13 14,
alpar@1
    60
         14 9, 15 14;
alpar@1
    61
alpar@1
    62
end;