Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. UnitDistanceQ[input_]:=Module[
  2. {g, x, F, v, min, nod, gl},
  3. g = input;
  4. gl = Length[g]; (* Vertex Count *)
  5. (* 2 vertex count variables x1, x2, etc... *)
  6. x = Table[Symbol@@ToExpression["x" <> ToString[i]], {i, 1, 2*gl}];
  7. (* The force to minimize is the squared error of the lengths *)
  8. F = !(
  9. *UnderoverscriptBox[([Sum]), (i = 1), (gl)](
  10. *UnderoverscriptBox[([Sum]), (j = 1), (gl)]g[[i, j]] ((((x[[2 i]] - x[[2 j]]))^2 + ((x[[2 i - 1]] - x[[2 j - 1]]))^2 - 1))^2));
  11. (* Initial vertex positions *)
  12. v = Table[gl*Random[], {i, 1, 2*gl}];
  13. (* Minimization *)
  14. {min, nod} = FindMinimum[F, Transpose[{x,v}], Method->"QuasiNewton"];
  15. (* Output Solution *)
  16. If[min < 10^-3,
  17. Print[{min,nod}];
  18. GraphPlot[g, VertexCoordinateRules ->
  19. Thread[Range[gl] -> Partition[ x /. nod, 2]],
  20. AspectRatio -> Automatic,
  21. VertexLabeling -> None,
  22. ImageSize->Small]
  23. ]
  24. ]
  25.  
  26. g = Graph[
  27. UndirectedEdge @@@ {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 6}, {3,
  28. 8}, {4, 5}, {4, 9}, {4, 10}, {5, 6}, {5, 10}, {6, 7}, {6,
  29. 10}, {7, 8}, {7, 10}, {8, 9}, {8, 10}, {9, 10}}];
  30. m = AdjacencyMatrix[g];
  31. UnitDistanceQ[m]
  32. (* gives
  33. FindMinimum::sdprec: Line search unable
  34. to find a sufficient decrease in the function value with
  35. MachinePrecision digit precision. >>
  36. *)
  37.  
  38. isUnitDistance[graph_] := Module[
  39. {verts, edges, n, coords, p, x, soln},
  40. verts = VertexList[graph];
  41. edges = EdgeList[graph];
  42. n = Length[verts];
  43. verts = verts /. Thread[verts -> Range[n]];
  44. coords = Array[x, {n, 2}];
  45. {x[1, 1], x[1, 2], x[2, 1], x[2, 2]} = {0, 0, 1, 0};
  46. p[j_] := {x[j, 1], x[j, 2]};
  47. polys =
  48. Map[(p[#[[1]]] - p[#[[2]]]).(p[#[[1]]] - p[#[[2]]]) - 1 &, edges];
  49. Quiet[soln = NSolve[polys]];
  50. If[soln === {}, False, True]
  51. ]
  52.  
  53. SeedRandom[111111];
  54. gg = Graph@
  55. Union[Sort /@ (RandomInteger[{1, 8}, {20, 2}] /. {j_, j_} :>
  56. Sequence[])]
  57.  
  58. GraphData["GolombGraph"]
  59. GraphData["GolombGraph", "UnitDistance"]
  60.  
  61. Cases[{#, GraphData[#, "UnitDistance"]} & /@ GraphData[], {g_, True} :> g]
  62. Length@%
  63.  
  64. e = {(x1 - x2)^2 + (y1 - y2)^2 == 1, (x1 - x3)^2 + (y1 - y3)^2 == 1,
  65. (x1 - x4)^2 + (y1 - y4)^2 == 1, (x2 - x3)^2 + (y2 - y3)^2 == 1,
  66. (x4 - x5)^2 + (y4 - y5)^2 == 1, (x5 - x6)^2 + (y5 - y6)^2 == 1,
  67. (x6 - x7)^2 + (y6 - y7)^2 == 1, (x7 - x8)^2 + (y7 - y8)^2 == 1,
  68. (x8 - x9)^2 + (y8 - y9)^2 == 1, (x9 -x10)^2 + (y9 -y10)^2 == 1,
  69. x1 == 0, y1 == 0, x2 == 1, y2 == 0};
  70. Reduce[e, {x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10}]
  71.  
  72. Meas[G_, i_: 0] := Module[{
  73. Ex = EdgeList[G],
  74. P = N[PropertyValue[{G, #}, VertexCoordinates] & /@ VertexList[G]]
  75. },
  76. Return[If[i == 0,
  77. Max[Abs[
  78. Map[Norm[#[[1]] - #[[2]]] &,
  79. Map[P[[#]] &, Map[List @@ # &, Ex], 1], 1] - 1]],
  80. Max[Abs[
  81. Map[Norm[#[[1]] - #[[2]]] &, Map[P[[#]] &, Map[List @@ # &,
  82. Select[Ex, MemberQ[#, i] &]
  83. ], 1], 1] - 1]]
  84. ]]
  85. ];
  86.  
  87. STEP[G_, [Epsilon]_: 0.05, [Delta]_: 0.1] := Module[{
  88. Vx = VertexList[G],
  89. Ex = EdgeList[G],
  90. n, m, P, i, G2, P2
  91. },
  92. n = Length[Vx]; m = Length[Ex];
  93. P = PropertyValue[{G, #}, VertexCoordinates] & /@ Vx;
  94. P2 = P;
  95. i = RandomInteger[{1, n}];
  96. P2[[i]] = P2[[i]] + [Delta] ({RandomReal[], RandomReal[]} - 0.5);
  97. G2 = Graph[Vx, Ex, VertexCoordinates -> P2];
  98. If[Meas[G2, i] < Meas[G, i] || RandomReal[] < [Epsilon], Return[G2], Return[G]
  99. ]]
  100.  
  101. G = RandomGraph[{15, 20}];
  102. P = Table[{RandomReal[], RandomReal[]}, {q, 1, Length[VertexList[G]]}];
  103. G = Graph[G, VertexCoordinates -> P];
  104. DATA = NestList[STEP, G, 5000];
  105. Manipulate[DATA[[i]], {i, 1, Length[DATA], 1}]
  106.  
  107. G = GraphData["GolombGraph"];
  108. P = Table[{RandomReal[], RandomReal[]}, {q, 1, Length[VertexList[G]]}];
  109. G = Graph[VertexList[G], EdgeList[G], VertexCoordinates -> P];
  110. DATA = NestList[STEP[#, 0.01, 0.2] &, G, 500000];
  111. Animate[ColumnForm[{DATA[[i]], Meas[DATA[[i]]]}], {i, 1, Length[DATA],1}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement