# Barabasi Albert Graph (for Scale Free Models)

BA Model for 50 nodes

The above graph completely satisfies the logic of the rich getting richer and the poor getting poorer. Code: The following code is a part of the function which we will eventually implement using the networkx library.

## Python

 def barabasi_albert_graph(n, m, seed=None):    """Returns a random graph according to the BarabÃ¡siâ€“Albert preferential    Attachment model.     A graph of n nodes is grown by attaching new nodes each with m    Edges that are preferentially attached to existing nodes with high degree.     Parameters    ----------    n : int        Number of nodes    m : int        Number of edges to attach from a new node to existing nodes    seed : int, optional        Seed for random number generator (default=None).     Returns    -------    G : Graph     Raises    ------    NetworkXError        If m does not satisfy 1 <= m < n.          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=empty_graph(m)    G.name="barabasi_albert_graph(%s,%s)"%(n,m)    # Target nodes for new edges    targets=list(range(m))    # List of existing nodes, with nodes repeated once for each adjacent edge    repeated_nodes=[]    # Start adding the other n-m nodes. The first node is m.    source=m    while source

The above code is a part of the networkx library which is used to handle the random graphs efficiently in python. One will have to install it before running the following code.

## Python

 >>> import networkx as nx>>> G= nx.barabasi_albert_graph(50,40)>>> nx.draw(G, with_labels=True)

To display the above graph, I used the matplotlib library. We need to install it before the execution of the below codes.

## Python

 >>> import matplotlib.pyplot as plt>>> plt.show()

So the final code seemed like:

## Python

 >>> import networkx as nx>>> import matplotlib.pyplot as plt >>> G= nx.barabasi_albert_graph(40,15)>>> nx.draw(G, with_labels=True)>>> plt.show()`

Output:

BA model for 40 nodes

Thus I would further like to describe more about the networkx library and it modules basically focusing on the centrality measure of a network (especially the scale free models). References You can read more about the same at

.

Previous
Next