Created
April 30, 2017 23:22
-
-
Save hagberg/9b81be32933551b324ed40ef6cb8c73f to your computer and use it in GitHub Desktop.
BA algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from networkx.utils import random_weighted_sample | |
def barabasi_albert_graph_rwc(n, m, seed=None): | |
if m < 1 or m >=n: | |
raise nx.NetworkXError("Barabási–Albert network must have m >= 1" | |
" and m < n, m = %d, n = %d" % (m, n)) | |
if seed is not None: | |
random.seed(seed) | |
# Add m initial nodes (m0 in barabasi-speak) | |
G=nx.empty_graph(m) | |
G.name="barabasi_albert_graph(%s,%s)"%(n,m) | |
# Target nodes for new edges | |
targets=list(range(m)) | |
# Start adding the other n-m nodes. The first node is m. | |
source=m | |
while source<n: | |
# Add edges to m nodes from the source. | |
G.add_edges_from(zip([source]*m,targets)) | |
# Now choose m unique nodes with random sample by degree weight. | |
targets = random_weighted_sample(dict(G.degree()), m) | |
source += 1 | |
return G |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment