I have a question, which I failed to find an answer to myself. I'm a casual user of Mathematica, but do have some knowledge about Dynamic module and other "Advanced" features.

My problem is formulated as follows:

1) I have a graph, which is built on 2*n vertices and contains k (for simplicity we can start with k = 2) perfect matchings on it.

2) I have an operation, that changes the graph by deleting l (again, let us take the simplest case of l = 2) edges from some matching and replacing them with l edges on same set of 2*l vertices.

3) I want to observe a process of random application of such operations to initially supplied graph. And I want to collect some data on every step to further visualize it.


Is it possible, using the dynamic module system to perform step-by-step iteration over the data structure? Say I don't want to get the result of application of z random operations on the graph at once, but would rather observe every step with control over the flow by some sort of "next" button.

The issue, as I see it, lies in following: even if I create a dynamic module for my case, and z will be used as a dynamic variable, specifying how many random operation I want to perform, changing it from z to z + 1 will reapply all z + 1 operations, rather than perform a single additional one.

Is there a way to use some kind of history for Dynamic module? Saying if I go forward, use previous result and update it by one?

Hope I've explained it understandably =)


  • 2
    $\begingroup$ I think your explanation is good but I would also like an MWE, that is, a Minimal Working Example. Please post a sample graph and a sample operation so that we can demonstrate possible solutions. $\endgroup$
    – C. E.
    Sep 20, 2014 at 17:59
  • $\begingroup$ Is there a reason you want to do this using a Dynamic construction, as opposed to defining a sequence of graphs? $\endgroup$ Sep 23, 2014 at 5:32
  • $\begingroup$ Maybe you could be interested in this post... mathematica.stackexchange.com/questions/61262/… $\endgroup$
    – Luca M
    Oct 30, 2014 at 6:06
  • $\begingroup$ Sergey, do you plan on ever coming back to this? I am hoping my answer was helpful or if not that I might get a better idea of what you are looking for. $\endgroup$ Nov 29, 2014 at 18:17

1 Answer 1


Well, I haven't quite understood your request for a Dynamic approach (I don't see the why or how of it) but since no one else has responded, here is what I can cook up in a few minutes:

G[n_] := G[n] = 
   Module[{e1, e2, Gr = G[n - 1], m = Length[G[n - 1]], t},
    e1 = RandomInteger[{1, m}];
    e2 = RandomInteger[{1, m}];
    While[Gr[[e1, 2]] != Gr[[e2, 2]] || e1 == e2,
     e2 = RandomInteger[{1, m}];
    t = Gr[[e1, 1, 2]];
    Gr[[e1, 1, 2]] = Gr[[e2, 1, 2]];
    Gr[[e2, 1, 2]] = t;
RandomMatching[n_] := Module[{m = {}, i, j, v = Range[1, n]},
  While[Length[v] != 0,
   i = RandomInteger[{1, Length[v]}];
   j = i;
   While[i == j, j = RandomInteger[{1, Length[v]}]];
   i = v[[i]];
   j = v[[j]];
   m = Append[m, i <-> j];
   v = Complement[v, {i, j}];
G[0] = Join[
   Map[Style[#, Red] &, RandomMatching[10]],
   Map[Style[#, Blue] &, RandomMatching[10]],
   Map[Style[#, Green] &, RandomMatching[10]]

 Graph[Range[1, 10],
  VertexCoordinates -> 
   Table[{Cos[\[Pi] k/5], Sin[\[Pi] k/5]}, {k, 0, 9}],
  VertexLabels -> "Name",
  ImagePadding -> 40
 {n, 0, 20, 1}]

I think it's relatively straightforward code, but of course that's just what I think, so if any part of it is confusing, feel free to ask.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.