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.

83 lines
2.0 KiB

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