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.

109 lines
3.5 KiB

  1. /* PLANARITY, Graph Planarity Testing */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* Given a graph G = (V, E), where V is a set of vertices and E is
  4. a set of edges (unordered pairs of vertices), this model tests if
  5. G is planar, and if it is, finds its faces for some embedding.
  6. It is assumed that G is loopless and the degree of any its vertex
  7. is at least 2.
  8. Though there exist linear-time algorithms to test graph planarity,
  9. this MIP feasibility model may be used, for example, to find
  10. an embedding subject to some additional constraints or an "optimal"
  11. embedding.
  12. This model is based on Mac Lane's planarity characterization that
  13. states that a finite undirected graph is planar iff the cycle space
  14. of the graph (in GF(2)) has a cycle basis in which each edge of the
  15. graph participates in at most two basis vectors. */
  16. param nv;
  17. /* number of vertices */
  18. set V := 1..nv;
  19. /* set of vertices */
  20. set E, within V cross V;
  21. /* set of edges */
  22. check{(i,j) in E} i <> j;
  23. /* graph must have no loops */
  24. set A := E union setof{(i,j) in E} (j,i);
  25. /* set of arcs, where every edge (i,j) gives two arcs i->j and j->i */
  26. check{i in V} sum{(i,j) in A} 1 >= 2;
  27. /* degree of any vertex must be at least 2 */
  28. param nf := 2 - nv + card(E);
  29. /* number of faces (including outer face) */
  30. set F := 1..nf;
  31. /* set of faces = set of vertices of dual graph */
  32. /* Let every face be assigned a unique color. Below we say that arc
  33. i->j has color f if on moving from vertex i to vertex j face f is
  34. located on the left to that arc. (Note that every face is defined
  35. by a cycle mentioned in Mac Lane's characterization. In this model
  36. cycles are constructed explicitly from arcs.) */
  37. var x{(i,j) in A, f in F}, binary;
  38. /* x[i,j,f] = 1 means that arc i->j has color f */
  39. s.t. r1{(i,j) in A}: sum{f in F} x[i,j,f] = 1;
  40. /* every arc must have exactly one color */
  41. s.t. r2{j in V, f in F}: sum{(i,j) in A} x[i,j,f] <= 1;
  42. /* two or more arcs of the same color must not enter the same vertex */
  43. s.t. r3{j in V, f in F}:
  44. sum{(i,j) in A} x[i,j,f] = sum{(j,i) in A} x[j,i,f];
  45. /* if arc of color f enters some vertex, exactly one arc of the same
  46. color must leave that vertex */
  47. s.t. r4{(i,j) in E, f in F}: x[i,j,f] + x[j,i,f] <= 1;
  48. /* arcs that correspond to the same edge must have different colors
  49. (to avoid invalid faces i->j->i) */
  50. s.t. r5{f in F}: sum{(i,j) in A} x[i,j,f] >= 1;
  51. /* every color must be used at least once */
  52. /* this is a feasibility problem, so no objective is needed */
  53. solve;
  54. printf "number of vertices = %d\n", nv;
  55. printf "number of edges = %d\n", card(A) / 2;
  56. printf "number of faces = %d\n", nf;
  57. for {f in F}
  58. { printf "face %d:", f;
  59. printf {(i,j) in A: x[i,j,f] = 1} " %d->%d", i, j;
  60. printf "\n";
  61. }
  62. data;
  63. /* The data below correspond to the following (planar) graph:
  64. 1 - 2 - - 3 - - 4
  65. | | |
  66. | | |
  67. 5 - 6 | |
  68. | \ | |
  69. | \ | |
  70. 7 - - - - 8 - - 9 - - - - 10- - -11
  71. | / | \ | |
  72. | / | \ | |
  73. | / | \ | |
  74. | / | \ | |
  75. 12 - - - -13 - - - - - - - 14- - -15 */
  76. param nv := 15;
  77. set E := (1,2) (1,5) (2,3) (3,4) (3,8) (4,9) (5,6) (5,7) (6,8)
  78. (7,8) (7,12) (8,9) (8,12) (8,13) (9,10) (9,14) (10,11)
  79. (10,14) (11,15) (12,13) (13,14) (14,15) ;
  80. end;