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.

152 lines
4.8 KiB

2 months ago
  1. /* Graceful Tree Labeling Problem */
  2. /* Author: Mike Appleby <mike@app.leby.org> */
  3. /* The Graceful Labeling Problem for a tree G = (V, E), where V is the
  4. set of vertices and E is the set of edges, is to find a labeling of
  5. the vertices with the integers between 1 and |V| inclusive, such
  6. that no two vertices share a label, and such that each edge is
  7. uniquely identified by the positive, or absolute difference between
  8. the labels of its endpoints.
  9. In other words, if vl are the vertex labels and el are the edge
  10. labels, then for every edge (u,v) in E, el[u,v]=abs(vl[u] - vl[v]).
  11. https://en.wikipedia.org/wiki/Graceful_labeling */
  12. set V;
  13. /* set of vertices */
  14. set E within V cross V;
  15. /* set of edges */
  16. set N := 1..card(V);
  17. /* vertex labels */
  18. set M := 1..card(V)-1;
  19. /* edge labels */
  20. var vx{V, N}, binary;
  21. /* binary encoding of vertex labels.
  22. vx[v,n] == 1 means vertex v has label n. */
  23. s.t. vxa{v in V}: sum{n in N} vx[v,n] = 1;
  24. /* each vertex is assigned exactly one label. */
  25. s.t. vxb{n in N}: sum{v in V} vx[v,n] = 1;
  26. /* each label is assigned to exactly one vertex. */
  27. var vl{V}, integer, >= 1, <= card(V);
  28. /* integer encoding of vertex labels.
  29. vl[v] == n means vertex v has label n. */
  30. s.t. vla{v in V}: vl[v] = sum{n in N} n * vx[v,n];
  31. /* by constraint vxa, exactly one of vx[v,n] == 1 and the rest are
  32. zero. So if vx[v,3] == 1, then vl[v] = 3. */
  33. var ex{E, M}, binary;
  34. /* binary encoding of edge labels.
  35. ex[u,v,n] == 1 means edge (u,v) has label n. */
  36. s.t. exa{(u,v) in E}: sum{m in M} ex[u,v,m] = 1;
  37. /* each edge is assigned exactly one label. */
  38. s.t. exb{m in M}: sum{(u,v) in E} ex[u,v,m] = 1;
  39. /* each label is assigned to exactly one edge. */
  40. var el{E}, integer, >= 1, <= card(E);
  41. /* integer encoding of edge labels.
  42. el[u,v] == n means edge (u,v) has label n. */
  43. s.t. ela{(u,v) in E}: el[u,v] = sum{m in M} m * ex[u,v,m];
  44. /* similar to vla above, define integer encoding of edge labels in
  45. terms of the corresponding binary variable. */
  46. var gt{E}, binary;
  47. /* gt[u,v] = 1 if vl[u] > vl[v] else 0.
  48. gt helps encode the absolute value constraint, below. */
  49. s.t. elb{(u,v) in E}: el[u,v] >= vl[u] - vl[v];
  50. s.t. elc{(u,v) in E}: el[u,v] <= vl[u] - vl[v] + 2*card(V)*(1-gt[u,v]);
  51. s.t. eld{(u,v) in E}: el[u,v] >= vl[v] - vl[u];
  52. s.t. ele{(u,v) in E}: el[u,v] <= vl[v] - vl[u] + 2*card(V)*gt[u,v];
  53. /* These four constraints together model the absolute value constraint
  54. of the graceful labeling problem: el[u,v] == abs(vl[u] - vl[v]).
  55. However, since the absolute value is a non-linear function, we
  56. transform it into a series of linear constraints, as above.
  57. To see that these four constraints model the absolute value
  58. condition, consider the following cases:
  59. if vl[u] > vl[v] and gt[u,v] == 0 then
  60. - ele is unsatisfiable, since the constraint ele amounts to
  61. el[u,v] <= vl[v] - vl[u] + 0 (since gt[u,v] == 0)
  62. <= -1 (since vl[u] > vl[v])
  63. but el[u,v] is declared with lower bound >= 1; hence, the
  64. constraints cannot be satisfied if vl[u] > vl[v] and
  65. gt[u,v] == 0.
  66. if vl[u] > vl[v] and gt[u,v] == 1 then
  67. - elb and elc together are equivalent to
  68. vl[u] - vl[v] <= el[u,v] <= vl[u] - vl[v], i.e.
  69. el[u,v] = vl[u] - vl[v]
  70. = abs(vl[u] - vl[v]) (since vl[u] > vl[v])
  71. - eld and elc together are equivalent to
  72. vl[v] - vl[u] <= el[u,v] <= vl[v] - vl[u] + 2|V|
  73. the tightest possible bounds are
  74. -1 <= el[u,v] <= |V|+1
  75. which is satisfied since both bounds are looser than the
  76. constraints on el's variable declaration, namely
  77. var el{E}, integer, >= 1, <= card(E);
  78. where |E| = |V|-1
  79. The cases for vl[v] > vl[u] are similar, but with roles reversed
  80. for elb/elc and eld/ele.
  81. In other words, when vl[u] > vl[v], then gt[u,v] == 1, elb and elc
  82. together model the absolute value constraint, and ele and eld are
  83. satisfied due to bounds constraints on el. When vl[v] > vl[u], then
  84. gt[u,v] == 0, ele and eld model the absolute value constraint, and
  85. elb and elc are satisfied due to bounds constraints on el.
  86. Note that vl[u] != vl[v], by a combination of constraints vxa, vxb,
  87. and vla. */
  88. solve;
  89. check 0 = card(N symdiff setof{v in V} vl[v]);
  90. /* every vertex label is assigned to one vertex */
  91. check 0 = card(M symdiff setof{(u,v) in E} el[u,v]);
  92. /* every edge label is assigned to one edge */
  93. check {(u,v) in E} el[u,v] = abs(vl[u] - vl[v]);
  94. /* every edge label for every edge (u,v) == abs(vl[u] - vl[v]) */
  95. printf "vertices:\n";
  96. for{v in V} { printf "\t%s: %d\n", v, vl[v]; }
  97. printf "edges:\n";
  98. printf "\torig\tvlabel\telabel\tabs(u-v)\n";
  99. for{(u,v) in E} {
  100. printf "\t(%s,%s)\t(%d,%d)\t%d\t%d\n",
  101. u, v, vl[u], vl[v], el[u,v], abs(vl[u]-vl[v]);
  102. }
  103. data;
  104. set V := a b c d e f g;
  105. set E := a b, a d, a g, b c, b e, e f;
  106. end;