├── .gitignore ├── requirements.txt ├── Aleksander_Molak_BA.pdf └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | networkx==2.4 2 | numpy==1.19.0 3 | matplotlib==3.1.3 -------------------------------------------------------------------------------- /Aleksander_Molak_BA.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlxndrMlk/Barabasi-Albert_Network/HEAD/Aleksander_Molak_BA.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Step-by-Step Barabási–Albert Model in Python 3 2 | #### by Aleksander Molak (06.2017; last update: 03.2021) 3 | email: aleksander.molak@gmail.com 4 | 5 | ***NOTE**: to make sure the code from this repo works, please check your libraries versions against `requirements.txt`. Good luck and have fun!* 6 | 7 | ### What is Barabási–Albert Network? 8 | The **Barabási–Albert (BA)** model is an algorithm for generating random scale-free networks 9 | using a preferential attachment mechanism. 10 | Several natural and human-made systems, including the Internet, the world wide web, citation networks, 11 | and some social networks are thought to be approximately scale-free and certainly contain few nodes (called hubs) 12 | with unusually high degree as compared to the other nodes of the network. 13 | The BA model tries to explain the existence of such nodes in real networks. 14 | The algorithm is named for its inventors Albert-László Barabási and Réka Albert 15 | and is a special case of a more general model called Price's model. 16 | (source: https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model) 17 | 18 | ### What is this project about? 19 | The goal of this project was to built a step-by-step Barabási–Albert Network Model. 20 | I used Python 3 and networkx library to meet this objective. 21 | 22 | **Note:** This implementation hasn't been optimized for computational speed or memory usage; feel free to reuse and improve this code. 23 | 24 | ### How does it work? 25 | When you run the script you are asked to specify network parameters: 26 | 27 | * Initial number of nodes (), where 28 | 29 | * Final number of nodes 30 | 31 | * **m** parameter (where ); This parameter controls how many new edges will every new node create 32 | 33 | 34 | When the script reaches final number of nodes you can visualize your network. For example you can use: 35 | 36 | ```python 37 | nx.draw(G, node_size=50, with_labels=0, alpha=0.6, node_color="#40a6d1", edge_color="#52bced") 38 | ``` 39 | 40 | and you should get a visualization similar to this: 41 | 42 | ![net_4_500_2](https://user-images.githubusercontent.com/28199898/29740901-0c37361a-8a62-11e7-8dc0-5c7abe6f2423.png) 43 | 44 | You can also visualize degree distribution, using `k_distr()` function using linear or log-log scale. 45 | 46 | Degree distribution of **Barabási–Albert** network is `k**(-3)` and so it gives a straight line in log-log scale. 47 | 48 | #### Examples: 49 | 50 | 51 | * Linear scale example 52 | 53 | ```python 54 | k_distrib(graph=G, colour='#40a6d1', alpha=.8) 55 | ``` 56 | 57 | ![net_4_500_2_distr_lin](https://user-images.githubusercontent.com/28199898/29740902-0c398046-8a62-11e7-9a30-2d0a00751f22.png) 58 | 59 | * Log-log scale example 60 | 61 | ```python 62 | k_distrib(graph=G, colour='#40a6d1', scale='log', alpha=.8, fit_line=True, expct_lo=3, expct_hi=14, expct_const=8) 63 | ``` 64 | 65 | ![net_4_500_2_distr_log](https://user-images.githubusercontent.com/28199898/29740900-0c371298-8a62-11e7-887a-8241533fd6c4.png) 66 | 67 | ***Note:** `expct_lo`, `expct_hi` and `expct_const` parameters are used to manually adjust theoretical distribution line in the plot* 68 | 69 | Network visualization function `k_distr()` in based on the animation script by Abdallah Sobehy: 70 | https://github.com/Abdallah-Sobehy/barabasi_albert/blob/master/BA.py 71 | 72 | 73 | To see a few models I made with this Python script check these ![visualizations](./Aleksander_Molak_BA.pdf). 74 | 75 | In case of any questions or if you simply wanna say hello, feel free to contact me 76 | 77 | 78 | 79 | Have fun! :) 80 | --------------------------------------------------------------------------------