Group Abstract Group Abstract

Message Boards Message Boards

Why is my implementation of a random network slower than the built-in?

Posted 7 years ago

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)

POSTED BY: Ektor Mariotti
2 Replies
Posted 7 years ago

This makes much sense!

Yes memory pre-allocation is probably the way to go, it's a pity that the documentation don't show up the time complexity of the algorithms!

I still think that my implementation is the most straight-forward to implement, but your reply was enlightening.

Maybe working with sparse array/matrices would make it even more efficient?

I think this it's an interesting topic not for the particular case of the Barabasi-Albert, but for more in general efficiently develop algorithms for graphs.

It's a pity that one have to tradoff simplicity for efficiency.

Thanks again Szabolcs!

POSTED BY: Ektor Mariotti
POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard