Hello everybody,
I wanted for accademic purposes to implement the algorithm that generate a Random Graph of the Barabasi-Albert kind. I know that there exsist already a built-in function that do exactly that, but for better understanding it I challenged myself to reproduce it.
I came up with this (relatively simple, but effective) code:
 
m0 = 2;(*How many edges to connect at each time step*)
n = 1000; \
(*How many nodes*)
g = CompleteGraph[m0, VertexLabels -> Automatic];
addBarabAlbert[g_Graph, m_Integer] := 
EdgeAdd[g, 
Rule[m, #] & /@ RandomSample[VertexDegree@g -> VertexList@g, m0]]
Do[g = addBarabAlbert[g, i], {i, m0, n}]
Which I think it gives me a correct solution, in fact you can see from the code below that the degree of the vertexes follow a power-law of the same nature as the one generated with the built-in wolfram function.
The only problem is that my implementation is kind of slow (compared to the built-in) and I was wondering why. Maybe knowing how I could set it up to make it faster will help me in the future with different problems.
Any ideas?
(In the attachment you find the brief code)
				
					
				
				
					
					
						
							
							Attachments: