# [✓] Replace AppendTo with usage of Reap Sow?

GROUPS:
 I have tried to figure out how to use Reap and Sow from searching online, but do not understand it. Can anyone help me with my line of code? The issue is that the data set is so large that it takes a very long time to compute.I currently have a list that has just over 623,000 elements in it. My code to then loop through the list to determine the VertexDegree and then store those elements in a new list is: s35vertlist = {}; Do[AppendTo[s35vertlist, VertexDegree[Subgraph[s35, cyclelists35[[i]]]]], {i, Length[cyclelists35]}] Where s35vertlist is my new empty list, and cyclelists35 is my list with the large number of elements in it. I have tried s35vertlist = {}; s35vertlist = Reap[Do[Sow[s35vertlist, VertexDegree[Subgraph[s35, cyclelists35[[i]]]]], {i, Length[cyclelists35]}]] But it returns Null when I know that it should have least one element. So, I am hoping that someone can quickly tell me how to get Reap/Sow to work instead of AppendTo. I am sorry if this is super simple and annoying, but I can't figure it out. Thank you so much.
6 months ago
4 Replies
 Kuba Podkalicki 2 Votes You don't need Sow/Reap here: s35vertlist = Table[VertexDegree[Subgraph[s35, cyclelists35[[i]]]], {i, Length[cyclelists35]}] should do, or s35vertlist = VertexDegree[Subgraph[s35, #]] & /@ cyclelists35 With respect to a functional style, maybe there is something graph specific which could make it even better but that is not my area.
6 months ago
 Neil Singer 2 Votes Justin,I would think that Map might be faster: s35vertlist = Map[VertexDegree[Subgraph[s35, #]] &, cyclelists35] Sow works like this -- you don't append to the list -- it just collects the values. I do not recommend this in your case because you want a value returned for every element in your cyclelist s35vertlist = Reap[Do[Sow[VertexDegree[Subgraph[s35, cyclelists35[[i]]]]], {i, Length[cyclelists35]}]] Try doing an AbsoluteTiming[] for your loop and the Map and post the times. I am guessing that the Map is faster.Regards
6 months ago
 Neil Singer 1 Vote Sorry, Crossed with Kuda's post at the same time... I would also time Kuda's table approach to see if it is faster than the loop and/or the Map. Curious to know which is faster.(Also, if you compile the function you will probably get a significant speedup with any of the approaches)
6 months ago
 Wow. I am so glad I posted this question. All three solutions worked much faster than my original part. The first Table implementation took 65.9754. The second that uses some mapping code I don't understand took 64.1661. And the One that uses the Map function name took 63.4734! Thank you both for you help. I will try to read more about Table and Map since I didn't understand how to use them before.