1 /* MAXFLOW, Maximum Flow Problem */
3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
5 /* The Maximum Flow Problem in a network G = (V, E), where V is a set
6 of nodes, E within V x V is a set of arcs, is to maximize the flow
7 from one given node s (source) to another given node t (sink) subject
8 to conservation of flow constraints at each node and flow capacities
11 param n, integer, >= 2;
14 set V, default {1..n};
17 set E, within V cross V;
20 param a{(i,j) in E}, > 0;
21 /* a[i,j] is capacity of arc (i,j) */
23 param s, symbolic, in V, default 1;
26 param t, symbolic, in V, != s, default n;
29 var x{(i,j) in E}, >= 0, <= a[i,j];
30 /* x[i,j] is elementary flow through arc (i,j) to be found */
33 /* total flow from s to t */
36 /* node[i] is conservation constraint for node i */
38 sum{(j,i) in E} x[j,i] + (if i = s then flow)
39 /* summary flow into node i through all ingoing arcs */
41 = /* must be equal to */
43 sum{(i,j) in E} x[i,j] + (if i = t then flow);
44 /* summary flow from node i through all outgoing arcs */
47 /* objective is to maximize the total flow through the network */
51 printf{1..56} "="; printf "\n";
52 printf "Maximum flow from node %s to node %s is %g\n\n", s, t, flow;
53 printf "Starting node Ending node Arc capacity Flow in arc\n";
54 printf "------------- ----------- ------------ -----------\n";
55 printf{(i,j) in E: x[i,j] != 0}: "%13s %11s %12g %11g\n", i, j,
57 printf{1..56} "="; printf "\n";
61 /* These data correspond to an example from [Christofides]. */
63 /* Optimal solution is 29 */