I'm assembling graphs programmatically, and looping through EdgeAdd[] a lot.

graph = EdgeAdd[graph, a -> b];

Is there a more efficient way to do the updates "in place" -- as opposed to the normal assignment?

  • 3
    $\begingroup$ Graphs are immutable objects ... so the general answer is "no". But perhaps there may be some trick on your particular situation. Tell us more about what you're trying to do $\endgroup$ Commented Dec 18, 2015 at 5:43
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Commented Dec 18, 2015 at 7:01
  • 1
    $\begingroup$ You can use sparse arrays to represent graphs. The question is: what else do you want to do with the graphs in the inner loop other than EdgeAdd and whether that thing is easy/fast with a sparse array. $\endgroup$
    – Szabolcs
    Commented Dec 18, 2015 at 8:30
  • $\begingroup$ I'm trying to assemble the graph from a list of associations. I was hoping to use something analogous to the AppendTo[] function. I don't have the list of associations when the graph is initially created. Thanks. $\endgroup$
    – vonkohorn
    Commented Dec 18, 2015 at 9:28


Your Answer

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

Browse other questions tagged or ask your own question.