Student Support Forum: 'improving speed of epidemic simulation, BA model' topicStudent Support Forum > General > "improving speed of epidemic simulation, BA model"

 Help | Reply To Topic
 Author Comment/Response Alen 10/11/08 11:08am I have a problem regarding a spped of execution of the latter code. Unfortunately, I don't know very much about how mathematica executes its commands and have already tried using sparse arrays. Firstly, I wrote this pretty fast code for generating random graph using preferential attachment: << Combinatorica` Needs["GraphUtilities`"] N0 = 2; II = 5000; m = 2; A1 = {}; A = SparseArray[{{i_, j_} /; N0 + 1 > j > i -> 1}, {II, II}]; a = Table[N0 - 1, {N0}]; ProgressIndicator[Dynamic[k], {N0, II}] Monitor[For[k = N0 + 1, k <= II, k++, prefodbrid = RandomSample[a -> Range[k - 1], m]; For[j = 1, j <= m, j++, A[[prefodbrid[[j]], k]] = 1; a[[prefodbrid[[j]]]]++;]; a = Append[a, m]; ], k] But then, code for simulating spread of a disease p = 0.5; q = 0.9; A = Normal[A]; B = A + Transpose[A]; neigh = Table[Position[B[[{i}, All]], 1][[All, 2]], {i, 1, II}]; Timing[ recovered = old = new = {}; AppendTo[old, 1]; While[old!= {}, potential = Flatten[neigh[[old]]]; potential2 = Complement[potential, old, recovered]; For[i = 1, i <= Length[potencijalni2], i++, If[RandomReal[] > (1 - p)^Count[potential, potential2[[i]]], AppendTo[new, potential2[[i]]]; ]; ]; For[i = 1, i <= Length[old], i++, If[RandomReal[] < q, AppendTo[recovered, old[[i]]]; old = Drop[old, {i}];]; ]; old = Union[old, new]; new = {}; ]] is quite slow. It starts with vertex 1 being infected and in every subsequent step every neighbor of the old infected vertex has a chance p of becoming newly infected, and all old infected vertices have a chance q of becoming recovered and immune, each. After that, newly infected vertices join the remaining old ones. Simulation ends when there are no remaining infected vertices. Lastly, regarding the plotting of a graph, a code G = FromAdjacencyMatrix[Normal[A]]; GraphPlot3D[G, VertexRenderingFunction -> ({Sphere[#1, (Degrees[G]/( 2*M[G]))[[#2]]]} &), EdgeRenderingFunction -> (Cylinder[#1, 0.003] &), Method -> HighDimensionalEmbedding] plots a graph, and I would like to use the same vertex and edge placements, changing only the rendering function so it assumes different color (black for example), but only on vertices and edges that meet certaing criteria. Is it possible and can I use that to increase speed of generating graph images? Attachment: infection simulation.nb, URL: ,
 Help | Reply To Topic