You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

  1. /* MFASP, Minimum Feedback Arc Set Problem */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* The Minimum Feedback Arc Set Problem for a given directed graph
  4. G = (V, E), where V is a set of vertices and E is a set of arcs, is
  5. to find a minimal subset of arcs, which being removed from the graph
  6. make it acyclic.
  7. Reference:
  8. Garey, M.R., and Johnson, D.S. (1979), Computers and Intractability:
  9. A guide to the theory of NP-completeness [Graph Theory, Covering and
  10. Partitioning, Minimum Feedback Arc Set, GT9]. */
  11. param n, integer, >= 0;
  12. /* number of vertices */
  13. set V, default 1..n;
  14. /* set of vertices */
  15. set E, within V cross V,
  16. default setof{i in V, j in V: i <> j and Uniform(0,1) <= 0.15} (i,j);
  17. /* set of arcs */
  18. printf "Graph has %d vertices and %d arcs\n", card(V), card(E);
  19. var x{(i,j) in E}, binary;
  20. /* x[i,j] = 1 means that (i->j) is a feedback arc */
  21. /* It is known that a digraph G = (V, E) is acyclic if and only if its
  22. vertices can be assigned numbers from 1 to |V| in such a way that
  23. k[i] + 1 <= k[j] for every arc (i->j) in E, where k[i] is a number
  24. assigned to vertex i. We may use this condition to require that the
  25. digraph G = (V, E \ E'), where E' is a subset of feedback arcs, is
  26. acyclic. */
  27. var k{i in V}, >= 1, <= card(V);
  28. /* k[i] is a number assigned to vertex i */
  29. s.t. r{(i,j) in E}: k[j] - k[i] >= 1 - card(V) * x[i,j];
  30. /* note that x[i,j] = 1 leads to a redundant constraint */
  31. minimize obj: sum{(i,j) in E} x[i,j];
  32. /* the objective is to minimize the cardinality of a subset of feedback
  33. arcs */
  34. solve;
  35. printf "Minimum feedback arc set:\n";
  36. printf{(i,j) in E: x[i,j]} "%d %d\n", i, j;
  37. data;
  38. /* The optimal solution is 3 */
  39. param n := 15;
  40. 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,
  41. 10 11, 10 14, 11 15, 12 7, 12 8, 12 13, 13 8, 13 12, 13 14,
  42. 14 9, 15 14;
  43. end;