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