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.

85 lines
2.7 KiB

  1. /* MAXCUT, Maximum Cut Problem */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* The Maximum Cut Problem in a network G = (V, E), where V is a set
  4. of nodes, E is a set of edges, is to find the partition of V into
  5. disjoint sets V1 and V2, which maximizes the sum of edge weights
  6. w(e), where edge e has one endpoint in V1 and other endpoint in V2.
  7. Reference:
  8. Garey, M.R., and Johnson, D.S. (1979), Computers and Intractability:
  9. A guide to the theory of NP-completeness [Network design, Cuts and
  10. Connectivity, Maximum Cut, ND16]. */
  11. set E, dimen 2;
  12. /* set of edges */
  13. param w{(i,j) in E}, >= 0, default 1;
  14. /* w[i,j] is weight of edge (i,j) */
  15. set V := (setof{(i,j) in E} i) union (setof{(i,j) in E} j);
  16. /* set of nodes */
  17. var x{i in V}, binary;
  18. /* x[i] = 0 means that node i is in set V1
  19. x[i] = 1 means that node i is in set V2 */
  20. /* We need to include in the objective function only that edges (i,j)
  21. from E, for which x[i] != x[j]. This can be modeled through binary
  22. variables s[i,j] as follows:
  23. s[i,j] = x[i] xor x[j] = (x[i] + x[j]) mod 2, (1)
  24. where s[i,j] = 1 iff x[i] != x[j], that leads to the following
  25. objective function:
  26. z = sum{(i,j) in E} w[i,j] * s[i,j]. (2)
  27. To describe "exclusive or" (1) we could think that s[i,j] is a minor
  28. bit of the sum x[i] + x[j]. Then introducing binary variables t[i,j],
  29. which represent a major bit of the sum x[i] + x[j], we can write:
  30. x[i] + x[j] = s[i,j] + 2 * t[i,j]. (3)
  31. An easy check shows that conditions (1) and (3) are equivalent.
  32. Note that condition (3) can be simplified by eliminating variables
  33. s[i,j]. Indeed, from (3) it follows that:
  34. s[i,j] = x[i] + x[j] - 2 * t[i,j]. (4)
  35. Since the expression in the right-hand side of (4) is integral, this
  36. condition can be rewritten in the equivalent form:
  37. 0 <= x[i] + x[j] - 2 * t[i,j] <= 1. (5)
  38. (One might note that (5) means t[i,j] = x[i] and x[j].)
  39. Substituting s[i,j] from (4) to (2) leads to the following objective
  40. function:
  41. z = sum{(i,j) in E} w[i,j] * (x[i] + x[j] - 2 * t[i,j]), (6)
  42. which does not include variables s[i,j]. */
  43. var t{(i,j) in E}, binary;
  44. /* t[i,j] = x[i] and x[j] = (x[i] + x[j]) div 2 */
  45. s.t. xor{(i,j) in E}: 0 <= x[i] + x[j] - 2 * t[i,j] <= 1;
  46. /* see (4) */
  47. maximize z: sum{(i,j) in E} w[i,j] * (x[i] + x[j] - 2 * t[i,j]);
  48. /* see (6) */
  49. data;
  50. /* In this example the network has 15 nodes and 22 edges. */
  51. /* Optimal solution is 20 */
  52. set E :=
  53. 1 2, 1 5, 2 3, 2 6, 3 4, 3 8, 4 9, 5 6, 5 7, 6 8, 7 8, 7 12, 8 9,
  54. 8 12, 9 10, 9 14, 10 11, 10 14, 11 15, 12 13, 13 14, 14 15;
  55. end;