0% found this document useful (0 votes)
16 views4 pages

Random graph.ipynb - Colab

Uploaded by

akhil.s18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Random graph.ipynb - Colab

Uploaded by

akhil.s18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12/4/24, 8:07 AM Random graph.

ipynb - Colab

keyboard_arrow_down Random graphs


import networkx as nx
import seaborn as sns # for visualization
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

A random graph has 𝑛 nodes


Each pair of nodes is connected with probability 𝑝
The random graph model is also called:

(𝑛, 𝑝) model
Erdos-Renyi model

n = 1000
p = 0.05
np.random.seed(42)
G = nx.fast_gnp_random_graph(n=n, p=p)

keyboard_arrow_down Plot the degree distribution


G.degree() ⇒ degree of each node

dict(G.degree()) ⇒ convert it into a dictionary

Series(dict(G.degree())) ⇒ convert it into a pandas Series

degrees = Series(dict(G.degree))
degrees[:5]

0 51
1 52
2 46
3 57
4 58
dtype: int64

keyboard_arrow_down Plotting the degree distribution


vc = degrees.value_counts().sort_index()
print(vc[:10])

32 1
33 2
34 3
35 5
36 3
37 15
38 13
39 16
40 26
41 17
Name: count, dtype: int64

vc.plot()

https://colab.research.google.com/drive/1FrpApfcdlwwxwv203cBFPIPos3muuR_A 1/4
12/4/24, 8:07 AM Random graph.ipynb - Colab
<Axes: >

We can smooth it out with a histogram.

Series(degrees).hist(bins=20)

<Axes: >

A better way to smooth things (when needed) is to do a Kernel Density Estimate (KDE).

df = DataFrame({'degrees':degrees})
sns.kdeplot(df)

https://colab.research.google.com/drive/1FrpApfcdlwwxwv203cBFPIPos3muuR_A 2/4
12/4/24, 8:07 AM Random graph.ipynb - Colab
<Axes: ylabel='Density'>

keyboard_arrow_down What is the expected degree of a node?


A node can have 𝑛 − 1 possible edges
Each edge occurs with probability 𝑝
So, on average, a node has degree=$(n-1)\times p$

df['degree/(n-1)'] = df['degrees'] / (n-1)


sns.kdeplot(df[['degree/(n-1)']])

<Axes: ylabel='Density'>

keyboard_arrow_down What is the clustering coefficient distribution?


nx.clustering(G) ⇒ clustering coefficient of each node, as a dictionary

CC = nx.clustering(G).values()
df['CC'] = CC

sns.kdeplot(df[['degree/(n-1)', 'CC']])

https://colab.research.google.com/drive/1FrpApfcdlwwxwv203cBFPIPos3muuR_A 3/4
12/4/24, 8:07 AM Random graph.ipynb - Colab
<Axes: ylabel='Density'>

keyboard_arrow_down Why is the clustering cofficient also ≈ 𝑝?


CC of a node
#pairs of my friends who know each other
=
#pairs of my friends

https://colab.research.google.com/drive/1FrpApfcdlwwxwv203cBFPIPos3muuR_A 4/4

You might also like